GovernorTimelockCompound.test.js 18 KB

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