truffle是以太坊(ethereum)開發智能合約(smart contract)過程當中最受歡迎的框架,本教程來安裝構建一個基本的Truffle項目並部署一個智能合約到區塊鏈。node
開始本文以前但願你已經瞭解區塊鏈、以太坊、智能合約等基本概念。web
安裝 Truffle 框架很是的簡單,只須要一行命令:mongodb
npm install -g truffle
固然前提是你已經安裝好了NodeJS而且版本要在5.0以上。npm
Tunffle還要求一個運行的以太坊客戶端,以便支持標準的JSON RPC API,有不少的選擇好比Ganache、geth。網絡
要使用大量的Tunffle命令,咱們經過使用一個現成的Tunffle項目來學習。第一步是建立一個Truffle項目。app
咱們能夠建立一個空的項目模板,不過剛開始構建項目,咱們可使用Tunffle Boxs,裏面有不少的示例應用程序和項目模板。本文使用MetaCoin box,它建立一個能夠在賬戶之間傳輸代幣的應用程序示例。框架
1.爲構建Truffle項目建立新目錄:工具
mkdir MetaCoin cd MetaCoin
2.下載 MetaCoin box,使用truffle unbox <box-name>
來下載各類示例,若是要建一個空的不包括智能合約的項目可使用truffle init
。學習
truffle unbox metacoin
上述命令完成後,咱們得到一個有如下目錄結構的項目:區塊鏈
打開contracts/MetaCoin.sol
,這是一個用Solidity編寫的智能合約文件,這個智能合約建了Metacoin代幣,咱們能夠注意到它引用了同目錄下的另一個solidity編寫的文件contracts/ConvertLib.sol
。
打開contracts/Migrations.sol
,這是一個單獨的Solidity文件,用來管理和更新部署的智能合同的狀態。這個文件每一個Tunffle項目都有,一般不用管。
打開migrations/1_initial_deployment.js
文件,這個腳本是爲了部署Migrations.sol
文件中的Migrations
合約。
打開migrations/2_deploy_contracts.js
文件,這個腳本是爲了部署MetaCoin
合約,會按順序執行完上一步的腳本後執行。
打開test/TestMetacoin.sol
文件,這是一個Solidity編寫的測試文件,確保你的合約正常工做。
打開test/metacoin.js
文件,這個腳本與上面的測試文件相似。
打開truffle.js
文件,用於設置網絡信息和其餘與項目相關的內容。文件是空白的,不要緊,由於咱們將使用一個內置有默認值的Truffle命令。
1.打開終端,執行命令:
truffle test ./test/TestMetacoin.sol
輸出結果是這樣的:
TestMetacoin √ testInitialBalanceUsingDeployedContract (71ms) √ testInitialBalanceWithNewMetaCoin (59ms) 2 passing (794ms)
2.運行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
輸出結果是這樣的:
Compiling .\contracts\ConvertLib.sol... Compiling .\contracts\MetaCoin.sol... Compiling .\contracts\Migrations.sol... Writing artifacts to .\build\contracts
當使用Tuffle開發一個多功能的私有鏈和控制檯時也可使用ganache,它做爲一個桌面應用程序來啓動私有鏈。對於以太坊和區塊鏈新手來講ganache是一個更容易理解的工具,由於它會顯示更多的信息給咱們。
除了運行Ganache以外,須要額外編輯一下Tunffle配置文件,以指向ganache實例。
1.下載和安裝 Ganache
2.打開 truffle.js
,用下面的內容替換:
module.exports = { networks: { development: { host: "127.0.0.1", port: 7545, network_id: "*" } } };
這樣就可使用Ganache的默認參數進行鏈接了。
3.保存一下這個文件。
4.啓動 Ganache
5.打開終端,用Ganache部署智能合約到區塊鏈
truffle migrate
輸出結果是這樣的:
Using network 'development'. Running migration: 1_initial_migration.js Replacing Migrations... ... 0x63b393bd50251ec5aa3e159070609ee7c61da55531ff5dea5b869e762263cb90 Migrations: 0xd6d1ea53b3a7dae2424a0525d6b1754045a0df9f Saving successful migration to network... ... 0xe463b4cb6a3bbba06ab36ac4d7ce04e2a220abd186c8d2bde092c3d5b2217ed6 Saving artifacts... Running migration: 2_deploy_contracts.js Replacing ConvertLib... ... 0xa59221bc26a24f1a2ee7838c36abdf3231a2954b96d28dd7def7b98bbb8a7f35 ConvertLib: 0x33b217190208f7b8d2b14d7a30ec3de7bd722ac6 Replacing MetaCoin... ... 0x5d51f5dc05e5d926323d580559354ad39035f16db268b91b6db5c7baddef5de5 MetaCoin: 0xcd2c65cc0b498cb7a3835cfb1e283ccd25862086 Saving successful migration to network... ... 0xeca6515f3fb47a477df99c3389d3452a48dfe507980bfd29a3c57837d6ef55c5 Saving artifacts...
內容顯示的是交易id和你所部署的智能合約地址。
6.在Ganache中,點擊Transactions
按鈕能夠看到被處理的交易。
7.要與合約進行交互,可使用Truffle控制檯。相似於Truffle Develop,惟一不一樣的是它鏈接到現有區塊鏈(在這種狀況下,由Ganache生成的)
truffle console
你會看到下面的提示:
truffle(development)>
使用控制檯經過下面的方式進行交互:
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});
MetaCoin.deployed().then(function(instance){return instance.getBalanceInEth(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});
MetaCoin.deployed().then(function(instance){return instance.sendCoin(web3.eth.accounts[1], 500);});
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[1]);}).then(function(value){return value.toNumber()});
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});
ok,Truffle快速入門的教程就到這裏,若是還有疑問能夠訪問咱們的在線互動課程:
原文轉載:《使用truffle部署以太坊智能合約到區塊鏈》