投票DApp-跟随视频学习

这篇具有很好参考价值的文章主要介绍了投票DApp-跟随视频学习。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

投票DApp-跟随视频学习

自动有10个账户 每个有100个以太

投票DApp-跟随视频学习 3.安装 npm、web3@0.20.1 、solc,查看版本

投票DApp-跟随视频学习

投票DApp-跟随视频学习 

/simple_vote_dapp$ sudo npm install web3

投票DApp-跟随视频学习 

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写......

报错:检查合约(待处理)

投票DApp-跟随视频学习

 

到了这里,关于投票DApp-跟随视频学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用javaAPI对HDFS进行文件上传,下载,新建文件及文件夹删除,遍历所有文件

    目录 //通过工具类来操作hdfs   hdfs dfs -put d:user_info.txt  /user_info.txt  // 将文件放入到hdfs中  2.通过工具类来操作hdfs   hdfs dfs -get hdfs路径   本地路经  将文件放入到本地Windows中 3.通过工具类来操作hdfs   hdfs dfs -mkdir -p  hdfs路径 4.通过工具类来操作hdfs  查看一个文件是否存在

    2024年02月12日
    浏览(51)
  • python-自动化篇-办公-批量新建文件夹并保存日志信息

    因为业务需要,每天都需要按当天的日期创建很多新文件夹。把这种重复又繁重的操作交给Python来做,一直是我的目标。先说下要求: 默认在桌面新建文件夹。 文件夹命名方式,“月.日-1”,比如7.8-1,7.8-2。7是代表当天的月份,不能显示07。8是代表当天的日期,不能显示

    2024年02月20日
    浏览(50)
  • Github仓库中怎么新建文件夹、Github的项目中如何上传图片

    在Github仓库中创建新文件夹的步骤如下: 首先进入您的Github仓库页面。 在仓库页面中,点击“Add file”按钮,然后选择“Create new file”。 在文件名输入框里输入文件夹名称,例如“my_folder/”,注意末尾要加上“/”来表示这是一个文件夹。 然后在文件名后面按回车键,Gith

    2024年02月04日
    浏览(52)
  • gitee新建仓库导入文件夹的入门避坑指引:关于我碰到的那些坑

    1.先贴上我参考的那些文章: 浅析Gitee怎么上传20个以上的文件-git-PHP中文网 Gitee如何上传整个项目文件夹_gitee上传文件夹-CSDN博客(这个是最重要的) git问题error: remote origin already exists._git error: remote origin already exists.-CSDN博客(这个是第二重要的) 解决 “fatal: Could not read fr

    2024年02月03日
    浏览(39)
  • 【.NET】C#/.NET新建项目sln,增加src和test文件夹问题和解决方案

    ​         经常逛github找优秀的.NET项目看,看到github上的项目的层级有src test,sln放在外层。如下图:  发现自己再Visaul Studio新建的项目即使添加了src和test,然后基于文件夹下新建项目,物理路径上也不是这样的结构。如图  物理路径下依然和sln是平级状态。 全程新建项目通

    2024年02月05日
    浏览(56)
  • 如何解决win11“无法枚举容器中的对象,访问被拒绝”、“右键新建只有文件夹,没有其他选项”的问题。

    形成原因:是文件夹Users权限的问题 解决方法:修改User权限即可 出错原因 我本以为按照网上的教程,右键E盘属性,找到安全选项卡,然后更改高级,然后更改所有者,然后开启 使用可从此对象继承的权限项目替换所有子对象的权限项目P,,,,等等操作。但是依然会报错

    2024年02月02日
    浏览(49)
  • Unity打开本地文件夹替换视频

    AVpro插件是一款很强大的视频播放插件,配合Unity使用有意想不到的效果,他的各项功能网上都有,我就不进行展开讨论了。 该插件的获取方式为AssetStore中购买下载,该插件有一个免费版本可供使用,付费版本的功能强大一点。 有需要的也可以私信我,我分享给你,你只能用

    2023年04月08日
    浏览(115)
  • Windows11打开此电脑,没有视频、图片等文件夹,解决方法

    那次更新完电脑,发现电脑变了个样 如果你打开“此电脑”,发现视频、图片、音乐等那些文件夹不见了,只剩下驱动器了。 如下图: 别慌, 我们可以通过注册表来解决,把视频、图片等这些文件夹添加回来,注册表在我下面这个百度网盘链接里: 链接: https://pan.baidu.

    2024年02月11日
    浏览(112)
  • Vue实现前端视频展示列表及特征提取、笔记、删除、文件夹组织功能

    Vue实现前端视频展示列表及特征提取、笔记、删除、文件夹组织功能 在前端展示上传的视频列表时,我们可以使用Element-UI中的Card组件来实现。同时,我们还可以添加一些功能,如缓存播放的视频、选择视频文本特征提取处理、写笔记、删除视频、组织成文件夹结构等。下面

    2024年02月10日
    浏览(54)
  • 【代码---利用一个小程序,读取文件夹中图片,将其合成为一个视频】

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 提示:这里可以添加本文要记录的大概内容: 创建一个程序将图像合成为视频通常需要使用图像处理和视频编码库。 下面是一个使用Python的例子,其中使用OpenCV来处理图像和生成视频: 提示:以下是本

    2024年01月25日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包