GovernorCompatibilityBravo.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const RLP = require('rlp');
  4. const Enums = require('../../helpers/enums');
  5. const { GovernorHelper } = require('../../helpers/governance');
  6. const { clockFromReceipt } = require('../../helpers/time');
  7. const { expectRevertCustomError } = require('../../helpers/customError');
  8. const Timelock = artifacts.require('CompTimelock');
  9. const Governor = artifacts.require('$GovernorCompatibilityBravoMock');
  10. const CallReceiver = artifacts.require('CallReceiverMock');
  11. const { shouldBehaveLikeEIP6372 } = require('../utils/EIP6372.behavior');
  12. function makeContractAddress(creator, nonce) {
  13. return web3.utils.toChecksumAddress(
  14. web3.utils
  15. .sha3(RLP.encode([creator, nonce]))
  16. .slice(12)
  17. .substring(14),
  18. );
  19. }
  20. const TOKENS = [
  21. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  22. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  23. ];
  24. contract('GovernorCompatibilityBravo', function (accounts) {
  25. const [owner, proposer, voter1, voter2, voter3, voter4, other] = accounts;
  26. const name = 'OZ-Governor';
  27. const version = '1';
  28. const tokenName = 'MockToken';
  29. const tokenSymbol = 'MTKN';
  30. const tokenSupply = web3.utils.toWei('100');
  31. const votingDelay = web3.utils.toBN(4);
  32. const votingPeriod = web3.utils.toBN(16);
  33. const proposalThreshold = web3.utils.toWei('10');
  34. const value = web3.utils.toWei('1');
  35. const votes = {
  36. [owner]: tokenSupply,
  37. [proposer]: proposalThreshold,
  38. [voter1]: web3.utils.toWei('10'),
  39. [voter2]: web3.utils.toWei('7'),
  40. [voter3]: web3.utils.toWei('5'),
  41. [voter4]: web3.utils.toWei('2'),
  42. [other]: 0,
  43. };
  44. for (const { mode, Token } of TOKENS) {
  45. describe(`using ${Token._json.contractName}`, function () {
  46. beforeEach(async function () {
  47. const [deployer] = await web3.eth.getAccounts();
  48. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  49. // Need to predict governance address to set it as timelock admin with a delayed transfer
  50. const nonce = await web3.eth.getTransactionCount(deployer);
  51. const predictGovernor = makeContractAddress(deployer, nonce + 1);
  52. this.timelock = await Timelock.new(predictGovernor, 2 * 86400);
  53. this.mock = await Governor.new(
  54. name,
  55. votingDelay,
  56. votingPeriod,
  57. proposalThreshold,
  58. this.timelock.address,
  59. this.token.address,
  60. );
  61. this.receiver = await CallReceiver.new();
  62. this.helper = new GovernorHelper(this.mock, mode);
  63. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  64. await this.token.$_mint(owner, tokenSupply);
  65. await this.helper.delegate({ token: this.token, to: proposer, value: votes[proposer] }, { from: owner });
  66. await this.helper.delegate({ token: this.token, to: voter1, value: votes[voter1] }, { from: owner });
  67. await this.helper.delegate({ token: this.token, to: voter2, value: votes[voter2] }, { from: owner });
  68. await this.helper.delegate({ token: this.token, to: voter3, value: votes[voter3] }, { from: owner });
  69. await this.helper.delegate({ token: this.token, to: voter4, value: votes[voter4] }, { from: owner });
  70. // default proposal
  71. this.proposal = this.helper.setProposal(
  72. [
  73. {
  74. target: this.receiver.address,
  75. value,
  76. signature: 'mockFunction()',
  77. },
  78. ],
  79. '<proposal description>',
  80. );
  81. });
  82. shouldBehaveLikeEIP6372(mode);
  83. it('deployment check', async function () {
  84. expect(await this.mock.name()).to.be.equal(name);
  85. expect(await this.mock.token()).to.be.equal(this.token.address);
  86. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  87. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  88. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  89. expect(await this.mock.quorumVotes()).to.be.bignumber.equal('0');
  90. expect(await this.mock.COUNTING_MODE()).to.be.equal('support=bravo&quorum=bravo');
  91. });
  92. it('nominal workflow', async function () {
  93. // Before
  94. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  95. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(false);
  96. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(false);
  97. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
  98. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(value);
  99. expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal('0');
  100. // Run proposal
  101. const txPropose = await this.helper.propose({ from: proposer });
  102. await this.helper.waitForSnapshot();
  103. await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
  104. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  105. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  106. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  107. await this.helper.waitForDeadline();
  108. await this.helper.queue();
  109. await this.helper.waitForEta();
  110. const txExecute = await this.helper.execute();
  111. // After
  112. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  113. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
  114. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
  115. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
  116. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal('0');
  117. expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
  118. const proposal = await this.mock.proposals(this.proposal.id);
  119. expect(proposal.id).to.be.bignumber.equal(this.proposal.id);
  120. expect(proposal.proposer).to.be.equal(proposer);
  121. expect(proposal.eta).to.be.bignumber.equal(await this.mock.proposalEta(this.proposal.id));
  122. expect(proposal.startBlock).to.be.bignumber.equal(await this.mock.proposalSnapshot(this.proposal.id));
  123. expect(proposal.endBlock).to.be.bignumber.equal(await this.mock.proposalDeadline(this.proposal.id));
  124. expect(proposal.canceled).to.be.equal(false);
  125. expect(proposal.executed).to.be.equal(true);
  126. const action = await this.mock.getActions(this.proposal.id);
  127. expect(action.targets).to.be.deep.equal(this.proposal.targets);
  128. // expect(action.values).to.be.deep.equal(this.proposal.values);
  129. expect(action.signatures).to.be.deep.equal(this.proposal.signatures);
  130. expect(action.calldatas).to.be.deep.equal(this.proposal.data);
  131. const voteReceipt1 = await this.mock.getReceipt(this.proposal.id, voter1);
  132. expect(voteReceipt1.hasVoted).to.be.equal(true);
  133. expect(voteReceipt1.support).to.be.bignumber.equal(Enums.VoteType.For);
  134. expect(voteReceipt1.votes).to.be.bignumber.equal(web3.utils.toWei('10'));
  135. const voteReceipt2 = await this.mock.getReceipt(this.proposal.id, voter2);
  136. expect(voteReceipt2.hasVoted).to.be.equal(true);
  137. expect(voteReceipt2.support).to.be.bignumber.equal(Enums.VoteType.For);
  138. expect(voteReceipt2.votes).to.be.bignumber.equal(web3.utils.toWei('7'));
  139. const voteReceipt3 = await this.mock.getReceipt(this.proposal.id, voter3);
  140. expect(voteReceipt3.hasVoted).to.be.equal(true);
  141. expect(voteReceipt3.support).to.be.bignumber.equal(Enums.VoteType.Against);
  142. expect(voteReceipt3.votes).to.be.bignumber.equal(web3.utils.toWei('5'));
  143. const voteReceipt4 = await this.mock.getReceipt(this.proposal.id, voter4);
  144. expect(voteReceipt4.hasVoted).to.be.equal(true);
  145. expect(voteReceipt4.support).to.be.bignumber.equal(Enums.VoteType.Abstain);
  146. expect(voteReceipt4.votes).to.be.bignumber.equal(web3.utils.toWei('2'));
  147. expectEvent(txPropose, 'ProposalCreated', {
  148. proposalId: this.proposal.id,
  149. proposer,
  150. targets: this.proposal.targets,
  151. // values: this.proposal.values,
  152. signatures: this.proposal.signatures.map(() => ''), // this event doesn't contain the proposal detail
  153. calldatas: this.proposal.fulldata,
  154. voteStart: web3.utils.toBN(await clockFromReceipt[mode](txPropose.receipt)).add(votingDelay),
  155. voteEnd: web3.utils
  156. .toBN(await clockFromReceipt[mode](txPropose.receipt))
  157. .add(votingDelay)
  158. .add(votingPeriod),
  159. description: this.proposal.description,
  160. });
  161. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  162. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  163. });
  164. it('double voting is forbidden', async function () {
  165. await this.helper.propose({ from: proposer });
  166. await this.helper.waitForSnapshot();
  167. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  168. await expectRevertCustomError(
  169. this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
  170. 'GovernorAlreadyCastVote',
  171. [voter1],
  172. );
  173. });
  174. it('with function selector and arguments', async function () {
  175. const target = this.receiver.address;
  176. this.helper.setProposal(
  177. [
  178. { target, data: this.receiver.contract.methods.mockFunction().encodeABI() },
  179. { target, data: this.receiver.contract.methods.mockFunctionWithArgs(17, 42).encodeABI() },
  180. { target, signature: 'mockFunctionNonPayable()' },
  181. {
  182. target,
  183. signature: 'mockFunctionWithArgs(uint256,uint256)',
  184. data: web3.eth.abi.encodeParameters(['uint256', 'uint256'], [18, 43]),
  185. },
  186. ],
  187. '<proposal description>',
  188. );
  189. await this.helper.propose({ from: proposer });
  190. await this.helper.waitForSnapshot();
  191. await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
  192. await this.helper.waitForDeadline();
  193. await this.helper.queue();
  194. await this.helper.waitForEta();
  195. const txExecute = await this.helper.execute();
  196. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  197. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  198. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalledWithArgs', {
  199. a: '17',
  200. b: '42',
  201. });
  202. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalledWithArgs', {
  203. a: '18',
  204. b: '43',
  205. });
  206. });
  207. it('with inconsistent array size for selector and arguments', async function () {
  208. const target = this.receiver.address;
  209. const signatures = ['mockFunction()']; // One signature
  210. const data = ['0x', this.receiver.contract.methods.mockFunctionWithArgs(17, 42).encodeABI()]; // Two data entries
  211. this.helper.setProposal(
  212. {
  213. targets: [target, target],
  214. values: [0, 0],
  215. signatures,
  216. data,
  217. },
  218. '<proposal description>',
  219. );
  220. await expectRevertCustomError(this.helper.propose({ from: proposer }), 'GovernorInvalidSignaturesLength', [
  221. signatures.length,
  222. data.length,
  223. ]);
  224. });
  225. describe('should revert', function () {
  226. describe('on propose', function () {
  227. it('if proposal does not meet proposalThreshold', async function () {
  228. await expectRevertCustomError(this.helper.propose({ from: other }), 'GovernorProposerInvalidTreshold', [
  229. other,
  230. votes[other],
  231. proposalThreshold,
  232. ]);
  233. });
  234. });
  235. describe('on vote', function () {
  236. it('if vote type is invalid', async function () {
  237. await this.helper.propose({ from: proposer });
  238. await this.helper.waitForSnapshot();
  239. await expectRevertCustomError(
  240. this.helper.vote({ support: 5 }, { from: voter1 }),
  241. 'GovernorInvalidVoteType',
  242. [],
  243. );
  244. });
  245. });
  246. });
  247. describe('cancel', function () {
  248. it('proposer can cancel', async function () {
  249. await this.helper.propose({ from: proposer });
  250. await this.helper.cancel('external', { from: proposer });
  251. });
  252. it('anyone can cancel if proposer drop below threshold', async function () {
  253. await this.helper.propose({ from: proposer });
  254. await this.token.transfer(voter1, web3.utils.toWei('1'), { from: proposer });
  255. await this.helper.cancel('external');
  256. });
  257. it('cannot cancel is proposer is still above threshold', async function () {
  258. await this.helper.propose({ from: proposer });
  259. await expectRevertCustomError(this.helper.cancel('external'), 'GovernorProposerInvalidTreshold', [
  260. proposer,
  261. votes[proposer],
  262. proposalThreshold,
  263. ]);
  264. });
  265. });
  266. });
  267. }
  268. });