|
1 | | -const hre = require('hardhat') |
2 | 1 | const { ethers } = require('hardhat') |
| 2 | +const { providers, Wallet } = require('ethers') |
3 | 3 | const { expect } = require('chai') |
4 | | - |
5 | 4 | const { arbLog, requireEnvVariables } = require('arb-shared-dependencies') |
6 | 5 | require('dotenv').config() |
| 6 | +requireEnvVariables(['PRIVATE_KEY', 'CHAIN_RPC']) |
7 | 7 |
|
8 | | -requireEnvVariables(['DEVNET_PRIVKEY', 'L2RPC']) |
| 8 | +/** |
| 9 | + * Set up: instantiate wallets connected to providers |
| 10 | + */ |
| 11 | +const walletPrivateKey = process.env.PRIVATE_KEY |
| 12 | +const chainProvider = new providers.JsonRpcProvider(process.env.CHAIN_RPC) |
| 13 | +const chainWallet = new Wallet(walletPrivateKey, chainProvider) |
9 | 14 |
|
10 | 15 | const main = async () => { |
11 | 16 | await arbLog('Simple Election DApp') |
12 | 17 |
|
13 | | - const l2Wallet = (await hre.ethers.getSigners())[0] |
14 | | - console.log('Your wallet address:', l2Wallet.address) |
15 | | - |
16 | | - const L2Election = await ( |
17 | | - await ethers.getContractFactory('Election') |
18 | | - ).connect(l2Wallet) |
19 | | - console.log('Deploying Election contract to L2') |
20 | | - const l2election = await L2Election.deploy() |
21 | | - await l2election.deployed() |
| 18 | + /** |
| 19 | + * Deploying the Election contract |
| 20 | + */ |
| 21 | + const Election = (await ethers.getContractFactory('Election')).connect( |
| 22 | + chainWallet |
| 23 | + ) |
| 24 | + console.log('Deploying Election contract') |
| 25 | + const election = await Election.deploy() |
| 26 | + await election.deployed() |
22 | 27 | console.log( |
23 | | - `Election contract is initialized with 2 candidates and deployed to ${l2election.address}` |
| 28 | + `Election contract is initialized with 2 candidates and deployed to ${election.address}` |
24 | 29 | ) |
25 | 30 |
|
26 | | - //Fetch the candidate count |
27 | | - const count = await l2election.candidatesCount() |
| 31 | + /** |
| 32 | + * Fetch the candidate count |
| 33 | + */ |
| 34 | + const count = await election.candidatesCount() |
28 | 35 | expect(count.toNumber()).to.equal(2) |
29 | 36 | console.log('The election is indeed initialized with two candidates!') |
30 | 37 |
|
31 | | - //Fetch the candidates values (id, name, voteCount) and make sure they are set correctly |
32 | | - var candidate1 = await l2election.candidates(1) |
| 38 | + /** |
| 39 | + * Fetch the candidates values (id, name, voteCount) and make sure they are set correctly |
| 40 | + */ |
| 41 | + var candidate1 = await election.candidates(1) |
33 | 42 | expect(candidate1[0].toNumber()).to.equal(1) |
34 | 43 | expect(candidate1[1]).to.equal('Candidate 1') |
35 | 44 | expect(candidate1[2].toNumber()).to.equal(0) |
36 | 45 |
|
37 | | - var candidate2 = await l2election.candidates(2) |
| 46 | + var candidate2 = await election.candidates(2) |
38 | 47 | expect(candidate2[0].toNumber()).to.equal(2) |
39 | 48 | expect(candidate2[1]).to.equal('Candidate 2') |
40 | 49 | expect(candidate2[2].toNumber()).to.equal(0) |
41 | 50 | console.log('candidates are initialized with the correct values!') |
42 | 51 |
|
43 | | - //Cast a vote for candidate1 |
| 52 | + /** |
| 53 | + * Cast a vote for candidate1 |
| 54 | + */ |
44 | 55 | var candidateId |
45 | 56 | var candidate |
46 | 57 | var voteCount |
47 | 58 | candidateId = 1 |
48 | 59 |
|
49 | | - const voteTx1 = await l2election.vote(candidateId) |
| 60 | + const voteTx1 = await election.vote(candidateId) |
50 | 61 | const vote1Rec = await voteTx1.wait() |
51 | 62 | expect(vote1Rec.status).to.equal(1) |
52 | | - console.log('Vote tx is executed!') |
| 63 | + console.log('Vote transaction is executed!') |
53 | 64 |
|
54 | | - const voted = await l2election.voters(l2Wallet.address) |
| 65 | + const voted = await election.voters(chainWallet.address) |
55 | 66 | expect(voted).to.be.true |
56 | 67 | console.log('You have voted for candidate1!') |
57 | 68 |
|
58 | | - //Fetch the candidate1 voteCount and make sure it's equal to 1 |
59 | | - candidate = await l2election.candidates(candidateId) |
| 69 | + /** |
| 70 | + * Fetch the candidate1 voteCount and make sure it's equal to 1 |
| 71 | + */ |
| 72 | + candidate = await election.candidates(candidateId) |
60 | 73 | voteCount = candidate[2] |
61 | 74 | expect(voteCount.toNumber()).to.equal(1) |
62 | 75 | console.log('Candidate1 has one vote!') |
63 | 76 |
|
64 | | - //Fetch Candidate2 and make sure it did not receive any votes yet |
65 | | - candidate = await l2election.candidates(2) |
| 77 | + /** |
| 78 | + * Fetch Candidate2 and make sure it did not receive any votes yet |
| 79 | + */ |
| 80 | + candidate = await election.candidates(2) |
66 | 81 | voteCount = candidate[2] |
67 | 82 | expect(voteCount.toNumber()).to.equal(0) |
68 | 83 | console.log('Candidate2 has zero vote!') |
|
0 commit comments