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 { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  6. const { GovernorHelper, timelockSalt } = require('../../helpers/governance');
  7. const { OperationState, ProposalState, VoteType } = require('../../helpers/enums');
  8. const time = require('../../helpers/time');
  9. const TOKENS = [
  10. { Token: '$ERC20Votes', mode: 'blocknumber' },
  11. { Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
  12. ];
  13. const DEFAULT_ADMIN_ROLE = ethers.ZeroHash;
  14. const PROPOSER_ROLE = ethers.id('PROPOSER_ROLE');
  15. const EXECUTOR_ROLE = ethers.id('EXECUTOR_ROLE');
  16. const CANCELLER_ROLE = ethers.id('CANCELLER_ROLE');
  17. const name = 'OZ-Governor';
  18. const version = '1';
  19. const tokenName = 'MockToken';
  20. const tokenSymbol = 'MTKN';
  21. const tokenSupply = ethers.parseEther('100');
  22. const votingDelay = 4n;
  23. const votingPeriod = 16n;
  24. const value = ethers.parseEther('1');
  25. const delay = time.duration.hours(1n);
  26. describe('GovernorTimelockControl', function () {
  27. for (const { Token, mode } of TOKENS) {
  28. const fixture = async () => {
  29. const [deployer, owner, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
  30. const receiver = await ethers.deployContract('CallReceiverMock');
  31. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, tokenName, version]);
  32. const timelock = await ethers.deployContract('TimelockController', [delay, [], [], deployer]);
  33. const mock = await ethers.deployContract('$GovernorTimelockControlMock', [
  34. name,
  35. votingDelay,
  36. votingPeriod,
  37. 0n,
  38. timelock,
  39. token,
  40. 0n,
  41. ]);
  42. await owner.sendTransaction({ to: timelock, value });
  43. await token.$_mint(owner, tokenSupply);
  44. await timelock.grantRole(PROPOSER_ROLE, mock);
  45. await timelock.grantRole(PROPOSER_ROLE, owner);
  46. await timelock.grantRole(CANCELLER_ROLE, mock);
  47. await timelock.grantRole(CANCELLER_ROLE, owner);
  48. await timelock.grantRole(EXECUTOR_ROLE, ethers.ZeroAddress);
  49. await timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
  50. const helper = new GovernorHelper(mock, mode);
  51. await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') });
  52. await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') });
  53. await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') });
  54. await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') });
  55. return { deployer, owner, voter1, voter2, voter3, voter4, other, receiver, token, mock, timelock, helper };
  56. };
  57. describe(`using ${Token}`, function () {
  58. beforeEach(async function () {
  59. Object.assign(this, await loadFixture(fixture));
  60. // default proposal
  61. this.proposal = this.helper.setProposal(
  62. [
  63. {
  64. target: this.receiver.target,
  65. value,
  66. data: this.receiver.interface.encodeFunctionData('mockFunction'),
  67. },
  68. ],
  69. '<proposal description>',
  70. );
  71. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  72. ...this.proposal.shortProposal.slice(0, 3),
  73. ethers.ZeroHash,
  74. timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
  75. );
  76. });
  77. it("doesn't accept ether transfers", async function () {
  78. await expect(this.owner.sendTransaction({ to: this.mock, value: 1n })).to.be.revertedWithCustomError(
  79. this.mock,
  80. 'GovernorDisabledDeposit',
  81. );
  82. });
  83. it('post deployment check', async function () {
  84. expect(await this.mock.name()).to.equal(name);
  85. expect(await this.mock.token()).to.equal(this.token);
  86. expect(await this.mock.votingDelay()).to.equal(votingDelay);
  87. expect(await this.mock.votingPeriod()).to.equal(votingPeriod);
  88. expect(await this.mock.quorum(0n)).to.equal(0n);
  89. expect(await this.mock.timelock()).to.equal(this.timelock);
  90. });
  91. it('nominal', async function () {
  92. expect(await this.mock.proposalEta(this.proposal.id)).to.equal(0n);
  93. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
  94. await this.helper.propose();
  95. await this.helper.waitForSnapshot();
  96. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  97. await this.helper.connect(this.voter2).vote({ support: VoteType.For });
  98. await this.helper.connect(this.voter3).vote({ support: VoteType.Against });
  99. await this.helper.connect(this.voter4).vote({ support: VoteType.Abstain });
  100. await this.helper.waitForDeadline();
  101. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
  102. const txQueue = await this.helper.queue();
  103. const eta = (await time.clockFromReceipt.timestamp(txQueue)) + delay;
  104. expect(await this.mock.proposalEta(this.proposal.id)).to.equal(eta);
  105. await this.helper.waitForEta();
  106. const txExecute = this.helper.execute();
  107. await expect(txQueue)
  108. .to.emit(this.mock, 'ProposalQueued')
  109. .withArgs(this.proposal.id, anyValue)
  110. .to.emit(this.timelock, 'CallScheduled')
  111. .withArgs(this.proposal.timelockid, ...Array(6).fill(anyValue))
  112. .to.emit(this.timelock, 'CallSalt')
  113. .withArgs(this.proposal.timelockid, anyValue);
  114. await expect(txExecute)
  115. .to.emit(this.mock, 'ProposalExecuted')
  116. .withArgs(this.proposal.id)
  117. .to.emit(this.timelock, 'CallExecuted')
  118. .withArgs(this.proposal.timelockid, ...Array(4).fill(anyValue))
  119. .to.emit(this.receiver, 'MockFunctionCalled');
  120. });
  121. describe('should revert', function () {
  122. describe('on queue', function () {
  123. it('if already queued', async function () {
  124. await this.helper.propose();
  125. await this.helper.waitForSnapshot();
  126. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  127. await this.helper.waitForDeadline();
  128. await this.helper.queue();
  129. await expect(this.helper.queue())
  130. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  131. .withArgs(
  132. this.proposal.id,
  133. ProposalState.Queued,
  134. GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded]),
  135. );
  136. });
  137. });
  138. describe('on execute', function () {
  139. it('if not queued', async function () {
  140. await this.helper.propose();
  141. await this.helper.waitForSnapshot();
  142. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  143. await this.helper.waitForDeadline(1n);
  144. expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Succeeded);
  145. await expect(this.helper.execute())
  146. .to.be.revertedWithCustomError(this.timelock, 'TimelockUnexpectedOperationState')
  147. .withArgs(this.proposal.timelockid, GovernorHelper.proposalStatesToBitMap(OperationState.Ready));
  148. });
  149. it('if too early', async function () {
  150. await this.helper.propose();
  151. await this.helper.waitForSnapshot();
  152. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  153. await this.helper.waitForDeadline();
  154. await this.helper.queue();
  155. expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Queued);
  156. await expect(this.helper.execute())
  157. .to.be.revertedWithCustomError(this.timelock, 'TimelockUnexpectedOperationState')
  158. .withArgs(this.proposal.timelockid, GovernorHelper.proposalStatesToBitMap(OperationState.Ready));
  159. });
  160. it('if already executed', async function () {
  161. await this.helper.propose();
  162. await this.helper.waitForSnapshot();
  163. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  164. await this.helper.waitForDeadline();
  165. await this.helper.queue();
  166. await this.helper.waitForEta();
  167. await this.helper.execute();
  168. await expect(this.helper.execute())
  169. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  170. .withArgs(
  171. this.proposal.id,
  172. ProposalState.Executed,
  173. GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded, ProposalState.Queued]),
  174. );
  175. });
  176. it('if already executed by another proposer', async function () {
  177. await this.helper.propose();
  178. await this.helper.waitForSnapshot();
  179. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  180. await this.helper.waitForDeadline();
  181. await this.helper.queue();
  182. await this.helper.waitForEta();
  183. await this.timelock.executeBatch(
  184. ...this.proposal.shortProposal.slice(0, 3),
  185. ethers.ZeroHash,
  186. timelockSalt(this.mock.target, this.proposal.shortProposal[3]),
  187. );
  188. await expect(this.helper.execute())
  189. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  190. .withArgs(
  191. this.proposal.id,
  192. ProposalState.Executed,
  193. GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded, ProposalState.Queued]),
  194. );
  195. });
  196. });
  197. });
  198. describe('cancel', function () {
  199. it('cancel before queue prevents scheduling', async function () {
  200. await this.helper.propose();
  201. await this.helper.waitForSnapshot();
  202. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  203. await this.helper.waitForDeadline();
  204. await expect(this.helper.cancel('internal'))
  205. .to.emit(this.mock, 'ProposalCanceled')
  206. .withArgs(this.proposal.id);
  207. expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Canceled);
  208. await expect(this.helper.queue())
  209. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  210. .withArgs(
  211. this.proposal.id,
  212. ProposalState.Canceled,
  213. GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded]),
  214. );
  215. });
  216. it('cancel after queue prevents executing', async function () {
  217. await this.helper.propose();
  218. await this.helper.waitForSnapshot();
  219. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  220. await this.helper.waitForDeadline();
  221. await this.helper.queue();
  222. await expect(this.helper.cancel('internal'))
  223. .to.emit(this.mock, 'ProposalCanceled')
  224. .withArgs(this.proposal.id);
  225. expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Canceled);
  226. await expect(this.helper.execute())
  227. .to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
  228. .withArgs(
  229. this.proposal.id,
  230. ProposalState.Canceled,
  231. GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded, ProposalState.Queued]),
  232. );
  233. });
  234. it('cancel on timelock is reflected on governor', async function () {
  235. await this.helper.propose();
  236. await this.helper.waitForSnapshot();
  237. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  238. await this.helper.waitForDeadline();
  239. await this.helper.queue();
  240. expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Queued);
  241. await expect(this.timelock.connect(this.owner).cancel(this.proposal.timelockid))
  242. .to.emit(this.timelock, 'Cancelled')
  243. .withArgs(this.proposal.timelockid);
  244. expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Canceled);
  245. });
  246. });
  247. describe('onlyGovernance', function () {
  248. describe('relay', function () {
  249. beforeEach(async function () {
  250. await this.token.$_mint(this.mock, 1);
  251. });
  252. it('is protected', async function () {
  253. await expect(
  254. this.mock
  255. .connect(this.owner)
  256. .relay(this.token, 0n, this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n])),
  257. )
  258. .to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
  259. .withArgs(this.owner);
  260. });
  261. it('can be executed through governance', async function () {
  262. this.helper.setProposal(
  263. [
  264. {
  265. target: this.mock.target,
  266. data: this.mock.interface.encodeFunctionData('relay', [
  267. this.token.target,
  268. 0n,
  269. this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n]),
  270. ]),
  271. },
  272. ],
  273. '<proposal description>',
  274. );
  275. await this.helper.propose();
  276. await this.helper.waitForSnapshot();
  277. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  278. await this.helper.waitForDeadline();
  279. await this.helper.queue();
  280. await this.helper.waitForEta();
  281. const txExecute = await this.helper.execute();
  282. await expect(txExecute).to.changeTokenBalances(this.token, [this.mock, this.other], [-1n, 1n]);
  283. await expect(txExecute).to.emit(this.token, 'Transfer').withArgs(this.mock, this.other, 1n);
  284. });
  285. it('is payable and can transfer eth to EOA', async function () {
  286. const t2g = 128n; // timelock to governor
  287. const g2o = 100n; // governor to eoa (other)
  288. this.helper.setProposal(
  289. [
  290. {
  291. target: this.mock.target,
  292. value: t2g,
  293. data: this.mock.interface.encodeFunctionData('relay', [this.other.address, g2o, '0x']),
  294. },
  295. ],
  296. '<proposal description>',
  297. );
  298. await this.helper.propose();
  299. await this.helper.waitForSnapshot();
  300. await this.helper.connect(this.voter1).vote({ support: VoteType.For });
  301. await this.helper.waitForDeadline();
  302. await this.helper.queue();
  303. await this.helper.waitForEta();
  304. await expect(this.helper.execute()).to.changeEtherBalances(
  305. [this.timelock, this.mock, this.other],
  306. [-t2g, t2g - g2o, g2o],
  307. );
  308. });
  309. it('protected against other proposers', async function () {
  310. const call = [
  311. this.mock,
  312. 0n,
  313. this.mock.interface.encodeFunctionData('relay', [ethers.ZeroAddress, 0n, '0x']),
  314. ethers.ZeroHash,
  315. ethers.ZeroHash,
  316. ];
  317. await this.timelock.connect(this.owner).schedule(...call, delay);
  318. await time.increaseBy.timestamp(delay);
  319. // Error bubbled up from Governor
  320. await expect(this.timelock.connect(this.owner).execute(...call)).to.be.revertedWithPanic(
  321. PANIC_CODES.POP_ON_EMPTY_ARRAY,
  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);
  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: 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, this.newTimelock);
  358. expect(await this.mock.timelock()).to.equal(this.newTimelock);
  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: 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. });