GovernorWithParams.test.js 11 KB

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