From 5b2a90eb8522ccf838469d5c6edf65bf33a315a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Fri, 8 Sep 2023 15:38:41 +0200 Subject: [PATCH] fix() create function to automatically test gasLimit --- test/governor/GovernorZama.ts | 58 ++++++++++++++++++++--------------- test/utils.ts | 16 ++++++++-- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/test/governor/GovernorZama.ts b/test/governor/GovernorZama.ts index e02be02a..16f88240 100644 --- a/test/governor/GovernorZama.ts +++ b/test/governor/GovernorZama.ts @@ -3,7 +3,7 @@ import { ethers } from 'hardhat'; import { createInstances } from '../instance'; import { getSigners } from '../signers'; -import { waitForBlock } from '../utils'; +import { createTransaction, waitForBlock } from '../utils'; import { deployCompFixture } from './Comp.fixture'; import { deployGovernorZamaFixture } from './GovernorZama.fixture'; @@ -38,21 +38,22 @@ describe('GovernorZama', function () { const transaction2 = await this.governor.__acceptAdmin(); await Promise.all([transaction.wait(), transaction2.wait()]); - await this.comp.delegate(this.signers.alice); - await this.comp.connect(this.signers.bob).delegate(this.signers.bob); - await this.comp.connect(this.signers.carol).delegate(this.signers.carol); + const delegate1 = await this.comp.delegate(this.signers.alice); + const delegate2 = await this.comp.connect(this.signers.bob).delegate(this.signers.bob); + const delegate3 = await this.comp.connect(this.signers.carol).delegate(this.signers.carol); + await Promise.all([delegate1, delegate2, delegate3]); }); it('should propose a vote', async function () { const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])]; - const tx = await this.governor.propose( + const tx = await createTransaction( + this.governor.propose, [this.signers.alice], ['0'], ['getBalanceOf(address)'], callDatas, 0, 'do nothing', - { gasLimit: 1000000 }, ); const proposal = await tx.wait(); expect(proposal?.status).to.equal(1); @@ -64,14 +65,14 @@ describe('GovernorZama', function () { it('should vote and return a Succeed', async function () { const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])]; - const tx = await this.governor.propose( + const tx = await createTransaction( + this.governor.propose, [this.signers.alice], ['0'], ['getBalanceOf(address)'], callDatas, 4, 'do nothing', - { gasLimit: 1000000 }, ); const proposal = await tx.wait(); expect(proposal?.status).to.equal(1); @@ -84,14 +85,18 @@ describe('GovernorZama', function () { // Cast some votes const encryptedSupportBob = this.instances.bob.encrypt32(1); - const txVoteBob = await this.governor - .connect(this.signers.bob) - ['castVote(uint256,bytes)'](proposalId, encryptedSupportBob, { gasLimit: 5000000 }); + const txVoteBob = await createTransaction( + this.governor.connect(this.signers.bob)['castVote(uint256,bytes)'], + proposalId, + encryptedSupportBob, + ); const encryptedSupportCarol = this.instances.carol.encrypt32(1); - const txVoteCarol = await this.governor - .connect(this.signers.carol) - ['castVote(uint256,bytes)'](proposalId, encryptedSupportCarol, { gasLimit: 5000000 }); + const txVoteCarol = await createTransaction( + this.governor.connect(this.signers.carol)['castVote(uint256,bytes)'], + proposalId, + encryptedSupportCarol, + ); const [bobResults, aliceResults] = await Promise.all([txVoteBob.wait(), txVoteCarol.wait()]); expect(bobResults?.status).to.equal(1); @@ -105,34 +110,37 @@ describe('GovernorZama', function () { it('should vote and return a Defeated ', async function () { const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])]; - const tx = await this.governor.propose( + const tx = await createTransaction( + this.governor.propose, [this.signers.alice], ['0'], ['getBalanceOf(address)'], callDatas, 4, 'do nothing', - { gasLimit: 1000000 }, ); const proposal = await tx.wait(); expect(proposal?.status).to.equal(1); const proposalId = await this.governor.latestProposalIds(this.signers.alice.address); const proposals = await this.governor.proposals(proposalId); - console.log(proposals); await waitForBlock(proposals.startBlock + 1n); // TOFIX: Votes are not casted. See _castVotes function in GovernorZama.sol // Cast some votes const encryptedSupportBob = this.instances.bob.encrypt32(0); - const txVoteBob = await this.governor - .connect(this.signers.bob) - ['castVote(uint256,bytes)'](proposalId, encryptedSupportBob, { gasLimit: 5000000 }); + const txVoteBob = await createTransaction( + this.governor.connect(this.signers.carol)['castVote(uint256,bytes)'], + proposalId, + encryptedSupportBob, + ); const encryptedSupportCarol = this.instances.carol.encrypt32(0); - const txVoteCarol = await this.governor - .connect(this.signers.carol) - ['castVote(uint256,bytes)'](proposalId, encryptedSupportCarol, { gasLimit: 5000000 }); + const txVoteCarol = await createTransaction( + this.governor.connect(this.signers.carol)['castVote(uint256,bytes)'], + proposalId, + encryptedSupportCarol, + ); const [bobResults, aliceResults] = await Promise.all([txVoteBob.wait(), txVoteCarol.wait()]); expect(bobResults?.status).to.equal(1); @@ -147,14 +155,14 @@ describe('GovernorZama', function () { it('should cancel', async function () { await this.comp.delegate(this.signers.alice.address); const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])]; - const tx = await this.governor.propose( + const tx = await createTransaction( + this.governor.propose, [this.signers.alice], ['0'], ['getBalanceOf(address)'], callDatas, 0, 'do nothing', - { gasLimit: 500000 }, ); const proposal = await tx.wait(); expect(proposal?.status).to.equal(1); diff --git a/test/utils.ts b/test/utils.ts index f756ee79..35626130 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,11 +1,14 @@ +import { ContractMethodArgs, Typed } from 'ethers'; import { ethers } from 'hardhat'; +import { TypedContractMethod } from '../types/common'; + export const waitForBlock = (blockNumber: bigint) => { return new Promise((resolve, reject) => { const waitBlock = async (currentBlock: number) => { - console.log(`Block ${currentBlock} reached! Waiting ${blockNumber}...`); + // console.log(`Block ${currentBlock} reached! Waiting ${blockNumber}...`); if (blockNumber <= BigInt(currentBlock)) { - console.log(`Block ${currentBlock} reached!`); + // console.log(`Block ${currentBlock} reached!`); await ethers.provider.off('block', waitBlock); resolve(blockNumber); } @@ -15,3 +18,12 @@ export const waitForBlock = (blockNumber: bigint) => { }); }); }; + +export const createTransaction = async ( + method: TypedContractMethod, + ...params: A +) => { + const gasLimit = await method.estimateGas(...params); + const updatedParams: ContractMethodArgs = [...params, { gasLimit: Math.round(+gasLimit.toString() * 1.2) }]; + return method(...updatedParams); +};