Migrate governance tests to ethers.js (#4728)
Co-authored-by: ernestognw <ernestognw@gmail.com> Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
@ -1,66 +1,62 @@
|
||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const ethSigUtil = require('eth-sig-util');
|
||||
const Wallet = require('ethereumjs-wallet').default;
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const Enums = require('../../helpers/enums');
|
||||
const { getDomain, domainType, ExtendedBallot } = require('../../helpers/eip712');
|
||||
const { GovernorHelper } = require('../../helpers/governance');
|
||||
const { expectRevertCustomError } = require('../../helpers/customError');
|
||||
|
||||
const Governor = artifacts.require('$GovernorWithParamsMock');
|
||||
const CallReceiver = artifacts.require('CallReceiverMock');
|
||||
const ERC1271WalletMock = artifacts.require('ERC1271WalletMock');
|
||||
|
||||
const rawParams = {
|
||||
uintParam: web3.utils.toBN('42'),
|
||||
strParam: 'These are my params',
|
||||
};
|
||||
|
||||
const encodedParams = web3.eth.abi.encodeParameters(['uint256', 'string'], Object.values(rawParams));
|
||||
const { bigint: Enums } = require('../../helpers/enums');
|
||||
const { getDomain, ExtendedBallot } = require('../../helpers/eip712');
|
||||
|
||||
const TOKENS = [
|
||||
{ Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
|
||||
{ Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
|
||||
{ Token: '$ERC20Votes', mode: 'blocknumber' },
|
||||
{ Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
|
||||
];
|
||||
|
||||
contract('GovernorWithParams', function (accounts) {
|
||||
const [owner, proposer, voter1, voter2, voter3, voter4] = accounts;
|
||||
const name = 'OZ-Governor';
|
||||
const version = '1';
|
||||
const tokenName = 'MockToken';
|
||||
const tokenSymbol = 'MTKN';
|
||||
const tokenSupply = ethers.parseEther('100');
|
||||
const votingDelay = 4n;
|
||||
const votingPeriod = 16n;
|
||||
const value = ethers.parseEther('1');
|
||||
|
||||
const name = 'OZ-Governor';
|
||||
const version = '1';
|
||||
const tokenName = 'MockToken';
|
||||
const tokenSymbol = 'MTKN';
|
||||
const tokenSupply = web3.utils.toWei('100');
|
||||
const votingDelay = web3.utils.toBN(4);
|
||||
const votingPeriod = web3.utils.toBN(16);
|
||||
const value = web3.utils.toWei('1');
|
||||
const params = {
|
||||
decoded: [42n, 'These are my params'],
|
||||
encoded: ethers.AbiCoder.defaultAbiCoder().encode(['uint256', 'string'], [42n, 'These are my params']),
|
||||
};
|
||||
|
||||
for (const { mode, Token } of TOKENS) {
|
||||
describe(`using ${Token._json.contractName}`, function () {
|
||||
describe('GovernorWithParams', function () {
|
||||
for (const { Token, mode } of TOKENS) {
|
||||
const fixture = async () => {
|
||||
const [owner, proposer, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
|
||||
const receiver = await ethers.deployContract('CallReceiverMock');
|
||||
|
||||
const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, version]);
|
||||
const mock = await ethers.deployContract('$GovernorWithParamsMock', [name, token]);
|
||||
|
||||
await owner.sendTransaction({ to: mock, value });
|
||||
await token.$_mint(owner, tokenSupply);
|
||||
|
||||
const helper = new GovernorHelper(mock, mode);
|
||||
await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') });
|
||||
await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') });
|
||||
await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') });
|
||||
await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') });
|
||||
|
||||
return { owner, proposer, voter1, voter2, voter3, voter4, other, receiver, token, mock, helper };
|
||||
};
|
||||
|
||||
describe(`using ${Token}`, function () {
|
||||
beforeEach(async function () {
|
||||
this.chainId = await web3.eth.getChainId();
|
||||
this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
|
||||
this.mock = await Governor.new(name, this.token.address);
|
||||
this.receiver = await CallReceiver.new();
|
||||
|
||||
this.helper = new GovernorHelper(this.mock, mode);
|
||||
|
||||
await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
|
||||
|
||||
await this.token.$_mint(owner, tokenSupply);
|
||||
await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
|
||||
await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
|
||||
await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
|
||||
await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
|
||||
// default proposal
|
||||
this.proposal = this.helper.setProposal(
|
||||
[
|
||||
{
|
||||
target: this.receiver.address,
|
||||
target: this.receiver.target,
|
||||
value,
|
||||
data: this.receiver.contract.methods.mockFunction().encodeABI(),
|
||||
data: this.receiver.interface.encodeFunctionData('mockFunction'),
|
||||
},
|
||||
],
|
||||
'<proposal description>',
|
||||
@ -68,201 +64,180 @@ contract('GovernorWithParams', function (accounts) {
|
||||
});
|
||||
|
||||
it('deployment check', async function () {
|
||||
expect(await this.mock.name()).to.be.equal(name);
|
||||
expect(await this.mock.token()).to.be.equal(this.token.address);
|
||||
expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
|
||||
expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
|
||||
expect(await this.mock.name()).to.equal(name);
|
||||
expect(await this.mock.token()).to.equal(this.token.target);
|
||||
expect(await this.mock.votingDelay()).to.equal(votingDelay);
|
||||
expect(await this.mock.votingPeriod()).to.equal(votingPeriod);
|
||||
});
|
||||
|
||||
it('nominal is unaffected', async function () {
|
||||
await this.helper.propose({ from: proposer });
|
||||
await this.helper.connect(this.proposer).propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
|
||||
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
|
||||
await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
|
||||
await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
|
||||
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For, reason: 'This is nice' });
|
||||
await this.helper.connect(this.voter2).vote({ support: Enums.VoteType.For });
|
||||
await this.helper.connect(this.voter3).vote({ support: Enums.VoteType.Against });
|
||||
await this.helper.connect(this.voter4).vote({ support: Enums.VoteType.Abstain });
|
||||
await this.helper.waitForDeadline();
|
||||
await this.helper.execute();
|
||||
|
||||
expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
|
||||
expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
|
||||
expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, this.owner)).to.be.false;
|
||||
expect(await this.mock.hasVoted(this.proposal.id, this.voter1)).to.be.true;
|
||||
expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.be.true;
|
||||
expect(await ethers.provider.getBalance(this.mock)).to.equal(0n);
|
||||
expect(await ethers.provider.getBalance(this.receiver)).to.equal(value);
|
||||
});
|
||||
|
||||
it('Voting with params is properly supported', async function () {
|
||||
await this.helper.propose({ from: proposer });
|
||||
await this.helper.connect(this.proposer).propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
|
||||
const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
|
||||
const weight = ethers.parseEther('7') - params.decoded[0];
|
||||
|
||||
const tx = await this.helper.vote(
|
||||
{
|
||||
await expect(
|
||||
this.helper.connect(this.voter2).vote({
|
||||
support: Enums.VoteType.For,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
},
|
||||
{ from: voter2 },
|
||||
);
|
||||
params: params.encoded,
|
||||
}),
|
||||
)
|
||||
.to.emit(this.mock, 'CountParams')
|
||||
.withArgs(...params.decoded)
|
||||
.to.emit(this.mock, 'VoteCastWithParams')
|
||||
.withArgs(
|
||||
this.voter2.address,
|
||||
this.proposal.id,
|
||||
Enums.VoteType.For,
|
||||
weight,
|
||||
'no particular reason',
|
||||
params.encoded,
|
||||
);
|
||||
|
||||
expectEvent(tx, 'CountParams', { ...rawParams });
|
||||
expectEvent(tx, 'VoteCastWithParams', {
|
||||
voter: voter2,
|
||||
proposalId: this.proposal.id,
|
||||
support: Enums.VoteType.For,
|
||||
weight,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
});
|
||||
|
||||
const votes = await this.mock.proposalVotes(this.proposal.id);
|
||||
expect(votes.forVotes).to.be.bignumber.equal(weight);
|
||||
expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([0n, weight, 0n]);
|
||||
});
|
||||
|
||||
describe('voting by signature', function () {
|
||||
beforeEach(async function () {
|
||||
this.voterBySig = Wallet.generate();
|
||||
this.voterBySig.address = web3.utils.toChecksumAddress(this.voterBySig.getAddressString());
|
||||
|
||||
this.data = (contract, message) =>
|
||||
getDomain(contract).then(domain => ({
|
||||
primaryType: 'ExtendedBallot',
|
||||
types: {
|
||||
EIP712Domain: domainType(domain),
|
||||
ExtendedBallot,
|
||||
},
|
||||
domain,
|
||||
message,
|
||||
}));
|
||||
|
||||
this.sign = privateKey => async (contract, message) =>
|
||||
ethSigUtil.signTypedMessage(privateKey, { data: await this.data(contract, message) });
|
||||
});
|
||||
|
||||
it('supports EOA signatures', async function () {
|
||||
await this.token.delegate(this.voterBySig.address, { from: voter2 });
|
||||
|
||||
const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
|
||||
|
||||
const nonce = await this.mock.nonces(this.voterBySig.address);
|
||||
await this.token.connect(this.voter2).delegate(this.other);
|
||||
|
||||
// Run proposal
|
||||
await this.helper.propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
const tx = await this.helper.vote({
|
||||
support: Enums.VoteType.For,
|
||||
voter: this.voterBySig.address,
|
||||
nonce,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
signature: this.sign(this.voterBySig.getPrivateKey()),
|
||||
});
|
||||
|
||||
expectEvent(tx, 'CountParams', { ...rawParams });
|
||||
expectEvent(tx, 'VoteCastWithParams', {
|
||||
voter: this.voterBySig.address,
|
||||
// Prepare vote
|
||||
const weight = ethers.parseEther('7') - params.decoded[0];
|
||||
const nonce = await this.mock.nonces(this.other);
|
||||
const data = {
|
||||
proposalId: this.proposal.id,
|
||||
support: Enums.VoteType.For,
|
||||
weight,
|
||||
voter: this.other.address,
|
||||
nonce,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
});
|
||||
params: params.encoded,
|
||||
signature: (contract, message) =>
|
||||
getDomain(contract).then(domain => this.other.signTypedData(domain, { ExtendedBallot }, message)),
|
||||
};
|
||||
|
||||
const votes = await this.mock.proposalVotes(this.proposal.id);
|
||||
expect(votes.forVotes).to.be.bignumber.equal(weight);
|
||||
expect(await this.mock.nonces(this.voterBySig.address)).to.be.bignumber.equal(nonce.addn(1));
|
||||
// Vote
|
||||
await expect(this.helper.vote(data))
|
||||
.to.emit(this.mock, 'CountParams')
|
||||
.withArgs(...params.decoded)
|
||||
.to.emit(this.mock, 'VoteCastWithParams')
|
||||
.withArgs(data.voter, data.proposalId, data.support, weight, data.reason, data.params);
|
||||
|
||||
expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([0n, weight, 0n]);
|
||||
expect(await this.mock.nonces(this.other)).to.equal(nonce + 1n);
|
||||
});
|
||||
|
||||
it('supports EIP-1271 signature signatures', async function () {
|
||||
const ERC1271WalletOwner = Wallet.generate();
|
||||
ERC1271WalletOwner.address = web3.utils.toChecksumAddress(ERC1271WalletOwner.getAddressString());
|
||||
|
||||
const wallet = await ERC1271WalletMock.new(ERC1271WalletOwner.address);
|
||||
|
||||
await this.token.delegate(wallet.address, { from: voter2 });
|
||||
|
||||
const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
|
||||
|
||||
const nonce = await this.mock.nonces(wallet.address);
|
||||
const wallet = await ethers.deployContract('ERC1271WalletMock', [this.other]);
|
||||
await this.token.connect(this.voter2).delegate(wallet);
|
||||
|
||||
// Run proposal
|
||||
await this.helper.propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
const tx = await this.helper.vote({
|
||||
support: Enums.VoteType.For,
|
||||
voter: wallet.address,
|
||||
nonce,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
signature: this.sign(ERC1271WalletOwner.getPrivateKey()),
|
||||
});
|
||||
|
||||
expectEvent(tx, 'CountParams', { ...rawParams });
|
||||
expectEvent(tx, 'VoteCastWithParams', {
|
||||
voter: wallet.address,
|
||||
// Prepare vote
|
||||
const weight = ethers.parseEther('7') - params.decoded[0];
|
||||
const nonce = await this.mock.nonces(this.other);
|
||||
const data = {
|
||||
proposalId: this.proposal.id,
|
||||
support: Enums.VoteType.For,
|
||||
weight,
|
||||
voter: wallet.target,
|
||||
nonce,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
});
|
||||
params: params.encoded,
|
||||
signature: (contract, message) =>
|
||||
getDomain(contract).then(domain => this.other.signTypedData(domain, { ExtendedBallot }, message)),
|
||||
};
|
||||
|
||||
const votes = await this.mock.proposalVotes(this.proposal.id);
|
||||
expect(votes.forVotes).to.be.bignumber.equal(weight);
|
||||
expect(await this.mock.nonces(wallet.address)).to.be.bignumber.equal(nonce.addn(1));
|
||||
// Vote
|
||||
await expect(this.helper.vote(data))
|
||||
.to.emit(this.mock, 'CountParams')
|
||||
.withArgs(...params.decoded)
|
||||
.to.emit(this.mock, 'VoteCastWithParams')
|
||||
.withArgs(data.voter, data.proposalId, data.support, weight, data.reason, data.params);
|
||||
|
||||
expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([0n, weight, 0n]);
|
||||
expect(await this.mock.nonces(wallet)).to.equal(nonce + 1n);
|
||||
});
|
||||
|
||||
it('reverts if signature does not match signer', async function () {
|
||||
await this.token.delegate(this.voterBySig.address, { from: voter2 });
|
||||
|
||||
const nonce = await this.mock.nonces(this.voterBySig.address);
|
||||
|
||||
const signature = this.sign(this.voterBySig.getPrivateKey());
|
||||
await this.token.connect(this.voter2).delegate(this.other);
|
||||
|
||||
// Run proposal
|
||||
await this.helper.propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
const voteParams = {
|
||||
|
||||
// Prepare vote
|
||||
const nonce = await this.mock.nonces(this.other);
|
||||
const data = {
|
||||
proposalId: this.proposal.id,
|
||||
support: Enums.VoteType.For,
|
||||
voter: this.voterBySig.address,
|
||||
voter: this.other.address,
|
||||
nonce,
|
||||
signature: async (...params) => {
|
||||
const sig = await signature(...params);
|
||||
const tamperedSig = web3.utils.hexToBytes(sig);
|
||||
tamperedSig[42] ^= 0xff;
|
||||
return web3.utils.bytesToHex(tamperedSig);
|
||||
},
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
params: params.encoded,
|
||||
// tampered signature
|
||||
signature: (contract, message) =>
|
||||
getDomain(contract)
|
||||
.then(domain => this.other.signTypedData(domain, { ExtendedBallot }, message))
|
||||
.then(signature => {
|
||||
const tamperedSig = ethers.toBeArray(signature);
|
||||
tamperedSig[42] ^= 0xff;
|
||||
return ethers.hexlify(tamperedSig);
|
||||
}),
|
||||
};
|
||||
|
||||
await expectRevertCustomError(this.helper.vote(voteParams), 'GovernorInvalidSignature', [voteParams.voter]);
|
||||
// Vote
|
||||
await expect(this.helper.vote(data))
|
||||
.to.be.revertedWithCustomError(this.mock, 'GovernorInvalidSignature')
|
||||
.withArgs(data.voter);
|
||||
});
|
||||
|
||||
it('reverts if vote nonce is incorrect', async function () {
|
||||
await this.token.delegate(this.voterBySig.address, { from: voter2 });
|
||||
|
||||
const nonce = await this.mock.nonces(this.voterBySig.address);
|
||||
await this.token.connect(this.voter2).delegate(this.other);
|
||||
|
||||
// Run proposal
|
||||
await this.helper.propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
const voteParams = {
|
||||
|
||||
// Prepare vote
|
||||
const nonce = await this.mock.nonces(this.other);
|
||||
const data = {
|
||||
proposalId: this.proposal.id,
|
||||
support: Enums.VoteType.For,
|
||||
voter: this.voterBySig.address,
|
||||
nonce: nonce.addn(1),
|
||||
signature: this.sign(this.voterBySig.getPrivateKey()),
|
||||
voter: this.other.address,
|
||||
nonce: nonce + 1n,
|
||||
reason: 'no particular reason',
|
||||
params: encodedParams,
|
||||
params: params.encoded,
|
||||
signature: (contract, message) =>
|
||||
getDomain(contract).then(domain => this.other.signTypedData(domain, { ExtendedBallot }, message)),
|
||||
};
|
||||
|
||||
await expectRevertCustomError(
|
||||
this.helper.vote(voteParams),
|
||||
// The signature check implies the nonce can't be tampered without changing the signer
|
||||
'GovernorInvalidSignature',
|
||||
[voteParams.voter],
|
||||
);
|
||||
// Vote
|
||||
await expect(this.helper.vote(data))
|
||||
.to.be.revertedWithCustomError(this.mock, 'GovernorInvalidSignature')
|
||||
.withArgs(data.voter);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user