GovernorTimelockCompound.test.js 17 KB

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