GovernorCompatibilityBravo.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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('with function selector and arguments', async function () {
  148. const target = this.receiver.address;
  149. this.helper.setProposal([
  150. { target, data: this.receiver.contract.methods.mockFunction().encodeABI() },
  151. { target, data: this.receiver.contract.methods.mockFunctionWithArgs(17, 42).encodeABI() },
  152. { target, signature: 'mockFunctionNonPayable()' },
  153. {
  154. target,
  155. signature: 'mockFunctionWithArgs(uint256,uint256)',
  156. data: web3.eth.abi.encodeParameters(['uint256', 'uint256'], [18, 43]),
  157. },
  158. ], '<proposal description>');
  159. await this.helper.propose({ from: proposer });
  160. await this.helper.waitForSnapshot();
  161. await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
  162. await this.helper.waitForDeadline();
  163. await this.helper.queue();
  164. await this.helper.waitForEta();
  165. const txExecute = await this.helper.execute();
  166. await expectEvent.inTransaction(
  167. txExecute.tx,
  168. this.receiver,
  169. 'MockFunctionCalled',
  170. );
  171. await expectEvent.inTransaction(
  172. txExecute.tx,
  173. this.receiver,
  174. 'MockFunctionCalled',
  175. );
  176. await expectEvent.inTransaction(
  177. txExecute.tx,
  178. this.receiver,
  179. 'MockFunctionCalledWithArgs',
  180. { a: '17', b: '42' },
  181. );
  182. await expectEvent.inTransaction(
  183. txExecute.tx,
  184. this.receiver,
  185. 'MockFunctionCalledWithArgs',
  186. { a: '18', b: '43' },
  187. );
  188. });
  189. describe('should revert', function () {
  190. describe('on propose', function () {
  191. it('if proposal doesnt meet proposalThreshold', async function () {
  192. await expectRevert(
  193. this.helper.propose({ from: other }),
  194. 'GovernorCompatibilityBravo: proposer votes below proposal threshold',
  195. );
  196. });
  197. });
  198. describe('on vote', function () {
  199. it('if vote type is invalide', async function () {
  200. await this.helper.propose({ from: proposer });
  201. await this.helper.waitForSnapshot();
  202. await expectRevert(
  203. this.helper.vote({ support: 5 }, { from: voter1 }),
  204. 'GovernorCompatibilityBravo: invalid vote type',
  205. );
  206. });
  207. });
  208. });
  209. describe('cancel', function () {
  210. it('proposer can cancel', async function () {
  211. await this.helper.propose({ from: proposer });
  212. await this.helper.cancel({ from: proposer });
  213. });
  214. it('anyone can cancel if proposer drop below threshold', async function () {
  215. await this.helper.propose({ from: proposer });
  216. await this.token.transfer(voter1, web3.utils.toWei('1'), { from: proposer });
  217. await this.helper.cancel();
  218. });
  219. it('cannot cancel is proposer is still above threshold', async function () {
  220. await this.helper.propose({ from: proposer });
  221. await expectRevert(this.helper.cancel(), 'GovernorBravo: proposer above threshold');
  222. });
  223. });
  224. });