GovernorVotesQuorumFraction.test.js 5.5 KB

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