Linux下基于ganache的简单投票DAPP([email protected]

写在前面

运行前

  1. 安装node和npm
  2. 下载文件及模块
git clone https://github.com/1172510217/voting_dapp.git
cd voting_dapp
npm install # 安装依赖
  1. 运行ganache-cli
./node_modules/.bin/ganache-cli --host "108.61.126.96" --port 8080
  • 需保证ganache-cli在整个服务器运行过程中一直运行
  • host参数替换为你的服务器网址,port参数替换为你需要监听的端口

运行

  1. 编译合约
// 重新开启一个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. 部署合约
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. 修改index.js
  • 修改第1行中的网址为你的ganache-cli监听网址
  • 修改合约部署地址为你的实际部署地址:可以在ganache-cli中找到对应的contractAddr
  1. 修改server.js
  • 修改最终监听的端口号为一个不同于ganache-cli监听端口号的非熟知端口号,比如8888
  • 修改最后一行中的网址为你的服务器网址(和ganache-cli的网址一致)
  1. 运行服务器
node server.js # 保证该进程一直存在,才可以使得网站被正常访问

注意

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文件中
发布了13 篇原创文章 · 获赞 8 · 访问量 9291

猜你喜欢

转载自blog.csdn.net/qq_43481201/article/details/104235425