GovernorPreventLateQuorum.test.js 8.5 KB

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