GovernorCompatibilityBravo.test.js 13 KB

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