GovernorWithParams.test.js 6.1 KB

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