GovernorVotesQuorumFraction.test.js 5.4 KB

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