diff --git a/tests/e2e/governance/counting.tests.ts b/tests/e2e/governance/counting.tests.ts index 0bef906d1..f572503ce 100644 --- a/tests/e2e/governance/counting.tests.ts +++ b/tests/e2e/governance/counting.tests.ts @@ -9,10 +9,11 @@ import ContractReceiver from "../../../typechain-generated/contracts/mock_receiv import {GovernorHelper} from "./helper"; import BN from "bn.js"; import {VoteType} from "../../../typechain-generated/types-arguments/my_governor"; +import {number} from "@noble/hashes/_assert"; describe('Counting', function () { async function setup( - totalSupply = 100000, + totalSupply = 30, votingDelay = 10, votingPeriod = 10, proposalThreshold = 0, @@ -33,11 +34,13 @@ describe('Counting', function () { const contractAddressGovernance = (await contractFactoryGovernance.new(contractAddressVotes, votingDelay, votingPeriod, proposalThreshold, numrator)).address const contractGovernance = new ContractGovernance(contractAddressGovernance, deployer, api) + await contractVotes.tx.setBlockTimestamp((await contractGovernance.query.blockTimestamp()).value.ok!) + const contractFactoryReceiver = new ConstructorsReceiver(api, deployer) const contractAddressReceiver = (await contractFactoryReceiver.new()).address const contractReceiver = new ContractReceiver(contractAddressReceiver, deployer, api) - const helper = new GovernorHelper(contractGovernance) + const helper = new GovernorHelper(contractGovernance, contractVotes) await helper.delegate(contractVotes, deployer, alice, 10) await helper.delegate(contractVotes, deployer, bob, 10) @@ -125,20 +128,32 @@ describe('Counting', function () { ) await expect(helper.propose(deployer)).to.eventually.be.fulfilled await helper.waitForSnapshot(1) - await expect(helper.delegate(contractVotes, deployer, alice, 10)).to.eventually.be.fulfilled - // await expect(helper.getVotes(alice)).to.equals(10) - // await expect(helper.castVote(alice, VoteType.for)).to.eventually.be.fulfilled - // await expect(helper.castVote(bob, VoteType.against)).to.eventually.be.fulfilled + await helper.increaseBlockTimestamp(2) + + await expect(await helper.getVotes(alice)).to.equals(10) + + + await expect(helper.castVote(alice, VoteType.for)).to.eventually.be.fulfilled + + await expect(await helper.hasVoted(alice)).to.equals(true) + await expect(await helper.hasVoted(bob)).to.equals(false) + + await expect(await helper.proposalVotes()).to.equal(Array.from([10, 0, 0]).toString()) + await expect(helper.castVote(bob, VoteType.against)).to.eventually.be.fulfilled - // await expect(await helper.hasVoted(alice)).to.equals(true) - // await expect((await contractVotes.query.delegates(alice.address)).value.ok!).to.equals(alice.address) - // await expect((await contractVotes.query.balanceOf(alice.address)).value.ok!.rawNumber.toNumber()).to.equals(20) + await expect(await helper.hasVoted(alice)).to.equals(true) + await expect(await helper.hasVoted(bob)).to.equals(true) + + await expect(await helper.proposalVotes()).to.equal(Array.from([10, 10, 0]).toString()) - await expect(helper.countVotes(alice, VoteType.for, 10)).to.eventually.be.fulfilled + await expect(helper.castVote(deployer, VoteType.abstain)).to.eventually.be.fulfilled + await expect(await helper.hasVoted(alice)).to.equals(true) + await expect(await helper.hasVoted(bob)).to.equals(true) + await expect(await helper.hasVoted(deployer)).to.equals(true) - await expect(await helper.proposalVotes()).to.equals([10, 0, 0]) + await expect(await helper.proposalVotes()).to.equal(Array.from([10, 10, 10]).toString()) await api.disconnect() }) diff --git a/tests/e2e/governance/helper.ts b/tests/e2e/governance/helper.ts index ca2faa093..535cfa090 100644 --- a/tests/e2e/governance/helper.ts +++ b/tests/e2e/governance/helper.ts @@ -62,11 +62,11 @@ export class GovernorHelper { } if(proposer) { - console.log((await this.governor?.query.propose([this.proposal!], this.description!))?.value) + // console.log((await this.governor?.query.propose([this.proposal!], this.description!))?.value) await this.governor?.withSigner(proposer).tx.propose([this.proposal!], this.description!) } else { - console.log((await this.governor?.query.propose([this.proposal!], this.description!))?.value) + // console.log((await this.governor?.query.propose([this.proposal!], this.description!))?.value) await this.governor?.tx.propose([this.proposal!], this.description!) } } @@ -86,6 +86,20 @@ export class GovernorHelper { await this.governor?.tx.setBlockTimestamp(proposalSnapshot + offset) await this.token?.tx.setBlockTimestamp(proposalSnapshot + offset) + // console.log(proposalSnapshot + offset) + } + + async increaseBlockTimestamp(timestamp = 0) { + if (this.proposal === undefined || this.description === undefined){ + throw new Error('Proposal not set') + } + + if(this.proposalId === undefined) { + this.proposalId = await this.getProposalId() + } + + await this.governor?.tx.increaseBlockTimestamp(timestamp) + await this.token?.tx.increaseBlockTimestamp(timestamp) } async castVote(voter: KeyringPair, vote: VoteType) { @@ -180,12 +194,12 @@ export class GovernorHelper { this.proposalId = await this.getProposalId() } - console.log('has voted') - console.log((await this.governor?.query.hasVoted(this.proposalId as unknown as number[], voter.address))?.value.ok!) + // console.log('has voted') + // console.log((await this.governor?.query.hasVoted(this.proposalId as unknown as number[], voter.address))?.value.ok!) return (await this.governor?.query.hasVoted(this.proposalId as unknown as number[], voter.address))?.value.ok! } - async proposalVotes(): Promise { + async proposalVotes(): Promise { if (this.proposal === undefined || this.description === undefined){ throw new Error('Proposal not set') } @@ -195,7 +209,8 @@ export class GovernorHelper { } const votes = (await this.governor?.query.proposalVotes(this.proposalId as unknown as number[]))?.value.ok!.ok! - return [votes[0].rawNumber.toNumber(), votes[1].rawNumber.toNumber(), votes[2].rawNumber.toNumber()] + const votesArr = Array.from(votes.map((vote) => vote.toNumber())) + return votesArr.toString() } async getVotes(voter: KeyringPair): Promise {