evictWorker.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import WorkingGroupsCommandBase from '../../base/WorkingGroupsCommandBase'
  2. import { apiModuleByGroup } from '../../Api'
  3. import { formatBalance } from '@polkadot/util'
  4. import chalk from 'chalk'
  5. import { flags } from '@oclif/command'
  6. import { isValidBalance } from '../../helpers/validation'
  7. import ExitCodes from '../../ExitCodes'
  8. import BN from 'bn.js'
  9. export default class WorkingGroupsEvictWorker extends WorkingGroupsCommandBase {
  10. static description = 'Evicts given worker. Requires lead access.'
  11. static args = [
  12. {
  13. name: 'workerId',
  14. required: true,
  15. description: 'Worker ID',
  16. },
  17. ]
  18. static flags = {
  19. ...WorkingGroupsCommandBase.flags,
  20. penalty: flags.string({
  21. description: 'Optional penalty in JOY',
  22. required: false,
  23. }),
  24. rationale: flags.string({
  25. description: 'Optional rationale',
  26. required: false,
  27. }),
  28. }
  29. async run() {
  30. const {
  31. args,
  32. flags: { penalty, rationale },
  33. } = this.parse(WorkingGroupsEvictWorker)
  34. const lead = await this.getRequiredLeadContext()
  35. const workerId = parseInt(args.workerId)
  36. // This will also make sure the worker is valid
  37. const groupMember = await this.getWorkerForLeadAction(workerId)
  38. if (penalty && !isValidBalance(penalty)) {
  39. this.error('Invalid penalty amount', { exit: ExitCodes.InvalidInput })
  40. }
  41. if (penalty && (!groupMember.stake || groupMember.stake.lt(new BN(penalty)))) {
  42. this.error('Penalty cannot exceed worker stake', { exit: ExitCodes.InvalidInput })
  43. }
  44. await this.sendAndFollowNamedTx(
  45. await this.getDecodedPair(lead.roleAccount.toString()),
  46. apiModuleByGroup[this.group],
  47. 'terminateRole',
  48. [workerId, penalty || null, rationale || null]
  49. )
  50. this.log(chalk.green(`Worker ${chalk.magentaBright(workerId.toString())} has been evicted!`))
  51. if (penalty) {
  52. this.log(chalk.green(`${chalk.magentaBright(formatBalance(penalty))} of worker's stake has been slashed!`))
  53. }
  54. }
  55. }