GovernorTimelockCompound.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 ] = 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. 'GovernorTimelock',
  41. ]);
  42. it('doesn\'t accept ether transfers', async function () {
  43. await expectRevert.unspecified(web3.eth.sendTransaction({ from: voter, to: this.mock.address, value: 1 }));
  44. });
  45. it('post deployment check', async function () {
  46. expect(await this.mock.name()).to.be.equal(name);
  47. expect(await this.mock.token()).to.be.equal(this.token.address);
  48. expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
  49. expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
  50. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  51. expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
  52. expect(await this.timelock.admin()).to.be.equal(this.mock.address);
  53. });
  54. describe('nominal', function () {
  55. beforeEach(async function () {
  56. this.settings = {
  57. proposal: [
  58. [ this.receiver.address ],
  59. [ web3.utils.toWei('0') ],
  60. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  61. '<proposal description>',
  62. ],
  63. voters: [
  64. { voter: voter, support: Enums.VoteType.For },
  65. ],
  66. steps: {
  67. queue: { delay: 7 * 86400 },
  68. },
  69. };
  70. });
  71. afterEach(async function () {
  72. expectEvent(
  73. this.receipts.propose,
  74. 'ProposalCreated',
  75. { proposalId: this.id },
  76. );
  77. expectEvent(
  78. this.receipts.queue,
  79. 'ProposalQueued',
  80. { proposalId: this.id },
  81. );
  82. await expectEvent.inTransaction(
  83. this.receipts.queue.transactionHash,
  84. this.timelock,
  85. 'QueueTransaction',
  86. { eta: this.eta },
  87. );
  88. expectEvent(
  89. this.receipts.execute,
  90. 'ProposalExecuted',
  91. { proposalId: this.id },
  92. );
  93. await expectEvent.inTransaction(
  94. this.receipts.execute.transactionHash,
  95. this.timelock,
  96. 'ExecuteTransaction',
  97. { eta: this.eta },
  98. );
  99. await expectEvent.inTransaction(
  100. this.receipts.execute.transactionHash,
  101. this.receiver,
  102. 'MockFunctionCalled',
  103. );
  104. });
  105. runGovernorWorkflow();
  106. });
  107. describe('not queued', function () {
  108. beforeEach(async function () {
  109. this.settings = {
  110. proposal: [
  111. [ this.receiver.address ],
  112. [ web3.utils.toWei('0') ],
  113. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  114. '<proposal description>',
  115. ],
  116. voters: [
  117. { voter: voter, support: Enums.VoteType.For },
  118. ],
  119. steps: {
  120. queue: { enable: false },
  121. execute: { error: 'GovernorTimelockCompound: proposal not yet queued' },
  122. },
  123. };
  124. });
  125. afterEach(async function () {
  126. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  127. });
  128. runGovernorWorkflow();
  129. });
  130. describe('to early', function () {
  131. beforeEach(async function () {
  132. this.settings = {
  133. proposal: [
  134. [ this.receiver.address ],
  135. [ web3.utils.toWei('0') ],
  136. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  137. '<proposal description>',
  138. ],
  139. voters: [
  140. { voter: voter, support: Enums.VoteType.For },
  141. ],
  142. steps: {
  143. execute: { error: 'Timelock::executeTransaction: Transaction hasn\'t surpassed time lock' },
  144. },
  145. };
  146. });
  147. afterEach(async function () {
  148. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  149. });
  150. runGovernorWorkflow();
  151. });
  152. describe('to late', function () {
  153. beforeEach(async function () {
  154. this.settings = {
  155. proposal: [
  156. [ this.receiver.address ],
  157. [ web3.utils.toWei('0') ],
  158. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  159. '<proposal description>',
  160. ],
  161. voters: [
  162. { voter: voter, support: Enums.VoteType.For },
  163. ],
  164. steps: {
  165. queue: { delay: 30 * 86400 },
  166. execute: { error: 'Governor: proposal not successful' },
  167. },
  168. };
  169. });
  170. afterEach(async function () {
  171. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Expired);
  172. });
  173. runGovernorWorkflow();
  174. });
  175. describe('deplicated underlying call', function () {
  176. beforeEach(async function () {
  177. this.settings = {
  178. proposal: [
  179. Array(2).fill(this.token.address),
  180. Array(2).fill(web3.utils.toWei('0')),
  181. Array(2).fill(this.token.contract.methods.approve(this.receiver.address, constants.MAX_UINT256).encodeABI()),
  182. '<proposal description>',
  183. ],
  184. voters: [
  185. { voter: voter, support: Enums.VoteType.For },
  186. ],
  187. steps: {
  188. queue: {
  189. error: 'GovernorTimelockCompound: identical proposal action already queued',
  190. },
  191. execute: {
  192. error: 'GovernorTimelockCompound: proposal not yet queued',
  193. },
  194. },
  195. };
  196. });
  197. runGovernorWorkflow();
  198. });
  199. describe('re-queue / re-execute', function () {
  200. beforeEach(async function () {
  201. this.settings = {
  202. proposal: [
  203. [ this.receiver.address ],
  204. [ web3.utils.toWei('0') ],
  205. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  206. '<proposal description>',
  207. ],
  208. voters: [
  209. { voter: voter, support: Enums.VoteType.For },
  210. ],
  211. steps: {
  212. queue: { delay: 7 * 86400 },
  213. },
  214. };
  215. });
  216. afterEach(async function () {
  217. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
  218. await expectRevert(
  219. this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  220. 'Governor: proposal not successful',
  221. );
  222. await expectRevert(
  223. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  224. 'Governor: proposal not successful',
  225. );
  226. });
  227. runGovernorWorkflow();
  228. });
  229. describe('cancel before queue prevents scheduling', function () {
  230. beforeEach(async function () {
  231. this.settings = {
  232. proposal: [
  233. [ this.receiver.address ],
  234. [ web3.utils.toWei('0') ],
  235. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  236. '<proposal description>',
  237. ],
  238. voters: [
  239. { voter: voter, support: Enums.VoteType.For },
  240. ],
  241. steps: {
  242. queue: { enable: false },
  243. execute: { enable: false },
  244. },
  245. };
  246. });
  247. afterEach(async function () {
  248. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  249. expectEvent(
  250. await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  251. 'ProposalCanceled',
  252. { proposalId: this.id },
  253. );
  254. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  255. await expectRevert(
  256. this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  257. 'Governor: proposal not successful',
  258. );
  259. });
  260. runGovernorWorkflow();
  261. });
  262. describe('cancel after queue prevents executing', function () {
  263. beforeEach(async function () {
  264. this.settings = {
  265. proposal: [
  266. [ this.receiver.address ],
  267. [ web3.utils.toWei('0') ],
  268. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  269. '<proposal description>',
  270. ],
  271. voters: [
  272. { voter: voter, support: Enums.VoteType.For },
  273. ],
  274. steps: {
  275. queue: { delay: 7 * 86400 },
  276. execute: { enable: false },
  277. },
  278. };
  279. });
  280. afterEach(async function () {
  281. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  282. const receipt = await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash);
  283. expectEvent(
  284. receipt,
  285. 'ProposalCanceled',
  286. { proposalId: this.id },
  287. );
  288. await expectEvent.inTransaction(
  289. receipt.receipt.transactionHash,
  290. this.timelock,
  291. 'CancelTransaction',
  292. );
  293. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  294. await expectRevert(
  295. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  296. 'Governor: proposal not successful',
  297. );
  298. });
  299. runGovernorWorkflow();
  300. });
  301. describe('updateTimelock', function () {
  302. beforeEach(async function () {
  303. this.newTimelock = await Timelock.new(this.mock.address, 7 * 86400);
  304. });
  305. it('protected', async function () {
  306. await expectRevert(
  307. this.mock.updateTimelock(this.newTimelock.address),
  308. 'Governor: onlyGovernance',
  309. );
  310. });
  311. describe('using workflow', function () {
  312. beforeEach(async function () {
  313. this.settings = {
  314. proposal: [
  315. [
  316. this.timelock.address,
  317. this.mock.address,
  318. ],
  319. [
  320. web3.utils.toWei('0'),
  321. web3.utils.toWei('0'),
  322. ],
  323. [
  324. this.timelock.contract.methods.setPendingAdmin(admin).encodeABI(),
  325. this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  326. ],
  327. '<proposal description>',
  328. ],
  329. voters: [
  330. { voter: voter, support: Enums.VoteType.For },
  331. ],
  332. steps: {
  333. queue: { delay: 7 * 86400 },
  334. },
  335. };
  336. });
  337. afterEach(async function () {
  338. expectEvent(
  339. this.receipts.propose,
  340. 'ProposalCreated',
  341. { proposalId: this.id },
  342. );
  343. expectEvent(
  344. this.receipts.execute,
  345. 'ProposalExecuted',
  346. { proposalId: this.id },
  347. );
  348. expectEvent(
  349. this.receipts.execute,
  350. 'TimelockChange',
  351. { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
  352. );
  353. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  354. });
  355. runGovernorWorkflow();
  356. });
  357. });
  358. describe('transfer timelock to new governor', function () {
  359. beforeEach(async function () {
  360. this.newGovernor = await Governor.new(name, this.token.address, 8, 32, this.timelock.address, 0);
  361. });
  362. describe('using workflow', function () {
  363. beforeEach(async function () {
  364. this.settings = {
  365. proposal: [
  366. [ this.timelock.address ],
  367. [ web3.utils.toWei('0') ],
  368. [ this.timelock.contract.methods.setPendingAdmin(this.newGovernor.address).encodeABI() ],
  369. '<proposal description>',
  370. ],
  371. voters: [
  372. { voter: voter, support: Enums.VoteType.For },
  373. ],
  374. steps: {
  375. queue: { delay: 7 * 86400 },
  376. },
  377. };
  378. });
  379. afterEach(async function () {
  380. expectEvent(
  381. this.receipts.propose,
  382. 'ProposalCreated',
  383. { proposalId: this.id },
  384. );
  385. expectEvent(
  386. this.receipts.execute,
  387. 'ProposalExecuted',
  388. { proposalId: this.id },
  389. );
  390. await expectEvent.inTransaction(
  391. this.receipts.execute.transactionHash,
  392. this.timelock,
  393. 'NewPendingAdmin',
  394. { newPendingAdmin: this.newGovernor.address },
  395. );
  396. await this.newGovernor.__acceptAdmin();
  397. expect(await this.timelock.admin()).to.be.bignumber.equal(this.newGovernor.address);
  398. });
  399. runGovernorWorkflow();
  400. });
  401. });
  402. });