GovernorWorkflow.behavior.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const { expectRevert, time } = require('@openzeppelin/test-helpers');
  2. async function getReceiptOrRevert (promise, error = undefined) {
  3. if (error) {
  4. await expectRevert(promise, error);
  5. return undefined;
  6. } else {
  7. const { receipt } = await promise;
  8. return receipt;
  9. }
  10. }
  11. function tryGet (obj, path = '') {
  12. try {
  13. return path.split('.').reduce((o, k) => o[k], obj);
  14. } catch (_) {
  15. return undefined;
  16. }
  17. }
  18. function runGovernorWorkflow () {
  19. beforeEach(async function () {
  20. this.receipts = {};
  21. this.descriptionHash = web3.utils.keccak256(this.settings.proposal.slice(-1).find(Boolean));
  22. this.id = await this.mock.hashProposal(...this.settings.proposal.slice(0, -1), this.descriptionHash);
  23. });
  24. it('run', async function () {
  25. // transfer tokens
  26. if (tryGet(this.settings, 'voters')) {
  27. for (const voter of this.settings.voters) {
  28. if (voter.weight) {
  29. await this.token.transfer(voter.voter, voter.weight, { from: this.settings.tokenHolder });
  30. }
  31. }
  32. }
  33. // propose
  34. if (this.mock.propose && tryGet(this.settings, 'steps.propose.enable') !== false) {
  35. this.receipts.propose = await getReceiptOrRevert(
  36. this.mock.methods['propose(address[],uint256[],bytes[],string)'](
  37. ...this.settings.proposal,
  38. { from: this.settings.proposer },
  39. ),
  40. tryGet(this.settings, 'steps.propose.error'),
  41. );
  42. if (tryGet(this.settings, 'steps.propose.error') === undefined) {
  43. this.deadline = await this.mock.proposalDeadline(this.id);
  44. this.snapshot = await this.mock.proposalSnapshot(this.id);
  45. }
  46. if (tryGet(this.settings, 'steps.propose.delay')) {
  47. await time.increase(tryGet(this.settings, 'steps.propose.delay'));
  48. }
  49. if (
  50. tryGet(this.settings, 'steps.propose.error') === undefined &&
  51. tryGet(this.settings, 'steps.propose.noadvance') !== true
  52. ) {
  53. await time.advanceBlockTo(this.snapshot);
  54. }
  55. }
  56. // vote
  57. if (tryGet(this.settings, 'voters')) {
  58. this.receipts.castVote = [];
  59. for (const voter of this.settings.voters) {
  60. if (!voter.signature) {
  61. this.receipts.castVote.push(
  62. await getReceiptOrRevert(
  63. voter.reason
  64. ? this.mock.castVoteWithReason(this.id, voter.support, voter.reason, { from: voter.voter })
  65. : this.mock.castVote(this.id, voter.support, { from: voter.voter }),
  66. voter.error,
  67. ),
  68. );
  69. } else {
  70. const { v, r, s } = await voter.signature({ proposalId: this.id, support: voter.support });
  71. this.receipts.castVote.push(
  72. await getReceiptOrRevert(
  73. this.mock.castVoteBySig(this.id, voter.support, v, r, s),
  74. voter.error,
  75. ),
  76. );
  77. }
  78. if (tryGet(voter, 'delay')) {
  79. await time.increase(tryGet(voter, 'delay'));
  80. }
  81. }
  82. }
  83. // fast forward
  84. if (tryGet(this.settings, 'steps.wait.enable') !== false) {
  85. await time.advanceBlockTo(this.deadline);
  86. }
  87. // queue
  88. if (this.mock.queue && tryGet(this.settings, 'steps.queue.enable') !== false) {
  89. this.receipts.queue = await getReceiptOrRevert(
  90. this.mock.methods['queue(address[],uint256[],bytes[],bytes32)'](
  91. ...this.settings.proposal.slice(0, -1),
  92. this.descriptionHash,
  93. { from: this.settings.queuer },
  94. ),
  95. tryGet(this.settings, 'steps.queue.error'),
  96. );
  97. this.eta = await this.mock.proposalEta(this.id);
  98. if (tryGet(this.settings, 'steps.queue.delay')) {
  99. await time.increase(tryGet(this.settings, 'steps.queue.delay'));
  100. }
  101. }
  102. // execute
  103. if (this.mock.execute && tryGet(this.settings, 'steps.execute.enable') !== false) {
  104. this.receipts.execute = await getReceiptOrRevert(
  105. this.mock.methods['execute(address[],uint256[],bytes[],bytes32)'](
  106. ...this.settings.proposal.slice(0, -1),
  107. this.descriptionHash,
  108. { from: this.settings.executer },
  109. ),
  110. tryGet(this.settings, 'steps.execute.error'),
  111. );
  112. if (tryGet(this.settings, 'steps.execute.delay')) {
  113. await time.increase(tryGet(this.settings, 'steps.execute.delay'));
  114. }
  115. }
  116. });
  117. }
  118. module.exports = {
  119. runGovernorWorkflow,
  120. };