decreaseWorkerStake.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(): Promise<void> {
  23. const { args } = this.parse(WorkingGroupsDecreaseWorkerStake)
  24. // Lead-only gate
  25. const lead = await this.getRequiredLeadContext()
  26. const workerId = parseInt(args.workerId)
  27. const groupMember = await this.getWorkerWithStakeForLeadAction(workerId)
  28. this.log(chalk.magentaBright('Current worker stake: ', formatBalance(groupMember.stake)))
  29. const balanceValidator = minMaxInt(1, groupMember.stake.toNumber())
  30. const balance = (await this.promptForParam(
  31. 'Balance',
  32. createParamOptions('amount', undefined, balanceValidator)
  33. )) as Balance
  34. await this.sendAndFollowNamedTx(
  35. await this.getDecodedPair(lead.roleAccount),
  36. apiModuleByGroup[this.group],
  37. 'decreaseStake',
  38. [workerId, balance]
  39. )
  40. this.log(
  41. chalk.green(
  42. `${chalk.magentaBright(formatBalance(balance))} from worker ${chalk.magentaBright(workerId)} stake ` +
  43. `has been returned to worker's role account (${chalk.magentaBright(groupMember.roleAccount.toString())})!`
  44. )
  45. )
  46. }
  47. }