教程:
區(qū)塊鏈技術(shù)(一):Truffle開發(fā)入門 (適用于Truffle v4.1.0)
http://wangxiaoming.com/blog/2016/04/30/blockchain-tech-truffle/
APR 30TH, 2016
以太坊是區(qū)塊鏈開發(fā)領(lǐng)域最好的編程平臺,而truffle是以太坊(Ethereum)最受歡迎的一個開發(fā)框架呼巴,這是我們第一篇區(qū)塊鏈技術(shù)文章介紹truffle的原因茁肠,實(shí)戰(zhàn)是最重要的事情患民,這篇文章不講原理,只搭建環(huán)境垦梆,運(yùn)行第一個區(qū)塊鏈程序(Dapp)匹颤。
//教程跟實(shí)際軟件的最新版本已經(jīng)有非常大的不同了⊥行桑基本已經(jīng)不適用了印蓖,下面按照我實(shí)際進(jìn)行的記錄。
安裝truffle
$ npm install -g truffle
//最新版跟教程的版本升級了很多京腥。
$ truffle version
Truffle v4.1.0 (core: 4.1.0)
Solidity v0.4.19 (solc-js)
依賴環(huán)境 NodeJS 訪問https://nodejs.org 官方網(wǎng)站下載安裝
系統(tǒng):Windows, Linux or Mac OS X赦肃,推薦Mac OS X,不建議使用Windows公浪,會碰到各種各樣的問題他宛,導(dǎo)致放棄。
需要安裝Ethereum客戶端欠气,來支持JSON RPC API調(diào)用 開發(fā)環(huán)境厅各,推薦使用EthereumJS TestRPC: https://github.com/ethereumjs/testrpc
安裝命令: $ npm install -g ethereumjs-testrpc
新建第一個項(xiàng)目
$ mkdir ding
$ cd ding
$ truffle init
默認(rèn)會生成一個MetaCoin的demo,可以從這個demo中學(xué)習(xí)truffle的架構(gòu)
//app目錄沒有了晃琳。所以默認(rèn)的init命令的模板已經(jīng)變的不完整了讯检。
重來找到一個新的模板項(xiàng)目。
$ truffle unbox webpack
--------------------------webpack在github上的說明---------------------------------
Webpack Truffle Box
This box it our most bare official implementation with Webpack. Includes contracts, migrations, tests, user interface and webpack build pipeline.
Installation
Install Truffle globally.
npm install -g truffle
Download the box. This also takes care of installing the necessary dependencies.
truffle unbox webpack
Run the development console.
truffle develop
Compile and migrate the smart contracts. Note inside the development console we don't preface commands with truffle.
compile
migrate
Run the webpack server for front-end hot reloading (outside the development console). Smart contract changes must be manually recompiled and migrated.
// Serves the front-end on http://localhost:8080
npm run dev
Truffle can run tests written in Solidity or JavaScript against your smart contracts. Note the command varies slightly if you're in or outside of the development console.
// If inside the development console.
test
// If outside the development console..
truffle test
-----------------------------------------------------------------------------------------
//下面繼續(xù)卫旱,進(jìn)入開發(fā)模式人灼。
$ truffle develop
//編譯項(xiàng)目
> compile
//部署智能合約
> migrate
//卡了我一天的步驟,
>test
TestMetacoin
? ? ? testInitialBalanceUsingDeployedContract (370ms)
? ? ? testInitialBalanceWithNewMetaCoin (305ms)
? Contract: MetaCoin
? ? ? should put 10000 MetaCoin in the first account (61ms)
? ? ? should call a function that depends on a linked library (361ms)
? ? ? should send coin correctly (374ms)
? 5 passing (5s)
$npm run dev
//啟動服務(wù)后顾翼,可以在瀏覽器訪問項(xiàng)目: http://localhost:8080/ 投放,
//可以實(shí)現(xiàn)從第一個賬戶中給其他賬戶發(fā)送你的新幣~~
//有興趣可以修改 app/index.html里面的內(nèi)容~~
好了,第一個區(qū)塊鏈程序跑起來了适贸,后面可以不斷地實(shí)踐深入學(xué)習(xí)了灸芳。有問題歡迎聯(lián)系我交流涝桅。