GovernorTimelockCompound.test.js 15 KB

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