Governor.test.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ethSigUtil = require('eth-sig-util');
  4. const Wallet = require('ethereumjs-wallet').default;
  5. const { fromRpcSig } = require('ethereumjs-util');
  6. const Enums = require('../helpers/enums');
  7. const { getDomain, domainType } = require('../helpers/eip712');
  8. const { GovernorHelper, proposalStatesToBitMap } = require('../helpers/governance');
  9. const { clockFromReceipt } = require('../helpers/time');
  10. const { expectRevertCustomError } = require('../helpers/customError');
  11. const { shouldSupportInterfaces } = require('../utils/introspection/SupportsInterface.behavior');
  12. const { shouldBehaveLikeEIP6372 } = require('./utils/EIP6372.behavior');
  13. const { ZERO_BYTES32 } = require('@openzeppelin/test-helpers/src/constants');
  14. const Governor = artifacts.require('$GovernorMock');
  15. const CallReceiver = artifacts.require('CallReceiverMock');
  16. const ERC721 = artifacts.require('$ERC721');
  17. const ERC1155 = artifacts.require('$ERC1155');
  18. const TOKENS = [
  19. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  20. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  21. { Token: artifacts.require('$ERC20VotesLegacyMock'), mode: 'blocknumber' },
  22. ];
  23. contract('Governor', function (accounts) {
  24. const [owner, proposer, voter1, voter2, voter3, voter4] = accounts;
  25. const name = 'OZ-Governor';
  26. const version = '1';
  27. const tokenName = 'MockToken';
  28. const tokenSymbol = 'MTKN';
  29. const tokenSupply = web3.utils.toWei('100');
  30. const votingDelay = web3.utils.toBN(4);
  31. const votingPeriod = web3.utils.toBN(16);
  32. const value = web3.utils.toWei('1');
  33. for (const { mode, Token } of TOKENS) {
  34. describe(`using ${Token._json.contractName}`, function () {
  35. beforeEach(async function () {
  36. this.chainId = await web3.eth.getChainId();
  37. try {
  38. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  39. } catch {
  40. // ERC20VotesLegacyMock has a different construction that uses version='1' by default.
  41. this.token = await Token.new(tokenName, tokenSymbol, tokenName);
  42. }
  43. this.mock = await Governor.new(
  44. name, // name
  45. votingDelay, // initialVotingDelay
  46. votingPeriod, // initialVotingPeriod
  47. 0, // initialProposalThreshold
  48. this.token.address, // tokenAddress
  49. 10, // quorumNumeratorValue
  50. );
  51. this.receiver = await CallReceiver.new();
  52. this.helper = new GovernorHelper(this.mock, mode);
  53. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  54. await this.token.$_mint(owner, tokenSupply);
  55. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  56. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  57. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  58. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  59. this.proposal = this.helper.setProposal(
  60. [
  61. {
  62. target: this.receiver.address,
  63. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  64. value,
  65. },
  66. ],
  67. '<proposal description>',
  68. );
  69. });
  70. shouldSupportInterfaces(['ERC165', 'ERC1155Receiver', 'Governor', 'GovernorWithParams', 'GovernorCancel']);
  71. shouldBehaveLikeEIP6372(mode);
  72. it('deployment check', async function () {
  73. expect(await this.mock.name()).to.be.equal(name);
  74. expect(await this.mock.token()).to.be.equal(this.token.address);
  75. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  76. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  77. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  78. expect(await this.mock.COUNTING_MODE()).to.be.equal('support=bravo&quorum=for,abstain');
  79. });
  80. it('nominal workflow', async function () {
  81. // Before
  82. expect(await this.mock.proposalProposer(this.proposal.id)).to.be.equal(constants.ZERO_ADDRESS);
  83. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  84. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(false);
  85. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(false);
  86. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(value);
  87. expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal('0');
  88. // Run proposal
  89. const txPropose = await this.helper.propose({ from: proposer });
  90. expectEvent(txPropose, 'ProposalCreated', {
  91. proposalId: this.proposal.id,
  92. proposer,
  93. targets: this.proposal.targets,
  94. // values: this.proposal.values,
  95. signatures: this.proposal.signatures,
  96. calldatas: this.proposal.data,
  97. voteStart: web3.utils.toBN(await clockFromReceipt[mode](txPropose.receipt)).add(votingDelay),
  98. voteEnd: web3.utils
  99. .toBN(await clockFromReceipt[mode](txPropose.receipt))
  100. .add(votingDelay)
  101. .add(votingPeriod),
  102. description: this.proposal.description,
  103. });
  104. await this.helper.waitForSnapshot();
  105. expectEvent(
  106. await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 }),
  107. 'VoteCast',
  108. {
  109. voter: voter1,
  110. support: Enums.VoteType.For,
  111. reason: 'This is nice',
  112. weight: web3.utils.toWei('10'),
  113. },
  114. );
  115. expectEvent(await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 }), 'VoteCast', {
  116. voter: voter2,
  117. support: Enums.VoteType.For,
  118. weight: web3.utils.toWei('7'),
  119. });
  120. expectEvent(await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 }), 'VoteCast', {
  121. voter: voter3,
  122. support: Enums.VoteType.Against,
  123. weight: web3.utils.toWei('5'),
  124. });
  125. expectEvent(await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 }), 'VoteCast', {
  126. voter: voter4,
  127. support: Enums.VoteType.Abstain,
  128. weight: web3.utils.toWei('2'),
  129. });
  130. await this.helper.waitForDeadline();
  131. const txExecute = await this.helper.execute();
  132. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  133. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  134. // After
  135. expect(await this.mock.proposalProposer(this.proposal.id)).to.be.equal(proposer);
  136. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  137. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
  138. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
  139. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
  140. expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
  141. });
  142. it('vote with signature', async function () {
  143. const voterBySig = Wallet.generate();
  144. const voterBySigAddress = web3.utils.toChecksumAddress(voterBySig.getAddressString());
  145. const signature = (contract, message) =>
  146. getDomain(contract)
  147. .then(domain => ({
  148. primaryType: 'Ballot',
  149. types: {
  150. EIP712Domain: domainType(domain),
  151. Ballot: [
  152. { name: 'proposalId', type: 'uint256' },
  153. { name: 'support', type: 'uint8' },
  154. ],
  155. },
  156. domain,
  157. message,
  158. }))
  159. .then(data => ethSigUtil.signTypedMessage(voterBySig.getPrivateKey(), { data }))
  160. .then(fromRpcSig);
  161. await this.token.delegate(voterBySigAddress, { from: voter1 });
  162. // Run proposal
  163. await this.helper.propose();
  164. await this.helper.waitForSnapshot();
  165. expectEvent(await this.helper.vote({ support: Enums.VoteType.For, signature }), 'VoteCast', {
  166. voter: voterBySigAddress,
  167. support: Enums.VoteType.For,
  168. });
  169. await this.helper.waitForDeadline();
  170. await this.helper.execute();
  171. // After
  172. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  173. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(false);
  174. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(false);
  175. expect(await this.mock.hasVoted(this.proposal.id, voterBySigAddress)).to.be.equal(true);
  176. });
  177. it('send ethers', async function () {
  178. const empty = web3.utils.toChecksumAddress(web3.utils.randomHex(20));
  179. this.proposal = this.helper.setProposal(
  180. [
  181. {
  182. target: empty,
  183. value,
  184. },
  185. ],
  186. '<proposal description>',
  187. );
  188. // Before
  189. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(value);
  190. expect(await web3.eth.getBalance(empty)).to.be.bignumber.equal('0');
  191. // Run proposal
  192. await this.helper.propose();
  193. await this.helper.waitForSnapshot();
  194. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  195. await this.helper.waitForDeadline();
  196. await this.helper.execute();
  197. // After
  198. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
  199. expect(await web3.eth.getBalance(empty)).to.be.bignumber.equal(value);
  200. });
  201. describe('should revert', function () {
  202. describe('on propose', function () {
  203. it('if proposal already exists', async function () {
  204. await this.helper.propose();
  205. await expectRevertCustomError(this.helper.propose(), 'GovernorIncorrectState', [
  206. this.proposal.id,
  207. Enums.ProposalState.Pending,
  208. ZERO_BYTES32,
  209. ]);
  210. });
  211. });
  212. describe('on vote', function () {
  213. it('if proposal does not exist', async function () {
  214. await expectRevertCustomError(
  215. this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
  216. 'GovernorNonexistentProposal',
  217. [this.proposal.id],
  218. );
  219. });
  220. it('if voting has not started', async function () {
  221. await this.helper.propose();
  222. await expectRevertCustomError(
  223. this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
  224. 'GovernorIncorrectState',
  225. [this.proposal.id, Enums.ProposalState.Pending, proposalStatesToBitMap([Enums.ProposalState.Active])],
  226. );
  227. });
  228. it('if support value is invalid', async function () {
  229. await this.helper.propose();
  230. await this.helper.waitForSnapshot();
  231. await expectRevertCustomError(
  232. this.helper.vote({ support: web3.utils.toBN('255') }),
  233. 'GovernorInvalidVoteType',
  234. [],
  235. );
  236. });
  237. it('if vote was already casted', async function () {
  238. await this.helper.propose();
  239. await this.helper.waitForSnapshot();
  240. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  241. await expectRevertCustomError(
  242. this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
  243. 'GovernorAlreadyCastVote',
  244. [voter1],
  245. );
  246. });
  247. it('if voting is over', async function () {
  248. await this.helper.propose();
  249. await this.helper.waitForDeadline();
  250. await expectRevertCustomError(
  251. this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
  252. 'GovernorIncorrectState',
  253. [this.proposal.id, Enums.ProposalState.Defeated, proposalStatesToBitMap([Enums.ProposalState.Active])],
  254. );
  255. });
  256. });
  257. describe('on execute', function () {
  258. it('if proposal does not exist', async function () {
  259. await expectRevertCustomError(this.helper.execute(), 'GovernorNonexistentProposal', [this.proposal.id]);
  260. });
  261. it('if quorum is not reached', async function () {
  262. await this.helper.propose();
  263. await this.helper.waitForSnapshot();
  264. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter3 });
  265. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  266. this.proposal.id,
  267. Enums.ProposalState.Active,
  268. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  269. ]);
  270. });
  271. it('if score not reached', async function () {
  272. await this.helper.propose();
  273. await this.helper.waitForSnapshot();
  274. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter1 });
  275. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  276. this.proposal.id,
  277. Enums.ProposalState.Active,
  278. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  279. ]);
  280. });
  281. it('if voting is not over', async function () {
  282. await this.helper.propose();
  283. await this.helper.waitForSnapshot();
  284. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  285. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  286. this.proposal.id,
  287. Enums.ProposalState.Active,
  288. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  289. ]);
  290. });
  291. it('if receiver revert without reason', async function () {
  292. this.helper.setProposal(
  293. [
  294. {
  295. target: this.receiver.address,
  296. data: this.receiver.contract.methods.mockFunctionRevertsNoReason().encodeABI(),
  297. },
  298. ],
  299. '<proposal description>',
  300. );
  301. await this.helper.propose();
  302. await this.helper.waitForSnapshot();
  303. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  304. await this.helper.waitForDeadline();
  305. await expectRevertCustomError(this.helper.execute(), 'GovernorFailedCall', []);
  306. });
  307. it('if receiver revert with reason', async function () {
  308. this.helper.setProposal(
  309. [
  310. {
  311. target: this.receiver.address,
  312. data: this.receiver.contract.methods.mockFunctionRevertsReason().encodeABI(),
  313. },
  314. ],
  315. '<proposal description>',
  316. );
  317. await this.helper.propose();
  318. await this.helper.waitForSnapshot();
  319. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  320. await this.helper.waitForDeadline();
  321. await expectRevert(this.helper.execute(), 'CallReceiverMock: reverting');
  322. });
  323. it('if proposal was already executed', async function () {
  324. await this.helper.propose();
  325. await this.helper.waitForSnapshot();
  326. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  327. await this.helper.waitForDeadline();
  328. await this.helper.execute();
  329. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  330. this.proposal.id,
  331. Enums.ProposalState.Executed,
  332. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  333. ]);
  334. });
  335. });
  336. });
  337. describe('state', function () {
  338. it('Unset', async function () {
  339. await expectRevertCustomError(this.mock.state(this.proposal.id), 'GovernorNonexistentProposal', [
  340. this.proposal.id,
  341. ]);
  342. });
  343. it('Pending & Active', async function () {
  344. await this.helper.propose();
  345. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Pending);
  346. await this.helper.waitForSnapshot();
  347. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Pending);
  348. await this.helper.waitForSnapshot(+1);
  349. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  350. });
  351. it('Defeated', async function () {
  352. await this.helper.propose();
  353. await this.helper.waitForDeadline();
  354. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  355. await this.helper.waitForDeadline(+1);
  356. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Defeated);
  357. });
  358. it('Succeeded', async function () {
  359. await this.helper.propose();
  360. await this.helper.waitForSnapshot();
  361. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  362. await this.helper.waitForDeadline();
  363. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  364. await this.helper.waitForDeadline(+1);
  365. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  366. });
  367. it('Executed', async function () {
  368. await this.helper.propose();
  369. await this.helper.waitForSnapshot();
  370. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  371. await this.helper.waitForDeadline();
  372. await this.helper.execute();
  373. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
  374. });
  375. });
  376. describe('cancel', function () {
  377. describe('internal', function () {
  378. it('before proposal', async function () {
  379. await expectRevertCustomError(this.helper.cancel('internal'), 'GovernorNonexistentProposal', [
  380. this.proposal.id,
  381. ]);
  382. });
  383. it('after proposal', async function () {
  384. await this.helper.propose();
  385. await this.helper.cancel('internal');
  386. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  387. await this.helper.waitForSnapshot();
  388. await expectRevertCustomError(
  389. this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
  390. 'GovernorIncorrectState',
  391. [this.proposal.id, Enums.ProposalState.Canceled, proposalStatesToBitMap([Enums.ProposalState.Active])],
  392. );
  393. });
  394. it('after vote', async function () {
  395. await this.helper.propose();
  396. await this.helper.waitForSnapshot();
  397. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  398. await this.helper.cancel('internal');
  399. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  400. await this.helper.waitForDeadline();
  401. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  402. this.proposal.id,
  403. Enums.ProposalState.Canceled,
  404. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  405. ]);
  406. });
  407. it('after deadline', async function () {
  408. await this.helper.propose();
  409. await this.helper.waitForSnapshot();
  410. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  411. await this.helper.waitForDeadline();
  412. await this.helper.cancel('internal');
  413. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  414. await expectRevertCustomError(this.helper.execute(), 'GovernorIncorrectState', [
  415. this.proposal.id,
  416. Enums.ProposalState.Canceled,
  417. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  418. ]);
  419. });
  420. it('after execution', async function () {
  421. await this.helper.propose();
  422. await this.helper.waitForSnapshot();
  423. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  424. await this.helper.waitForDeadline();
  425. await this.helper.execute();
  426. await expectRevertCustomError(this.helper.cancel('internal'), 'GovernorIncorrectState', [
  427. this.proposal.id,
  428. Enums.ProposalState.Executed,
  429. proposalStatesToBitMap(
  430. [Enums.ProposalState.Canceled, Enums.ProposalState.Expired, Enums.ProposalState.Executed],
  431. { inverted: true },
  432. ),
  433. ]);
  434. });
  435. });
  436. describe('public', function () {
  437. it('before proposal', async function () {
  438. await expectRevertCustomError(this.helper.cancel('external'), 'GovernorNonexistentProposal', [
  439. this.proposal.id,
  440. ]);
  441. });
  442. it('after proposal', async function () {
  443. await this.helper.propose();
  444. await this.helper.cancel('external');
  445. });
  446. it('after proposal - restricted to proposer', async function () {
  447. await this.helper.propose();
  448. await expectRevertCustomError(this.helper.cancel('external', { from: owner }), 'GovernorOnlyProposer', [
  449. owner,
  450. ]);
  451. });
  452. it('after vote started', async function () {
  453. await this.helper.propose();
  454. await this.helper.waitForSnapshot(1); // snapshot + 1 block
  455. await expectRevertCustomError(this.helper.cancel('external'), 'GovernorIncorrectState', [
  456. this.proposal.id,
  457. Enums.ProposalState.Active,
  458. proposalStatesToBitMap([Enums.ProposalState.Pending]),
  459. ]);
  460. });
  461. it('after vote', async function () {
  462. await this.helper.propose();
  463. await this.helper.waitForSnapshot();
  464. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  465. await expectRevertCustomError(this.helper.cancel('external'), 'GovernorIncorrectState', [
  466. this.proposal.id,
  467. Enums.ProposalState.Active,
  468. proposalStatesToBitMap([Enums.ProposalState.Pending]),
  469. ]);
  470. });
  471. it('after deadline', async function () {
  472. await this.helper.propose();
  473. await this.helper.waitForSnapshot();
  474. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  475. await this.helper.waitForDeadline();
  476. await expectRevertCustomError(this.helper.cancel('external'), 'GovernorIncorrectState', [
  477. this.proposal.id,
  478. Enums.ProposalState.Succeeded,
  479. proposalStatesToBitMap([Enums.ProposalState.Pending]),
  480. ]);
  481. });
  482. it('after execution', async function () {
  483. await this.helper.propose();
  484. await this.helper.waitForSnapshot();
  485. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  486. await this.helper.waitForDeadline();
  487. await this.helper.execute();
  488. await expectRevertCustomError(this.helper.cancel('external'), 'GovernorIncorrectState', [
  489. this.proposal.id,
  490. Enums.ProposalState.Executed,
  491. proposalStatesToBitMap([Enums.ProposalState.Pending]),
  492. ]);
  493. });
  494. });
  495. });
  496. describe('proposal length', function () {
  497. it('empty', async function () {
  498. this.helper.setProposal([], '<proposal description>');
  499. await expectRevertCustomError(this.helper.propose(), 'GovernorInvalidProposalLength', [0, 0, 0]);
  500. });
  501. it('mismatch #1', async function () {
  502. this.helper.setProposal(
  503. {
  504. targets: [],
  505. values: [web3.utils.toWei('0')],
  506. data: [this.receiver.contract.methods.mockFunction().encodeABI()],
  507. },
  508. '<proposal description>',
  509. );
  510. await expectRevertCustomError(this.helper.propose(), 'GovernorInvalidProposalLength', [0, 1, 1]);
  511. });
  512. it('mismatch #2', async function () {
  513. this.helper.setProposal(
  514. {
  515. targets: [this.receiver.address],
  516. values: [],
  517. data: [this.receiver.contract.methods.mockFunction().encodeABI()],
  518. },
  519. '<proposal description>',
  520. );
  521. await expectRevertCustomError(this.helper.propose(), 'GovernorInvalidProposalLength', [1, 1, 0]);
  522. });
  523. it('mismatch #3', async function () {
  524. this.helper.setProposal(
  525. {
  526. targets: [this.receiver.address],
  527. values: [web3.utils.toWei('0')],
  528. data: [],
  529. },
  530. '<proposal description>',
  531. );
  532. await expectRevertCustomError(this.helper.propose(), 'GovernorInvalidProposalLength', [1, 0, 1]);
  533. });
  534. });
  535. describe('onlyGovernance updates', function () {
  536. it('setVotingDelay is protected', async function () {
  537. await expectRevertCustomError(this.mock.setVotingDelay('0'), 'GovernorOnlyGovernance', []);
  538. });
  539. it('setVotingPeriod is protected', async function () {
  540. await expectRevertCustomError(this.mock.setVotingPeriod('32'), 'GovernorOnlyGovernance', []);
  541. });
  542. it('setProposalThreshold is protected', async function () {
  543. await expectRevertCustomError(
  544. this.mock.setProposalThreshold('1000000000000000000'),
  545. 'GovernorOnlyGovernance',
  546. [],
  547. );
  548. });
  549. it('can setVotingDelay through governance', async function () {
  550. this.helper.setProposal(
  551. [
  552. {
  553. target: this.mock.address,
  554. data: this.mock.contract.methods.setVotingDelay('0').encodeABI(),
  555. },
  556. ],
  557. '<proposal description>',
  558. );
  559. await this.helper.propose();
  560. await this.helper.waitForSnapshot();
  561. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  562. await this.helper.waitForDeadline();
  563. expectEvent(await this.helper.execute(), 'VotingDelaySet', { oldVotingDelay: '4', newVotingDelay: '0' });
  564. expect(await this.mock.votingDelay()).to.be.bignumber.equal('0');
  565. });
  566. it('can setVotingPeriod through governance', async function () {
  567. this.helper.setProposal(
  568. [
  569. {
  570. target: this.mock.address,
  571. data: this.mock.contract.methods.setVotingPeriod('32').encodeABI(),
  572. },
  573. ],
  574. '<proposal description>',
  575. );
  576. await this.helper.propose();
  577. await this.helper.waitForSnapshot();
  578. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  579. await this.helper.waitForDeadline();
  580. expectEvent(await this.helper.execute(), 'VotingPeriodSet', { oldVotingPeriod: '16', newVotingPeriod: '32' });
  581. expect(await this.mock.votingPeriod()).to.be.bignumber.equal('32');
  582. });
  583. it('cannot setVotingPeriod to 0 through governance', async function () {
  584. const votingPeriod = 0;
  585. this.helper.setProposal(
  586. [
  587. {
  588. target: this.mock.address,
  589. data: this.mock.contract.methods.setVotingPeriod(votingPeriod).encodeABI(),
  590. },
  591. ],
  592. '<proposal description>',
  593. );
  594. await this.helper.propose();
  595. await this.helper.waitForSnapshot();
  596. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  597. await this.helper.waitForDeadline();
  598. await expectRevertCustomError(this.helper.execute(), 'GovernorInvalidVotingPeriod', [votingPeriod]);
  599. });
  600. it('can setProposalThreshold to 0 through governance', async function () {
  601. this.helper.setProposal(
  602. [
  603. {
  604. target: this.mock.address,
  605. data: this.mock.contract.methods.setProposalThreshold('1000000000000000000').encodeABI(),
  606. },
  607. ],
  608. '<proposal description>',
  609. );
  610. await this.helper.propose();
  611. await this.helper.waitForSnapshot();
  612. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  613. await this.helper.waitForDeadline();
  614. expectEvent(await this.helper.execute(), 'ProposalThresholdSet', {
  615. oldProposalThreshold: '0',
  616. newProposalThreshold: '1000000000000000000',
  617. });
  618. expect(await this.mock.proposalThreshold()).to.be.bignumber.equal('1000000000000000000');
  619. });
  620. });
  621. describe('safe receive', function () {
  622. describe('ERC721', function () {
  623. const name = 'Non Fungible Token';
  624. const symbol = 'NFT';
  625. const tokenId = web3.utils.toBN(1);
  626. beforeEach(async function () {
  627. this.token = await ERC721.new(name, symbol);
  628. await this.token.$_mint(owner, tokenId);
  629. });
  630. it('can receive an ERC721 safeTransfer', async function () {
  631. await this.token.safeTransferFrom(owner, this.mock.address, tokenId, { from: owner });
  632. });
  633. });
  634. describe('ERC1155', function () {
  635. const uri = 'https://token-cdn-domain/{id}.json';
  636. const tokenIds = {
  637. 1: web3.utils.toBN(1000),
  638. 2: web3.utils.toBN(2000),
  639. 3: web3.utils.toBN(3000),
  640. };
  641. beforeEach(async function () {
  642. this.token = await ERC1155.new(uri);
  643. await this.token.$_mintBatch(owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
  644. });
  645. it('can receive ERC1155 safeTransfer', async function () {
  646. await this.token.safeTransferFrom(
  647. owner,
  648. this.mock.address,
  649. ...Object.entries(tokenIds)[0], // id + amount
  650. '0x',
  651. { from: owner },
  652. );
  653. });
  654. it('can receive ERC1155 safeBatchTransfer', async function () {
  655. await this.token.safeBatchTransferFrom(
  656. owner,
  657. this.mock.address,
  658. Object.keys(tokenIds),
  659. Object.values(tokenIds),
  660. '0x',
  661. { from: owner },
  662. );
  663. });
  664. });
  665. });
  666. });
  667. }
  668. });