1.环境搭建
ganache-cli
以上环境基于以太坊基础环境搭建(1)搭建。从零搭建环境的老铁注意环境的差异,版本不同会导致恶心问题的出现。
jiangwei@jiangwei:~$ mkdir Voting_Dapp
jiangwei@jiangwei:~$ cd Voting_Dapp/
#用npm命令安装ganache、web3js solc
jiangwei@jiangwei:~/Voting_Dapp$ npm install ganache-cli web3@0.20.1 solc@0.4.22
#启动ganache客户端
jiangwei@jiangwei:~/Voting_Dapp$ node_modules/.bin/ganache-cli
2.部署合约到ganache
编写合约
vote.sol
pragma solidity >=0.4.22;
contract Voting{
mapping (bytes32=>uint8) public votesReceived;
bytes32[] public candidateList;
constructor(bytes32[] memory candidateNames) public {
candidateList = candidateNames;
}
function totalVotesFor(bytes32 candidate) view public returns (uint8) {
require(validCandidate(candidate));
return votesReceived[candidate];
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate));
votesReceived[candidate] += 1;
}
function validCandidate(bytes32 candidate) view public returns (bool){
for(uint i=0; i<candidateList.length; i++){
if(candidateList[i] == candidate){
return true;
}
}
return false;
}
}
部署合约
合约编译
jiangwei@jiangwei:~/Voting_Dapp$ node
> var Web3 = require('web3')
> var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
> web3.eth.accounts
[ '0x95829474604bf598db577fc974b48c6e5b52923f',
'0x314e179a5fa497e0a5d844af79058ff3460f49e6',
'0xc5ba38f22c02e1e60f0e5f73e128a5667a66d01d',
'0xcdd0f81aafe50c6fdad3a36967f02b939ec7d427',
'0x4a069e645caff79f8e8c354d25ef23aa0cf6f7f7',
'0x11a4f7dac5256fe6f4ee4a1917ce33292c6cf3b4',
'0x179e75f78b83b5133f316aa013ad72aa88a47cf1',
'0x02a18d196293561f9af6f95519c369b62d1a68c4',
'0x72723d6e5e917c22aa2f4e199a67ad23b63faedf',
'0x5399ff2b2a016498fe5cf038e0834b5c2e51062a' ]
> code = fs.readFileSync('vote.sol').toString();
'pragma solidity >=0.4.22;\n\ncontract Voting{\n mapping (bytes32=>uint8) public votesReceived;\n bytes32[] public candidateList;\n constructor(bytes32[] memory candidateNames) public {\n candidateList = candidateNames;\n }\n\n function totalVotesFor(bytes32 candidate) view public returns (uint8) {\n require(validCandidate(candidate));\n return votesReceived[candidate];\n }\n\n function voteForCandidate(bytes32 candidate) public {\n require(validCandidate(candidate));\n votesReceived[candidate] += 1;\n }\n\n\tfunction validCandidate(bytes32 candidate) view public returns (bool){\n\t\tfor(uint i=0; i<candidateList.length; i++){\n\t\t\tif(candidateList[i] == candidate){\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}\n\n'
> solc = require('solc');
{
version: [Function],
semver: [Function: versionToSemver],
license: [Function],
lowlevel:
{
compileSingle: null,
compileMulti: null,
compileCallback: null,
compileStandard: [Function: compileStandard] },
features:
{
legacySingleInput: false,
multipleInputs: true,
importCallback: true,
nativeStandardJSON: true },
compile: [Function: compileStandardWrapper],
loadRemoteVersion: [Function: loadRemoteVersion],
setupMethods: [Function: setupMethods] }
> compiledCode = solc.compile(code);
{
contracts:
{
':Voting':
{
assembly: [Object],
bytecode:
'608060405234801561001057600080fd5b50604051610412380380610412833981018060405281019080805182019291905050508060019080519060200190610049929190610050565b50506100c8565b828054828255906000526020600020908101928215610092579160200282015b82811115610091578251829060001916905591602001919060010190610070565b5b50905061009f91906100a3565b5090565b6100c591905b808211156100c15760008160009055506001016100a9565b5090565b90565b61033b806100d76000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632f265cf714610072578063392e6678146100bd5780637021939f14610106578063b13c744b14610151578063cc9ab2671461019a575b600080fd5b34801561007e57600080fd5b506100a160048036038101908080356000191690602001909291905050506101cb565b604051808260ff1660ff16815260200191505060405180910390f35b3480156100c957600080fd5b506100ec6004803603810190808035600019169060200190929190505050610210565b604051808215151515815260200191505060405180910390f35b34801561011257600080fd5b50610135600480360381019080803560001916906020019092919050505061026f565b604051808260ff1660ff16815260200191505060405180910390f35b34801561015d57600080fd5b5061017c6004803603810190808035906020019092919050505061028f565b60405180826000191660001916815260200191505060405180910390f35b3480156101a657600080fd5b506101c960048036038101908080356000191690602001909291905050506102b2565b005b60006101d682610210565b15156101e157600080fd5b600080836000191660001916815260200190815260200160002060009054906101000a900460ff169050919050565b600080600090505b60018054905081101561026457826000191660018281548110151561023957fe5b90600052602060002001546000191614156102575760019150610269565b8080600101915050610218565b600091505b50919050565b60006020528060005260406000206000915054906101000a900460ff1681565b600181