-
Notifications
You must be signed in to change notification settings - Fork 115
/
slashWorker.ts
65 lines (56 loc) · 1.83 KB
/
slashWorker.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
65
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 WorkingGroupsSlashWorker extends WorkingGroupsCommandBase {
static description = 'Slashes given worker stake. Requires lead access.'
static args = [
{
name: 'workerId',
required: true,
description: 'Worker ID',
},
{
name: 'amount',
required: true,
description: 'Slash amount',
},
]
static flags = {
...WorkingGroupsCommandBase.flags,
rationale: flags.string({
name: 'Optional rationale',
required: false,
}),
}
async run(): Promise<void> {
const {
args: { amount, workerId },
flags: { rationale },
} = this.parse(WorkingGroupsSlashWorker)
// Lead-only gate
const lead = await this.getRequiredLeadContext()
const groupMember = await this.getWorkerWithStakeForLeadAction(parseInt(workerId))
this.log(chalk.magentaBright('Current worker stake: ', formatBalance(groupMember.stake)))
if (!isValidBalance(amount) || groupMember.stake.lt(new BN(amount))) {
this.error('Invalid slash amount', { exit: ExitCodes.InvalidInput })
}
await this.sendAndFollowNamedTx(
await this.getDecodedPair(lead.roleAccount.toString()),
apiModuleByGroup[this.group],
'slashStake',
[workerId, amount, rationale || null]
)
this.log(
chalk.green(
`${chalk.magentaBright(formatBalance(amount))} from worker ${chalk.magentaBright(
workerId.toString()
)} stake has been successfully slashed!`
)
)
}
}