GovernorCompatibilityBravo.test.js 12 KB

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