1.新建文件夹
zy@ubuntu:~/Desktop$ cd test
zy@ubuntu:~/Desktop/test$ ls
contract genesis.json go-ethereum web3
zy@ubuntu:~/Desktop/test$ mkdir simple_vote_dapp
zy@ubuntu:~/Desktop/test$ cd simple_vote_dapp/
zy@ubuntu:~/Desktop/test/simple_vote_dapp$ ls
zy@ubuntu:~/Desktop/test/simple_vote_dapp$ npm init
zy@ubuntu:~/Desktop/test/simple_vote_dapp$ ls
package.json
zy@ubuntu:~/Desktop/test/simple_vote_dapp$ cat package.json
2.ganache安装 模拟区块链,相对geth更加方便的测试平台
/simple_vote_dapp$ npm i --save ganache-cli
/simple_vote_dapp$ node_modules/.bin/ganache-cli
版本v6.12.2
自动有10个账户 每个有100个以太
3.安装 npm、web3@0.20.1 、solc,查看版本
/simple_vote_dapp$ sudo npm install web3
4.智能合约编写
初始化候选者 进行投票+1 返回投票总数
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract Voting{ bytes32[] public candidateList; //候选人数组 mapping(bytes32=>uint8) public votesReceived; //得票数 constructor( bytes32[] memory candidateListName ) { candidateList=candidateListName; } //判断投票人是否在数组中 function validateCandidate(bytes32 candidateName) internal view returns(bool){ for(uint8 i=0;i<candidateList.length;i++){ if(candidateName==candidateList[i]) return true; } return false; } //投票功能 function vote(bytes32 candidateName) public { require(validateCandidate(candidateName)); votesReceived[candidateName]+=1; } //返回候选人的票数 function totalVotesFor(bytes32 candidateName) public view returns(uint8){ require(validateCandidate(candidateName)); return votesReceived[candidateName]; } }
5.将合约放入Ubuntu
文章来源地址https://www.toymoban.com/news/detail-425471.html
/test/simple_vote_dapp$ vi Voting.sol
/test/simple_vote_dapp$ cat Voting.sol
6.启动ganache
node_modules/.bin/ganache-cli
7.编译合约 连接ganache 获取测试链上的账户号
进入node
> var Web3=require('web3')
undefined
> var web3= new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
undefined
> web3.isConnected()
true
> web3.eth.accounts
[
'0x31dee8501f5faecca08344d9e3830e4c8b98b631',
'0x6266b55095211f34fbb13f9d294d8b863159155f',
'0x7f0497d982764d52751ef5eba764274cc627c79f',
'0xde39a69a9b2f8fe87c3195a9f8a55216ebc9c9f1',
'0xaa741688c7dbbbb69dd8ee2fbe9303cc19f9706f',
'0xade58d4916975ab26b368d3303a0937e3b55f891',
'0xee45d986046bcb8d3a29355467f9515af513c271',
'0x1419917397d267155a4e33de191a9dcf9ec095f3',
'0xebea0e81fe9db102ecd0cf3f25b079e30a29d20e',
'0xb2d3856a6692cc65f74f7ca467e38832b54cb6e9'
]
> var solc = require('solc')
undefined
// 读取solidity文件,采用同步读取为字符串类型
> var sourceCode= fs.readFileSync('Voting.sol').toString()
undefined
// solc编译solidity文件
> var compileCode = solc.compile(sourceCode)
undefined
> compileCode
{
contracts: {
':Voting': {
assembly: [Object],
bytecode: '608060405234801561001057600080fd5b506040516103c53803806103c5833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100c8565b828054828255906000526020600020908101928215610092579160200282015b82811115610091578251829060001916905591602001919060010190610070565b5b50905061009f91906100a3565b5090565b6100c591905b808211156100c15760008160009055506001016100a9565b5090565b90565b6102ee806100d76000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634db33f92146100675780634dfb9c80146100b2578063a69beaba146100fd578063b13c744b1461012e575b600080fd5b34801561007357600080fd5b506100966004803603810190808035600019169060200190929190505050610177565b604051808260ff1660ff16815260200191505060405180910390f35b3480156100be57600080fd5b506100e16004803603810190808035600019169060200190929190505050610197565b604051808260ff1660ff16815260200191505060405180910390f35b34801561010957600080fd5b5061012c60048036038101908080356000191690602001909291905050506101dd565b005b34801561013a57600080fd5b506101596004803603810190808035906020019092919050505061023a565b60405180826000191660001916815260200191505060405180910390f35b60016020528060005260406000206000915054906101000a900460ff1681565b60006101a28261025d565b15156101ad57600080fd5b60016000836000191660001916815260200190815260200160002060009054906101000a900460ff169050919050565b6101e68161025d565b15156101f157600080fd5b6001806000836000191660001916815260200190815260200160002060008282829054906101000a900460ff160192506101000a81548160ff021916908360ff16021790555050565b60008181548110151561024957fe5b906000526020600020016000915090505481565b600080600090505b6000805490508160ff1610156102b75760008160ff1681548110151561028757fe5b906000526020600020015460001916836000191614156102aa57600191506102bc565b8080600101915050610265565b600091505b509190505600a165627a7a72305820fd465411c8ec744911a25690bde10ff6a011d08a9106d57e4829f6018eea48d20029',
functionHashes: [Object],
gasEstimates: [Object],
interface: '[{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votingMap","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidateListName","type":"bytes32"}],"name":"totalVoters","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidateListName","type":"bytes32"}],"name":"vote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"candidateListName","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]',
metadata: '{"compiler":{"version":"0.4.22+commit.4cb486ee"},"language":"Solidity","output":{"abi":[{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votingMap","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidateListName","type":"bytes32"}],"name":"totalVoters","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidateListName","type":"bytes32"}],"name":"vote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"candidateListName","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"":"Voting"},"evmVersion":"byzantium","libraries":{},"optimizer":{"enabled":false,"runs":200},"remappings":[]},"sources":{"":{"keccak256":"0xbc6dcd5acbb1adb539bfc1492050933e0a5c6600cd37507b82e207a2d20ae804","urls":["bzzr://8f9330873abf413b8b00637aa9e468f951c91d351e0c8ecd030797a83b977047"]}},"version":1}',
opcodes: 'PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x40 MLOAD PUSH2 0x3C5 CODESIZE SUB DUP1 PUSH2 0x3C5 DUP4 CODECOPY DUP2 ADD DUP1 PUSH1 0x40 MSTORE DUP2 ADD SWAP1 DUP1 DUP1 MLOAD DUP3 ADD SWAP3 SWAP2 SWAP1 POP POP POP DUP1 PUSH1 0x0 SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH2 0x49 SWAP3 SWAP2 SWAP1 PUSH2 0x50 JUMP JUMPDEST POP POP PUSH2 0xC8 JUMP JUMPDEST DUP3 DUP1 SLOAD DUP3 DUP3 SSTORE SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 DUP2 ADD SWAP3 DUP3 ISZERO PUSH2 0x92 JUMPI SWAP2 PUSH1 0x20 MUL DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0x91 JUMPI DUP3 MLOAD DUP3 SWAP1 PUSH1 0x0 NOT AND SWAP1 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x70 JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0x9F SWAP2 SWAP1 PUSH2 0xA3 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH2 0xC5 SWAP2 SWAP1 JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0xC1 JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0xA9 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST SWAP1 JUMP JUMPDEST PUSH2 0x2EE DUP1 PUSH2 0xD7 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH2 0x62 JUMPI PUSH1 0x0 CALLDATALOAD PUSH29 0x100000000000000000000000000000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF AND DUP1 PUSH4 0x4DB33F92 EQ PUSH2 0x67 JUMPI DUP1 PUSH4 0x4DFB9C80 EQ PUSH2 0xB2 JUMPI DUP1 PUSH4 0xA69BEABA EQ PUSH2 0xFD JUMPI DUP1 PUSH4 0xB13C744B EQ PUSH2 0x12E JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x73 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x96 PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD PUSH1 0x0 NOT AND SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x177 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 PUSH1 0xFF AND PUSH1 0xFF AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0xBE JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0xE1 PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD PUSH1 0x0 NOT AND SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x197 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 PUSH1 0xFF AND PUSH1 0xFF AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x109 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x12C PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD PUSH1 0x0 NOT AND SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x1DD JUMP JUMPDEST STOP JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x13A JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x159 PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x23A JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 PUSH1 0x0 NOT AND PUSH1 0x0 NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x1 PUSH1 0x20 MSTORE DUP1 PUSH1 0x0 MSTORE PUSH1 0x40 PUSH1 0x0 KECCAK256 PUSH1 0x0 SWAP2 POP SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH1 0xFF AND DUP2 JUMP JUMPDEST PUSH1 0x0 PUSH2 0x1A2 DUP3 PUSH2 0x25D JUMP JUMPDEST ISZERO ISZERO PUSH2 0x1AD JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x1 PUSH1 0x0 DUP4 PUSH1 0x0 NOT AND PUSH1 0x0 NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP1 DUP2 MSTORE PUSH1 0x20 ADD PUSH1 0x0 KECCAK256 PUSH1 0x0 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH1 0xFF AND SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH2 0x1E6 DUP2 PUSH2 0x25D JUMP JUMPDEST ISZERO ISZERO PUSH2 0x1F1 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x1 DUP1 PUSH1 0x0 DUP4 PUSH1 0x0 NOT AND PUSH1 0x0 NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP1 DUP2 MSTORE PUSH1 0x20 ADD PUSH1 0x0 KECCAK256 PUSH1 0x0 DUP3 DUP3 DUP3 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH1 0xFF AND ADD SWAP3 POP PUSH2 0x100 EXP DUP2 SLOAD DUP2 PUSH1 0xFF MUL NOT AND SWAP1 DUP4 PUSH1 0xFF AND MUL OR SWAP1 SSTORE POP POP JUMP JUMPDEST PUSH1 0x0 DUP2 DUP2 SLOAD DUP2 LT ISZERO ISZERO PUSH2 0x249 JUMPI INVALID JUMPDEST SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 ADD PUSH1 0x0 SWAP2 POP SWAP1 POP SLOAD DUP2 JUMP JUMPDEST PUSH1 0x0 DUP1 PUSH1 0x0 SWAP1 POP JUMPDEST PUSH1 0x0 DUP1 SLOAD SWAP1 POP DUP2 PUSH1 0xFF AND LT ISZERO PUSH2 0x2B7 JUMPI PUSH1 0x0 DUP2 PUSH1 0xFF AND DUP2 SLOAD DUP2 LT ISZERO ISZERO PUSH2 0x287 JUMPI INVALID JUMPDEST SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 ADD SLOAD PUSH1 0x0 NOT AND DUP4 PUSH1 0x0 NOT AND EQ ISZERO PUSH2 0x2AA JUMPI PUSH1 0x1 SWAP2 POP PUSH2 0x2BC JUMP JUMPDEST DUP1 DUP1 PUSH1 0x1 ADD SWAP2 POP POP PUSH2 0x265 JUMP JUMPDEST PUSH1 0x0 SWAP2 POP JUMPDEST POP SWAP2 SWAP1 POP JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 REVERT 0x46 SLOAD GT 0xc8 0xec PUSH21 0x4911A25690BDE10FF6A011D08A9106D57E4829F601 DUP15 0xea 0x48 0xd2 STOP 0x29 ',
runtimeBytecode: '608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634db33f92146100675780634dfb9c80146100b2578063a69beaba146100fd578063b13c744b1461012e575b600080fd5b34801561007357600080fd5b506100966004803603810190808035600019169060200190929190505050610177565b604051808260ff1660ff16815260200191505060405180910390f35b3480156100be57600080fd5b506100e16004803603810190808035600019169060200190929190505050610197565b604051808260ff1660ff16815260200191505060405180910390f35b34801561010957600080fd5b5061012c60048036038101908080356000191690602001909291905050506101dd565b005b34801561013a57600080fd5b506101596004803603810190808035906020019092919050505061023a565b60405180826000191660001916815260200191505060405180910390f35b60016020528060005260406000206000915054906101000a900460ff1681565b60006101a28261025d565b15156101ad57600080fd5b60016000836000191660001916815260200190815260200160002060009054906101000a900460ff169050919050565b6101e68161025d565b15156101f157600080fd5b6001806000836000191660001916815260200190815260200160002060008282829054906101000a900460ff160192506101000a81548160ff021916908360ff16021790555050565b60008181548110151561024957fe5b906000526020600020016000915090505481565b600080600090505b6000805490508160ff1610156102b75760008160ff1681548110151561028757fe5b906000526020600020015460001916836000191614156102aa57600191506102bc565b8080600101915050610265565b600091505b509190505600a165627a7a72305820fd465411c8ec744911a25690bde10ff6a011d08a9106d57e4829f6018eea48d20029',
srcmap: '26:851:0:-;;;139:97;8:9:-1;5:2;;;30:1;27;20:12;5:2;139:97:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;212:17;196:13;:33;;;;;;;;;;;;:::i;:::-;;139:97;26:851;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;',
srcmapRuntime: '26:851:0:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;88:40;;8:9:-1;5:2;;;30:1;27;20:12;5:2;88:40:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;691:181;;8:9:-1;5:2;;;30:1;27;20:12;5:2;691:181:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;529:152;;8:9:-1;5:2;;;30:1;27;20:12;5:2;529:152:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;52:30;;8:9:-1;5:2;;;30:1;27;20:12;5:2;52:30:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;88:40;;;;;;;;;;;;;;;;;;;;;;:::o;691:181::-;759:5;783:36;801:17;783;:36::i;:::-;775:45;;;;;;;;837:9;:28;847:17;837:28;;;;;;;;;;;;;;;;;;;;;;;;;;;830:35;;691:181;;;:::o;529:152::-;594:36;612:17;594;:36::i;:::-;586:45;;;;;;;;673:1;641:9;:28;651:17;641:28;;;;;;;;;;;;;;;;;;:33;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;529:152;:::o;52:30::-;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;246:273::-;322:4;341:7;349:1;341:9;;337:154;355:13;:20;;;;353:1;:22;;;337:154;;;420:13;434:1;420:16;;;;;;;;;;;;;;;;;;;;399:37;;;:17;:37;;;;396:85;;;462:4;455:11;;;;396:85;378:3;;;;;;;337:154;;;507:5;500:12;;246:273;;;;;:::o'
}
},
sourceList: [ '' ],
sources: { '': { AST: [Object] } }
}
// 从编译结果中拿到 abi 和 bin 的内容
> var abi =JSON.parse( compileCode.contracts[':Voting'].interface)
undefined
> abi
[
{
constant: true,
inputs: [ [Object] ],
name: 'votingMap',
outputs: [ [Object] ],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [ [Object] ],
name: 'totalVoters',
outputs: [ [Object] ],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: false,
inputs: [ [Object] ],
name: 'vote',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function'
},
{
constant: true,
inputs: [ [Object] ],
name: 'candidateList',
outputs: [ [Object] ],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
inputs: [ [Object] ],
payable: false,
stateMutability: 'nonpayable',
type: 'constructor'
}
]
> var byteCode=compileCode.contracts[':Voting'].bytecode
undefined
> byteCode
'608060405234801561001057600080fd5b506040516103c53803806103c5833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100c8565b828054828255906000526020600020908101928215610092579160200282015b82811115610091578251829060001916905591602001919060010190610070565b5b50905061009f91906100a3565b5090565b6100c591905b808211156100c15760008160009055506001016100a9565b5090565b90565b6102ee806100d76000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634db33f92146100675780634dfb9c80146100b2578063a69beaba146100fd578063b13c744b1461012e575b600080fd5b34801561007357600080fd5b506100966004803603810190808035600019169060200190929190505050610177565b604051808260ff1660ff16815260200191505060405180910390f35b3480156100be57600080fd5b506100e16004803603810190808035600019169060200190929190505050610197565b604051808260ff1660ff16815260200191505060405180910390f35b34801561010957600080fd5b5061012c60048036038101908080356000191690602001909291905050506101dd565b005b34801561013a57600080fd5b506101596004803603810190808035906020019092919050505061023a565b60405180826000191660001916815260200191505060405180910390f35b60016020528060005260406000206000915054906101000a900460ff1681565b60006101a28261025d565b15156101ad57600080fd5b60016000836000191660001916815260200190815260200160002060009054906101000a900460ff169050919050565b6101e68161025d565b15156101f157600080fd5b6001806000836000191660001916815260200190815260200160002060008282829054906101000a900460ff160192506101000a81548160ff021916908360ff16021790555050565b60008181548110151561024957fe5b906000526020600020016000915090505481565b600080600090505b6000805490508160ff1610156102b75760008160ff1681548110151561028757fe5b906000526020600020015460001916836000191614156102aa57600191506102bc565b8080600101915050610265565b600091505b509190505600a165627a7a72305820fd465411c8ec744911a25690bde10ff6a011d08a9106d57e4829f6018eea48d20029'
// 部署合约
> var VotingContract = web3.eth.contract(abi)
undefined
> var deployTxObj = {data:byteCode,from:web3.eth.accounts[0],gas:3000000}
undefined
> var contractInstance = VotingContract.new(['Bob','Li','Zhu'],deployTxObj)
undefined
// 查看合约地址,验证部署是否成功
> contractInstance.address
'0x30d727e16da276cf8fbec4def300b879711f82a0'
进行投票,并查看票数
> contractInstance.vote('Li',{from:web3.eth.accounts[0]})
'0x8c9285c78394edf36bc2cb20acc5dbeded80ed6a355ff13ffa47525e731c3c5f'
> contractInstance.totalVoters('Li')
BigNumber { s: 1, e: 0, c: [ 1 ] }
> contractInstance.totalVoters('Li').toString()
'1'
> contractInstance.vote.sendTransaction("Bob",{from:web3.eth.accounts[0]})
'0x56e07f69b5cdc3eaa33dff024f3f0564387aac058b166b722aaea47eb9c08eed'
web3.isConnected()报错: is not a function 解决:版本太高,改为0.20的......
compileCode 报错:'{"errors":[{"component":"general","formattedMessage":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 2, Column 1\\n Extra non-whitespace after JSON value.\\n","message":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 2, Column 1\\n Extra non-whitespace after JSON value.\\n","severity":"error","type":"JSONError"}]}'
解决:solc版本改为0.4.22 智能合约sol文件重新按0.4.22写......
报错:检查合约(待处理)
文章来源:https://www.toymoban.com/news/detail-425471.html
到了这里,关于投票DApp-跟随视频学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!