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('ERC20VotesMock');
  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);
  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. this.token.address,
  36. votingDelay,
  37. votingPeriod,
  38. this.timelock.address,
  39. 0,
  40. );
  41. this.receiver = await CallReceiver.new();
  42. this.helper = new GovernorHelper(this.mock);
  43. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  44. await this.token.mint(owner, tokenSupply);
  45. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  46. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  47. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  48. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  49. // default proposal
  50. this.proposal = this.helper.setProposal([
  51. {
  52. target: this.receiver.address,
  53. value,
  54. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  55. },
  56. ], '<proposal description>');
  57. });
  58. shouldSupportInterfaces([
  59. 'ERC165',
  60. 'Governor',
  61. 'GovernorWithParams',
  62. 'GovernorTimelock',
  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 expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  103. });
  104. it('if proposal contains duplicate calls', async function () {
  105. const action = {
  106. target: this.token.address,
  107. data: this.token.contract.methods.approve(this.receiver.address, constants.MAX_UINT256).encodeABI(),
  108. };
  109. this.helper.setProposal([ action, action ], '<proposal description>');
  110. await this.helper.propose();
  111. await this.helper.waitForSnapshot();
  112. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  113. await this.helper.waitForDeadline();
  114. await expectRevert(
  115. this.helper.queue(),
  116. 'GovernorTimelockCompound: identical proposal action already queued',
  117. );
  118. await expectRevert(
  119. this.helper.execute(),
  120. 'GovernorTimelockCompound: proposal not yet queued',
  121. );
  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 expectRevert(
  132. this.helper.execute(),
  133. 'GovernorTimelockCompound: proposal not yet queued',
  134. );
  135. });
  136. it('if too early', async function () {
  137. await this.helper.propose();
  138. await this.helper.waitForSnapshot();
  139. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  140. await this.helper.waitForDeadline();
  141. await this.helper.queue();
  142. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  143. await expectRevert(
  144. this.helper.execute(),
  145. 'Timelock::executeTransaction: Transaction hasn\'t surpassed time lock',
  146. );
  147. });
  148. it('if too late', async function () {
  149. await this.helper.propose();
  150. await this.helper.waitForSnapshot();
  151. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  152. await this.helper.waitForDeadline();
  153. await this.helper.queue();
  154. await this.helper.waitForEta(+30 * 86400);
  155. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Expired);
  156. await expectRevert(
  157. this.helper.execute(),
  158. 'Governor: proposal not successful',
  159. );
  160. });
  161. it('if already executed', async function () {
  162. await this.helper.propose();
  163. await this.helper.waitForSnapshot();
  164. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  165. await this.helper.waitForDeadline();
  166. await this.helper.queue();
  167. await this.helper.waitForEta();
  168. await this.helper.execute();
  169. await expectRevert(
  170. this.helper.execute(),
  171. 'Governor: proposal not successful',
  172. );
  173. });
  174. });
  175. });
  176. describe('cancel', function () {
  177. it('cancel before queue prevents scheduling', async function () {
  178. await this.helper.propose();
  179. await this.helper.waitForSnapshot();
  180. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  181. await this.helper.waitForDeadline();
  182. expectEvent(
  183. await this.helper.cancel(),
  184. 'ProposalCanceled',
  185. { proposalId: this.proposal.id },
  186. );
  187. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  188. await expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  189. });
  190. it('cancel after queue prevents executing', async function () {
  191. await this.helper.propose();
  192. await this.helper.waitForSnapshot();
  193. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  194. await this.helper.waitForDeadline();
  195. await this.helper.queue();
  196. expectEvent(
  197. await this.helper.cancel(),
  198. 'ProposalCanceled',
  199. { proposalId: this.proposal.id },
  200. );
  201. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  202. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  203. });
  204. });
  205. describe('onlyGovernance', function () {
  206. describe('relay', function () {
  207. beforeEach(async function () {
  208. await this.token.mint(this.mock.address, 1);
  209. });
  210. it('is protected', async function () {
  211. await expectRevert(
  212. this.mock.relay(
  213. this.token.address,
  214. 0,
  215. this.token.contract.methods.transfer(other, 1).encodeABI(),
  216. ),
  217. 'Governor: onlyGovernance',
  218. );
  219. });
  220. it('can be executed through governance', async function () {
  221. this.helper.setProposal([
  222. {
  223. target: this.mock.address,
  224. data: this.mock.contract.methods.relay(
  225. this.token.address,
  226. 0,
  227. this.token.contract.methods.transfer(other, 1).encodeABI(),
  228. ).encodeABI(),
  229. },
  230. ], '<proposal description>');
  231. expect(await this.token.balanceOf(this.mock.address), 1);
  232. expect(await this.token.balanceOf(other), 0);
  233. await this.helper.propose();
  234. await this.helper.waitForSnapshot();
  235. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  236. await this.helper.waitForDeadline();
  237. await this.helper.queue();
  238. await this.helper.waitForEta();
  239. const txExecute = await this.helper.execute();
  240. expect(await this.token.balanceOf(this.mock.address), 0);
  241. expect(await this.token.balanceOf(other), 1);
  242. expectEvent.inTransaction(
  243. txExecute.tx,
  244. this.token,
  245. 'Transfer',
  246. { from: this.mock.address, to: other, value: '1' },
  247. );
  248. });
  249. });
  250. describe('updateTimelock', function () {
  251. beforeEach(async function () {
  252. this.newTimelock = await Timelock.new(this.mock.address, 7 * 86400);
  253. });
  254. it('is protected', async function () {
  255. await expectRevert(
  256. this.mock.updateTimelock(this.newTimelock.address),
  257. 'Governor: onlyGovernance',
  258. );
  259. });
  260. it('can be executed through governance to', async function () {
  261. this.helper.setProposal([
  262. {
  263. target: this.timelock.address,
  264. data: this.timelock.contract.methods.setPendingAdmin(owner).encodeABI(),
  265. },
  266. {
  267. target: this.mock.address,
  268. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  269. },
  270. ], '<proposal description>');
  271. await this.helper.propose();
  272. await this.helper.waitForSnapshot();
  273. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  274. await this.helper.waitForDeadline();
  275. await this.helper.queue();
  276. await this.helper.waitForEta();
  277. const txExecute = await this.helper.execute();
  278. expectEvent(
  279. txExecute,
  280. 'TimelockChange',
  281. { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
  282. );
  283. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  284. });
  285. });
  286. it('can transfer timelock to new governor', async function () {
  287. const newGovernor = await Governor.new(name, this.token.address, 8, 32, this.timelock.address, 0);
  288. this.helper.setProposal([
  289. {
  290. target: this.timelock.address,
  291. data: this.timelock.contract.methods.setPendingAdmin(newGovernor.address).encodeABI(),
  292. },
  293. ], '<proposal description>');
  294. await this.helper.propose();
  295. await this.helper.waitForSnapshot();
  296. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  297. await this.helper.waitForDeadline();
  298. await this.helper.queue();
  299. await this.helper.waitForEta();
  300. const txExecute = await this.helper.execute();
  301. await expectEvent.inTransaction(
  302. txExecute.tx,
  303. this.timelock,
  304. 'NewPendingAdmin',
  305. { newPendingAdmin: newGovernor.address },
  306. );
  307. await newGovernor.__acceptAdmin();
  308. expect(await this.timelock.admin()).to.be.bignumber.equal(newGovernor.address);
  309. });
  310. });
  311. });