Truffle是針對(duì)基于以太坊的Solidity語(yǔ)言的一套開發(fā)框架。本身基于Javascript。
安裝
npm install -g truffle
創(chuàng)建工程
truffle init會(huì)幫我們創(chuàng)建一個(gè)空工程。
D:\Android_Code\2018\blockchain
λ mkdir HelloDapp && cd HelloDapp
D:\Android_Code\2018\blockchain\HelloDapp
λ truffle.cmd init
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
D:\Android_Code\2018\blockchain\HelloDapp
λ ls
contracts/ migrations/ test/ truffle-config.js truffle.js
編寫工程
這里推薦使用Goland或idea系列作為編輯器, 打開HelloDapp目錄。然后下載Intellij-Solidity插件,就能支持Solidity語(yǔ)言。
具體操作步湊為: File->Settings->Plugins->Browse Respositories->Intellij-Solidity->Install。
1. 在contracts目錄下新建Hello.sol文件。
pragma solidity ^0.4.23;
contract Hello {
function print() public pure returns(string) {
return "Hello World";
}
function add(uint n1, uint n2) public pure returns(uint) {
return n1 + n2;
}
}
2. 在test木下新建TestHello.sol文件。
pragma solidity ^0.4.23;
import "../contracts/Hello.sol";
import "truffle/Assert.sol";
contract TestHello {
function testAdd() public {
Hello hello = new Hello();
uint result = hello.add(1, 20);
Assert.equal(result, 21, "TestAdd");
}
function testPrint() public {
Hello hello = new Hello();
string memory result = hello.print();
Assert.equal(result, "Hello World", "TestPrint");
}
}
注意方法名必須以test開頭。否則單元測(cè)試不會(huì)執(zhí)行該函數(shù)。
3. 在migrations目錄下新建2_deploy_hello.js文件。
var Hello = artifacts.require("./Hello.sol");
module.exports = function(deployer) {
deployer.deploy(Hello);
};
測(cè)試
在命令行執(zhí)行truffle test
D:\Android_Code\2018\blockchain\HelloDapp
λ truffle.cmd test
Compiling .\contracts\Hello.sol...
Compiling .\contracts\Migrations.sol...
Compiling .\test\TestHello.sol...
Compiling truffle/Assert.sol...
TestHello
√ testAdd (80ms)
√ testPrint (102ms)
2 passing (1s)
D:\Android_Code\2018\blockchain\HelloDapp
λ truffle.cmd test
Compiling .\contracts\Hello.sol...
Compiling .\contracts\Migrations.sol...
Compiling .\test\TestHello.sol...
Compiling truffle/Assert.sol...
TestHello
√ testAdd (78ms)
√ testPrint (101ms)
2 passing (919ms)
如果只測(cè)試單個(gè)文件,也可以執(zhí)行
truffle.cmd test ./test/TestHello.sol
編譯
D:\Android_Code\2018\blockchain\HelloDapp
λ truffle.cmd compile
Compiling .\contracts\Hello.sol...
Compiling .\contracts\Migrations.sol...
Writing artifacts to .\build\contracts
會(huì)在工程的build目錄下生成對(duì)應(yīng)合約的.json文件,里面描述了合約的具體信息。如果有語(yǔ)法錯(cuò)誤會(huì)輸出錯(cuò)誤提示。
部署合約
如果想部署合約,我們需要連接上一條區(qū)塊鏈。Truffle內(nèi)置了一條私鏈用于測(cè)試。它只能在本地電腦上使用,不能與以太坊主鏈交互。
我們可以使用 truffle.cmd develop命令創(chuàng)建私鏈,并與其交互。
D:\Android_Code\2018\blockchain\HelloDapp
λ truffle.cmd 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) 0x5aeda56215b167893e80b4fe645ba6d5bab767de
Private Keys:
(0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
(1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
(2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
(3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
(4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
(5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
(6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
(7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
(8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
(9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5
Mnemonic: 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(develop)>
默認(rèn)使用端口9545創(chuàng)建了一條測(cè)試鏈,并且為了方便測(cè)試,創(chuàng)建了10個(gè)賬號(hào)。然后進(jìn)入了truffle交互模式里。
接著使用migrate命令。部署合約到連上
truffle(develop)> migrate
Using network 'develop'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0x65c8b0cad9b8ea585a216fbf5e214386373e490ae7737176f01f7dcb7f8b4d00
Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
Saving successful migration to network...
... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
Saving artifacts...
Running migration: 2_deploy_hello.js
Deploying Hello...
... 0x4664bd718a7d9ef33ad9cbffcee02e38d80d0d4103e68b796e3e95c1f5bc0fc4
Hello: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
Saving successful migration to network...
... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0
Saving artifacts...
truffle(develop)>
這就成功部署到鏈上了,并且顯示了交易id和合約地址。
其中0x345ca3e014aaf5dca488057592ee47305d9b3e10就是Hello合約的地址,
0x4664bd718a7d9ef33ad9cbffcee02e38d80d0d4103e68b796e3e95c1f5bc0fc4就是交易id。
與合約交互
truffle(develop)> Hello.address
'0x345ca3e014aaf5dca488057592ee47305d9b3e10'
truffle(develop)> Hello.deployed().then(inst => {HelloInst = inst});
undefined
truffle(develop)> HelloInst.print();
'Hello World'
truffle(develop)> Hello.deployed().then(function(inst) {HelloInst = inst;});
undefined
truffle(develop)> HelloInst.print();
'Hello World'
truffle(develop)> HelloInst.add(10, 200);
BigNumber { s: 1, e: 2, c: [ 210 ] }
truffle(develop)> HelloInst.add(1, 30).then(val => val.toNumber());
31
truffle(develop)> HelloInst.address
'0x345ca3e014aaf5dca488057592ee47305d9b3e10'
支持es6的箭頭寫法簡(jiǎn)化了不少。Hello.address和HelloInst.address都輸了合約的地址
注意:
1.如果合約里寫有構(gòu)造函數(shù)
contract Hello {
function Hello() public {
}
}
如果是使用了新的版本編譯器??赡軙?huì)報(bào)錯(cuò)Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
最新版語(yǔ)言編譯需要這么寫:
contract Hello {
constructors() public {
}
}
2.修改了代碼再執(zhí)行migrate并不會(huì)生效,它只會(huì)部署沒有部署過(guò)的合約。強(qiáng)制重新部署,可執(zhí)行
migrate --reset。如果只想部署某一個(gè)合約,可執(zhí)行migrate -f 2。
3.執(zhí)行truffle,不是truffle.cmd。會(huì)優(yōu)先執(zhí)行到當(dāng)前工程目錄下truffle.js配置文件。所以執(zhí)行truffle.cmd可以避免這個(gè)問(wèn)題。
4.truffle交互模式下,可少輸入truffle。比如系統(tǒng)命令下執(zhí)行truffle migrate,在truffle模式下只用輸入migrate即可。連按2次ctrl+c或輸入.exit可退出交互模式。