GovernorWithParams.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Enums = require('../../helpers/enums');
  6. const { getDomain, domainType, ExtendedBallot } = require('../../helpers/eip712');
  7. const { GovernorHelper } = require('../../helpers/governance');
  8. const { expectRevertCustomError } = require('../../helpers/customError');
  9. const Governor = artifacts.require('$GovernorWithParamsMock');
  10. const CallReceiver = artifacts.require('CallReceiverMock');
  11. const ERC1271WalletMock = artifacts.require('ERC1271WalletMock');
  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. },
  113. domain,
  114. message,
  115. }));
  116. this.sign = privateKey => async (contract, message) =>
  117. ethSigUtil.signTypedMessage(privateKey, { data: await this.data(contract, message) });
  118. });
  119. it('supports EOA signatures', async function () {
  120. await this.token.delegate(this.voterBySig.address, { from: voter2 });
  121. const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  122. const nonce = await this.mock.nonces(this.voterBySig.address);
  123. // Run proposal
  124. await this.helper.propose();
  125. await this.helper.waitForSnapshot();
  126. const tx = await this.helper.vote({
  127. support: Enums.VoteType.For,
  128. voter: this.voterBySig.address,
  129. nonce,
  130. reason: 'no particular reason',
  131. params: encodedParams,
  132. signature: this.sign(this.voterBySig.getPrivateKey()),
  133. });
  134. expectEvent(tx, 'CountParams', { ...rawParams });
  135. expectEvent(tx, 'VoteCastWithParams', {
  136. voter: this.voterBySig.address,
  137. proposalId: this.proposal.id,
  138. support: Enums.VoteType.For,
  139. weight,
  140. reason: 'no particular reason',
  141. params: encodedParams,
  142. });
  143. const votes = await this.mock.proposalVotes(this.proposal.id);
  144. expect(votes.forVotes).to.be.bignumber.equal(weight);
  145. expect(await this.mock.nonces(this.voterBySig.address)).to.be.bignumber.equal(nonce.addn(1));
  146. });
  147. it('supports EIP-1271 signature signatures', async function () {
  148. const ERC1271WalletOwner = Wallet.generate();
  149. ERC1271WalletOwner.address = web3.utils.toChecksumAddress(ERC1271WalletOwner.getAddressString());
  150. const wallet = await ERC1271WalletMock.new(ERC1271WalletOwner.address);
  151. await this.token.delegate(wallet.address, { from: voter2 });
  152. const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
  153. const nonce = await this.mock.nonces(wallet.address);
  154. // Run proposal
  155. await this.helper.propose();
  156. await this.helper.waitForSnapshot();
  157. const tx = await this.helper.vote({
  158. support: Enums.VoteType.For,
  159. voter: wallet.address,
  160. nonce,
  161. reason: 'no particular reason',
  162. params: encodedParams,
  163. signature: this.sign(ERC1271WalletOwner.getPrivateKey()),
  164. });
  165. expectEvent(tx, 'CountParams', { ...rawParams });
  166. expectEvent(tx, 'VoteCastWithParams', {
  167. voter: wallet.address,
  168. proposalId: this.proposal.id,
  169. support: Enums.VoteType.For,
  170. weight,
  171. reason: 'no particular reason',
  172. params: encodedParams,
  173. });
  174. const votes = await this.mock.proposalVotes(this.proposal.id);
  175. expect(votes.forVotes).to.be.bignumber.equal(weight);
  176. expect(await this.mock.nonces(wallet.address)).to.be.bignumber.equal(nonce.addn(1));
  177. });
  178. it('reverts if signature does not match signer', async function () {
  179. await this.token.delegate(this.voterBySig.address, { from: voter2 });
  180. const nonce = await this.mock.nonces(this.voterBySig.address);
  181. const signature = this.sign(this.voterBySig.getPrivateKey());
  182. // Run proposal
  183. await this.helper.propose();
  184. await this.helper.waitForSnapshot();
  185. const voteParams = {
  186. support: Enums.VoteType.For,
  187. voter: this.voterBySig.address,
  188. nonce,
  189. signature: async (...params) => {
  190. const sig = await signature(...params);
  191. const tamperedSig = web3.utils.hexToBytes(sig);
  192. tamperedSig[42] ^= 0xff;
  193. return web3.utils.bytesToHex(tamperedSig);
  194. },
  195. reason: 'no particular reason',
  196. params: encodedParams,
  197. };
  198. await expectRevertCustomError(this.helper.vote(voteParams), 'GovernorInvalidSignature', [voteParams.voter]);
  199. });
  200. it('reverts if vote nonce is incorrect', async function () {
  201. await this.token.delegate(this.voterBySig.address, { from: voter2 });
  202. const nonce = await this.mock.nonces(this.voterBySig.address);
  203. // Run proposal
  204. await this.helper.propose();
  205. await this.helper.waitForSnapshot();
  206. const voteParams = {
  207. support: Enums.VoteType.For,
  208. voter: this.voterBySig.address,
  209. nonce: nonce.addn(1),
  210. signature: this.sign(this.voterBySig.getPrivateKey()),
  211. reason: 'no particular reason',
  212. params: encodedParams,
  213. };
  214. await expectRevertCustomError(
  215. this.helper.vote(voteParams),
  216. // The signature check implies the nonce can't be tampered without changing the signer
  217. 'GovernorInvalidSignature',
  218. [voteParams.voter],
  219. );
  220. });
  221. });
  222. });
  223. }
  224. });