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:
Renan Souza
2023-12-18 17:09:23 -03:00
committed by GitHub
parent d155600d55
commit c3cd70811b
22 changed files with 3663 additions and 3861 deletions

View File

@ -1,88 +1,79 @@
const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
const Enums = require('../../helpers/enums');
const { GovernorHelper, proposalStatesToBitMap, timelockSalt } = require('../../helpers/governance');
const { expectRevertCustomError } = require('../../helpers/customError');
const { clockFromReceipt } = require('../../helpers/time');
const Timelock = artifacts.require('TimelockController');
const Governor = artifacts.require('$GovernorTimelockControlMock');
const CallReceiver = artifacts.require('CallReceiverMock');
const ERC721 = artifacts.require('$ERC721');
const ERC1155 = artifacts.require('$ERC1155');
const { GovernorHelper, timelockSalt } = require('../../helpers/governance');
const { bigint: Enums } = require('../../helpers/enums');
const { bigint: time } = require('../../helpers/time');
const TOKENS = [
{ Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
{ Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
{ Token: '$ERC20Votes', mode: 'blocknumber' },
{ Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
];
contract('GovernorTimelockControl', function (accounts) {
const [owner, voter1, voter2, voter3, voter4, other] = accounts;
const DEFAULT_ADMIN_ROLE = ethers.ZeroHash;
const PROPOSER_ROLE = ethers.id('PROPOSER_ROLE');
const EXECUTOR_ROLE = ethers.id('EXECUTOR_ROLE');
const CANCELLER_ROLE = ethers.id('CANCELLER_ROLE');
const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
const PROPOSER_ROLE = web3.utils.soliditySha3('PROPOSER_ROLE');
const EXECUTOR_ROLE = web3.utils.soliditySha3('EXECUTOR_ROLE');
const CANCELLER_ROLE = web3.utils.soliditySha3('CANCELLER_ROLE');
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 delay = time.duration.hours(1n);
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');
describe('GovernorTimelockControl', function () {
for (const { Token, mode } of TOKENS) {
const fixture = async () => {
const [deployer, owner, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
const receiver = await ethers.deployContract('CallReceiverMock');
const delay = 3600;
const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, version]);
const timelock = await ethers.deployContract('TimelockController', [delay, [], [], deployer]);
const mock = await ethers.deployContract('$GovernorTimelockControlMock', [
name,
votingDelay,
votingPeriod,
0n,
timelock,
token,
0n,
]);
for (const { mode, Token } of TOKENS) {
describe(`using ${Token._json.contractName}`, function () {
await owner.sendTransaction({ to: timelock, value });
await token.$_mint(owner, tokenSupply);
await timelock.grantRole(PROPOSER_ROLE, mock);
await timelock.grantRole(PROPOSER_ROLE, owner);
await timelock.grantRole(CANCELLER_ROLE, mock);
await timelock.grantRole(CANCELLER_ROLE, owner);
await timelock.grantRole(EXECUTOR_ROLE, ethers.ZeroAddress);
await timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
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 { deployer, owner, voter1, voter2, voter3, voter4, other, receiver, token, mock, timelock, helper };
};
describe(`using ${Token}`, function () {
beforeEach(async function () {
const [deployer] = await web3.eth.getAccounts();
this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
this.timelock = await Timelock.new(delay, [], [], deployer);
this.mock = await Governor.new(
name,
votingDelay,
votingPeriod,
0,
this.timelock.address,
this.token.address,
0,
);
this.receiver = await CallReceiver.new();
this.helper = new GovernorHelper(this.mock, mode);
this.PROPOSER_ROLE = await this.timelock.PROPOSER_ROLE();
this.EXECUTOR_ROLE = await this.timelock.EXECUTOR_ROLE();
this.CANCELLER_ROLE = await this.timelock.CANCELLER_ROLE();
await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
// normal setup: governor is proposer, everyone is executor, timelock is its own admin
await this.timelock.grantRole(PROPOSER_ROLE, this.mock.address);
await this.timelock.grantRole(PROPOSER_ROLE, owner);
await this.timelock.grantRole(CANCELLER_ROLE, this.mock.address);
await this.timelock.grantRole(CANCELLER_ROLE, owner);
await this.timelock.grantRole(EXECUTOR_ROLE, constants.ZERO_ADDRESS);
await this.timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
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>',
@ -90,54 +81,63 @@ contract('GovernorTimelockControl', function (accounts) {
this.proposal.timelockid = await this.timelock.hashOperationBatch(
...this.proposal.shortProposal.slice(0, 3),
'0x0',
timelockSalt(this.mock.address, this.proposal.shortProposal[3]),
ethers.ZeroHash,
timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
);
});
it("doesn't accept ether transfers", async function () {
await expectRevert.unspecified(web3.eth.sendTransaction({ from: owner, to: this.mock.address, value: 1 }));
await expect(this.owner.sendTransaction({ to: this.mock, value: 1n })).to.be.revertedWithCustomError(
this.mock,
'GovernorDisabledDeposit',
);
});
it('post 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.quorum(0)).to.be.bignumber.equal('0');
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);
expect(await this.mock.quorum(0n)).to.equal(0n);
expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
expect(await this.mock.timelock()).to.equal(this.timelock.target);
});
it('nominal', async function () {
expect(await this.mock.proposalEta(this.proposal.id)).to.be.bignumber.equal('0');
expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.equal(true);
expect(await this.mock.proposalEta(this.proposal.id)).to.equal(0n);
expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { 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 });
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();
expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
const txQueue = await this.helper.queue();
const eta = (await time.clockFromReceipt.timestamp(txQueue)) + delay;
expect(await this.mock.proposalEta(this.proposal.id)).to.equal(eta);
await this.helper.waitForEta();
const eta = web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(delay);
expect(await this.mock.proposalEta(this.proposal.id)).to.be.bignumber.equal(eta);
expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.equal(true);
const txExecute = this.helper.execute();
const txExecute = await this.helper.execute();
await expect(txQueue)
.to.emit(this.mock, 'ProposalQueued')
.withArgs(this.proposal.id, anyValue)
.to.emit(this.timelock, 'CallScheduled')
.withArgs(this.proposal.timelockid, ...Array(6).fill(anyValue))
.to.emit(this.timelock, 'CallSalt')
.withArgs(this.proposal.timelockid, anyValue);
expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallScheduled', { id: this.proposal.timelockid });
await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallSalt', {
id: this.proposal.timelockid,
});
expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
await expectEvent.inTransaction(txExecute.tx, this.timelock, 'CallExecuted', { id: this.proposal.timelockid });
await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
await expect(txExecute)
.to.emit(this.mock, 'ProposalExecuted')
.withArgs(this.proposal.id)
.to.emit(this.timelock, 'CallExecuted')
.withArgs(this.proposal.timelockid, ...Array(4).fill(anyValue))
.to.emit(this.receiver, 'MockFunctionCalled');
});
describe('should revert', function () {
@ -145,14 +145,16 @@ contract('GovernorTimelockControl', function (accounts) {
it('if already queued', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await expectRevertCustomError(this.helper.queue(), 'GovernorUnexpectedProposalState', [
this.proposal.id,
Enums.ProposalState.Queued,
proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
]);
await expect(this.helper.queue())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
Enums.ProposalState.Queued,
GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
);
});
});
@ -160,66 +162,69 @@ contract('GovernorTimelockControl', function (accounts) {
it('if not queued', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.waitForDeadline(+1);
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline(1n);
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Succeeded);
await expectRevertCustomError(this.helper.execute(), 'TimelockUnexpectedOperationState', [
this.proposal.timelockid,
proposalStatesToBitMap(Enums.OperationState.Ready),
]);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.timelock, 'TimelockUnexpectedOperationState')
.withArgs(this.proposal.timelockid, GovernorHelper.proposalStatesToBitMap(Enums.OperationState.Ready));
});
it('if too early', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Queued);
await expectRevertCustomError(this.helper.execute(), 'TimelockUnexpectedOperationState', [
this.proposal.timelockid,
proposalStatesToBitMap(Enums.OperationState.Ready),
]);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.timelock, 'TimelockUnexpectedOperationState')
.withArgs(this.proposal.timelockid, GovernorHelper.proposalStatesToBitMap(Enums.OperationState.Ready));
});
it('if already executed', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
await this.helper.execute();
await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
this.proposal.id,
Enums.ProposalState.Executed,
proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
]);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
Enums.ProposalState.Executed,
GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
);
});
it('if already executed by another proposer', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
await this.timelock.executeBatch(
...this.proposal.shortProposal.slice(0, 3),
'0x0',
timelockSalt(this.mock.address, this.proposal.shortProposal[3]),
ethers.ZeroHash,
timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
);
await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
this.proposal.id,
Enums.ProposalState.Executed,
proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
]);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
Enums.ProposalState.Executed,
GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
);
});
});
});
@ -228,178 +233,179 @@ contract('GovernorTimelockControl', function (accounts) {
it('cancel before queue prevents scheduling', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
await expect(this.helper.cancel('internal'))
.to.emit(this.mock, 'ProposalCanceled')
.withArgs(this.proposal.id);
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
await expectRevertCustomError(this.helper.queue(), 'GovernorUnexpectedProposalState', [
this.proposal.id,
Enums.ProposalState.Canceled,
proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
]);
expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Canceled);
await expect(this.helper.queue())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
Enums.ProposalState.Canceled,
GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
);
});
it('cancel after queue prevents executing', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
await expect(this.helper.cancel('internal'))
.to.emit(this.mock, 'ProposalCanceled')
.withArgs(this.proposal.id);
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
this.proposal.id,
Enums.ProposalState.Canceled,
proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
]);
expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Canceled);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
Enums.ProposalState.Canceled,
GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
);
});
it('cancel on timelock is reflected on governor', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Queued);
expectEvent(await this.timelock.cancel(this.proposal.timelockid, { from: owner }), 'Cancelled', {
id: this.proposal.timelockid,
});
await expect(this.timelock.connect(this.owner).cancel(this.proposal.timelockid))
.to.emit(this.timelock, 'Cancelled')
.withArgs(this.proposal.timelockid);
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Canceled);
});
});
describe('onlyGovernance', function () {
describe('relay', function () {
beforeEach(async function () {
await this.token.$_mint(this.mock.address, 1);
await this.token.$_mint(this.mock, 1);
});
it('is protected', async function () {
await expectRevertCustomError(
this.mock.relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI(), {
from: owner,
}),
'GovernorOnlyExecutor',
[owner],
);
await expect(
this.mock
.connect(this.owner)
.relay(this.token, 0n, this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n])),
)
.to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
.withArgs(this.owner.address);
});
it('can be executed through governance', async function () {
this.helper.setProposal(
[
{
target: this.mock.address,
data: this.mock.contract.methods
.relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI())
.encodeABI(),
target: this.mock.target,
data: this.mock.interface.encodeFunctionData('relay', [
this.token.target,
0n,
this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n]),
]),
},
],
'<proposal description>',
);
expect(await this.token.balanceOf(this.mock.address), 1);
expect(await this.token.balanceOf(other), 0);
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
const txExecute = await this.helper.execute();
expect(await this.token.balanceOf(this.mock.address), 0);
expect(await this.token.balanceOf(other), 1);
await expect(txExecute).to.changeTokenBalances(this.token, [this.mock, this.other], [-1n, 1n]);
await expectEvent.inTransaction(txExecute.tx, this.token, 'Transfer', {
from: this.mock.address,
to: other,
value: '1',
});
await expect(txExecute).to.emit(this.token, 'Transfer').withArgs(this.mock.target, this.other.address, 1n);
});
it('is payable and can transfer eth to EOA', async function () {
const t2g = web3.utils.toBN(128); // timelock to governor
const g2o = web3.utils.toBN(100); // governor to eoa (other)
const t2g = 128n; // timelock to governor
const g2o = 100n; // governor to eoa (other)
this.helper.setProposal(
[
{
target: this.mock.address,
target: this.mock.target,
value: t2g,
data: this.mock.contract.methods.relay(other, g2o, '0x').encodeABI(),
data: this.mock.interface.encodeFunctionData('relay', [this.other.address, g2o, '0x']),
},
],
'<proposal description>',
);
expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
const timelockBalance = await web3.eth.getBalance(this.timelock.address).then(web3.utils.toBN);
const otherBalance = await web3.eth.getBalance(other).then(web3.utils.toBN);
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
await this.helper.execute();
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(timelockBalance.sub(t2g));
expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(t2g.sub(g2o));
expect(await web3.eth.getBalance(other)).to.be.bignumber.equal(otherBalance.add(g2o));
await expect(this.helper.execute()).to.changeEtherBalances(
[this.timelock, this.mock, this.other],
[-t2g, t2g - g2o, g2o],
);
});
it('protected against other proposers', async function () {
const target = this.mock.address;
const value = web3.utils.toWei('0');
const data = this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI();
const predecessor = constants.ZERO_BYTES32;
const salt = constants.ZERO_BYTES32;
const call = [
this.mock,
0n,
this.mock.interface.encodeFunctionData('relay', [ethers.ZeroAddress, 0n, '0x']),
ethers.ZeroHash,
ethers.ZeroHash,
];
await this.timelock.schedule(target, value, data, predecessor, salt, delay, { from: owner });
await this.timelock.connect(this.owner).schedule(...call, delay);
await time.increase(delay);
await time.clock.timestamp().then(clock => time.forward.timestamp(clock + delay));
await expectRevertCustomError(
this.timelock.execute(target, value, data, predecessor, salt, { from: owner }),
'QueueEmpty', // Bubbled up from Governor
[],
// Error bubbled up from Governor
await expect(this.timelock.connect(this.owner).execute(...call)).to.be.revertedWithCustomError(
this.mock,
'QueueEmpty',
);
});
});
describe('updateTimelock', function () {
beforeEach(async function () {
this.newTimelock = await Timelock.new(
this.newTimelock = await ethers.deployContract('TimelockController', [
delay,
[this.mock.address],
[this.mock.address],
constants.ZERO_ADDRESS,
);
[this.mock],
[this.mock],
ethers.ZeroAddress,
]);
});
it('is protected', async function () {
await expectRevertCustomError(
this.mock.updateTimelock(this.newTimelock.address, { from: owner }),
'GovernorOnlyExecutor',
[owner],
);
await expect(this.mock.connect(this.owner).updateTimelock(this.newTimelock))
.to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
.withArgs(this.owner.address);
});
it('can be executed through governance to', async function () {
this.helper.setProposal(
[
{
target: this.mock.address,
data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
target: this.mock.target,
data: this.mock.interface.encodeFunctionData('updateTimelock', [this.newTimelock.target]),
},
],
'<proposal description>',
@ -407,81 +413,64 @@ contract('GovernorTimelockControl', function (accounts) {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
const txExecute = await this.helper.execute();
expectEvent(txExecute, 'TimelockChange', {
oldTimelock: this.timelock.address,
newTimelock: this.newTimelock.address,
});
await expect(this.helper.execute())
.to.emit(this.mock, 'TimelockChange')
.withArgs(this.timelock.target, this.newTimelock.target);
expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
expect(await this.mock.timelock()).to.equal(this.newTimelock.target);
});
});
describe('on safe receive', function () {
describe('ERC721', function () {
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = web3.utils.toBN(1);
const tokenId = 1n;
beforeEach(async function () {
this.token = await ERC721.new(name, symbol);
await this.token.$_mint(owner, tokenId);
this.token = await ethers.deployContract('$ERC721', ['Non Fungible Token', 'NFT']);
await this.token.$_mint(this.owner, tokenId);
});
it("can't receive an ERC721 safeTransfer", async function () {
await expectRevertCustomError(
this.token.safeTransferFrom(owner, this.mock.address, tokenId, { from: owner }),
'GovernorDisabledDeposit',
[],
);
await expect(
this.token.connect(this.owner).safeTransferFrom(this.owner, this.mock, tokenId),
).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
});
});
describe('ERC1155', function () {
const uri = 'https://token-cdn-domain/{id}.json';
const tokenIds = {
1: web3.utils.toBN(1000),
2: web3.utils.toBN(2000),
3: web3.utils.toBN(3000),
1: 1000n,
2: 2000n,
3: 3000n,
};
beforeEach(async function () {
this.token = await ERC1155.new(uri);
await this.token.$_mintBatch(owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
this.token = await ethers.deployContract('$ERC1155', ['https://token-cdn-domain/{id}.json']);
await this.token.$_mintBatch(this.owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
});
it("can't receive ERC1155 safeTransfer", async function () {
await expectRevertCustomError(
this.token.safeTransferFrom(
owner,
this.mock.address,
await expect(
this.token.connect(this.owner).safeTransferFrom(
this.owner,
this.mock,
...Object.entries(tokenIds)[0], // id + amount
'0x',
{ from: owner },
),
'GovernorDisabledDeposit',
[],
);
).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
});
it("can't receive ERC1155 safeBatchTransfer", async function () {
await expectRevertCustomError(
this.token.safeBatchTransferFrom(
owner,
this.mock.address,
Object.keys(tokenIds),
Object.values(tokenIds),
'0x',
{ from: owner },
),
'GovernorDisabledDeposit',
[],
);
await expect(
this.token
.connect(this.owner)
.safeBatchTransferFrom(this.owner, this.mock, Object.keys(tokenIds), Object.values(tokenIds), '0x'),
).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
});
});
});
@ -491,8 +480,8 @@ contract('GovernorTimelockControl', function (accounts) {
this.helper.setProposal(
[
{
target: this.mock.address,
data: this.mock.contract.methods.nonGovernanceFunction().encodeABI(),
target: this.mock.target,
data: this.mock.interface.encodeFunctionData('nonGovernanceFunction'),
},
],
'<proposal description>',
@ -500,7 +489,7 @@ contract('GovernorTimelockControl', function (accounts) {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();