manageWorkerAsLead.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Api, WorkingGroups } from '../../Api'
  2. import { FlowArgs } from '../../Scenario'
  3. import {
  4. ApplyForOpeningFixture,
  5. AddWorkerOpeningFixture,
  6. BeginApplicationReviewFixture,
  7. FillOpeningFixture,
  8. DecreaseStakeFixture,
  9. SlashFixture,
  10. TerminateRoleFixture,
  11. } from '../../fixtures/workingGroupModule'
  12. import { BuyMembershipHappyCaseFixture } from '../../fixtures/membershipModule'
  13. import BN from 'bn.js'
  14. import { OpeningId } from '@joystream/types/hiring'
  15. import { assert } from 'chai'
  16. import Debugger from 'debug'
  17. import { FixtureRunner } from '../../Fixture'
  18. export default {
  19. storage: async function ({ api, env }: FlowArgs): Promise<void> {
  20. return manageWorkerAsLead(api, env, WorkingGroups.StorageWorkingGroup)
  21. },
  22. content: async function ({ api, env }: FlowArgs): Promise<void> {
  23. return manageWorkerAsLead(api, env, WorkingGroups.ContentDirectoryWorkingGroup)
  24. },
  25. }
  26. async function manageWorkerAsLead(api: Api, env: NodeJS.ProcessEnv, group: WorkingGroups): Promise<void> {
  27. const debug = Debugger(`manageWorkerAsLead:${group}`)
  28. debug('Started')
  29. const applicationStake: BN = new BN(env.WORKING_GROUP_APPLICATION_STAKE!)
  30. const roleStake: BN = new BN(env.WORKING_GROUP_ROLE_STAKE!)
  31. const firstRewardInterval: BN = new BN(env.LONG_REWARD_INTERVAL!)
  32. const rewardInterval: BN = new BN(env.LONG_REWARD_INTERVAL!)
  33. const payoutAmount: BN = new BN(env.PAYOUT_AMOUNT!)
  34. const unstakingPeriod: BN = new BN(env.STORAGE_WORKING_GROUP_UNSTAKING_PERIOD!)
  35. const openingActivationDelay: BN = new BN(0)
  36. const paidTerms = api.createPaidTermId(new BN(+env.MEMBERSHIP_PAID_TERMS!))
  37. const lead = await api.getGroupLead(group)
  38. assert(lead)
  39. const applicants = api.createKeyPairs(5).map((key) => key.address)
  40. const memberSetFixture = new BuyMembershipHappyCaseFixture(api, applicants, paidTerms)
  41. await new FixtureRunner(memberSetFixture).run()
  42. const addWorkerOpeningFixture: AddWorkerOpeningFixture = new AddWorkerOpeningFixture(
  43. api,
  44. applicationStake,
  45. roleStake,
  46. openingActivationDelay,
  47. unstakingPeriod,
  48. group
  49. )
  50. // Add worker opening
  51. await new FixtureRunner(addWorkerOpeningFixture).run()
  52. assert(addWorkerOpeningFixture.getCreatedOpeningId())
  53. // First apply for worker opening
  54. const applyForWorkerOpeningFixture = new ApplyForOpeningFixture(
  55. api,
  56. applicants,
  57. applicationStake,
  58. roleStake,
  59. addWorkerOpeningFixture.getCreatedOpeningId() as OpeningId,
  60. group
  61. )
  62. await new FixtureRunner(applyForWorkerOpeningFixture).run()
  63. const applicationIds = applyForWorkerOpeningFixture.getApplicationIds()
  64. assert.equal(applicants.length, applicationIds.length)
  65. const applicationIdsToHire = applicationIds.slice(0, 2)
  66. // Begin application review
  67. const beginApplicationReviewFixture = new BeginApplicationReviewFixture(
  68. api,
  69. addWorkerOpeningFixture.getCreatedOpeningId() as OpeningId,
  70. group
  71. )
  72. await new FixtureRunner(beginApplicationReviewFixture).run()
  73. // Fill worker opening
  74. const fillOpeningFixture = new FillOpeningFixture(
  75. api,
  76. applicationIdsToHire,
  77. addWorkerOpeningFixture.getCreatedOpeningId() as OpeningId,
  78. firstRewardInterval,
  79. rewardInterval,
  80. payoutAmount,
  81. group
  82. )
  83. await new FixtureRunner(fillOpeningFixture).run()
  84. const firstWorkerId = fillOpeningFixture.getWorkerIds()[0]
  85. const decreaseStakeFixture = new DecreaseStakeFixture(api, firstWorkerId, group)
  86. // Decrease worker stake
  87. await new FixtureRunner(decreaseStakeFixture).run()
  88. const slashFixture: SlashFixture = new SlashFixture(api, firstWorkerId, group)
  89. // Slash worker
  90. await new FixtureRunner(slashFixture).run()
  91. const terminateRoleFixture = new TerminateRoleFixture(api, firstWorkerId, group)
  92. // Terminate workers
  93. await new FixtureRunner(terminateRoleFixture).run()
  94. debug('Done')
  95. }