EDITORIAL
Run before
- Installation node and npm
- Download files and modules
git clone https://github.com/1172510217/voting_dapp.git
cd voting_dapp
npm install
- 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
- Compile contract
var Web3 = require('web3');
var solc = require('solc');
var web3 = new Web3(new Web3.providers.HttpProvider('http://108.61.126.96:8080'));
var compiledCode = solc.compile(fs.readFileSync('Voting.sol').toString());
- 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});
- 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
- 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)
- 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 node server.js > server_out.log 2>&1 &