forked from mrsmkl/eth-patricia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestvote.js
More file actions
45 lines (36 loc) · 1.77 KB
/
testvote.js
File metadata and controls
45 lines (36 loc) · 1.77 KB
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
34
35
36
37
38
39
40
41
42
43
44
// populate the voting contract with transactions
const Web3 = require("../web3.js/packages/web3")
const web3 = new Web3()
const fs = require("fs")
var host = process.argv[2] || "localhost"
// web3.setProvider(new web3.providers.WebsocketProvider('ws://' + host + ':8546'))
web3.setProvider(new web3.providers.HttpProvider('http://' + host + ':8545'))
const dir = "./compiled/"
var send_opt
async function createContract(name, args) {
var code = "0x" + fs.readFileSync(dir + name + ".bin")
var abi = JSON.parse(fs.readFileSync(dir + name + ".abi"))
return new web3.eth.Contract(abi).deploy({data: code, arguments:args}).send(send_opt)
}
async function main() {
var accts = await web3.eth.getAccounts()
send_opt = {gas:4700000, from:accts[0]}
var vote = await createContract("Vote")
console.log("Contract at", vote.options.address)
await vote.methods.setBalance(123).send({gas:4700000, from:accts[0], gasPrice:1})
await vote.methods.setBalance(234).send({gas:4700000, from:accts[1], gasPrice:1})
await vote.methods.setBalance(321).send({gas:4700000, from:accts[2], gasPrice:1})
await vote.methods.setBalance(345).send({gas:4700000, from:accts[3], gasPrice:1})
var bnum1 = await web3.eth.getBlockNumber()
await vote.methods.yes().send({gas:4700000, from:accts[0], gasPrice:1})
console.log("Vote 1")
await vote.methods.no().send({gas:4700000, from:accts[1], gasPrice:1})
console.log("Vote 2")
await vote.methods.yes().send({gas:4700000, from:accts[2], gasPrice:1})
console.log("Vote 3")
await vote.methods.yes().send({gas:4700000, from:accts[3], gasPrice:1})
console.log("Vote 4")
var bnum2 = await web3.eth.getBlockNumber()
console.log("Start block", bnum1.toString(16), "End block", bnum2.toString(16))
}
main()