GovernorVotesSuperQuorumFraction.test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { GovernorHelper } = require('../../helpers/governance');
  5. const { ProposalState, VoteType } = require('../../helpers/enums');
  6. const time = require('../../helpers/time');
  7. const TOKENS = [
  8. { Token: '$ERC20Votes', mode: 'blocknumber' },
  9. { Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
  10. ];
  11. const name = 'OZ-Governor';
  12. const version = '1';
  13. const tokenName = 'MockToken';
  14. const tokenSymbol = 'MTKN';
  15. const tokenSupply = ethers.parseEther('100');
  16. const quorumRatio = 8n; // percents
  17. const superQuorumRatio = 50n; // percents
  18. const newSuperQuorumRatio = 15n; // percents
  19. const votingDelay = 4n;
  20. const votingPeriod = 16n;
  21. const value = ethers.parseEther('1');
  22. describe('GovernorVotesSuperQuorumFraction', function () {
  23. for (const { Token, mode } of TOKENS) {
  24. const fixture = async () => {
  25. const [owner, voter1, voter2, voter3, voter4] = await ethers.getSigners();
  26. const receiver = await ethers.deployContract('CallReceiverMock');
  27. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, tokenName, version]);
  28. const mock = await ethers.deployContract('$GovernorVotesSuperQuorumFractionMock', [
  29. name,
  30. votingDelay,
  31. votingPeriod,
  32. 0n,
  33. token,
  34. quorumRatio,
  35. superQuorumRatio,
  36. ]);
  37. await owner.sendTransaction({ to: mock, value });
  38. await token.$_mint(owner, tokenSupply);
  39. const helper = new GovernorHelper(mock, mode);
  40. await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('30') });
  41. await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('20') });
  42. await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('15') });
  43. await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('5') });
  44. return { owner, voter1, voter2, voter3, voter4, receiver, token, mock, helper };
  45. };
  46. describe(`using ${Token}`, function () {
  47. beforeEach(async function () {
  48. Object.assign(this, await loadFixture(fixture));
  49. // default proposal
  50. this.proposal = this.helper.setProposal(
  51. [
  52. {
  53. target: this.receiver.target,
  54. value,
  55. data: this.receiver.interface.encodeFunctionData('mockFunction'),
  56. },
  57. ],
  58. '<proposal description>',
  59. );
  60. });
  61. it('deployment check', async function () {
  62. await expect(this.mock.name()).to.eventually.equal(name);
  63. await expect(this.mock.token()).to.eventually.equal(this.token);
  64. await expect(this.mock.votingDelay()).to.eventually.equal(votingDelay);
  65. await expect(this.mock.votingPeriod()).to.eventually.equal(votingPeriod);
  66. await expect(this.mock.quorumNumerator()).to.eventually.equal(quorumRatio);
  67. await expect(this.mock.superQuorumNumerator()).to.eventually.equal(superQuorumRatio);
  68. await expect(this.mock.quorumDenominator()).to.eventually.equal(100n);
  69. await expect(time.clock[mode]().then(clock => this.mock.superQuorum(clock - 1n))).to.eventually.equal(
  70. (tokenSupply * superQuorumRatio) / 100n,
  71. );
  72. });
  73. it('proposal remains active until super quorum is reached', async function () {
  74. await this.helper.propose();
  75. await this.helper.waitForSnapshot();
  76. // Vote with voter1 (30%) - above quorum (8%) but below super quorum (50%)
  77. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  78. // Check proposal is still active
  79. await expect(this.mock.state(this.proposal.id)).to.eventually.equal(ProposalState.Active);
  80. // Vote with voter2 (20%) - now matches super quorum
  81. await this.helper.connect(this.voter2).vote({ support: VoteType.For });
  82. // Proposal should no longer be active
  83. await expect(this.mock.state(this.proposal.id)).to.eventually.equal(ProposalState.Succeeded);
  84. });
  85. describe('super quorum updates', function () {
  86. it('updateSuperQuorumNumerator is protected', async function () {
  87. await expect(this.mock.connect(this.owner).updateSuperQuorumNumerator(newSuperQuorumRatio))
  88. .to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
  89. .withArgs(this.owner);
  90. });
  91. it('can update super quorum through governance', async function () {
  92. this.helper.setProposal(
  93. [
  94. {
  95. target: this.mock.target,
  96. data: this.mock.interface.encodeFunctionData('updateSuperQuorumNumerator', [newSuperQuorumRatio]),
  97. },
  98. ],
  99. '<proposal description>',
  100. );
  101. await this.helper.propose();
  102. await this.helper.waitForSnapshot();
  103. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  104. await this.helper.connect(this.voter2).vote({ support: VoteType.For });
  105. await this.helper.waitForDeadline();
  106. await expect(this.helper.execute())
  107. .to.emit(this.mock, 'SuperQuorumNumeratorUpdated')
  108. .withArgs(superQuorumRatio, newSuperQuorumRatio);
  109. await expect(this.mock.superQuorumNumerator()).to.eventually.equal(newSuperQuorumRatio);
  110. });
  111. it('cannot set super quorum below quorum', async function () {
  112. const invalidSuperQuorum = quorumRatio - 1n;
  113. await expect(this.mock.$_updateSuperQuorumNumerator(invalidSuperQuorum))
  114. .to.be.revertedWithCustomError(this.mock, 'GovernorInvalidSuperQuorumTooSmall')
  115. .withArgs(invalidSuperQuorum, quorumRatio);
  116. });
  117. it('cannot set super quorum above denominator', async function () {
  118. const denominator = await this.mock.quorumDenominator();
  119. const invalidSuperQuorum = BigInt(denominator) + 1n;
  120. await expect(this.mock.$_updateSuperQuorumNumerator(invalidSuperQuorum))
  121. .to.be.revertedWithCustomError(this.mock, 'GovernorInvalidSuperQuorumFraction')
  122. .withArgs(invalidSuperQuorum, denominator);
  123. });
  124. it('cannot set quorum above super quorum', async function () {
  125. const invalidQuorum = superQuorumRatio + 1n;
  126. await expect(this.mock.$_updateQuorumNumerator(invalidQuorum))
  127. .to.be.revertedWithCustomError(this.mock, 'GovernorInvalidQuorumTooLarge')
  128. .withArgs(invalidQuorum, superQuorumRatio);
  129. });
  130. });
  131. });
  132. }
  133. });