前言
上一篇我們介紹了Geth的安裝午衰,今天我們介紹如何部署智能合約。如果你還沒有看上一篇介紹的內(nèi)容,我建議你先看看上一篇從零開始以太坊區(qū)塊鏈開發(fā)指南一氨菇。部署智能合約首先需要一個由solidity編寫的合約文件以及用于開發(fā)以太坊的框架Truffle
安裝Solc
solc是用來編譯智能合約文件的編譯器诀姚。
npm install -g solc
安裝完成之后檢查是否安裝成功响牛。
$solcjs --version
0.4.19+commit.c4cbbb05.Emscripten.clang
輸出版本號則表示安裝成功。
安裝Truffle
npm install -g truffle
安裝完Truffle之后赫段,我們運行命令呀打。
truffle version
輸出
Truffle v4.0.3 (core: 4.0.3)
Solidity v0.4.18 (solc-js)
表示我們Truffle安裝成功。
創(chuàng)建solidity工程
- 我們選擇一個目錄創(chuàng)建一個solidity工程糯笙,在該目錄下運行
truffle init
創(chuàng)建solidity工程
該命令會自動創(chuàng)建一個工程和相關文件贬丛。并且給我們創(chuàng)建了一個自帶的合約文件Migrations.sol
truffle創(chuàng)建的工程目錄
- 創(chuàng)建vote.sol
在上面由truffle init
創(chuàng)建的contracts文件夾下添加Voting.sol文件,這是一個投票應用,只要你有過編程經(jīng)歷给涕,我相信solidity的語法對你來說毫無難度豺憔,將下面的solidity代碼copy進去
pragma solidity ^0.4.2;
contract Voting{
//voter struct
struct Voter{
bytes32 name;
bool voted;//is voted or not
uint vote;//vote who
uint givenRightTime;//aurth time
uint votetime;//vote time
}
struct Proposal{
bytes32 name;
uint voteCount;
}
address public chairperson;//vote initiator
mapping(address=>Voter)public voters;//voters
Proposal[] public proposals;//can vote to who
//constructor
function Voting(bytes32[] proposalNames) public{
chairperson = msg.sender;
//init proposals
for(uint i = 0;i<proposalNames.length;i++){
proposals.push(Proposal({name:proposalNames[i],voteCount:0}));
}
}
function giveRightToVote(address voter,bytes32 voterName) public{
if(msg.sender != chairperson || voters[voter].voted){
revert();
}
voters[voter].name = voterName;
voters[voter].voted = false;
voters[voter].votetime = 0 ;
voters[voter].givenRightTime = now;
}
//vote
function vote(uint proposalIndex)public{
Voter storage sender = voters[msg.sender];
//check is voted
if(sender.voted){
revert();
}
//modify sender status
sender.voted = true ;
sender.votetime = now;
sender.vote = proposalIndex;
proposals[proposalIndex].voteCount += 1;
}
//get winner
function winningProposalIndex()public constant returns(uint winningProposalIndex){
uint winningVoteCount = 0;
for(uint p = 0 ;p<proposals.length;p++){
if(proposals[p].voteCount > winningVoteCount){
winningVoteCount = proposals[p].voteCount;
winningProposalIndex = p ;
}
}
}
function winnerName()public constant returns(bytes32 winnerName){
winnerName = proposals[winningProposalIndex()].name;
}
}
- 利用Truffle編譯合約
打開工程里面的truffle.js,將下面的配置復制進去。
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: "localhost",
port: 8081,
network_id: "*", // Match any network id
gas: 1000000
}
}
};
host和port是你本地一個以太坊節(jié)點的IP地址和端口,gas是部署需要花費的coin够庙,接著打開工程文件的migrations文件夾下的1_initial_migration.js將你要部署的文件改成Voting.sol
// var Migrations = artifacts.require("./Migrations.sol");
var Voting = artifacts.require("./Voting.sol");
module.exports = function(deployer) {
deployer.deploy(Voting,['hexo','tfboys']);
};
- truffle部署合約
運行
truffle compile
編譯成功
運行 truffle migrate
truffle migrate
正在部署
對應的以太坊節(jié)點上可以看到對應的輸出
以太坊節(jié)點輸出
這證明我們的合約已經(jīng)部署到我們的私有鏈上去了恭应。
運行命令
txpool.status
已經(jīng)被打包的合約準備被部署
我們運行
miner.start()
之后truffle會出現(xiàn)
部署成功
表面我們的合約部署成功了。
好了首启,我們已經(jīng)成功部署了一個合約暮屡,下一章我們將說明如何調(diào)用這個合約。