GovernorStorage.test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
  5. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  6. const { GovernorHelper, timelockSalt } = require('../../helpers/governance');
  7. const { VoteType } = require('../../helpers/enums');
  8. const TOKENS = [
  9. { Token: '$ERC20Votes', mode: 'blocknumber' },
  10. { Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
  11. ];
  12. const DEFAULT_ADMIN_ROLE = ethers.ZeroHash;
  13. const PROPOSER_ROLE = ethers.id('PROPOSER_ROLE');
  14. const EXECUTOR_ROLE = ethers.id('EXECUTOR_ROLE');
  15. const CANCELLER_ROLE = ethers.id('CANCELLER_ROLE');
  16. const name = 'OZ-Governor';
  17. const version = '1';
  18. const tokenName = 'MockToken';
  19. const tokenSymbol = 'MTKN';
  20. const tokenSupply = ethers.parseEther('100');
  21. const votingDelay = 4n;
  22. const votingPeriod = 16n;
  23. const value = ethers.parseEther('1');
  24. const delay = 3600n;
  25. describe('GovernorStorage', function () {
  26. for (const { Token, mode } of TOKENS) {
  27. const fixture = async () => {
  28. const [deployer, owner, proposer, voter1, voter2, voter3, voter4] = await ethers.getSigners();
  29. const receiver = await ethers.deployContract('CallReceiverMock');
  30. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, version]);
  31. const timelock = await ethers.deployContract('TimelockController', [delay, [], [], deployer]);
  32. const mock = await ethers.deployContract('$GovernorStorageMock', [
  33. name,
  34. votingDelay,
  35. votingPeriod,
  36. 0n,
  37. timelock,
  38. token,
  39. 0n,
  40. ]);
  41. await owner.sendTransaction({ to: timelock, value });
  42. await token.$_mint(owner, tokenSupply);
  43. await timelock.grantRole(PROPOSER_ROLE, mock);
  44. await timelock.grantRole(PROPOSER_ROLE, owner);
  45. await timelock.grantRole(CANCELLER_ROLE, mock);
  46. await timelock.grantRole(CANCELLER_ROLE, owner);
  47. await timelock.grantRole(EXECUTOR_ROLE, ethers.ZeroAddress);
  48. await timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
  49. const helper = new GovernorHelper(mock, mode);
  50. await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') });
  51. await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') });
  52. await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') });
  53. await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') });
  54. return { deployer, owner, proposer, voter1, voter2, voter3, voter4, receiver, token, timelock, mock, helper };
  55. };
  56. describe(`using ${Token}`, function () {
  57. beforeEach(async function () {
  58. Object.assign(this, await loadFixture(fixture));
  59. // initiate fresh proposal
  60. this.proposal = this.helper.setProposal(
  61. [
  62. {
  63. target: this.receiver.target,
  64. data: this.receiver.interface.encodeFunctionData('mockFunction'),
  65. value,
  66. },
  67. ],
  68. '<proposal description>',
  69. );
  70. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  71. ...this.proposal.shortProposal.slice(0, 3),
  72. ethers.ZeroHash,
  73. timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
  74. );
  75. });
  76. describe('proposal indexing', function () {
  77. it('before propose', async function () {
  78. expect(await this.mock.proposalCount()).to.equal(0n);
  79. await expect(this.mock.proposalDetailsAt(0n)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  80. await expect(this.mock.proposalDetails(this.proposal.id))
  81. .to.be.revertedWithCustomError(this.mock, 'GovernorNonexistentProposal')
  82. .withArgs(this.proposal.id);
  83. });
  84. it('after propose', async function () {
  85. await this.helper.propose();
  86. expect(await this.mock.proposalCount()).to.equal(1n);
  87. expect(await this.mock.proposalDetailsAt(0n)).to.deep.equal([
  88. this.proposal.id,
  89. this.proposal.targets,
  90. this.proposal.values,
  91. this.proposal.data,
  92. this.proposal.descriptionHash,
  93. ]);
  94. expect(await this.mock.proposalDetails(this.proposal.id)).to.deep.equal([
  95. this.proposal.targets,
  96. this.proposal.values,
  97. this.proposal.data,
  98. this.proposal.descriptionHash,
  99. ]);
  100. });
  101. });
  102. it('queue and execute by id', async function () {
  103. await this.helper.propose();
  104. await this.helper.waitForSnapshot();
  105. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  106. await this.helper.connect(this.voter2).vote({ support: VoteType.For });
  107. await this.helper.connect(this.voter3).vote({ support: VoteType.Against });
  108. await this.helper.connect(this.voter4).vote({ support: VoteType.Abstain });
  109. await this.helper.waitForDeadline();
  110. await expect(this.mock.queue(this.proposal.id))
  111. .to.emit(this.mock, 'ProposalQueued')
  112. .withArgs(this.proposal.id, anyValue)
  113. .to.emit(this.timelock, 'CallScheduled')
  114. .withArgs(this.proposal.timelockid, ...Array(6).fill(anyValue))
  115. .to.emit(this.timelock, 'CallSalt')
  116. .withArgs(this.proposal.timelockid, anyValue);
  117. await this.helper.waitForEta();
  118. await expect(this.mock.execute(this.proposal.id))
  119. .to.emit(this.mock, 'ProposalExecuted')
  120. .withArgs(this.proposal.id)
  121. .to.emit(this.timelock, 'CallExecuted')
  122. .withArgs(this.proposal.timelockid, ...Array(4).fill(anyValue))
  123. .to.emit(this.receiver, 'MockFunctionCalled');
  124. });
  125. it('cancel by id', async function () {
  126. await this.helper.connect(this.proposer).propose();
  127. await expect(this.mock.connect(this.proposer).cancel(this.proposal.id))
  128. .to.emit(this.mock, 'ProposalCanceled')
  129. .withArgs(this.proposal.id);
  130. });
  131. });
  132. }
  133. });