GovernorCompatibilityBravo.test.js 11 KB

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