GovernorWithParams.test.js 6.2 KB

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