mkdir hello_world_voting
安装testrpc 和web3
npm install ethereumjs-testrpc web3@0.20.1
启动testrpc
node_modules/.bin/testrpc
----------------------------------------------------------------------------------------------
编写Voting代码.....
pragma solidity ^0.4.17;
contract Voting {
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;
function Voting(bytes32[] 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;
}
}
----------------------------------------------------------------------------------------------
安装solc
npm install solc
使用web3与blockchain交互
node
> Web3 = require('web3')
#使用server地址初始化Web3
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
与blockchain交互
web3.eth.accounts #会显示出测试环境的所有账号列表,默认每个账号会有1000个ETH并且不需要密码操作
编译contract
code = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)
部署contract
//合约接口
abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
//获取编译后的可执行机器码
byteCode = compiledCode.contracts[':Voting'].bytecode
//部署合约,new来执行代码的构造函数,传入需要的参数。data指定机器码,from指定合约发布人,gas消费
deployedContract = VotingContract.new(['A','B','C'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
//发布成功才能获取合约地址
deployedContract.address
获取合约实例
contractInstance = VotingContract.at(deployedContract.address)
调用合约
//查询某个用户票数,两种方法相同
contractInstance.voteForCandidate('A', {from: web3.eth.accounts[2]})
安装testrpc 和web3
npm install ethereumjs-testrpc web3@0.20.1
启动testrpc
node_modules/.bin/testrpc
----------------------------------------------------------------------------------------------
编写Voting代码.....
pragma solidity ^0.4.17;
contract Voting {
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;
function Voting(bytes32[] 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;
}
}
----------------------------------------------------------------------------------------------
安装solc
npm install solc
使用web3与blockchain交互
node
> Web3 = require('web3')
#使用server地址初始化Web3
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
与blockchain交互
web3.eth.accounts #会显示出测试环境的所有账号列表,默认每个账号会有1000个ETH并且不需要密码操作
编译contract
code = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)
部署contract
//合约接口
abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
//获取编译后的可执行机器码
byteCode = compiledCode.contracts[':Voting'].bytecode
//部署合约,new来执行代码的构造函数,传入需要的参数。data指定机器码,from指定合约发布人,gas消费
deployedContract = VotingContract.new(['A','B','C'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
//发布成功才能获取合约地址
deployedContract.address
获取合约实例
contractInstance = VotingContract.at(deployedContract.address)
调用合约
//查询某个用户票数,两种方法相同
contractInstance.totalVotesFor.call('A')
contractInstance.totalVotesFor('A').toString()
contractInstance.voteForCandidate('A', {from: web3.eth.accounts[2]})