GovernorWithParams.test.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, toRpcSig } = require('ethereumjs-util');
  6. const Enums = require('../../helpers/enums');
  7. const { getDomain, domainType } = require('../../helpers/eip712');
  8. const { GovernorHelper } = require('../../helpers/governance');
  9. const { expectRevertCustomError } = require('../../helpers/customError');
  10. const Governor = artifacts.require('$GovernorWithParamsMock');
  11. const CallReceiver = artifacts.require('CallReceiverMock');
  12. const rawParams = {
  13. uintParam: web3.utils.toBN('42'),
  14. strParam: 'These are my params',
  15. };
  16. const encodedParams = web3.eth.abi.encodeParameters(['uint256', 'string'], Object.values(rawParams));
  17. const TOKENS = [
  18. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  19. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  20. ];
  21. contract('GovernorWithParams', function (accounts) {
  22. const [owner, proposer, voter1, voter2, voter3, voter4] = accounts;
  23. const name = 'OZ-Governor';
  24. const version = '1';
  25. const tokenName = 'MockToken';
  26. const tokenSymbol = 'MTKN';
  27. const tokenSupply = web3.utils.toWei('100');
  28. const votingDelay = web3.utils.toBN(4);
  29. const votingPeriod = web3.utils.toBN(16);
  30. const value = web3.utils.toWei('1');
  31. for (const { mode, Token } of TOKENS) {
  32. describe(`using ${Token._json.contractName}`, function () {
  33. beforeEach(async function () {
  34. this.chainId = await web3.eth.getChainId();
  35. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  36. this.mock = await Governor.new(name, this.token.address);
  37. this.receiver = await CallReceiver.new();
  38. this.helper = new GovernorHelper(this.mock, mode);
  39. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  40. await this.token.$_mint(owner, tokenSupply);
  41. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  42. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  43. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  44. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  45. // default proposal
  46. this.proposal = this.helper.setProposal(
  47. [
  48. {
  49. target: this.receiver.address,
  50. value,
  51. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  52. },
  53. ],
  54. '<proposal description>',
  55. );
  56. });
  57. it('deployment check', async function () {
  58. expect(await this.mock.name()).to.be.equal(name);
  59. expect(await this.mock.token()).to.be.equal(this.token.address);
  60. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  61. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  62. });
  63. it('nominal is unaffected', async function () {
  64. await this.helper.propose({ from: proposer });
  65. await this.helper.waitForSnapshot();
  66. await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
  67. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  68. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  69. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  70. await this.helper.waitForDeadline();
  71. await this.helper.execute();
  72. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  73. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
  74. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
  75. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
  76. expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
  77. });
  78. it('Voting with params is properly supported', async function () {
  79. await this.helper.propose({ from: proposer });
  80. await this.helper.waitForSnapshot();
  81. const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  82. const tx = await this.helper.vote(
  83. {
  84. support: Enums.VoteType.For,
  85. reason: 'no particular reason',
  86. params: encodedParams,
  87. },
  88. { from: voter2 },
  89. );
  90. expectEvent(tx, 'CountParams', { ...rawParams });
  91. expectEvent(tx, 'VoteCastWithParams', {
  92. voter: voter2,
  93. proposalId: this.proposal.id,
  94. support: Enums.VoteType.For,
  95. weight,
  96. reason: 'no particular reason',
  97. params: encodedParams,
  98. });
  99. const votes = await this.mock.proposalVotes(this.proposal.id);
  100. expect(votes.forVotes).to.be.bignumber.equal(weight);
  101. });
  102. describe('voting by signature', function () {
  103. beforeEach(async function () {
  104. this.voterBySig = Wallet.generate();
  105. this.voterBySig.address = web3.utils.toChecksumAddress(this.voterBySig.getAddressString());
  106. this.data = (contract, message) =>
  107. getDomain(contract).then(domain => ({
  108. primaryType: 'ExtendedBallot',
  109. types: {
  110. EIP712Domain: domainType(domain),
  111. ExtendedBallot: [
  112. { name: 'proposalId', type: 'uint256' },
  113. { name: 'support', type: 'uint8' },
  114. { name: 'voter', type: 'address' },
  115. { name: 'nonce', type: 'uint256' },
  116. { name: 'reason', type: 'string' },
  117. { name: 'params', type: 'bytes' },
  118. ],
  119. },
  120. domain,
  121. message,
  122. }));
  123. this.signature = (contract, message) =>
  124. this.data(contract, message)
  125. .then(data => ethSigUtil.signTypedMessage(this.voterBySig.getPrivateKey(), { data }))
  126. .then(fromRpcSig);
  127. await this.token.delegate(this.voterBySig.address, { from: voter2 });
  128. // Run proposal
  129. await this.helper.propose();
  130. await this.helper.waitForSnapshot();
  131. });
  132. it('is properly supported', async function () {
  133. const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  134. const nonce = await this.mock.nonces(this.voterBySig.address);
  135. const tx = await this.helper.vote({
  136. support: Enums.VoteType.For,
  137. voter: this.voterBySig.address,
  138. nonce,
  139. reason: 'no particular reason',
  140. params: encodedParams,
  141. signature: this.signature,
  142. });
  143. expectEvent(tx, 'CountParams', { ...rawParams });
  144. expectEvent(tx, 'VoteCastWithParams', {
  145. voter: this.voterBySig.address,
  146. proposalId: this.proposal.id,
  147. support: Enums.VoteType.For,
  148. weight,
  149. reason: 'no particular reason',
  150. params: encodedParams,
  151. });
  152. const votes = await this.mock.proposalVotes(this.proposal.id);
  153. expect(votes.forVotes).to.be.bignumber.equal(weight);
  154. expect(await this.mock.nonces(this.voterBySig.address)).to.be.bignumber.equal(nonce.addn(1));
  155. });
  156. it('reverts if signature does not match signer', async function () {
  157. const nonce = await this.mock.nonces(this.voterBySig.address);
  158. const voteParams = {
  159. support: Enums.VoteType.For,
  160. voter: this.voterBySig.address,
  161. nonce,
  162. signature: async (...params) => {
  163. const sig = await this.signature(...params);
  164. sig.s[12] ^= 0xff;
  165. return sig;
  166. },
  167. reason: 'no particular reason',
  168. params: encodedParams,
  169. };
  170. const { r, s, v } = await this.helper.sign(voteParams);
  171. const message = this.helper.forgeMessage(voteParams);
  172. const data = await this.data(this.mock, message);
  173. await expectRevertCustomError(this.helper.vote(voteParams), 'GovernorInvalidSigner', [
  174. ethSigUtil.recoverTypedSignature({ sig: toRpcSig(v, r, s), data }),
  175. voteParams.voter,
  176. ]);
  177. });
  178. it('reverts if vote nonce is incorrect', async function () {
  179. const nonce = await this.mock.nonces(this.voterBySig.address);
  180. const voteParams = {
  181. support: Enums.VoteType.For,
  182. voter: this.voterBySig.address,
  183. nonce: nonce.addn(1),
  184. signature: this.signature,
  185. reason: 'no particular reason',
  186. params: encodedParams,
  187. };
  188. const { r, s, v } = await this.helper.sign(voteParams);
  189. const message = this.helper.forgeMessage(voteParams);
  190. const data = await this.data(this.mock, { ...message, nonce });
  191. await expectRevertCustomError(
  192. this.helper.vote(voteParams),
  193. // The signature check implies the nonce can't be tampered without changing the signer
  194. 'GovernorInvalidSigner',
  195. [ethSigUtil.recoverTypedSignature({ sig: toRpcSig(v, r, s), data }), voteParams.voter],
  196. );
  197. });
  198. });
  199. });
  200. }
  201. });