Linux-based ganache simple voting DAPP ([email protected]) under

EDITORIAL

Run before

  1. Installation node and npm
  2. Download files and modules
git clone https://github.com/1172510217/voting_dapp.git
cd voting_dapp
npm install # 安装依赖
  1. Run ganache-cli
./node_modules/.bin/ganache-cli --host "108.61.126.96" --port 8080
  • The need to ensure ganache-cli has been running during the entire server is running
  • host parameter substitution for your server address, port alternate port parameters you need to listen

run

  1. Compile contract
// 重新开启一个bash,并进入到voting_dapp中,输入node,在node控制台中操作
var Web3 = require('web3');
var solc = require('solc');
var web3 = new Web3(new Web3.providers.HttpProvider('http://108.61.126.96:8080')); // 这里的网址和上面ganache-cli的网址一致
var compiledCode = solc.compile(fs.readFileSync('Voting.sol').toString());
  1. Deployment contract
var Voting = compiledCode.contracts[':Voting'];
var abi = JSON.parse(Voting.interface);
var byteCode = Voting.bytecode;
var candidates =['Alice','Bob','Cary'];
var candidatesHex = new Array(candidates.length);
for (let i = 0; i < candidates.length; i++){candidatesHex[i] = web3.utils.asciiToHex(candidates[i]);}
var account;
web3.eth.getAccounts().then(function(res){account=res[0]});
var VotingContract = new web3.eth.Contract(abi);
var contractInstance = VotingContract.deploy({data:byteCode,arguments:[candidatesHex]}).send({from:account,gas:4700000});
  1. Modify index.js
  • Modify the first line of the URL monitor URL for your ganache-cli
  • Modify the contract to deploy the address for your actual deployment Address: You can find the corresponding contractAddr in the ganache-cli
  1. Modify server.js
  • The final port number of listening to a well-known port number different from the non-ganache-cli listening port number, for example 8888
  • Modify the last line of the URL for your server address (URL and ganache-cli consistent)
  1. Run the server
node server.js # 保证该进程一直存在,才可以使得网站被正常访问

note

nohup ./node_modules/.bin/ganache-cli --host 108.61.126.9 --port 8080 > ganache_out.log 2>&1 & # 要保证一直运行,可以使用nohup命令,终端输出重定向到ganache_out.log文件中
nohup node server.js > server_out.log 2>&1 &# 要保证一直运行,可以使用nohup命令,终端输出重定向到server_out.log文件中
Published 13 original articles · won praise 8 · views 9291

Guess you like

Origin blog.csdn.net/qq_43481201/article/details/104235425