GovernorVotesQuorumFraction.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(
  24. name,
  25. votingDelay,
  26. votingPeriod,
  27. 0,
  28. this.token.address,
  29. ratio,
  30. );
  31. this.receiver = await CallReceiver.new();
  32. this.helper = new GovernorHelper(this.mock);
  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. target: this.receiver.address,
  43. value,
  44. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  45. },
  46. ], '<proposal description>');
  47. });
  48. it('deployment check', async function () {
  49. expect(await this.mock.name()).to.be.equal(name);
  50. expect(await this.mock.token()).to.be.equal(this.token.address);
  51. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  52. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  53. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  54. expect(await this.mock.quorumNumerator()).to.be.bignumber.equal(ratio);
  55. expect(await this.mock.quorumDenominator()).to.be.bignumber.equal('100');
  56. expect(await time.latestBlock().then(blockNumber => this.mock.quorum(blockNumber.subn(1))))
  57. .to.be.bignumber.equal(tokenSupply.mul(ratio).divn(100));
  58. });
  59. it('quroum reached', async function () {
  60. await this.helper.propose();
  61. await this.helper.waitForSnapshot();
  62. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  63. await this.helper.waitForDeadline();
  64. await this.helper.execute();
  65. });
  66. it('quroum not reached', async function () {
  67. await this.helper.propose();
  68. await this.helper.waitForSnapshot();
  69. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  70. await this.helper.waitForDeadline();
  71. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  72. });
  73. describe('onlyGovernance updates', function () {
  74. it('updateQuorumNumerator is protected', async function () {
  75. await expectRevert(
  76. this.mock.updateQuorumNumerator(newRatio),
  77. 'Governor: onlyGovernance',
  78. );
  79. });
  80. it('can updateQuorumNumerator through governance', async function () {
  81. this.helper.setProposal([
  82. {
  83. target: this.mock.address,
  84. data: this.mock.contract.methods.updateQuorumNumerator(newRatio).encodeABI(),
  85. },
  86. ], '<proposal description>');
  87. await this.helper.propose();
  88. await this.helper.waitForSnapshot();
  89. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  90. await this.helper.waitForDeadline();
  91. expectEvent(
  92. await this.helper.execute(),
  93. 'QuorumNumeratorUpdated',
  94. { oldQuorumNumerator: ratio, newQuorumNumerator: newRatio },
  95. );
  96. expect(await this.mock.quorumNumerator()).to.be.bignumber.equal(newRatio);
  97. expect(await this.mock.quorumDenominator()).to.be.bignumber.equal('100');
  98. // it takes one block for the new quorum to take effect
  99. expect(await time.latestBlock().then(blockNumber => this.mock.quorum(blockNumber.subn(1))))
  100. .to.be.bignumber.equal(tokenSupply.mul(ratio).divn(100));
  101. await time.advanceBlock();
  102. expect(await time.latestBlock().then(blockNumber => this.mock.quorum(blockNumber.subn(1))))
  103. .to.be.bignumber.equal(tokenSupply.mul(newRatio).divn(100));
  104. });
  105. it('cannot updateQuorumNumerator over the maximum', async function () {
  106. this.helper.setProposal([
  107. {
  108. target: this.mock.address,
  109. data: this.mock.contract.methods.updateQuorumNumerator('101').encodeABI(),
  110. },
  111. ], '<proposal description>');
  112. await this.helper.propose();
  113. await this.helper.waitForSnapshot();
  114. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  115. await this.helper.waitForDeadline();
  116. await expectRevert(
  117. this.helper.execute(),
  118. 'GovernorVotesQuorumFraction: quorumNumerator over quorumDenominator',
  119. );
  120. });
  121. });
  122. });