decreaseWorkerStake.ts 1.8 KB

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