Skip to content

Commit

Permalink
make counting tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
prxgr4mm3r committed Aug 16, 2023
1 parent 7863269 commit 1036b4b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
37 changes: 26 additions & 11 deletions tests/e2e/governance/counting.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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()
})
Expand Down
27 changes: 21 additions & 6 deletions tests/e2e/governance/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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<number[]> {
async proposalVotes(): Promise<string> {
if (this.proposal === undefined || this.description === undefined){
throw new Error('Proposal not set')
}
Expand All @@ -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<number|undefined> {
Expand Down

0 comments on commit 1036b4b

Please sign in to comment.