-
Notifications
You must be signed in to change notification settings - Fork 1
/
interact.js
39 lines (32 loc) · 1.08 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
34
35
36
37
38
39
// interact.js
require("dotenv").config();
const API_KEY = process.env.API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const contract = require("./blockchain/build/contracts/Election.json");
const { ethers } = require("ethers");
// Provider
const alchemyProvider = new ethers.providers.AlchemyProvider(
(network = "ropsten"),
API_KEY
);
// Signer
const signer = new ethers.Wallet(PRIVATE_KEY, alchemyProvider);
// Contract
const electionContract = new ethers.Contract(
CONTRACT_ADDRESS,
contract.abi,
signer
);
async function main() {
const count = await electionContract.candidatesCount();
console.log("The candidate count is: " + count);
const vote1 = await electionContract.candidates(1);
console.log("The vote status of candidate 1 is: " + vote1);
console.log("Voter 2 is voting to candidate 1...");
const tx = await electionContract.vote(1, 5);
await tx.wait();
const newVoteCount1 = await electionContract.candidates(1);
console.log("The vote status of candidate 1 is: " + newVoteCount1);
}
main();