-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.js
33 lines (28 loc) · 1.13 KB
/
interact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// make sure that the address is passed in like
// node interact.js 0xca8bab43d250f9128178f5750972d9061b508c2e
if (process.argv.length != 3) {
console.error("must pass a voting contract address")
}
const contractAddress = process.argv[2]
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
const fs = require('fs')
let abi
try {
abi = fs.readFileSync('abi.json').toString()
} catch (err) {
throw err;
}
// create contract object with ABI
const VotingContract = web3.eth.contract(JSON.parse(abi))
// instantiate voting contract object with address
const contractInstance = VotingContract.at(contractAddress)
// log out the number of votes for 2B
const from = {from: web3.eth.accounts[0]}
console.log(contractInstance.totalVotesFor.call('2B', from).toString())
// increment the number of votes for 2B 3 times
contractInstance.voteForCandidate('2B', from)
contractInstance.voteForCandidate('2B', from)
contractInstance.voteForCandidate('2B', from)
// log out the number of votes for 2B again to see the updated value
console.log(contractInstance.totalVotesFor.call('2B', from).toString())