GovernorTimelockCompound.test.js 13 KB

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