evictWorker.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import WorkingGroupsCommandBase from '../../base/WorkingGroupsCommandBase'
  2. import { apiModuleByGroup } from '../../Api'
  3. import { formatBalance } from '@polkadot/util'
  4. import chalk from 'chalk'
  5. import { createParamOptions } from '../../helpers/promptOptions'
  6. export default class WorkingGroupsEvictWorker extends WorkingGroupsCommandBase {
  7. static description = 'Evicts given worker. Requires lead access.'
  8. static args = [
  9. {
  10. name: 'workerId',
  11. required: true,
  12. description: 'Worker ID',
  13. },
  14. ]
  15. static flags = {
  16. ...WorkingGroupsCommandBase.flags,
  17. }
  18. async run() {
  19. const { args } = this.parse(WorkingGroupsEvictWorker)
  20. const account = await this.getRequiredSelectedAccount()
  21. // Lead-only gate
  22. await this.getRequiredLead()
  23. const workerId = parseInt(args.workerId)
  24. // This will also make sure the worker is valid
  25. const groupMember = await this.getWorkerForLeadAction(workerId)
  26. // TODO: Terminate worker text limits? (minMaxStr)
  27. const rationale = await this.promptForParam('Bytes', createParamOptions('rationale'))
  28. const shouldSlash = groupMember.stake
  29. ? await this.simplePrompt({
  30. message: `Should the worker stake (${formatBalance(groupMember.stake)}) be slashed?`,
  31. type: 'confirm',
  32. default: false,
  33. })
  34. : false
  35. await this.requestAccountDecoding(account)
  36. await this.sendAndFollowNamedTx(account, apiModuleByGroup[this.group], 'terminateRole', [
  37. workerId,
  38. rationale,
  39. shouldSlash,
  40. ])
  41. this.log(chalk.green(`Worker ${chalk.white(workerId)} has been evicted!`))
  42. if (shouldSlash) {
  43. this.log(chalk.green(`Worker stake totalling ${chalk.white(formatBalance(groupMember.stake))} has been slashed!`))
  44. }
  45. }
  46. }