updateWorkerReward.ts 2.3 KB

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