updateRewardAccount.ts 1.7 KB

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