GovernorTimelockCompound.test.js 14 KB

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