GovernorWithParams.test.js 6.8 KB

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