GovernorWeightQuorumFraction.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { BN, expectEvent, time } = require('@openzeppelin/test-helpers');
  2. const Enums = require('../../helpers/enums');
  3. const {
  4. runGovernorWorkflow,
  5. } = require('./../GovernorWorkflow.behavior');
  6. const Token = artifacts.require('ERC20VotesMock');
  7. const Governor = artifacts.require('GovernorMock');
  8. const CallReceiver = artifacts.require('CallReceiverMock');
  9. contract('GovernorVotesQuorumFraction', function (accounts) {
  10. const [ owner, voter1, voter2, voter3, voter4 ] = accounts;
  11. const name = 'OZ-Governor';
  12. // const version = '1';
  13. const tokenName = 'MockToken';
  14. const tokenSymbol = 'MTKN';
  15. const tokenSupply = new BN(web3.utils.toWei('100'));
  16. const ratio = new BN(8); // percents
  17. const newRatio = new BN(6); // percents
  18. beforeEach(async function () {
  19. this.owner = owner;
  20. this.token = await Token.new(tokenName, tokenSymbol);
  21. this.mock = await Governor.new(name, this.token.address, 4, 16, ratio);
  22. this.receiver = await CallReceiver.new();
  23. await this.token.mint(owner, tokenSupply);
  24. await this.token.delegate(voter1, { from: voter1 });
  25. await this.token.delegate(voter2, { from: voter2 });
  26. await this.token.delegate(voter3, { from: voter3 });
  27. await this.token.delegate(voter4, { from: voter4 });
  28. });
  29. it('deployment check', async function () {
  30. expect(await this.mock.name()).to.be.equal(name);
  31. expect(await this.mock.token()).to.be.equal(this.token.address);
  32. expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
  33. expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
  34. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  35. expect(await this.mock.quorumNumerator()).to.be.bignumber.equal(ratio);
  36. expect(await this.mock.quorumDenominator()).to.be.bignumber.equal('100');
  37. expect(await time.latestBlock().then(blockNumber => this.mock.quorum(blockNumber.subn(1))))
  38. .to.be.bignumber.equal(tokenSupply.mul(ratio).divn(100));
  39. });
  40. describe('quroum not reached', function () {
  41. beforeEach(async function () {
  42. this.settings = {
  43. proposal: [
  44. [ this.receiver.address ],
  45. [ web3.utils.toWei('0') ],
  46. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  47. '<proposal description>',
  48. ],
  49. tokenHolder: owner,
  50. voters: [
  51. { voter: voter1, weight: web3.utils.toWei('1'), support: Enums.VoteType.For },
  52. ],
  53. steps: {
  54. execute: { error: 'Governor: proposal not successful' },
  55. },
  56. };
  57. });
  58. runGovernorWorkflow();
  59. });
  60. describe('update quorum ratio through proposal', function () {
  61. beforeEach(async function () {
  62. this.settings = {
  63. proposal: [
  64. [ this.mock.address ],
  65. [ web3.utils.toWei('0') ],
  66. [ this.mock.contract.methods.updateQuorumNumerator(newRatio).encodeABI() ],
  67. '<proposal description>',
  68. ],
  69. tokenHolder: owner,
  70. voters: [
  71. { voter: voter1, weight: tokenSupply, support: Enums.VoteType.For },
  72. ],
  73. };
  74. });
  75. afterEach(async function () {
  76. await expectEvent.inTransaction(
  77. this.receipts.execute.transactionHash,
  78. this.mock,
  79. 'QuorumNumeratorUpdated',
  80. {
  81. oldQuorumNumerator: ratio,
  82. newQuorumNumerator: newRatio,
  83. },
  84. );
  85. expect(await this.mock.quorumNumerator()).to.be.bignumber.equal(newRatio);
  86. expect(await this.mock.quorumDenominator()).to.be.bignumber.equal('100');
  87. expect(await time.latestBlock().then(blockNumber => this.mock.quorum(blockNumber.subn(1))))
  88. .to.be.bignumber.equal(tokenSupply.mul(newRatio).divn(100));
  89. });
  90. runGovernorWorkflow();
  91. });
  92. describe('update quorum over the maximum', function () {
  93. beforeEach(async function () {
  94. this.settings = {
  95. proposal: [
  96. [ this.mock.address ],
  97. [ web3.utils.toWei('0') ],
  98. [ this.mock.contract.methods.updateQuorumNumerator(new BN(101)).encodeABI() ],
  99. '<proposal description>',
  100. ],
  101. tokenHolder: owner,
  102. voters: [
  103. { voter: voter1, weight: tokenSupply, support: Enums.VoteType.For },
  104. ],
  105. steps: {
  106. execute: { error: 'GovernorVotesQuorumFraction: quorumNumerator over quorumDenominator' },
  107. },
  108. };
  109. });
  110. runGovernorWorkflow();
  111. });
  112. });