updateWorkerReward.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import WorkingGroupsCommandBase from '../../base/WorkingGroupsCommandBase'
  2. import { apiModuleByGroup } from '../../Api'
  3. import { formatBalance } from '@polkadot/util'
  4. import chalk from 'chalk'
  5. import { Reward } from '../../Types'
  6. import { positiveInt } from '../../validators/common'
  7. import { createParamOptions } from '../../helpers/promptOptions'
  8. import ExitCodes from '../../ExitCodes'
  9. import { BalanceOfMint } from '@joystream/types/mint'
  10. export default class WorkingGroupsUpdateWorkerReward extends WorkingGroupsCommandBase {
  11. static description = "Change given worker's reward (amount only). Requires lead access."
  12. static args = [
  13. {
  14. name: 'workerId',
  15. required: true,
  16. description: 'Worker ID',
  17. },
  18. ]
  19. static flags = {
  20. ...WorkingGroupsCommandBase.flags,
  21. }
  22. formatReward(reward?: Reward): string {
  23. return reward
  24. ? formatBalance(reward.value) +
  25. (reward.interval ? ` / ${reward.interval} block(s)` : '') +
  26. (reward.nextPaymentBlock ? ` (next payment: #${reward.nextPaymentBlock})` : '')
  27. : 'NONE'
  28. }
  29. async run(): Promise<void> {
  30. const { args } = this.parse(WorkingGroupsUpdateWorkerReward)
  31. const lead = await this.getRequiredLeadContext()
  32. const workerId = parseInt(args.workerId)
  33. // This will also make sure the worker is valid
  34. const groupMember = await this.getWorkerForLeadAction(workerId)
  35. const { reward } = groupMember
  36. if (!reward) {
  37. this.error('There is no reward relationship associated with this worker!', { exit: ExitCodes.InvalidInput })
  38. }
  39. console.log(chalk.magentaBright(`Current worker reward: ${this.formatReward(reward)}`))
  40. const newRewardValue = (await this.promptForParam(
  41. 'BalanceOfMint',
  42. createParamOptions('new_amount', undefined, positiveInt())
  43. )) as BalanceOfMint
  44. await this.sendAndFollowNamedTx(
  45. await this.getDecodedPair(lead.roleAccount),
  46. apiModuleByGroup[this.group],
  47. 'updateRewardAmount',
  48. [workerId, newRewardValue]
  49. )
  50. const updatedGroupMember = await this.getApi().groupMember(this.group, workerId)
  51. this.log(chalk.green(`Worker ${chalk.magentaBright(workerId)} reward has been updated!`))
  52. this.log(chalk.green(`New worker reward: ${chalk.magentaBright(this.formatReward(updatedGroupMember.reward))}`))
  53. }
  54. }