brew install npm
npm install -g truffle
truffle version
创建项目文件夹,并初始化
mkdir truffle-demo
cd truffle-demo
truffle init
ls
看到目录结构
contracts/: 合约目录
migrations/:部署文件目录
test/: 测试文件目录
truffle-config.js: Truffle配置文件
这里只是看一下结构,这几个文件夹都是空的,我们删除这些文件,使用官方truffle boxes来创建一个示例项目
注意,这里需要
truffle unbox metacoin ./
尴尬,并没有成功,看提示是墙的问题,但我开代理还是失败
只能围魏救赵,直接下载
找到metacoin box在github上的项目 GitHub - truffle-box/metacoin-box: MetaCoin smart contracts example box
git clone https://github.com/truffle-box/metacoin-box.git
将项目中文件复制进入之前的项目文件夹中(或者直接用这个也可以,我这里不想在git仓库里处理,所以直接原项目复制)
truffle test
This version of µWS is not compatible with your Node.js build:
Error: Cannot find module './uws_darwin_x64_111.node'
Falling back to a NodeJS implementation; performance may be degraded.
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol-bin. Attempt #1
> Compiling ./contracts/MetaCoin.sol
> Compiling ./test/TestMetaCoin.sol
> Artifacts written to /var/folders/62/dyvwqt0j0cxg5fxl9hcl81600000gn/T/test--40394-LWbLWwA1cTLY
> Compiled successfully using:
- solc: 0.8.13+commit.abaa5c0e.Emscripten.clang
⠼ Fetching solc version list from solc-bin. Attempt #1
✓ Downloading compiler. Attempt #1.
TestMetaCoinc version list from solc-bin. Attempt #1
✔ testInitialBalanceUsingDeployedContract (66ms)#1
✔ testInitialBalanceWithNewMetaCoin (42ms)tempt #1
✓ Downloading compiler. Attempt #1.
Contract: MetaCoin
✔ should put 10000 MetaCoin in the first account (62ms)
✔ should call a function that depends on a linked library (70ms)
✔ should send coin correctly (106ms)in. Attempt #1
⠇ Fetching solc version list from solc-bin. Attempt #1
✓ Downloading compiler. Attempt #1.
5 passing (5s)
报错没查清楚原因
truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Artifacts written to ~/truffle-demo/build/contracts
> Compiled successfully using:
- solc: 0.8.13+commit.abaa5c0e.Emscripten.clang
在新生成build文件夹中可以看到编译的abi文件
cd ./build/contracts
ls
# 显示:ConvertLib.json MetaCoin.json
truffle develop
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0x0be9e88a5173858fddf1579ae3672c62bdf729c7
(1) 0x187cad452783187499f30fe71a722ec604382ab1
(2) 0x8d02dea3e65d19861cd7823fb233b703dc422005
(3) 0xba2958c0c0543a429476a7385bdca179b964f03c
(4) 0xd1bf3035b30145931b4d3a6dd9ce70c6835e97db
(5) 0x8d39611b812f252a9e2bd6ba80a6716e9982188e
(6) 0xf029eb241e05107dabbafa87c5f3de6ea5a78137
(7) 0x7180703bc2d0826218044d3acb226e1d32b72348
(8) 0xf389dae15ec4c23b67e61b868edae2b20e04c1fc
(9) 0x66a7f7aec76a37779a1b0f40ea0ba8c00ee53acd
Private Keys:
(0) 769b84baf16a13725b8d00f82a4e6b35c5615ae59e89e6f34f9073c33f20da73
(1) 6559607a1ad976d6d13c99d30d30641e032cc2c6d59bcd36a96160ecb0285e6c
(2) a7acc3deab622447b328e4cb0f3a6ec410b2f40e88d8207ede552c1524ff33ec
(3) 9f9e8af62ce3fb54937c6f6c9ba4b41a67ba2515f176a3619ad461e0998da379
(4) 963c80601ea0be35f61f6d602f095986f5c88131ee17ee911085621427413c3e
(5) 92787155dfaf426c5724a288fdd891bc5fbc36d2e73bfcf5d31b5481729b2888
(6) ec7115af1c6d368f7f015ae05c7acd73403fff71b0ac4135f87b94e232e7e8ae
(7) 034bf1d52889c6d7e52ae731284f8bb1e6bad548ba87b323d3bcdc7de87b0857
(8) 6dbe0800023bab78454e22be16d5d5102c48a52ac4c7e861a1e64cc34e99783e
(9) 46eb1dfcc0bbb9ac9181eaafa62814c0e1c79cdd01c85541a2bbb60ce93b0694
Mnemonic: awesome select income genius logic farm feel injury consider next fault approve
⚠️ Important ⚠️ : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.
truffle(develop)>
可以看到本地启动了一个测试环境,地址是http://127.0.0.1:9545/
此时已经进入truffle的develop命令行环境
migrate
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'develop'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_deploy_contracts.js
=====================
Deploying 'ConvertLib'
----------------------
> transaction hash: 0x7c1fbcf5ae619327c44662b65dc7165b5422967d42fe7b0be062d02ec56e2aab
> Blocks: 0 Seconds: 0
> contract address: 0x3d880418f8235A725ebd81F8139e4C81b2b283af
> block number: 1
> block timestamp: 1680180220
> account: 0x0be9E88a5173858fDDf1579aE3672C62bDf729c7
> balance: 99.999468208
> gas used: 157568 (0x26780)
> gas price: 3.375 gwei
> value sent: 0 ETH
> total cost: 0.000531792 ETH
Linking
-------
* Contract: MetaCoin <--> Library: ConvertLib (at address: 0x3d880418f8235A725ebd81F8139e4C81b2b283af)
Deploying 'MetaCoin'
--------------------
> transaction hash: 0x1e9060504eede232cfc7509fb64f01b4356ddb9f77afd4d7cf127847426dea12
> Blocks: 0 Seconds: 0
> contract address: 0xd137e1c1cc04D43C7518D702F1D59DAA46Fc9fc1
> block number: 2
> block timestamp: 1680180220
> account: 0x0be9E88a5173858fDDf1579aE3672C62bDf729c7
> balance: 99.998105632065943366
> gas used: 416594 (0x65b52)
> gas price: 3.270752661 gwei
> value sent: 0 ETH
> total cost: 0.001362575934056634 ETH
> Saving artifacts
-------------------------------------
> Total cost: 0.001894367934056634 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.001894367934056634 ETH
除了启动truffle自带的区块链实例,也可以启动ganache实例,并修改配置文件进行连接
相关内容将在下篇文章中讨论
打开truffle-config.js文件,修改如下内容
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
}
}
};
端口是你自己启动的端口
truffle migrate
部署合约到配置的网路
truffle console
会进入与区块链的交互,与2.6中相同
交互命令:
truffle(development)> let instance = await MetaCoin.deployed()
truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> let balance = await instance.getBalance(accounts[0])
truffle(development)> balance.toNumber()
truffle(development)> let ether = await instance.getBalanceInEth(accounts[0])
truffle(development)> ether.toNumber()
truffle(development)> instance.sendCoin(accounts[1], 500)
truffle(development)> let received = await instance.getBalance(accounts[1])
truffle(development)> received.toNumber()
truffle(development)> let newBalance = await instance.getBalance(accounts[0])
truffle(development)> newBalance.toNumber()
参考文档:
Truffle quickstart - Truffle Suite