GovernorWithParams.test.js 11 KB

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