GovernorTimelockCompound.test.js 13 KB

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