天天看點

【區塊鍊】Truffle 部署 編譯 測試 智能合約 的 完整實踐操作Truffle 部署 編譯 測試 智能合約 的 完整實踐操作

搭建開發環境

建立一個Truffle項目

編寫智能合約

編譯轉移智能合約

測試智能合約

建立使用者界面連接配接智能合約

在浏覽器中通路Dapp

Node.js v6+ LTS and npm (comes with Node)

Git

這裡配置腳本略過。。。。

/contracts 智能合約

/migrations 遷移系統來處理智能合約部署

/test 測試

truffle.js 配置檔案

其他部分是nodejs的配置檔案以及檔案夾

在<code>contracts</code>中建立<code>Adoption.sol</code>檔案

打開另一個視窗

這裡注意,我的測試demo指定了端口,那麼也要修改對應的配置檔案。

編譯智能合約

檢視項目目錄,會發現<code>migrations</code>目錄中已經存在了一個檔案<code>1_initial_migration.js</code>

我們開始建立第二個檔案<code>2_deploy_contracts.js</code>

執行遷移

同時,你在<code>testrpc</code>上還能看到下面的輸出

在<code>test</code>檔案夾中建立檔案<code>TestAdoption.sol</code>

運作測試指令

同時,在<code>testrpc</code>視窗中,輸出如下内容

打開檔案<code>/src/js/app.js</code>

下面的代碼展示了如何調用web3,調用合約的方法,以及綁定資料到UI

安裝擴充程式

建立測試網絡賬戶

從其他測試網絡錢包轉移eth

【區塊鍊】Truffle 部署 編譯 測試 智能合約 的 完整實踐操作Truffle 部署 編譯 測試 智能合約 的 完整實踐操作

打開<code>bs-config.json</code>檔案

打開<code>package.json</code>檔案,下面配置了lite-server的部分

當運作<code>npm run dev</code>的時候,就會啟動<code>lite-server</code>

報錯了。。。。

然後會自動打開浏覽器,<code>http://localhost:3000/</code>

【區塊鍊】Truffle 部署 編譯 測試 智能合約 的 完整實踐操作Truffle 部署 編譯 測試 智能合約 的 完整實踐操作

點選adopt按鈕,會跳出提示,支付合約費用,完成調用。

Truffle能完成智能合約的整套流程。

Truffle Box 提供了現成的項目,可以快速上手。

Truffle官網文檔夠詳細,點個贊。

<a href="http://truffleframework.com/docs/getting_started/project" target="_blank">http://truffleframework.com/docs/getting_started/project</a>

<a href="http://truffleframework.com/tutorials/pet-shop" target="_blank">http://truffleframework.com/tutorials/pet-shop</a>

<a href="https://github.com/ethereumjs/testrpc" target="_blank">https://github.com/ethereumjs/testrpc</a>

<a href="http://truffleframework.com/boxes/" target="_blank">http://truffleframework.com/boxes/</a>

<a href="https://github.com/trufflesuite/truffle-core/blob/master/lib/testing/Assert.sol" target="_blank">https://github.com/trufflesuite/truffle-core/blob/master/lib/testing/Assert.sol</a>

<a href="https://github.com/ethereum/web3.js/" target="_blank">https://github.com/ethereum/web3.js/</a>

<a href="https://metamask.io/" target="_blank">https://metamask.io/</a>

繼續閱讀