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