GovernorTimelockControl.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
  5. const { GovernorHelper, timelockSalt } = require('../../helpers/governance');
  6. const { bigint: Enums } = require('../../helpers/enums');
  7. const { bigint: time } = require('../../helpers/time');
  8. const TOKENS = [
  9. { Token: '$ERC20Votes', mode: 'blocknumber' },
  10. { Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
  11. ];
  12. const DEFAULT_ADMIN_ROLE = ethers.ZeroHash;
  13. const PROPOSER_ROLE = ethers.id('PROPOSER_ROLE');
  14. const EXECUTOR_ROLE = ethers.id('EXECUTOR_ROLE');
  15. const CANCELLER_ROLE = ethers.id('CANCELLER_ROLE');
  16. const name = 'OZ-Governor';
  17. const version = '1';
  18. const tokenName = 'MockToken';
  19. const tokenSymbol = 'MTKN';
  20. const tokenSupply = ethers.parseEther('100');
  21. const votingDelay = 4n;
  22. const votingPeriod = 16n;
  23. const value = ethers.parseEther('1');
  24. const delay = time.duration.hours(1n);
  25. describe('GovernorTimelockControl', function () {
  26. for (const { Token, mode } of TOKENS) {
  27. const fixture = async () => {
  28. const [deployer, owner, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
  29. const receiver = await ethers.deployContract('CallReceiverMock');
  30. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, version]);
  31. const timelock = await ethers.deployContract('TimelockController', [delay, [], [], deployer]);
  32. const mock = await ethers.deployContract('$GovernorTimelockControlMock', [
  33. name,
  34. votingDelay,
  35. votingPeriod,
  36. 0n,
  37. timelock,
  38. token,
  39. 0n,
  40. ]);
  41. await owner.sendTransaction({ to: timelock, value });
  42. await token.$_mint(owner, tokenSupply);
  43. await timelock.grantRole(PROPOSER_ROLE, mock);
  44. await timelock.grantRole(PROPOSER_ROLE, owner);
  45. await timelock.grantRole(CANCELLER_ROLE, mock);
  46. await timelock.grantRole(CANCELLER_ROLE, owner);
  47. await timelock.grantRole(EXECUTOR_ROLE, ethers.ZeroAddress);
  48. await timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
  49. const helper = new GovernorHelper(mock, mode);
  50. await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') });
  51. await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') });
  52. await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') });
  53. await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') });
  54. return { deployer, owner, voter1, voter2, voter3, voter4, other, receiver, token, mock, timelock, helper };
  55. };
  56. describe(`using ${Token}`, function () {
  57. beforeEach(async function () {
  58. Object.assign(this, await loadFixture(fixture));
  59. // default proposal
  60. this.proposal = this.helper.setProposal(
  61. [
  62. {
  63. target: this.receiver.target,
  64. value,
  65. data: this.receiver.interface.encodeFunctionData('mockFunction'),
  66. },
  67. ],
  68. '<proposal description>',
  69. );
  70. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  71. ...this.proposal.shortProposal.slice(0, 3),
  72. ethers.ZeroHash,
  73. timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
  74. );
  75. });
  76. it("doesn't accept ether transfers", async function () {
  77. await expect(this.owner.sendTransaction({ to: this.mock, value: 1n })).to.be.revertedWithCustomError(
  78. this.mock,
  79. 'GovernorDisabledDeposit',
  80. );
  81. });
  82. it('post deployment check', async function () {
  83. expect(await this.mock.name()).to.equal(name);
  84. expect(await this.mock.token()).to.equal(this.token.target);
  85. expect(await this.mock.votingDelay()).to.equal(votingDelay);
  86. expect(await this.mock.votingPeriod()).to.equal(votingPeriod);
  87. expect(await this.mock.quorum(0n)).to.equal(0n);
  88. expect(await this.mock.timelock()).to.equal(this.timelock.target);
  89. });
  90. it('nominal', async function () {
  91. expect(await this.mock.proposalEta(this.proposal.id)).to.equal(0n);
  92. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
  93. await this.helper.propose();
  94. await this.helper.waitForSnapshot();
  95. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  96. await this.helper.connect(this.voter2).vote({ support: Enums.VoteType.For });
  97. await this.helper.connect(this.voter3).vote({ support: Enums.VoteType.Against });
  98. await this.helper.connect(this.voter4).vote({ support: Enums.VoteType.Abstain });
  99. await this.helper.waitForDeadline();
  100. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
  101. const txQueue = await this.helper.queue();
  102. const eta = (await time.clockFromReceipt.timestamp(txQueue)) + delay;
  103. expect(await this.mock.proposalEta(this.proposal.id)).to.equal(eta);
  104. await this.helper.waitForEta();
  105. const txExecute = this.helper.execute();
  106. await expect(txQueue)
  107. .to.emit(this.mock, 'ProposalQueued')
  108. .withArgs(this.proposal.id, anyValue)
  109. .to.emit(this.timelock, 'CallScheduled')
  110. .withArgs(this.proposal.timelockid, ...Array(6).fill(anyValue))
  111. .to.emit(this.timelock, 'CallSalt')
  112. .withArgs(this.proposal.timelockid, anyValue);
  113. await expect(txExecute)
  114. .to.emit(this.mock, 'ProposalExecuted')
  115. .withArgs(this.proposal.id)
  116. .to.emit(this.timelock, 'CallExecuted')
  117. .withArgs(this.proposal.timelockid, ...Array(4).fill(anyValue))
  118. .to.emit(this.receiver, 'MockFunctionCalled');
  119. });
  120. describe('should revert', function () {
  121. describe('on queue', function () {
  122. it('if already queued', async function () {
  123. await this.helper.propose();
  124. await this.helper.waitForSnapshot();
  125. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  126. await this.helper.waitForDeadline();
  127. await this.helper.queue();
  128. await expect(this.helper.queue())
  129. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  130. .withArgs(
  131. this.proposal.id,
  132. Enums.ProposalState.Queued,
  133. GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
  134. );
  135. });
  136. });
  137. describe('on execute', function () {
  138. it('if not queued', async function () {
  139. await this.helper.propose();
  140. await this.helper.waitForSnapshot();
  141. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  142. await this.helper.waitForDeadline(1n);
  143. expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Succeeded);
  144. await expect(this.helper.execute())
  145. .to.be.revertedWithCustomError(this.timelock, 'TimelockUnexpectedOperationState')
  146. .withArgs(this.proposal.timelockid, GovernorHelper.proposalStatesToBitMap(Enums.OperationState.Ready));
  147. });
  148. it('if too early', async function () {
  149. await this.helper.propose();
  150. await this.helper.waitForSnapshot();
  151. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  152. await this.helper.waitForDeadline();
  153. await this.helper.queue();
  154. expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Queued);
  155. await expect(this.helper.execute())
  156. .to.be.revertedWithCustomError(this.timelock, 'TimelockUnexpectedOperationState')
  157. .withArgs(this.proposal.timelockid, GovernorHelper.proposalStatesToBitMap(Enums.OperationState.Ready));
  158. });
  159. it('if already executed', async function () {
  160. await this.helper.propose();
  161. await this.helper.waitForSnapshot();
  162. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  163. await this.helper.waitForDeadline();
  164. await this.helper.queue();
  165. await this.helper.waitForEta();
  166. await this.helper.execute();
  167. await expect(this.helper.execute())
  168. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  169. .withArgs(
  170. this.proposal.id,
  171. Enums.ProposalState.Executed,
  172. GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  173. );
  174. });
  175. it('if already executed by another proposer', async function () {
  176. await this.helper.propose();
  177. await this.helper.waitForSnapshot();
  178. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  179. await this.helper.waitForDeadline();
  180. await this.helper.queue();
  181. await this.helper.waitForEta();
  182. await this.timelock.executeBatch(
  183. ...this.proposal.shortProposal.slice(0, 3),
  184. ethers.ZeroHash,
  185. timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
  186. );
  187. await expect(this.helper.execute())
  188. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  189. .withArgs(
  190. this.proposal.id,
  191. Enums.ProposalState.Executed,
  192. GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  193. );
  194. });
  195. });
  196. });
  197. describe('cancel', function () {
  198. it('cancel before queue prevents scheduling', async function () {
  199. await this.helper.propose();
  200. await this.helper.waitForSnapshot();
  201. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  202. await this.helper.waitForDeadline();
  203. await expect(this.helper.cancel('internal'))
  204. .to.emit(this.mock, 'ProposalCanceled')
  205. .withArgs(this.proposal.id);
  206. expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Canceled);
  207. await expect(this.helper.queue())
  208. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  209. .withArgs(
  210. this.proposal.id,
  211. Enums.ProposalState.Canceled,
  212. GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
  213. );
  214. });
  215. it('cancel after queue prevents executing', async function () {
  216. await this.helper.propose();
  217. await this.helper.waitForSnapshot();
  218. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  219. await this.helper.waitForDeadline();
  220. await this.helper.queue();
  221. await expect(this.helper.cancel('internal'))
  222. .to.emit(this.mock, 'ProposalCanceled')
  223. .withArgs(this.proposal.id);
  224. expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Canceled);
  225. await expect(this.helper.execute())
  226. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  227. .withArgs(
  228. this.proposal.id,
  229. Enums.ProposalState.Canceled,
  230. GovernorHelper.proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  231. );
  232. });
  233. it('cancel on timelock is reflected on governor', async function () {
  234. await this.helper.propose();
  235. await this.helper.waitForSnapshot();
  236. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  237. await this.helper.waitForDeadline();
  238. await this.helper.queue();
  239. expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Queued);
  240. await expect(this.timelock.connect(this.owner).cancel(this.proposal.timelockid))
  241. .to.emit(this.timelock, 'Cancelled')
  242. .withArgs(this.proposal.timelockid);
  243. expect(await this.mock.state(this.proposal.id)).to.equal(Enums.ProposalState.Canceled);
  244. });
  245. });
  246. describe('onlyGovernance', function () {
  247. describe('relay', function () {
  248. beforeEach(async function () {
  249. await this.token.$_mint(this.mock, 1);
  250. });
  251. it('is protected', async function () {
  252. await expect(
  253. this.mock
  254. .connect(this.owner)
  255. .relay(this.token, 0n, this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n])),
  256. )
  257. .to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
  258. .withArgs(this.owner.address);
  259. });
  260. it('can be executed through governance', async function () {
  261. this.helper.setProposal(
  262. [
  263. {
  264. target: this.mock.target,
  265. data: this.mock.interface.encodeFunctionData('relay', [
  266. this.token.target,
  267. 0n,
  268. this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n]),
  269. ]),
  270. },
  271. ],
  272. '<proposal description>',
  273. );
  274. await this.helper.propose();
  275. await this.helper.waitForSnapshot();
  276. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  277. await this.helper.waitForDeadline();
  278. await this.helper.queue();
  279. await this.helper.waitForEta();
  280. const txExecute = await this.helper.execute();
  281. await expect(txExecute).to.changeTokenBalances(this.token, [this.mock, this.other], [-1n, 1n]);
  282. await expect(txExecute).to.emit(this.token, 'Transfer').withArgs(this.mock.target, this.other.address, 1n);
  283. });
  284. it('is payable and can transfer eth to EOA', async function () {
  285. const t2g = 128n; // timelock to governor
  286. const g2o = 100n; // governor to eoa (other)
  287. this.helper.setProposal(
  288. [
  289. {
  290. target: this.mock.target,
  291. value: t2g,
  292. data: this.mock.interface.encodeFunctionData('relay', [this.other.address, g2o, '0x']),
  293. },
  294. ],
  295. '<proposal description>',
  296. );
  297. await this.helper.propose();
  298. await this.helper.waitForSnapshot();
  299. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  300. await this.helper.waitForDeadline();
  301. await this.helper.queue();
  302. await this.helper.waitForEta();
  303. await expect(this.helper.execute()).to.changeEtherBalances(
  304. [this.timelock, this.mock, this.other],
  305. [-t2g, t2g - g2o, g2o],
  306. );
  307. });
  308. it('protected against other proposers', async function () {
  309. const call = [
  310. this.mock,
  311. 0n,
  312. this.mock.interface.encodeFunctionData('relay', [ethers.ZeroAddress, 0n, '0x']),
  313. ethers.ZeroHash,
  314. ethers.ZeroHash,
  315. ];
  316. await this.timelock.connect(this.owner).schedule(...call, delay);
  317. await time.increaseBy.timestamp(delay);
  318. // Error bubbled up from Governor
  319. await expect(this.timelock.connect(this.owner).execute(...call)).to.be.revertedWithCustomError(
  320. this.mock,
  321. 'QueueEmpty',
  322. );
  323. });
  324. });
  325. describe('updateTimelock', function () {
  326. beforeEach(async function () {
  327. this.newTimelock = await ethers.deployContract('TimelockController', [
  328. delay,
  329. [this.mock],
  330. [this.mock],
  331. ethers.ZeroAddress,
  332. ]);
  333. });
  334. it('is protected', async function () {
  335. await expect(this.mock.connect(this.owner).updateTimelock(this.newTimelock))
  336. .to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
  337. .withArgs(this.owner.address);
  338. });
  339. it('can be executed through governance to', async function () {
  340. this.helper.setProposal(
  341. [
  342. {
  343. target: this.mock.target,
  344. data: this.mock.interface.encodeFunctionData('updateTimelock', [this.newTimelock.target]),
  345. },
  346. ],
  347. '<proposal description>',
  348. );
  349. await this.helper.propose();
  350. await this.helper.waitForSnapshot();
  351. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  352. await this.helper.waitForDeadline();
  353. await this.helper.queue();
  354. await this.helper.waitForEta();
  355. await expect(this.helper.execute())
  356. .to.emit(this.mock, 'TimelockChange')
  357. .withArgs(this.timelock.target, this.newTimelock.target);
  358. expect(await this.mock.timelock()).to.equal(this.newTimelock.target);
  359. });
  360. });
  361. describe('on safe receive', function () {
  362. describe('ERC721', function () {
  363. const tokenId = 1n;
  364. beforeEach(async function () {
  365. this.token = await ethers.deployContract('$ERC721', ['Non Fungible Token', 'NFT']);
  366. await this.token.$_mint(this.owner, tokenId);
  367. });
  368. it("can't receive an ERC721 safeTransfer", async function () {
  369. await expect(
  370. this.token.connect(this.owner).safeTransferFrom(this.owner, this.mock, tokenId),
  371. ).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
  372. });
  373. });
  374. describe('ERC1155', function () {
  375. const tokenIds = {
  376. 1: 1000n,
  377. 2: 2000n,
  378. 3: 3000n,
  379. };
  380. beforeEach(async function () {
  381. this.token = await ethers.deployContract('$ERC1155', ['https://token-cdn-domain/{id}.json']);
  382. await this.token.$_mintBatch(this.owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
  383. });
  384. it("can't receive ERC1155 safeTransfer", async function () {
  385. await expect(
  386. this.token.connect(this.owner).safeTransferFrom(
  387. this.owner,
  388. this.mock,
  389. ...Object.entries(tokenIds)[0], // id + amount
  390. '0x',
  391. ),
  392. ).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
  393. });
  394. it("can't receive ERC1155 safeBatchTransfer", async function () {
  395. await expect(
  396. this.token
  397. .connect(this.owner)
  398. .safeBatchTransferFrom(this.owner, this.mock, Object.keys(tokenIds), Object.values(tokenIds), '0x'),
  399. ).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
  400. });
  401. });
  402. });
  403. });
  404. it('clear queue of pending governor calls', async function () {
  405. this.helper.setProposal(
  406. [
  407. {
  408. target: this.mock.target,
  409. data: this.mock.interface.encodeFunctionData('nonGovernanceFunction'),
  410. },
  411. ],
  412. '<proposal description>',
  413. );
  414. await this.helper.propose();
  415. await this.helper.waitForSnapshot();
  416. await this.helper.connect(this.voter1).vote({ support: Enums.VoteType.For });
  417. await this.helper.waitForDeadline();
  418. await this.helper.queue();
  419. await this.helper.waitForEta();
  420. await this.helper.execute();
  421. // This path clears _governanceCall as part of the afterExecute call,
  422. // but we have not way to check that the cleanup actually happened other
  423. // then coverage reports.
  424. });
  425. });
  426. }
  427. });