-
Notifications
You must be signed in to change notification settings - Fork 115
/
updateWorkerReward.ts
61 lines (50 loc) · 1.91 KB
/
updateWorkerReward.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
import WorkingGroupsCommandBase from '../../base/WorkingGroupsCommandBase'
import { apiModuleByGroup } from '../../Api'
import { formatBalance } from '@polkadot/util'
import chalk from 'chalk'
import { Reward } from '../../Types'
import ExitCodes from '../../ExitCodes'
import { isValidBalance } from '../../helpers/validation'
export default class WorkingGroupsUpdateWorkerReward extends WorkingGroupsCommandBase {
static description = "Change given worker's reward (amount only). Requires lead access."
static args = [
{
name: 'workerId',
required: true,
description: 'Worker ID',
},
{
name: 'newReward',
required: true,
description: 'New reward',
},
]
static flags = {
...WorkingGroupsCommandBase.flags,
}
formatReward(reward?: Reward): string {
return reward ? formatBalance(reward.valuePerBlock) + ' / block' : 'NONE'
}
async run(): Promise<void> {
const {
args: { workerId, newReward },
} = this.parse(WorkingGroupsUpdateWorkerReward)
if (!isValidBalance(newReward)) {
this.error('Invalid reward', { exit: ExitCodes.InvalidInput })
}
const lead = await this.getRequiredLeadContext()
// This will also make sure the worker is valid
const groupMember = await this.getWorkerForLeadAction(workerId)
const { reward } = groupMember
this.log(chalk.magentaBright(`Current worker reward: ${this.formatReward(reward)}`))
await this.sendAndFollowNamedTx(
await this.getDecodedPair(lead.roleAccount),
apiModuleByGroup[this.group],
'updateRewardAmount',
[workerId, newReward]
)
const updatedGroupMember = await this.getApi().groupMember(this.group, workerId)
this.log(chalk.green(`Worker ${chalk.magentaBright(workerId.toString())} reward has been updated!`))
this.log(chalk.green(`New worker reward: ${chalk.magentaBright(this.formatReward(updatedGroupMember.reward))}`))
}
}