以太坊最受欢迎的开发框架是使用web3.js的Truffle。
官方网站:
Truffle Quickstart - Truffle Suitehttps://trufflesuite.com/docs/truffle/quickstart.htmlTruffle使用以太坊虚拟机(EVM)为区块链提供世界一流的开发环境、测试框架和资产管道,旨在让开发人员的生活更轻松。
npm install -g truffle
环境要求:
Truffle 大多数命令都是在 Truffle 项目目录下运行的。 所以第一步是创建一个 Truffle 项目。 可以创建一个空项目模板,不过对于Truffle初学者,推荐使用Truffle Boxes,它提供了示例应用代码和项目模板。 下面将使用MetaCoin box作为案例,它创建一个可以在帐户之间转移的Token(代币)。
1、为Truffle项目创建新目录:
mkdir MetaCoin
cd MetaCoin
2、下载(“unbox”)MetaCoin box:
truffle unbox metacoin
可以使用 truffle unbox
命令下载其他的 Box
3、要创建不含智能合约的空白Truffle项目,可以使用truffle init。
truffle init
可以使用一个可选的选项 –force 在当前目录下初始化项目,不管当前目录的状态(即使它包含了其他人文件和目录)。 这个选项可用于 init 和 unbox 命令,不过要小心他会覆盖当前已经存在的文件或目录。
操作完成后,就有这样的一个项目目录结构:
在终端中,运行Solidity测试:
truffle test ./test/TestMetaCoin.sol
输出信息:
TestMetacoin
√ testInitialBalanceUsingDeployedContract (71ms)
√ testInitialBalanceWithNewMetaCoin (59ms)2 passing (794ms)
运行JavaScript测试:
truffle test ./test/metacoin.js
输出信息
Contract: MetaCoin
√ should put 10000 MetaCoin in the first account
√ should call a function that depends on a linked library (40ms)
√ should send coin correctly (129ms)3 passing (255ms)
编译智能合约:
truffle compile
首次运行时,将编译所有合约。 在后续运行中,Truffle将仅编译自上次编译以来有更改的合约。 如果我们想覆盖此行为,可以使用 --all
选项运行上面的命令。
输出信息:
Compiling .\contracts\ConvertLib.sol...
Compiling .\contracts\MetaCoin.sol...
Compiling .\contracts\Migrations.sol...Writing artifacts to .\build\contracts
truffle允许我们指定项目中使用哪个版本的编译器,只需要在truffle项目的配置文件truffle.js中设定compiler选项。例如,下面的配置文件将当前项目的solidity编译器设置为0.8.0版本:
module.exports = {
networks: {
... etc ...
},
compilers: {
solc: {
version: "0.8.0"
}
}
};
只需要在compilers.solc.version
处指定要启用的solidity编译器版本就行了,truffle会自动从官方拉取相应版本的编译器,无需手动下载配置。
要部署我们的智能合约,我们需要连接到区块链。Truffle有一个内置的个人区块链,可用于测试。此区块链是你的本地区块链,不与以太坊主网络交互。
你可以创建此区块链,并使用Truffle Develop与之交互。
运行Truffle Develop:
truffle develop
输出信息:
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
(1) 0xf17f52151ebef6c7334fad080c5704d77216b732
(2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
(3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
(4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
(5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
(6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
(7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
(8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
(9) 0x5aeda56215b167893e80b4fe645ba6d5bab767dePrivate Keys:
(0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
(1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
(2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
(3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
(4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
(5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
(6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
(7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
(8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
(9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5Mnemonic: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat
⚠️ 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(development)>
这里列出了在与区块链交互时可以使用的十个帐户(及其私钥)。
在Truffle develop提示符上,可以通过省略truffle前缀来运行Truffle命令。例如,要在提示符上运行truffle compile,请键入compile。将编译的合约部署到区块链的命令是truffle migrate,因此在提示符处键入:
migrate
输出信息:
Starting migrations...
======================
> Network name: 'develop'
> Network id: 4447
> Block gas limit: 67219751_initial_migration.js
======================Deploying 'Migrations'
----------------------
> transaction hash: 0x3fd222279dad48583a3320decd0a2d12e82e728ba9a0f19bdaaff98c72a030a2
> Blocks: 0 Seconds: 0
> contract address: 0xa0AdaB6E829C818d50c75F17CFCc2e15bfd55a63
> account: 0x627306090abab3a6e1400e9345bc60c78a8bef57
> balance: 99.99445076
> gas used: 277462
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00554924 ETH> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00554924 ETH2_deploy_contracts.js
=====================Deploying 'ConvertLib'
----------------------
> transaction hash: 0x97e8168f1c05fc40dd8ffc529b9a2bf45cc7c55b07b6b9a5a22173235ee247b6
> Blocks: 0 Seconds: 0
> contract address: 0xfb39FeaeF3ac3fd46e2123768e559BCe6bD638d6
> account: 0x627306090abab3a6e1400e9345bc60c78a8bef57
> balance: 99.9914458
> gas used: 108240
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0021648 ETHLinking
-------
* Contract: MetaCoin <--> Library: ConvertLib (at address: 0xfb39FeaeF3ac3fd46e2123768e559BCe6bD638d6)Deploying 'MetaCoin'
--------------------
> transaction hash: 0xee4994097c10e7314cc83adf899d67f51f22e08b920e95b6d3f75c5eb498bde4
> Blocks: 0 Seconds: 0
> contract address: 0x6891Ac4E2EF3dA9bc88C96fEDbC9eA4d6D88F768
> account: 0x627306090abab3a6e1400e9345bc60c78a8bef57
> balance: 99.98449716
> gas used: 347432
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00694864 ETH> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00911344 ETHSummary
=======
> Total deployments: 3
> Final cost: 0.01466268 ETH
这将显示已部署合约的交易ID和地址。它还包括成本摘要和实时状态更新。
虽然Truffle Develop是一个多合一的个人区块链和控制台,但你也可以使用桌面应用程序Ganache启动你的个人区块链。Ganache对于以太坊和区块链的新手来说,这可能是一个更易于理解的工具,因为它预先显示更多信息。
除了运行Ganache外,唯一的额外步骤是它需要编辑Truffle配置文件才能指向Ganache实例。
1、下载并安装Ganache。
2、在文本编辑器中打开truffle-config.js。将内容替换为以下内容:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
}
}
};
这将允许使用Ganache的默认连接参数进行连接。
3、保存并关闭该文件。
4、启动Ganache
5、在终端上,将合约迁移到Ganache创建的区块链:
truffle migrate
输出结果:
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /home/david/work/MetaCoin/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clangStarting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)1_initial_migration.js
======================Deploying 'Migrations'
----------------------
> transaction hash: 0x3eef05e35ce694c0b7112cc22ba462b9cc0563abc2cc444ee9683b6d89865e3c
> Blocks: 0 Seconds: 0
> contract address: 0x8CdaF0CD259887258Bc13a92C0a6dA92698644C0
> block number: 1
> block timestamp: 1587421933
> account: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
> balance: 99.9967165
> gas used: 164175 (0x2814f)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0032835 ETH> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0032835 ETH2_deploy_contracts.js
=====================Deploying 'ConvertLib'
----------------------
> transaction hash: 0x23de020bafa41e30615b1d775d2fa9604e876415408e012d1f0faf79eed3a32f
> Blocks: 0 Seconds: 0
> contract address: 0x345cA3e014Aaf5dcA488057592ee47305D9B3e10
> block number: 3
> block timestamp: 1587421933
> account: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
> balance: 99.99396028
> gas used: 95470 (0x174ee)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0019094 ETHLinking
-------
* Contract: MetaCoin <--> Library: ConvertLib (at address: 0x345cA3e014Aaf5dcA488057592ee47305D9B3e10)Deploying 'MetaCoin'
--------------------
> transaction hash: 0x0cc20353422c4d435f72e8e7850f8178f43bf8d00c8c0d09cc8e0ccdfa81b799
> Blocks: 0 Seconds: 0
> contract address: 0xf25186B5081Ff5cE73482AD761DB0eB0d25abfBF
> block number: 4
> block timestamp: 1587421934
> account: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
> balance: 99.98822922
> gas used: 286553 (0x45f59)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00573106 ETH> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00764046 ETHSummary
=======
> Total deployments: 3
> Final cost: 0.01092396 ETH
这将显示已部署合约的交易ID和地址。它还包括费用摘要和实时状态更新。
truffle console
你将看到以下提示:
truffle(development)>
控制台通过以下方式与合约互动:
从Truffle v5起,控制台支持异步/等待功能,从而实现与合约的更简单的交互。
首先建立已部署的MetaCoin合约实例和由Truffle的内置区块链或Ganache创建的帐户:
truffle(development)> let instance = await MetaCoin.deployed()
truffle(development)> let accounts = await web3.eth.getAccounts()
检查部署合约的帐户的metacoin余额:
truffle(development)> let balance = await instance.getBalance(accounts[0])
truffle(development)> balance.toNumber()
查看该余额价值多少ETH(并注意合约将metacoin定义为价值2ETH):
truffle(development)> let ether = await instance.getBalanceInEth(accounts[0])
truffle(development)> ether.toNumber()
将一些metacoin从一个帐户转移到另一个帐户:
truffle(development)> instance.sendCoin(accounts[1], 500)
检查收到metacoin的帐户余额:
truffle(development)> let received = await instance.getBalance(accounts[1])
truffle(development)> received.toNumber()
检查发送metacoin的帐户余额:
truffle(development)> let newBalance = await instance.getBalance(accounts[0])
truffle(development)> newBalance.toNumber()