GovernorVotesQuorumFraction.test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const { expectEvent, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper, proposalStatesToBitMap } = require('../../helpers/governance');
  5. const { clock } = require('../../helpers/time');
  6. const { expectRevertCustomError } = require('../../helpers/customError');
  7. const Governor = artifacts.require('$GovernorMock');
  8. const CallReceiver = artifacts.require('CallReceiverMock');
  9. const TOKENS = [
  10. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  11. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  12. ];
  13. contract('GovernorVotesQuorumFraction', function (accounts) {
  14. const [owner, voter1, voter2, voter3, voter4] = accounts;
  15. const name = 'OZ-Governor';
  16. const version = '1';
  17. const tokenName = 'MockToken';
  18. const tokenSymbol = 'MTKN';
  19. const tokenSupply = web3.utils.toBN(web3.utils.toWei('100'));
  20. const ratio = web3.utils.toBN(8); // percents
  21. const newRatio = web3.utils.toBN(6); // percents
  22. const votingDelay = web3.utils.toBN(4);
  23. const votingPeriod = web3.utils.toBN(16);
  24. const value = web3.utils.toWei('1');
  25. for (const { mode, Token } of TOKENS) {
  26. describe(`using ${Token._json.contractName}`, function () {
  27. beforeEach(async function () {
  28. this.owner = owner;
  29. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  30. this.mock = await Governor.new(name, votingDelay, votingPeriod, 0, this.token.address, ratio);
  31. this.receiver = await CallReceiver.new();
  32. this.helper = new GovernorHelper(this.mock, mode);
  33. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  34. await this.token.$_mint(owner, tokenSupply);
  35. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  36. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  37. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  38. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  39. // default proposal
  40. this.proposal = this.helper.setProposal(
  41. [
  42. {
  43. target: this.receiver.address,
  44. value,
  45. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  46. },
  47. ],
  48. '<proposal description>',
  49. );
  50. });
  51. it('deployment check', async function () {
  52. expect(await this.mock.name()).to.be.equal(name);
  53. expect(await this.mock.token()).to.be.equal(this.token.address);
  54. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  55. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  56. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  57. expect(await this.mock.quorumNumerator()).to.be.bignumber.equal(ratio);
  58. expect(await this.mock.quorumDenominator()).to.be.bignumber.equal('100');
  59. expect(await clock[mode]().then(timepoint => this.mock.quorum(timepoint - 1))).to.be.bignumber.equal(
  60. tokenSupply.mul(ratio).divn(100),
  61. );
  62. });
  63. it('quroum reached', async function () {
  64. await this.helper.propose();
  65. await this.helper.waitForSnapshot();
  66. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  67. await this.helper.waitForDeadline();
  68. await this.helper.execute();
  69. });
  70. it('quroum not reached', async function () {
  71. await this.helper.propose();
  72. await this.helper.waitForSnapshot();
  73. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  74. await this.helper.waitForDeadline();
  75. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  76. this.proposal.id,
  77. Enums.ProposalState.Defeated,
  78. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  79. ]);
  80. });
  81. describe('onlyGovernance updates', function () {
  82. it('updateQuorumNumerator is protected', async function () {
  83. await expectRevertCustomError(this.mock.updateQuorumNumerator(newRatio), 'GovernorOnlyGovernance', []);
  84. });
  85. it('can updateQuorumNumerator through governance', async function () {
  86. this.helper.setProposal(
  87. [
  88. {
  89. target: this.mock.address,
  90. data: this.mock.contract.methods.updateQuorumNumerator(newRatio).encodeABI(),
  91. },
  92. ],
  93. '<proposal description>',
  94. );
  95. await this.helper.propose();
  96. await this.helper.waitForSnapshot();
  97. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  98. await this.helper.waitForDeadline();
  99. expectEvent(await this.helper.execute(), 'QuorumNumeratorUpdated', {
  100. oldQuorumNumerator: ratio,
  101. newQuorumNumerator: newRatio,
  102. });
  103. expect(await this.mock.quorumNumerator()).to.be.bignumber.equal(newRatio);
  104. expect(await this.mock.quorumDenominator()).to.be.bignumber.equal('100');
  105. // it takes one block for the new quorum to take effect
  106. expect(await clock[mode]().then(blockNumber => this.mock.quorum(blockNumber - 1))).to.be.bignumber.equal(
  107. tokenSupply.mul(ratio).divn(100),
  108. );
  109. await time.advanceBlock();
  110. expect(await clock[mode]().then(blockNumber => this.mock.quorum(blockNumber - 1))).to.be.bignumber.equal(
  111. tokenSupply.mul(newRatio).divn(100),
  112. );
  113. });
  114. it('cannot updateQuorumNumerator over the maximum', async function () {
  115. const quorumNumerator = 101;
  116. this.helper.setProposal(
  117. [
  118. {
  119. target: this.mock.address,
  120. data: this.mock.contract.methods.updateQuorumNumerator(quorumNumerator).encodeABI(),
  121. },
  122. ],
  123. '<proposal description>',
  124. );
  125. await this.helper.propose();
  126. await this.helper.waitForSnapshot();
  127. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  128. await this.helper.waitForDeadline();
  129. const quorumDenominator = await this.mock.quorumDenominator();
  130. await expectRevertCustomError(this.helper.execute(), 'GovernorInvalidQuorumFraction', [
  131. quorumNumerator,
  132. quorumDenominator,
  133. ]);
  134. });
  135. });
  136. });
  137. }
  138. });