GovernorTimelockCompound.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const RLP = require('rlp');
  5. const {
  6. runGovernorWorkflow,
  7. } = require('../GovernorWorkflow.behavior');
  8. const {
  9. shouldSupportInterfaces,
  10. } = require('../../utils/introspection/SupportsInterface.behavior');
  11. const Token = artifacts.require('ERC20VotesMock');
  12. const Timelock = artifacts.require('CompTimelock');
  13. const Governor = artifacts.require('GovernorTimelockCompoundMock');
  14. const CallReceiver = artifacts.require('CallReceiverMock');
  15. function makeContractAddress (creator, nonce) {
  16. return web3.utils.toChecksumAddress(web3.utils.sha3(RLP.encode([creator, nonce])).slice(12).substring(14));
  17. }
  18. contract('GovernorTimelockCompound', function (accounts) {
  19. const [ admin, voter, other ] = accounts;
  20. const name = 'OZ-Governor';
  21. // const version = '1';
  22. const tokenName = 'MockToken';
  23. const tokenSymbol = 'MTKN';
  24. const tokenSupply = web3.utils.toWei('100');
  25. beforeEach(async function () {
  26. const [ deployer ] = await web3.eth.getAccounts();
  27. this.token = await Token.new(tokenName, tokenSymbol);
  28. // Need to predict governance address to set it as timelock admin with a delayed transfer
  29. const nonce = await web3.eth.getTransactionCount(deployer);
  30. const predictGovernor = makeContractAddress(deployer, nonce + 1);
  31. this.timelock = await Timelock.new(predictGovernor, 2 * 86400);
  32. this.mock = await Governor.new(name, this.token.address, 4, 16, this.timelock.address, 0);
  33. this.receiver = await CallReceiver.new();
  34. await this.token.mint(voter, tokenSupply);
  35. await this.token.delegate(voter, { from: voter });
  36. });
  37. shouldSupportInterfaces([
  38. 'ERC165',
  39. 'Governor',
  40. 'GovernorWithParams',
  41. 'GovernorTimelock',
  42. ]);
  43. it('doesn\'t accept ether transfers', async function () {
  44. await expectRevert.unspecified(web3.eth.sendTransaction({ from: voter, to: this.mock.address, value: 1 }));
  45. });
  46. it('post deployment check', async function () {
  47. expect(await this.mock.name()).to.be.equal(name);
  48. expect(await this.mock.token()).to.be.equal(this.token.address);
  49. expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
  50. expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
  51. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  52. expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
  53. expect(await this.timelock.admin()).to.be.equal(this.mock.address);
  54. });
  55. describe('nominal', function () {
  56. beforeEach(async function () {
  57. this.settings = {
  58. proposal: [
  59. [ this.receiver.address ],
  60. [ web3.utils.toWei('0') ],
  61. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  62. '<proposal description>',
  63. ],
  64. voters: [
  65. { voter: voter, support: Enums.VoteType.For },
  66. ],
  67. steps: {
  68. queue: { delay: 7 * 86400 },
  69. },
  70. };
  71. });
  72. afterEach(async function () {
  73. expectEvent(
  74. this.receipts.propose,
  75. 'ProposalCreated',
  76. { proposalId: this.id },
  77. );
  78. expectEvent(
  79. this.receipts.queue,
  80. 'ProposalQueued',
  81. { proposalId: this.id },
  82. );
  83. await expectEvent.inTransaction(
  84. this.receipts.queue.transactionHash,
  85. this.timelock,
  86. 'QueueTransaction',
  87. { eta: this.eta },
  88. );
  89. expectEvent(
  90. this.receipts.execute,
  91. 'ProposalExecuted',
  92. { proposalId: this.id },
  93. );
  94. await expectEvent.inTransaction(
  95. this.receipts.execute.transactionHash,
  96. this.timelock,
  97. 'ExecuteTransaction',
  98. { eta: this.eta },
  99. );
  100. await expectEvent.inTransaction(
  101. this.receipts.execute.transactionHash,
  102. this.receiver,
  103. 'MockFunctionCalled',
  104. );
  105. });
  106. runGovernorWorkflow();
  107. });
  108. describe('not queued', function () {
  109. beforeEach(async function () {
  110. this.settings = {
  111. proposal: [
  112. [ this.receiver.address ],
  113. [ web3.utils.toWei('0') ],
  114. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  115. '<proposal description>',
  116. ],
  117. voters: [
  118. { voter: voter, support: Enums.VoteType.For },
  119. ],
  120. steps: {
  121. queue: { enable: false },
  122. execute: { error: 'GovernorTimelockCompound: proposal not yet queued' },
  123. },
  124. };
  125. });
  126. afterEach(async function () {
  127. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  128. });
  129. runGovernorWorkflow();
  130. });
  131. describe('to early', function () {
  132. beforeEach(async function () {
  133. this.settings = {
  134. proposal: [
  135. [ this.receiver.address ],
  136. [ web3.utils.toWei('0') ],
  137. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  138. '<proposal description>',
  139. ],
  140. voters: [
  141. { voter: voter, support: Enums.VoteType.For },
  142. ],
  143. steps: {
  144. execute: { error: 'Timelock::executeTransaction: Transaction hasn\'t surpassed time lock' },
  145. },
  146. };
  147. });
  148. afterEach(async function () {
  149. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  150. });
  151. runGovernorWorkflow();
  152. });
  153. describe('to late', function () {
  154. beforeEach(async function () {
  155. this.settings = {
  156. proposal: [
  157. [ this.receiver.address ],
  158. [ web3.utils.toWei('0') ],
  159. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  160. '<proposal description>',
  161. ],
  162. voters: [
  163. { voter: voter, support: Enums.VoteType.For },
  164. ],
  165. steps: {
  166. queue: { delay: 30 * 86400 },
  167. execute: { error: 'Governor: proposal not successful' },
  168. },
  169. };
  170. });
  171. afterEach(async function () {
  172. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Expired);
  173. });
  174. runGovernorWorkflow();
  175. });
  176. describe('deplicated underlying call', function () {
  177. beforeEach(async function () {
  178. this.settings = {
  179. proposal: [
  180. Array(2).fill(this.token.address),
  181. Array(2).fill(web3.utils.toWei('0')),
  182. Array(2).fill(this.token.contract.methods.approve(this.receiver.address, constants.MAX_UINT256).encodeABI()),
  183. '<proposal description>',
  184. ],
  185. voters: [
  186. { voter: voter, support: Enums.VoteType.For },
  187. ],
  188. steps: {
  189. queue: {
  190. error: 'GovernorTimelockCompound: identical proposal action already queued',
  191. },
  192. execute: {
  193. error: 'GovernorTimelockCompound: proposal not yet queued',
  194. },
  195. },
  196. };
  197. });
  198. runGovernorWorkflow();
  199. });
  200. describe('re-queue / re-execute', function () {
  201. beforeEach(async function () {
  202. this.settings = {
  203. proposal: [
  204. [ this.receiver.address ],
  205. [ web3.utils.toWei('0') ],
  206. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  207. '<proposal description>',
  208. ],
  209. voters: [
  210. { voter: voter, support: Enums.VoteType.For },
  211. ],
  212. steps: {
  213. queue: { delay: 7 * 86400 },
  214. },
  215. };
  216. });
  217. afterEach(async function () {
  218. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
  219. await expectRevert(
  220. this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  221. 'Governor: proposal not successful',
  222. );
  223. await expectRevert(
  224. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  225. 'Governor: proposal not successful',
  226. );
  227. });
  228. runGovernorWorkflow();
  229. });
  230. describe('cancel before queue prevents scheduling', function () {
  231. beforeEach(async function () {
  232. this.settings = {
  233. proposal: [
  234. [ this.receiver.address ],
  235. [ web3.utils.toWei('0') ],
  236. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  237. '<proposal description>',
  238. ],
  239. voters: [
  240. { voter: voter, support: Enums.VoteType.For },
  241. ],
  242. steps: {
  243. queue: { enable: false },
  244. execute: { enable: false },
  245. },
  246. };
  247. });
  248. afterEach(async function () {
  249. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  250. expectEvent(
  251. await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  252. 'ProposalCanceled',
  253. { proposalId: this.id },
  254. );
  255. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  256. await expectRevert(
  257. this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  258. 'Governor: proposal not successful',
  259. );
  260. });
  261. runGovernorWorkflow();
  262. });
  263. describe('cancel after queue prevents executing', function () {
  264. beforeEach(async function () {
  265. this.settings = {
  266. proposal: [
  267. [ this.receiver.address ],
  268. [ web3.utils.toWei('0') ],
  269. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  270. '<proposal description>',
  271. ],
  272. voters: [
  273. { voter: voter, support: Enums.VoteType.For },
  274. ],
  275. steps: {
  276. queue: { delay: 7 * 86400 },
  277. execute: { enable: false },
  278. },
  279. };
  280. });
  281. afterEach(async function () {
  282. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  283. const receipt = await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash);
  284. expectEvent(
  285. receipt,
  286. 'ProposalCanceled',
  287. { proposalId: this.id },
  288. );
  289. await expectEvent.inTransaction(
  290. receipt.receipt.transactionHash,
  291. this.timelock,
  292. 'CancelTransaction',
  293. );
  294. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  295. await expectRevert(
  296. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  297. 'Governor: proposal not successful',
  298. );
  299. });
  300. runGovernorWorkflow();
  301. });
  302. describe('relay', function () {
  303. beforeEach(async function () {
  304. await this.token.mint(this.mock.address, 1);
  305. this.call = [
  306. this.token.address,
  307. 0,
  308. this.token.contract.methods.transfer(other, 1).encodeABI(),
  309. ];
  310. });
  311. it('protected', async function () {
  312. await expectRevert(
  313. this.mock.relay(...this.call),
  314. 'Governor: onlyGovernance',
  315. );
  316. });
  317. describe('using workflow', function () {
  318. beforeEach(async function () {
  319. this.settings = {
  320. proposal: [
  321. [
  322. this.mock.address,
  323. ],
  324. [
  325. web3.utils.toWei('0'),
  326. ],
  327. [
  328. this.mock.contract.methods.relay(...this.call).encodeABI(),
  329. ],
  330. '<proposal description>',
  331. ],
  332. voters: [
  333. { voter: voter, support: Enums.VoteType.For },
  334. ],
  335. steps: {
  336. queue: { delay: 7 * 86400 },
  337. },
  338. };
  339. expect(await this.token.balanceOf(this.mock.address), 1);
  340. expect(await this.token.balanceOf(other), 0);
  341. });
  342. afterEach(async function () {
  343. expect(await this.token.balanceOf(this.mock.address), 0);
  344. expect(await this.token.balanceOf(other), 1);
  345. });
  346. runGovernorWorkflow();
  347. });
  348. });
  349. describe('updateTimelock', function () {
  350. beforeEach(async function () {
  351. this.newTimelock = await Timelock.new(this.mock.address, 7 * 86400);
  352. });
  353. it('protected', async function () {
  354. await expectRevert(
  355. this.mock.updateTimelock(this.newTimelock.address),
  356. 'Governor: onlyGovernance',
  357. );
  358. });
  359. describe('using workflow', function () {
  360. beforeEach(async function () {
  361. this.settings = {
  362. proposal: [
  363. [
  364. this.timelock.address,
  365. this.mock.address,
  366. ],
  367. [
  368. web3.utils.toWei('0'),
  369. web3.utils.toWei('0'),
  370. ],
  371. [
  372. this.timelock.contract.methods.setPendingAdmin(admin).encodeABI(),
  373. this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  374. ],
  375. '<proposal description>',
  376. ],
  377. voters: [
  378. { voter: voter, support: Enums.VoteType.For },
  379. ],
  380. steps: {
  381. queue: { delay: 7 * 86400 },
  382. },
  383. };
  384. });
  385. afterEach(async function () {
  386. expectEvent(
  387. this.receipts.propose,
  388. 'ProposalCreated',
  389. { proposalId: this.id },
  390. );
  391. expectEvent(
  392. this.receipts.execute,
  393. 'ProposalExecuted',
  394. { proposalId: this.id },
  395. );
  396. expectEvent(
  397. this.receipts.execute,
  398. 'TimelockChange',
  399. { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
  400. );
  401. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  402. });
  403. runGovernorWorkflow();
  404. });
  405. });
  406. describe('transfer timelock to new governor', function () {
  407. beforeEach(async function () {
  408. this.newGovernor = await Governor.new(name, this.token.address, 8, 32, this.timelock.address, 0);
  409. });
  410. describe('using workflow', function () {
  411. beforeEach(async function () {
  412. this.settings = {
  413. proposal: [
  414. [ this.timelock.address ],
  415. [ web3.utils.toWei('0') ],
  416. [ this.timelock.contract.methods.setPendingAdmin(this.newGovernor.address).encodeABI() ],
  417. '<proposal description>',
  418. ],
  419. voters: [
  420. { voter: voter, support: Enums.VoteType.For },
  421. ],
  422. steps: {
  423. queue: { delay: 7 * 86400 },
  424. },
  425. };
  426. });
  427. afterEach(async function () {
  428. expectEvent(
  429. this.receipts.propose,
  430. 'ProposalCreated',
  431. { proposalId: this.id },
  432. );
  433. expectEvent(
  434. this.receipts.execute,
  435. 'ProposalExecuted',
  436. { proposalId: this.id },
  437. );
  438. await expectEvent.inTransaction(
  439. this.receipts.execute.transactionHash,
  440. this.timelock,
  441. 'NewPendingAdmin',
  442. { newPendingAdmin: this.newGovernor.address },
  443. );
  444. await this.newGovernor.__acceptAdmin();
  445. expect(await this.timelock.admin()).to.be.bignumber.equal(this.newGovernor.address);
  446. });
  447. runGovernorWorkflow();
  448. });
  449. });
  450. });