GovernorTimelockCompound.test.js 18 KB

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