GovernorVotesQuorumFraction.test.js 6.2 KB

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