環(huán)境準備
ubuntu 16.04甩苛, 64位
還需要安裝以太坊相關的環(huán)境:
- nodejs
- truffle
- solidity
- testrpc
可以參考我之前的一篇文章:
http://blog.csdn.net/pony_maggie/article/details/79531534
另外,本篇還會用到webpack笙隙,安裝教程網(wǎng)上也有很多吼虎。這部分如果不熟悉的話請自行查閱學習下叨叙。需要注意的是本篇我用的webpack版本是3.x重付,本文寫作時webpack4.x已經發(fā)布玄组。4.x改動還是比較大整以,建議大家使用3.x的版本運行本文中的代碼示例胧辽。
編寫智能合約
首先在用戶目錄下新建conference目錄,進入目錄執(zhí)行truffle init公黑,該命令會建立如下的子目錄和文件:
- contracts/: 智能合約存放的目錄邑商,默認情況下已經幫你創(chuàng)建 Migrations.sol合約。
- migrations/: 存放部署腳本
- test/: 存放測試腳本
- truffle.js: truffle的配置文件
修改truffle.js文件凡蚜,改成如下:
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // 匹配任何network id
}
}
};
這里是設置我們稍后要部署智能合約的位置, 否則會報網(wǎng)絡錯誤人断。
開啟一個終端,輸入testrpc運行測試節(jié)點朝蜘。testrpc是一個完整的在內存中的區(qū)塊鏈測試環(huán)境恶迈,啟動 testrpc 經后,會默認創(chuàng)建10個帳號谱醇,Available Accounts是帳號列表暇仲,Private Keys是相對應的帳號密鑰。
進入contracts目錄副渴,這里是存放合約代碼的地方奈附。我們可以使用sublime等工具編寫測試合約代碼。我這里只貼出部分代碼煮剧,文章最后會給出完整源碼的地址斥滤。
pragma solidity ^0.4.19;
contract Conference { // can be killed, so the owner gets sent the money in the end
address public organizer;
mapping (address => uint) public registrantsPaid;
uint public numRegistrants;
uint public quota;
event Deposit(address _from, uint _amount); // so you can log the event
event Refund(address _to, uint _amount); // so you can log the event
function Conference() {
organizer = msg.sender;
quota = 100;
numRegistrants = 0;
}
...
合約內容很簡單,是一個針對會議的智能合約勉盅,通過它參會者可以買票佑颇,組織者可以設置參會人數(shù)上限,以及退款策略菇篡。
編譯部署智能合約
修改migrations下的1_initial_migration.js文件,改成如下:
//var Migrations = artifacts.require("./Migrations.sol");
var Conference = artifacts.require("./Conference.sol");
module.exports = function(deployer) {
//deployer.deploy(Migrations);
deployer.deploy(Conference);
};
編譯一喘,
$ sudo truffle compile --compile-all
注意看下有無報錯驱还。
Truffle僅默認編譯自上次編譯后被修改過的文件,來減少不必要的編譯凸克。如果你想編譯全部文件议蟆,可以使用--compile-all選項。
然后會多出一個build目錄萎战,該目錄下的文件都不要做任何的修改咐容。
部署,
$ sudo truffle migrate --reset
這個命令會執(zhí)行所有migrations目錄下的js文件蚂维。如果之前執(zhí)行過truffle migrate命令戳粒,再次執(zhí)行路狮,只會部署新的js文件,如果沒有新的js文件蔚约,不會起任何作用奄妨。如果使用--reset參數(shù),則會重新的執(zhí)行所有腳本的部署苹祟。
測試下砸抛,在test目錄新增一個conference.js測試文件,
var Conference = artifacts.require("./Conference.sol");
contract('Conference', function(accounts) {
console.log("start testing");
//console.log(accounts);
var owner_account = accounts[0];
var sender_account = accounts[1];
it("Initial conference settings should match", function(done) {
Conference.new({from: owner_account}).then(
function(conference) {
conference.quota.call().then(
function(quota) {
assert.equal(quota, 100, "Quota doesn't match!");
}).then(
function() {
return conference.numRegistrants.call();
}).then(
function(num) {
assert.equal(num, 0, "Registrants doesn't match!");
return conference.organizer.call();
}).then(
function(organizer) {
assert.equal(organizer, owner_account, "Owner doesn't match!");
done();
}).catch(done);
}).catch(done);
});
...
這里只貼出部分代碼树枫,四個測試case直焙,運行truffle test查看測試結果。
$ truffle test
Using network 'development'.
start testing
Contract: Conference
? Initial conference settings should match (191ms)
? Should update quota (174ms)
? Should let you buy a ticket (717ms)
? Should issue a refund by owner only (714ms)
4 passing (2s)
編寫web應用
在conference目錄下執(zhí)行npm init砂轻,然后一路回車奔誓,會生成一個名為package.json的文件,編輯這個文件舔清,在scripts部分增加兩個命令丝里,最終如下:
{
"name": "conference",
"version": "1.0.0",
"description": "",
"main": "truffle-config.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack",
"server": "webpack-dev-server --open"
},
"author": "",
"license": "ISC"
}
package.json文件定義了這個項目所需要的各種模塊,以及項目的配置信息(比如名稱体谒、版本杯聚、許可證等元數(shù)據(jù))。npm 命令根據(jù)這個配置文件抒痒,自動下載所需的模塊幌绍,也就是配置項目所需的運行和開發(fā)環(huán)境。
然后在conference目錄下新建app目錄故响,并創(chuàng)建index.html文件傀广,如下:
<!DOCTYPE html>
<html>
<head>
<title>Conference DApp2</title>
<link rel='stylesheet' type='text/css'>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="./app.js"></script>
</head>
<body>
<h1>Conference DApp</h1>
<div class="section">
Contract deployed at: <div id="confAddress"></div>
</div>
<div class="section">
Organizer: <input type="text" id="confOrganizer" />
</div>
<div class="section">
Quota: <input type="text" id="confQuota" />
<button id="changeQuota">Change</button>
<span id="changeQuotaResult"></span>
</div>
<div class="section">
Registrants: <span id="numRegistrants">0</span>
</div>
<hr/>
</body>
</html>
然后在app目錄下新建javascripts目錄和styleheets目錄,分別存放js腳本文件和css樣式文件彩届。真正和合約交互的就是腳本文件伪冰。
腳本文件名為app.js,部分代碼如下:
import "../stylesheets/app.css";
import { default as Web3 } from 'web3';
import { default as contract } from 'truffle-contract';
import conference_artifacts from '../../build/contracts/Conference.json'
var accounts, sim;
var Conference = contract(conference_artifacts);
window.addEventListener('load', function() {
//alert("aaaaa");
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
Conference.setProvider(web3.currentProvider);
App.start();
$("#changeQuota").click(function() {
var newquota = $("#confQuota").val();
App.changeQuota(newquota);
});
// Wire up the UI elements
});
...
這個代碼我也不打算過多的解釋樟蠕,主要就是用JS加wweb3 API調用合約的函數(shù)而已贮聂。
到這里為止,web部分基本已經準備好了寨辩,我們只需要用webpack打包部署即可吓懈。webpack打包還需要一個配置文件,名為webpack.config.js靡狞,這個文件是告訴webpack打包的規(guī)則耻警,涉及webpack的用法,這里不做過多的解釋。
打包部署web應用
打包部署需要安裝webpack和相關的組件甘穿,安裝的方式有全局安裝和局部安裝兩種腮恩。所謂的局部安裝,是指組件都是安裝在項目的目錄下(conference/node_modules)扒磁。我這里采用的就是局部安裝庆揪。根據(jù)我們項目的實際情況,需要安裝以下組件妨托,
npm install --save-dev webpack@3.0.0
npm install babel-loader --save-dev
npm install babel-core --save-dev
npm install html-loader --save-dev
npm install --save-dev webpack-dev-server@2.11.0
npm install html-webpack-plugin --save-dev
npm install truffle-contract --save-dev
npm install --save-dev style-loader css-loader
環(huán)境裝好缸榛,可以打包了。
$ sudo npm run start
> conference@1.0.0 start /home/pony/ethereum/conference
> webpack
Hash: ec8b764f75c05b477d9d
Version: webpack 3.0.0
Time: 2686ms
Asset Size Chunks Chunk Names
bundle.js 3.36 MB 0 [emitted] [big] main
./index.html 740 bytes [emitted]
[10] (webpack)/buildin/global.js 509 bytes {0} [built]
[16] (webpack)/buildin/module.js 517 bytes {0} [built]
[47] ./app/javascripts/app.js 3.85 kB {0} [built]
[48] ./app/stylesheets/app.css 1.08 kB {0} [built]
[49] ./node_modules/css-loader!./app/stylesheets/app.css 413 bytes {0} [built]
[175] ./build/contracts/Conference.json 71.1 kB {0} [built]
+ 170 hidden modules
Child html-webpack-plugin for "index.html":
[0] ./node_modules/html-webpack-plugin/lib/loader.js!./app/index.html 706 bytes {0} [built]
沒報錯的話兰伤,進入build目錄可以看到bundle.js和index.html兩個文件内颗,這兩個就是最終打包好的網(wǎng)頁文件。
然后部署敦腔,
$ sudo npm run server
> conference@1.0.0 server /home/pony/ethereum/conference
> webpack-dev-server --open
Project is running at http://localhost:8080/
webpack output is served from /
Content not from webpack is served from ./build
404s will fallback to /index.html
Hash: ecae3662137376f80de0
Version: webpack 3.0.0
...
這樣相當于運行了一個小型的nodejs服務器均澳,我們可以在瀏覽器輸入http://localhost:8080/看看效果:
可以看到合約的發(fā)布地址和會議組織者地址(msg.sender)都已經成功的顯示出來了,點擊change按鈕還可以改變quota的值符衔。
本文的代碼我已經上傳到github上找前。
https://github.com/pony-maggie/conference
參考