GovernorStorage.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectRevertCustomError } = require('../../helpers/customError');
  4. const Enums = require('../../helpers/enums');
  5. const { GovernorHelper, timelockSalt } = require('../../helpers/governance');
  6. const Timelock = artifacts.require('TimelockController');
  7. const Governor = artifacts.require('$GovernorStorageMock');
  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('GovernorStorage', function (accounts) {
  14. const [owner, voter1, voter2, voter3, voter4] = accounts;
  15. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  16. const PROPOSER_ROLE = web3.utils.soliditySha3('PROPOSER_ROLE');
  17. const EXECUTOR_ROLE = web3.utils.soliditySha3('EXECUTOR_ROLE');
  18. const CANCELLER_ROLE = web3.utils.soliditySha3('CANCELLER_ROLE');
  19. const name = 'OZ-Governor';
  20. const version = '1';
  21. const tokenName = 'MockToken';
  22. const tokenSymbol = 'MTKN';
  23. const tokenSupply = web3.utils.toWei('100');
  24. const votingDelay = web3.utils.toBN(4);
  25. const votingPeriod = web3.utils.toBN(16);
  26. const value = web3.utils.toWei('1');
  27. for (const { mode, Token } of TOKENS) {
  28. describe(`using ${Token._json.contractName}`, function () {
  29. beforeEach(async function () {
  30. const [deployer] = await web3.eth.getAccounts();
  31. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  32. this.timelock = await Timelock.new(3600, [], [], deployer);
  33. this.mock = await Governor.new(
  34. name,
  35. votingDelay,
  36. votingPeriod,
  37. 0,
  38. this.timelock.address,
  39. this.token.address,
  40. 0,
  41. );
  42. this.receiver = await CallReceiver.new();
  43. this.helper = new GovernorHelper(this.mock, mode);
  44. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  45. // normal setup: governor is proposer, everyone is executor, timelock is its own admin
  46. await this.timelock.grantRole(PROPOSER_ROLE, this.mock.address);
  47. await this.timelock.grantRole(PROPOSER_ROLE, owner);
  48. await this.timelock.grantRole(CANCELLER_ROLE, this.mock.address);
  49. await this.timelock.grantRole(CANCELLER_ROLE, owner);
  50. await this.timelock.grantRole(EXECUTOR_ROLE, constants.ZERO_ADDRESS);
  51. await this.timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
  52. await this.token.$_mint(owner, tokenSupply);
  53. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  54. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  55. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  56. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  57. // default proposal
  58. this.proposal = this.helper.setProposal(
  59. [
  60. {
  61. target: this.receiver.address,
  62. value,
  63. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  64. },
  65. ],
  66. '<proposal description>',
  67. );
  68. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  69. ...this.proposal.shortProposal.slice(0, 3),
  70. '0x0',
  71. timelockSalt(this.mock.address, this.proposal.shortProposal[3]),
  72. );
  73. });
  74. describe('proposal indexing', function () {
  75. it('before propose', async function () {
  76. expect(await this.mock.proposalCount()).to.be.bignumber.equal('0');
  77. // panic code 0x32 (out-of-bound)
  78. await expectRevert.unspecified(this.mock.proposalDetailsAt(0));
  79. await expectRevertCustomError(this.mock.proposalDetails(this.proposal.id), 'GovernorNonexistentProposal', [
  80. this.proposal.id,
  81. ]);
  82. });
  83. it('after propose', async function () {
  84. await this.helper.propose();
  85. expect(await this.mock.proposalCount()).to.be.bignumber.equal('1');
  86. const proposalDetailsAt0 = await this.mock.proposalDetailsAt(0);
  87. expect(proposalDetailsAt0[0]).to.be.bignumber.equal(this.proposal.id);
  88. expect(proposalDetailsAt0[1]).to.be.deep.equal(this.proposal.targets);
  89. expect(proposalDetailsAt0[2].map(x => x.toString())).to.be.deep.equal(this.proposal.values);
  90. expect(proposalDetailsAt0[3]).to.be.deep.equal(this.proposal.fulldata);
  91. expect(proposalDetailsAt0[4]).to.be.equal(this.proposal.descriptionHash);
  92. const proposalDetailsForId = await this.mock.proposalDetails(this.proposal.id);
  93. expect(proposalDetailsForId[0]).to.be.deep.equal(this.proposal.targets);
  94. expect(proposalDetailsForId[1].map(x => x.toString())).to.be.deep.equal(this.proposal.values);
  95. expect(proposalDetailsForId[2]).to.be.deep.equal(this.proposal.fulldata);
  96. expect(proposalDetailsForId[3]).to.be.equal(this.proposal.descriptionHash);
  97. });
  98. });
  99. it('queue and execute by id', async function () {
  100. await this.helper.propose();
  101. await this.helper.waitForSnapshot();
  102. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  103. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  104. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  105. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  106. await this.helper.waitForDeadline();
  107. const txQueue = await this.mock.queue(this.proposal.id);
  108. await this.helper.waitForEta();
  109. const txExecute = await this.mock.execute(this.proposal.id);
  110. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  111. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallScheduled', { id: this.proposal.timelockid });
  112. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallSalt', {
  113. id: this.proposal.timelockid,
  114. });
  115. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  116. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'CallExecuted', { id: this.proposal.timelockid });
  117. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  118. });
  119. it('cancel by id', async function () {
  120. await this.helper.propose();
  121. const txCancel = await this.mock.cancel(this.proposal.id);
  122. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  123. });
  124. });
  125. }
  126. });