GovernorTimelockCompound.test.js 13 KB

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