GovernorPreventLateQuorum.test.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper } = require('../../helpers/governance');
  5. const { clockFromReceipt } = require('../../helpers/time');
  6. const Governor = artifacts.require('$GovernorPreventLateQuorumMock');
  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('GovernorPreventLateQuorum', function (accounts) {
  13. const [owner, proposer, 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.toWei('100');
  19. const votingDelay = web3.utils.toBN(4);
  20. const votingPeriod = web3.utils.toBN(16);
  21. const lateQuorumVoteExtension = web3.utils.toBN(8);
  22. const quorum = web3.utils.toWei('1');
  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(
  30. name,
  31. votingDelay,
  32. votingPeriod,
  33. 0,
  34. this.token.address,
  35. lateQuorumVoteExtension,
  36. quorum,
  37. );
  38. this.receiver = await CallReceiver.new();
  39. this.helper = new GovernorHelper(this.mock, mode);
  40. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  41. await this.token.$_mint(owner, tokenSupply);
  42. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  43. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  44. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  45. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  46. // default proposal
  47. this.proposal = this.helper.setProposal(
  48. [
  49. {
  50. target: this.receiver.address,
  51. value,
  52. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  53. },
  54. ],
  55. '<proposal description>',
  56. );
  57. });
  58. it('deployment check', async function () {
  59. expect(await this.mock.name()).to.be.equal(name);
  60. expect(await this.mock.token()).to.be.equal(this.token.address);
  61. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  62. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  63. expect(await this.mock.quorum(0)).to.be.bignumber.equal(quorum);
  64. expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal(lateQuorumVoteExtension);
  65. });
  66. it('nominal workflow unaffected', async function () {
  67. const txPropose = await this.helper.propose({ from: proposer });
  68. await this.helper.waitForSnapshot();
  69. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  70. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  71. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  72. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  73. await this.helper.waitForDeadline();
  74. await this.helper.execute();
  75. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  76. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
  77. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
  78. expect(await this.mock.hasVoted(this.proposal.id, voter3)).to.be.equal(true);
  79. expect(await this.mock.hasVoted(this.proposal.id, voter4)).to.be.equal(true);
  80. await this.mock.proposalVotes(this.proposal.id).then(results => {
  81. expect(results.forVotes).to.be.bignumber.equal(web3.utils.toWei('17'));
  82. expect(results.againstVotes).to.be.bignumber.equal(web3.utils.toWei('5'));
  83. expect(results.abstainVotes).to.be.bignumber.equal(web3.utils.toWei('2'));
  84. });
  85. const voteStart = web3.utils.toBN(await clockFromReceipt[mode](txPropose.receipt)).add(votingDelay);
  86. const voteEnd = web3.utils
  87. .toBN(await clockFromReceipt[mode](txPropose.receipt))
  88. .add(votingDelay)
  89. .add(votingPeriod);
  90. expect(await this.mock.proposalSnapshot(this.proposal.id)).to.be.bignumber.equal(voteStart);
  91. expect(await this.mock.proposalDeadline(this.proposal.id)).to.be.bignumber.equal(voteEnd);
  92. expectEvent(txPropose, 'ProposalCreated', {
  93. proposalId: this.proposal.id,
  94. proposer,
  95. targets: this.proposal.targets,
  96. // values: this.proposal.values.map(value => web3.utils.toBN(value)),
  97. signatures: this.proposal.signatures,
  98. calldatas: this.proposal.data,
  99. voteStart,
  100. voteEnd,
  101. description: this.proposal.description,
  102. });
  103. });
  104. it('Delay is extended to prevent last minute take-over', async function () {
  105. const txPropose = await this.helper.propose({ from: proposer });
  106. // compute original schedule
  107. const startBlock = web3.utils.toBN(await clockFromReceipt[mode](txPropose.receipt)).add(votingDelay);
  108. const endBlock = web3.utils
  109. .toBN(await clockFromReceipt[mode](txPropose.receipt))
  110. .add(votingDelay)
  111. .add(votingPeriod);
  112. expect(await this.mock.proposalSnapshot(this.proposal.id)).to.be.bignumber.equal(startBlock);
  113. expect(await this.mock.proposalDeadline(this.proposal.id)).to.be.bignumber.equal(endBlock);
  114. // wait for the last minute to vote
  115. await this.helper.waitForDeadline(-1);
  116. const txVote = await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  117. // cannot execute yet
  118. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  119. // compute new extended schedule
  120. const extendedDeadline = web3.utils
  121. .toBN(await clockFromReceipt[mode](txVote.receipt))
  122. .add(lateQuorumVoteExtension);
  123. expect(await this.mock.proposalSnapshot(this.proposal.id)).to.be.bignumber.equal(startBlock);
  124. expect(await this.mock.proposalDeadline(this.proposal.id)).to.be.bignumber.equal(extendedDeadline);
  125. // still possible to vote
  126. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter1 });
  127. await this.helper.waitForDeadline();
  128. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  129. await this.helper.waitForDeadline(+1);
  130. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Defeated);
  131. // check extension event
  132. expectEvent(txVote, 'ProposalExtended', { proposalId: this.proposal.id, extendedDeadline });
  133. });
  134. describe('onlyGovernance updates', function () {
  135. it('setLateQuorumVoteExtension is protected', async function () {
  136. await expectRevert(this.mock.setLateQuorumVoteExtension(0), 'Governor: onlyGovernance');
  137. });
  138. it('can setLateQuorumVoteExtension through governance', async function () {
  139. this.helper.setProposal(
  140. [
  141. {
  142. target: this.mock.address,
  143. data: this.mock.contract.methods.setLateQuorumVoteExtension('0').encodeABI(),
  144. },
  145. ],
  146. '<proposal description>',
  147. );
  148. await this.helper.propose();
  149. await this.helper.waitForSnapshot();
  150. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  151. await this.helper.waitForDeadline();
  152. expectEvent(await this.helper.execute(), 'LateQuorumVoteExtensionSet', {
  153. oldVoteExtension: lateQuorumVoteExtension,
  154. newVoteExtension: '0',
  155. });
  156. expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal('0');
  157. });
  158. });
  159. });
  160. }
  161. });