Governor.test.js 25 KB

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