updateRewardAccount.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import WorkingGroupsCommandBase from '../../base/WorkingGroupsCommandBase'
  2. import { apiModuleByGroup } from '../../Api'
  3. import { validateAddress } from '../../helpers/validation'
  4. import chalk from 'chalk'
  5. import ExitCodes from '../../ExitCodes'
  6. export default class WorkingGroupsUpdateRewardAccount extends WorkingGroupsCommandBase {
  7. static description = 'Updates the worker/lead reward account (requires current role account to be selected)'
  8. static args = [
  9. {
  10. name: 'accountAddress',
  11. required: false,
  12. description: 'New reward account address (if omitted, one of the existing CLI accounts can be selected)',
  13. },
  14. ]
  15. static flags = {
  16. ...WorkingGroupsCommandBase.flags,
  17. }
  18. async run() {
  19. const { args } = this.parse(WorkingGroupsUpdateRewardAccount)
  20. const account = await this.getRequiredSelectedAccount()
  21. // Worker-only gate
  22. const worker = await this.getRequiredWorker()
  23. if (!worker.reward) {
  24. this.error('There is no reward relationship associated with this role!', { exit: ExitCodes.InvalidInput })
  25. }
  26. let newRewardAccount: string = args.accountAddress
  27. if (!newRewardAccount) {
  28. const accounts = await this.fetchAccounts()
  29. newRewardAccount = (await this.promptForAccount(accounts, undefined, 'Choose the new reward account')).address
  30. }
  31. validateAddress(newRewardAccount)
  32. await this.requestAccountDecoding(account)
  33. await this.sendAndFollowExtrinsic(account, apiModuleByGroup[this.group], 'updateRewardAccount', [
  34. worker.workerId,
  35. newRewardAccount,
  36. ])
  37. this.log(chalk.green(`Succesfully updated the reward account to: ${chalk.white(newRewardAccount)})`))
  38. }
  39. }