GovernorTimelockCompound.test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper, proposalStatesToBitMap } = require('../../helpers/governance');
  5. const { expectRevertCustomError } = require('../../helpers/customError');
  6. const { computeCreateAddress } = require('../../helpers/create');
  7. const { clockFromReceipt } = require('../../helpers/time');
  8. const Timelock = artifacts.require('CompTimelock');
  9. const Governor = artifacts.require('$GovernorTimelockCompoundMock');
  10. const CallReceiver = artifacts.require('CallReceiverMock');
  11. const ERC721 = artifacts.require('$ERC721');
  12. const ERC1155 = artifacts.require('$ERC1155');
  13. const TOKENS = [
  14. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  15. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  16. ];
  17. contract('GovernorTimelockCompound', function (accounts) {
  18. const [owner, voter1, voter2, voter3, voter4, other] = accounts;
  19. const name = 'OZ-Governor';
  20. const version = '1';
  21. const tokenName = 'MockToken';
  22. const tokenSymbol = 'MTKN';
  23. const tokenSupply = web3.utils.toWei('100');
  24. const votingDelay = web3.utils.toBN(4);
  25. const votingPeriod = web3.utils.toBN(16);
  26. const value = web3.utils.toWei('1');
  27. const defaultDelay = 2 * 86400;
  28. for (const { mode, Token } of TOKENS) {
  29. describe(`using ${Token._json.contractName}`, function () {
  30. beforeEach(async function () {
  31. const [deployer] = await web3.eth.getAccounts();
  32. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  33. // Need to predict governance address to set it as timelock admin with a delayed transfer
  34. const nonce = await web3.eth.getTransactionCount(deployer);
  35. const predictGovernor = computeCreateAddress(deployer, nonce + 1);
  36. this.timelock = await Timelock.new(predictGovernor, defaultDelay);
  37. this.mock = await Governor.new(
  38. name,
  39. votingDelay,
  40. votingPeriod,
  41. 0,
  42. this.timelock.address,
  43. this.token.address,
  44. 0,
  45. );
  46. this.receiver = await CallReceiver.new();
  47. this.helper = new GovernorHelper(this.mock, mode);
  48. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  49. await this.token.$_mint(owner, tokenSupply);
  50. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  51. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  52. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  53. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  54. // default proposal
  55. this.proposal = this.helper.setProposal(
  56. [
  57. {
  58. target: this.receiver.address,
  59. value,
  60. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  61. },
  62. ],
  63. '<proposal description>',
  64. );
  65. });
  66. it("doesn't accept ether transfers", async function () {
  67. await expectRevert.unspecified(web3.eth.sendTransaction({ from: owner, to: this.mock.address, value: 1 }));
  68. });
  69. it('post deployment check', async function () {
  70. expect(await this.mock.name()).to.be.equal(name);
  71. expect(await this.mock.token()).to.be.equal(this.token.address);
  72. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  73. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  74. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  75. expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
  76. expect(await this.timelock.admin()).to.be.equal(this.mock.address);
  77. });
  78. it('nominal', async function () {
  79. expect(await this.mock.proposalEta(this.proposal.id)).to.be.bignumber.equal('0');
  80. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.equal(true);
  81. await this.helper.propose();
  82. await this.helper.waitForSnapshot();
  83. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  84. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  85. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  86. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  87. await this.helper.waitForDeadline();
  88. const txQueue = await this.helper.queue();
  89. const eta = web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(defaultDelay);
  90. expect(await this.mock.proposalEta(this.proposal.id)).to.be.bignumber.equal(eta);
  91. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.equal(true);
  92. await this.helper.waitForEta();
  93. const txExecute = await this.helper.execute();
  94. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  95. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'QueueTransaction', { eta });
  96. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  97. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'ExecuteTransaction', { eta });
  98. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  99. });
  100. describe('should revert', function () {
  101. describe('on queue', function () {
  102. it('if already queued', async function () {
  103. await this.helper.propose();
  104. await this.helper.waitForSnapshot();
  105. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  106. await this.helper.waitForDeadline();
  107. await this.helper.queue();
  108. await expectRevertCustomError(this.helper.queue(), 'GovernorUnexpectedProposalState', [
  109. this.proposal.id,
  110. Enums.ProposalState.Queued,
  111. proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
  112. ]);
  113. });
  114. it('if proposal contains duplicate calls', async function () {
  115. const action = {
  116. target: this.token.address,
  117. data: this.token.contract.methods.approve(this.receiver.address, constants.MAX_UINT256).encodeABI(),
  118. };
  119. const { id } = this.helper.setProposal([action, action], '<proposal description>');
  120. await this.helper.propose();
  121. await this.helper.waitForSnapshot();
  122. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  123. await this.helper.waitForDeadline();
  124. await expectRevertCustomError(this.helper.queue(), 'GovernorAlreadyQueuedProposal', [id]);
  125. await expectRevertCustomError(this.helper.execute(), 'GovernorNotQueuedProposal', [id]);
  126. });
  127. });
  128. describe('on execute', function () {
  129. it('if not queued', async function () {
  130. await this.helper.propose();
  131. await this.helper.waitForSnapshot();
  132. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  133. await this.helper.waitForDeadline(+1);
  134. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  135. await expectRevertCustomError(this.helper.execute(), 'GovernorNotQueuedProposal', [this.proposal.id]);
  136. });
  137. it('if too early', async function () {
  138. await this.helper.propose();
  139. await this.helper.waitForSnapshot();
  140. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  141. await this.helper.waitForDeadline();
  142. await this.helper.queue();
  143. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  144. await expectRevert(
  145. this.helper.execute(),
  146. "Timelock::executeTransaction: Transaction hasn't surpassed time lock",
  147. );
  148. });
  149. it('if too late', async function () {
  150. await this.helper.propose();
  151. await this.helper.waitForSnapshot();
  152. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  153. await this.helper.waitForDeadline();
  154. await this.helper.queue();
  155. await this.helper.waitForEta(+30 * 86400);
  156. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Expired);
  157. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  158. this.proposal.id,
  159. Enums.ProposalState.Expired,
  160. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  161. ]);
  162. });
  163. it('if already executed', async function () {
  164. await this.helper.propose();
  165. await this.helper.waitForSnapshot();
  166. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  167. await this.helper.waitForDeadline();
  168. await this.helper.queue();
  169. await this.helper.waitForEta();
  170. await this.helper.execute();
  171. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  172. this.proposal.id,
  173. Enums.ProposalState.Executed,
  174. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  175. ]);
  176. });
  177. });
  178. describe('on safe receive', function () {
  179. describe('ERC721', function () {
  180. const name = 'Non Fungible Token';
  181. const symbol = 'NFT';
  182. const tokenId = web3.utils.toBN(1);
  183. beforeEach(async function () {
  184. this.token = await ERC721.new(name, symbol);
  185. await this.token.$_mint(owner, tokenId);
  186. });
  187. it("can't receive an ERC721 safeTransfer", async function () {
  188. await expectRevertCustomError(
  189. this.token.safeTransferFrom(owner, this.mock.address, tokenId, { from: owner }),
  190. 'GovernorDisabledDeposit',
  191. [],
  192. );
  193. });
  194. });
  195. describe('ERC1155', function () {
  196. const uri = 'https://token-cdn-domain/{id}.json';
  197. const tokenIds = {
  198. 1: web3.utils.toBN(1000),
  199. 2: web3.utils.toBN(2000),
  200. 3: web3.utils.toBN(3000),
  201. };
  202. beforeEach(async function () {
  203. this.token = await ERC1155.new(uri);
  204. await this.token.$_mintBatch(owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
  205. });
  206. it("can't receive ERC1155 safeTransfer", async function () {
  207. await expectRevertCustomError(
  208. this.token.safeTransferFrom(
  209. owner,
  210. this.mock.address,
  211. ...Object.entries(tokenIds)[0], // id + amount
  212. '0x',
  213. { from: owner },
  214. ),
  215. 'GovernorDisabledDeposit',
  216. [],
  217. );
  218. });
  219. it("can't receive ERC1155 safeBatchTransfer", async function () {
  220. await expectRevertCustomError(
  221. this.token.safeBatchTransferFrom(
  222. owner,
  223. this.mock.address,
  224. Object.keys(tokenIds),
  225. Object.values(tokenIds),
  226. '0x',
  227. { from: owner },
  228. ),
  229. 'GovernorDisabledDeposit',
  230. [],
  231. );
  232. });
  233. });
  234. });
  235. });
  236. describe('cancel', function () {
  237. it('cancel before queue prevents scheduling', 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 this.helper.waitForDeadline();
  242. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  243. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  244. await expectRevertCustomError(this.helper.queue(), 'GovernorUnexpectedProposalState', [
  245. this.proposal.id,
  246. Enums.ProposalState.Canceled,
  247. proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
  248. ]);
  249. });
  250. it('cancel after queue prevents executing', async function () {
  251. await this.helper.propose();
  252. await this.helper.waitForSnapshot();
  253. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  254. await this.helper.waitForDeadline();
  255. await this.helper.queue();
  256. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  257. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  258. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  259. this.proposal.id,
  260. Enums.ProposalState.Canceled,
  261. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  262. ]);
  263. });
  264. });
  265. describe('onlyGovernance', function () {
  266. describe('relay', function () {
  267. beforeEach(async function () {
  268. await this.token.$_mint(this.mock.address, 1);
  269. });
  270. it('is protected', async function () {
  271. await expectRevertCustomError(
  272. this.mock.relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI(), {
  273. from: owner,
  274. }),
  275. 'GovernorOnlyExecutor',
  276. [owner],
  277. );
  278. });
  279. it('can be executed through governance', async function () {
  280. this.helper.setProposal(
  281. [
  282. {
  283. target: this.mock.address,
  284. data: this.mock.contract.methods
  285. .relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI())
  286. .encodeABI(),
  287. },
  288. ],
  289. '<proposal description>',
  290. );
  291. expect(await this.token.balanceOf(this.mock.address), 1);
  292. expect(await this.token.balanceOf(other), 0);
  293. await this.helper.propose();
  294. await this.helper.waitForSnapshot();
  295. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  296. await this.helper.waitForDeadline();
  297. await this.helper.queue();
  298. await this.helper.waitForEta();
  299. const txExecute = await this.helper.execute();
  300. expect(await this.token.balanceOf(this.mock.address), 0);
  301. expect(await this.token.balanceOf(other), 1);
  302. await expectEvent.inTransaction(txExecute.tx, this.token, 'Transfer', {
  303. from: this.mock.address,
  304. to: other,
  305. value: '1',
  306. });
  307. });
  308. });
  309. describe('updateTimelock', function () {
  310. beforeEach(async function () {
  311. this.newTimelock = await Timelock.new(this.mock.address, 7 * 86400);
  312. });
  313. it('is protected', async function () {
  314. await expectRevertCustomError(
  315. this.mock.updateTimelock(this.newTimelock.address, { from: owner }),
  316. 'GovernorOnlyExecutor',
  317. [owner],
  318. );
  319. });
  320. it('can be executed through governance to', async function () {
  321. this.helper.setProposal(
  322. [
  323. {
  324. target: this.timelock.address,
  325. data: this.timelock.contract.methods.setPendingAdmin(owner).encodeABI(),
  326. },
  327. {
  328. target: this.mock.address,
  329. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  330. },
  331. ],
  332. '<proposal description>',
  333. );
  334. await this.helper.propose();
  335. await this.helper.waitForSnapshot();
  336. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  337. await this.helper.waitForDeadline();
  338. await this.helper.queue();
  339. await this.helper.waitForEta();
  340. const txExecute = await this.helper.execute();
  341. expectEvent(txExecute, 'TimelockChange', {
  342. oldTimelock: this.timelock.address,
  343. newTimelock: this.newTimelock.address,
  344. });
  345. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  346. });
  347. });
  348. it('can transfer timelock to new governor', async function () {
  349. const newGovernor = await Governor.new(name, 8, 32, 0, this.timelock.address, this.token.address, 0);
  350. this.helper.setProposal(
  351. [
  352. {
  353. target: this.timelock.address,
  354. data: this.timelock.contract.methods.setPendingAdmin(newGovernor.address).encodeABI(),
  355. },
  356. ],
  357. '<proposal description>',
  358. );
  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. await this.helper.queue();
  364. await this.helper.waitForEta();
  365. const txExecute = await this.helper.execute();
  366. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'NewPendingAdmin', {
  367. newPendingAdmin: newGovernor.address,
  368. });
  369. await newGovernor.__acceptAdmin();
  370. expect(await this.timelock.admin()).to.be.bignumber.equal(newGovernor.address);
  371. });
  372. });
  373. });
  374. }
  375. });