GovernorWithParams.test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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('ERC20VotesCompMock');
  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(
  17. [ 'uint256', 'string' ],
  18. Object.values(rawParams),
  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 = new BN(4);
  28. const votingPeriod = new BN(16);
  29. const value = web3.utils.toWei('1');
  30. beforeEach(async function () {
  31. this.chainId = await web3.eth.getChainId();
  32. this.token = await Token.new(tokenName, tokenSymbol);
  33. this.mock = await Governor.new(name, this.token.address);
  34. this.receiver = await CallReceiver.new();
  35. this.helper = new GovernorHelper(this.mock);
  36. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  37. await this.token.mint(owner, tokenSupply);
  38. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  39. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  40. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  41. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  42. // default proposal
  43. this.proposal = this.helper.setProposal([
  44. {
  45. target: this.receiver.address,
  46. value,
  47. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  48. },
  49. ], '<proposal description>');
  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. support: Enums.VoteType.For,
  78. reason: 'no particular reason',
  79. params: encodedParams,
  80. }, { from: voter2 });
  81. expectEvent(tx, 'CountParams', { ...rawParams });
  82. expectEvent(tx, 'VoteCastWithParams', {
  83. voter: voter2,
  84. proposalId: this.proposal.id,
  85. support: Enums.VoteType.For,
  86. weight,
  87. reason: 'no particular reason',
  88. params: encodedParams,
  89. });
  90. const votes = await this.mock.proposalVotes(this.proposal.id);
  91. expect(votes.forVotes).to.be.bignumber.equal(weight);
  92. });
  93. it('Voting with params by signature is properly supported', async function () {
  94. const voterBySig = Wallet.generate();
  95. const voterBySigAddress = web3.utils.toChecksumAddress(voterBySig.getAddressString());
  96. const signature = async (message) => {
  97. return fromRpcSig(ethSigUtil.signTypedMessage(
  98. voterBySig.getPrivateKey(),
  99. {
  100. data: {
  101. types: {
  102. EIP712Domain,
  103. ExtendedBallot: [
  104. { name: 'proposalId', type: 'uint256' },
  105. { name: 'support', type: 'uint8' },
  106. { name: 'reason', type: 'string' },
  107. { name: 'params', type: 'bytes' },
  108. ],
  109. },
  110. domain: { name, version, chainId: this.chainId, verifyingContract: this.mock.address },
  111. primaryType: 'ExtendedBallot',
  112. message,
  113. },
  114. },
  115. ));
  116. };
  117. await this.token.delegate(voterBySigAddress, { from: voter2 });
  118. // Run proposal
  119. await this.helper.propose();
  120. await this.helper.waitForSnapshot();
  121. const weight = new BN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  122. const tx = await this.helper.vote({
  123. support: Enums.VoteType.For,
  124. reason: 'no particular reason',
  125. params: encodedParams,
  126. signature,
  127. });
  128. expectEvent(tx, 'CountParams', { ...rawParams });
  129. expectEvent(tx, 'VoteCastWithParams', {
  130. voter: voterBySigAddress,
  131. proposalId: this.proposal.id,
  132. support: Enums.VoteType.For,
  133. weight,
  134. reason: 'no particular reason',
  135. params: encodedParams,
  136. });
  137. const votes = await this.mock.proposalVotes(this.proposal.id);
  138. expect(votes.forVotes).to.be.bignumber.equal(weight);
  139. });
  140. });