GovernorWithParams.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ethSigUtil = require('eth-sig-util');
  4. const Wallet = require('ethereumjs-wallet').default;
  5. const { fromRpcSig } = require('ethereumjs-util');
  6. const Enums = require('../../helpers/enums');
  7. const { getDomain, domainType } = require('../../helpers/eip712');
  8. const { GovernorHelper } = require('../../helpers/governance');
  9. const Governor = artifacts.require('$GovernorWithParamsMock');
  10. const CallReceiver = artifacts.require('CallReceiverMock');
  11. const rawParams = {
  12. uintParam: web3.utils.toBN('42'),
  13. strParam: 'These are my params',
  14. };
  15. const encodedParams = web3.eth.abi.encodeParameters(['uint256', 'string'], Object.values(rawParams));
  16. const TOKENS = [
  17. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  18. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  19. ];
  20. contract('GovernorWithParams', function (accounts) {
  21. const [owner, proposer, voter1, voter2, voter3, voter4] = accounts;
  22. const name = 'OZ-Governor';
  23. const tokenName = 'MockToken';
  24. const tokenSymbol = 'MTKN';
  25. const tokenSupply = web3.utils.toWei('100');
  26. const votingDelay = web3.utils.toBN(4);
  27. const votingPeriod = web3.utils.toBN(16);
  28. const value = web3.utils.toWei('1');
  29. for (const { mode, Token } of TOKENS) {
  30. describe(`using ${Token._json.contractName}`, function () {
  31. beforeEach(async function () {
  32. this.chainId = await web3.eth.getChainId();
  33. this.token = await Token.new(tokenName, tokenSymbol, tokenName);
  34. this.mock = await Governor.new(name, this.token.address);
  35. this.receiver = await CallReceiver.new();
  36. this.helper = new GovernorHelper(this.mock, mode);
  37. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  38. await this.token.$_mint(owner, tokenSupply);
  39. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  40. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  41. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  42. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  43. // default proposal
  44. this.proposal = this.helper.setProposal(
  45. [
  46. {
  47. target: this.receiver.address,
  48. value,
  49. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  50. },
  51. ],
  52. '<proposal description>',
  53. );
  54. });
  55. it('deployment check', async function () {
  56. expect(await this.mock.name()).to.be.equal(name);
  57. expect(await this.mock.token()).to.be.equal(this.token.address);
  58. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  59. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  60. });
  61. it('nominal is unaffected', async function () {
  62. await this.helper.propose({ from: proposer });
  63. await this.helper.waitForSnapshot();
  64. await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
  65. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  66. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  67. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  68. await this.helper.waitForDeadline();
  69. await this.helper.execute();
  70. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  71. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
  72. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
  73. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
  74. expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
  75. });
  76. it('Voting with params is properly supported', async function () {
  77. await this.helper.propose({ from: proposer });
  78. await this.helper.waitForSnapshot();
  79. const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  80. const tx = await this.helper.vote(
  81. {
  82. support: Enums.VoteType.For,
  83. reason: 'no particular reason',
  84. params: encodedParams,
  85. },
  86. { from: voter2 },
  87. );
  88. expectEvent(tx, 'CountParams', { ...rawParams });
  89. expectEvent(tx, 'VoteCastWithParams', {
  90. voter: voter2,
  91. proposalId: this.proposal.id,
  92. support: Enums.VoteType.For,
  93. weight,
  94. reason: 'no particular reason',
  95. params: encodedParams,
  96. });
  97. const votes = await this.mock.proposalVotes(this.proposal.id);
  98. expect(votes.forVotes).to.be.bignumber.equal(weight);
  99. });
  100. it('Voting with params by signature is properly supported', async function () {
  101. const voterBySig = Wallet.generate();
  102. const voterBySigAddress = web3.utils.toChecksumAddress(voterBySig.getAddressString());
  103. const signature = (contract, message) =>
  104. getDomain(contract)
  105. .then(domain => ({
  106. primaryType: 'ExtendedBallot',
  107. types: {
  108. EIP712Domain: domainType(domain),
  109. ExtendedBallot: [
  110. { name: 'proposalId', type: 'uint256' },
  111. { name: 'support', type: 'uint8' },
  112. { name: 'reason', type: 'string' },
  113. { name: 'params', type: 'bytes' },
  114. ],
  115. },
  116. domain,
  117. message,
  118. }))
  119. .then(data => ethSigUtil.signTypedMessage(voterBySig.getPrivateKey(), { data }))
  120. .then(fromRpcSig);
  121. await this.token.delegate(voterBySigAddress, { from: voter2 });
  122. // Run proposal
  123. await this.helper.propose();
  124. await this.helper.waitForSnapshot();
  125. const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  126. const tx = await this.helper.vote({
  127. support: Enums.VoteType.For,
  128. reason: 'no particular reason',
  129. params: encodedParams,
  130. signature,
  131. });
  132. expectEvent(tx, 'CountParams', { ...rawParams });
  133. expectEvent(tx, 'VoteCastWithParams', {
  134. voter: voterBySigAddress,
  135. proposalId: this.proposal.id,
  136. support: Enums.VoteType.For,
  137. weight,
  138. reason: 'no particular reason',
  139. params: encodedParams,
  140. });
  141. const votes = await this.mock.proposalVotes(this.proposal.id);
  142. expect(votes.forVotes).to.be.bignumber.equal(weight);
  143. });
  144. });
  145. }
  146. });