什么是Truffle
Truffle是一個(gè)世界級(jí)的開(kāi)發(fā)環(huán)境悠反,測(cè)試框架,以太坊的資源管理通道注盈,致力于讓以太坊上的開(kāi)發(fā)變得簡(jiǎn)單晃危,Truffle提供:
- 內(nèi)置的智能合約編譯,鏈接老客,部署和二進(jìn)制文件的管理僚饭。
- 針對(duì)快速迭代開(kāi)發(fā)的自動(dòng)化合約測(cè)試。
- 可腳本化胧砰,可擴(kuò)展的部署與遷移框架鳍鸵。
- 用于部署到任意數(shù)量的公網(wǎng)或私網(wǎng)的網(wǎng)絡(luò)環(huán)境管理。
- 使用EthPM&NPM提供的包管理尉间,使用ERC190標(biāo)準(zhǔn)偿乖。
- 與合約直接通信的交互控制臺(tái)。
- 可配的構(gòu)建流程哲嘲,支持緊密集成贪薪。
- 在Truffle環(huán)境里支持執(zhí)行外部的腳本。
本篇文章將介紹怎樣使用Truffle進(jìn)行智能合約的編譯眠副、部署和測(cè)試画切。
第一步,安裝Truffle
$ sudo npm install -g truffle
安裝完成后執(zhí)行下面的命令囱怕,確保Truffle被正確安裝
zhujiantao@ubuntu:~$ truffle
Truffle v4.1.14 - a development framework for Ethereum
Usage: truffle <command> [options]
Commands:
init Initialize new and empty Ethereum project
compile Compile contract source files
migrate Run migrations to deploy contracts
deploy (alias for migrate)
build Execute build pipeline (if configuration present)
test Run JavaScript and Solidity tests
debug Interactively debug any transaction on the blockchain (experimental)
opcode Print the compiled opcodes for a given contract
console Run a console with contract abstractions and commands available
develop Open a console with a local development blockchain
create Helper to create new contracts, migrations and tests
install Install a package from the Ethereum Package Registry
publish Publish a package to the Ethereum Package Registry
networks Show addresses for deployed contracts on each network
watch Watch filesystem for changes and rebuild the project automatically
serve Serve the build directory on localhost and watch for changes
exec Execute a JS module within this Truffle environment
unbox Download a Truffle Box, a pre-built Truffle project
version Show version number and exit
See more at http://truffleframework.com/docs
第二步霍弹,創(chuàng)建并初始化項(xiàng)目
創(chuàng)建工程目錄并執(zhí)行初始化命令
zhujiantao@ubuntu:~$ mkdir dapp
zhujiantao@ubuntu:~$ cd dapp/
zhujiantao@ubuntu:~/dapp$ truffle init
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
zhujiantao@ubuntu:~/dapp$ tree
.
├── contracts
│ └── Migrations.sol
├── migrations
│ └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js
3 directories, 4 files
執(zhí)行初始化命令后可以看到目錄下生成了一些目錄及文件毫别,其中:contracts目錄下存放合約文件,migrations下存放遷移部署腳本庞萍,test目錄下存放測(cè)試文件拧烦,truffle.js和truffle-config.js為Truffle配置文件
接下來(lái)介紹一下 Truffle Boxes忘闻,Truffle Boxes裝有很多一些實(shí)用的項(xiàng)目模板钝计,能讓你忽略一些環(huán)境配置問(wèn)題,從而使你能更專注于你的dapp特性開(kāi)發(fā)齐佳。此外私恬,Truffle Boxes還包含其他有用的組件, Solidity 合約和庫(kù), 前后端視圖等等。更多可查看官網(wǎng)
那就下載個(gè)模板看看炼吴,MetaCoin是一個(gè)簡(jiǎn)單的代幣應(yīng)用本鸣,以此為例
zhujiantao@ubuntu:~/dapp$ truffle unbox metacoin
Downloading...
Error: Something already exists at the destination. `truffle init` and `truffle unbox` must be executed in an empty folder. Stopping to prevent overwriting data.
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-box/lib/utils/unbox.js:22:1
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
可以看到truffle init和truffle unbox都需要在空文件夾中執(zhí)行,以防止數(shù)據(jù)重寫硅蹦,沒(méi)關(guān)系荣德,重新建一個(gè)工程目錄好了。
zhujiantao@ubuntu:~$ mkdir MetaCoin
zhujiantao@ubuntu:~$ cd MetaCoin/
zhujiantao@ubuntu:~/MetaCoin$ truffle unbox metacoin
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile contracts: truffle compile
Migrate contracts: truffle migrate
Test contracts: truffle test
zhujiantao@ubuntu:~/MetaCoin$ tree
.
├── contracts
│ ├── ConvertLib.sol
│ ├── MetaCoin.sol
│ └── Migrations.sol
├── migrations
│ ├── 1_initial_migration.js
│ └── 2_deploy_contracts.js
├── test
│ ├── metacoin.js
│ └── TestMetacoin.sol
├── truffle-config.js
└── truffle.js
3 directories, 9 files
相比執(zhí)行init童芹,多了關(guān)于metacoin的合約和庫(kù)文件涮瞻,合約部署文件和測(cè)試文件,關(guān)于這些文件的詳細(xì)內(nèi)容將在下篇文章中一一拆解
第三步假褪,啟動(dòng)以太坊客戶端
我們需要安裝Ethereum客戶端來(lái)支持JSON RPC API的調(diào)用署咽。
有許多的以太坊客戶端可以選擇。我們推薦在本地開(kāi)發(fā)和主網(wǎng)部署時(shí)使用不同客戶端生音。
本地開(kāi)發(fā)可以使用Canache宁否、Ethereumjs-testrpc、或者truffle自帶的Truffle Develop缀遍。
主網(wǎng)部署時(shí)使用Geth慕匠。
這里以Ethereumjs-testrpc為例,事實(shí)上域醇,合約在部署到主網(wǎng)之前絮重,都應(yīng)先使用TestRPC進(jìn)行充分測(cè)試。
安裝
$ sudo npm install -g ethereumjs-testrpc
運(yùn)行
zhujiantao@ubuntu:~$ testrpc
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x14b1cf637d214963cd31c923711930aa7b97fd5b
(1) 0x8703ffa009e31e0eebf3aa2dacb32486e06077c0
(2) 0x2237e37056828dfb27e94a3274132a59ae5b4413
(3) 0x1744da91d3eca87fbcf2711740b12e62739965ec
(4) 0x3c35f08665a047f90ddce14d0f82b703a240a56f
(5) 0x2d4fa09b9f71d7b66a9c36d15eeda7311f49b837
(6) 0xc75df94d9a941cbab2a1297bd5770f0c036e4f89
(7) 0x7aaa5ff5f64970268183e06f56110a30571f753f
(8) 0x7c3b0b8193fff996b01bc77154fb91f3b361a646
(9) 0x2a5c667ba684bf0b7db61d7dec17751a6f6e03f8
Private Keys
==================
(0) 237fda9ce07f11cff672e58f633b514575613e96cb2653ea9d08ca69e882f042
(1) 50ea263a896e3b4ec3337ede58131e71dfdd71e7afa879b0957f3a3443b14b85
(2) dd251d5192bb0a73c1a9896f394099d646f6c8e2e96b88e7288f4e83d70a7392
(3) 3409d7a67896bbf2e6a8c9e414bb75284cf87fc66b9bb0e222cb8648a494709e
(4) e1229c76e7c1e5aad682c8b27da5d1a67171c7b3ad560fdb52cb1ae3855cc31d
(5) b627f6f491e8ed270733cd2483287cfa092d0b56b15de0f838ffc06257416c5b
(6) b25209ef82d3a44205d72b418b86476ac710afa1dcc582525954bc31db6c6832
(7) 20b540aa45c394306d38c1d37458d9c46d404311f822fc149fcd523c2e34d84d
(8) 838e1a99cca13ced482d9af2dbba6911a4ee3b676baa395a1462fd29fec070a5
(9) f586c3fe792d63b7459e399454d1798c21eb191db861aff67d9ec771cc22f83a
HD Wallet
==================
Mnemonic: virus bamboo couch ankle afraid glide family attend empty bulb stadium cricket
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
第四步歹苦,編譯部署合約
編輯配置文件truffle.js青伤,保證配置文件中指定網(wǎng)絡(luò)和你運(yùn)行的網(wǎng)絡(luò)一致。
這里要注意的是:Ganache默認(rèn)運(yùn)行在7545端口殴瘦,Ethereumjs-testrpc 默認(rèn)運(yùn)行在8545端口狠角,Truffle Develop 默認(rèn)運(yùn)行在9545端口
module.exports = {
networks: {
development: {
host: 'localhost',
port: '8545',
network_id: '*' // Match any network id
}
}
};
使用truffle compile命令編譯,生成build文件夾
zhujiantao@ubuntu:~/MetaCoin$ truffle compile
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts
zhujiantao@ubuntu:~/MetaCoin$ tree
.
├── build
│ └── contracts
│ ├── ConvertLib.json
│ ├── MetaCoin.json
│ └── Migrations.json
├── contracts
│ ├── ConvertLib.sol
│ ├── MetaCoin.sol
│ └── Migrations.sol
├── migrations
│ ├── 1_initial_migration.js
│ └── 2_deploy_contracts.js
├── test
│ ├── metacoin.js
│ └── TestMetacoin.sol
├── truffle-config.js
└── truffle.js
5 directories, 12 files
部署合約
zhujiantao@ubuntu:~/MetaCoin$ truffle migrate
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0xf8e61ddf4663ae812678e0a62324911866b3b38c48d933c50c554a17f56da4f0
Migrations: 0xb8d96e14501462f922b05d08bd1e461f59e71b77
Saving successful migration to network...
... 0xe6806b8ded1054326b5b795828c5306f186e584794732cba9f1fb50bd8e1dad8
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying ConvertLib...
... 0xddac202a2dc8dbe32b96867080e64ce6931dd05e3e2a9b1b29ef7b7970615a3b
ConvertLib: 0xef4d768c8cb7dec157d40e06af71598176b15c5b
Linking ConvertLib to MetaCoin
Deploying MetaCoin...
... 0x4c2d8dcc99ffb631208a5568140e194515b03f38b9019d9c833770cfb3c262b6
MetaCoin: 0xbc2104e9f774ea0a1d5f708401ff71ba451670b3
Saving successful migration to network...
... 0xabacbc322a9af2d359a49cdbc0e5a4ab55914956f95b4bc177b1789621bea3c9
Saving artifacts...
第五步蚪腋,測(cè)試合約
zhujiantao@ubuntu:~/MetaCoin$ truffle test
Using network 'development'.
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./test/TestMetacoin.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...
TestMetacoin
? testInitialBalanceUsingDeployedContract (88ms)
? testInitialBalanceWithNewMetaCoin (94ms)
Contract: MetaCoin
? should put 10000 MetaCoin in the first account
? should call a function that depends on a linked library (50ms)
? should send coin correctly (128ms)
5 passing (1s)
可以看到測(cè)試結(jié)果和用時(shí)等相關(guān)信息丰歌。
那么今天就先到這里啦姨蟋,接下來(lái),分析metacoin和webpack代碼立帖,學(xué)習(xí)怎樣編寫合約眼溶、遷移腳本和測(cè)試腳本,七夕快樂(lè)晓勇,嗒嗒嗒堂飞。