decreaseWorkerStake.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import WorkingGroupsCommandBase from '../../base/WorkingGroupsCommandBase'
  2. import { apiModuleByGroup } from '../../Api'
  3. import { WorkerId } from '@joystream/types/working-group'
  4. import { Balance } from '@polkadot/types/interfaces'
  5. import { formatBalance } from '@polkadot/util'
  6. import { minMaxInt } from '../../validators/common'
  7. import chalk from 'chalk'
  8. import { createParamOptions } from '../../helpers/promptOptions'
  9. export default class WorkingGroupsDecreaseWorkerStake extends WorkingGroupsCommandBase {
  10. static description =
  11. 'Decreases given worker stake by an amount that will be returned to the worker role account. ' +
  12. 'Requires lead access.'
  13. static args = [
  14. {
  15. name: 'workerId',
  16. required: true,
  17. description: 'Worker ID',
  18. },
  19. ]
  20. static flags = {
  21. ...WorkingGroupsCommandBase.flags,
  22. }
  23. async run() {
  24. const { args } = this.parse(WorkingGroupsDecreaseWorkerStake)
  25. const account = await this.getRequiredSelectedAccount()
  26. // Lead-only gate
  27. await this.getRequiredLead()
  28. const workerId = parseInt(args.workerId)
  29. const groupMember = await this.getWorkerWithStakeForLeadAction(workerId)
  30. this.log(chalk.white('Current worker stake: ', formatBalance(groupMember.stake)))
  31. const balanceValidator = minMaxInt(1, groupMember.stake.toNumber())
  32. const balance = (await this.promptForParam(
  33. 'Balance',
  34. createParamOptions('amount', undefined, balanceValidator)
  35. )) as Balance
  36. await this.requestAccountDecoding(account)
  37. await this.sendAndFollowExtrinsic(account, apiModuleByGroup[this.group], 'decreaseStake', [
  38. new WorkerId(workerId),
  39. balance,
  40. ])
  41. this.log(
  42. chalk.green(
  43. `${chalk.white(formatBalance(balance))} from worker ${chalk.white(workerId)} stake ` +
  44. `has been returned to worker's role account (${chalk.white(groupMember.roleAccount.toString())})!`
  45. )
  46. )
  47. }
  48. }