updateRoleAccount.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. export default class WorkingGroupsUpdateRoleAccount extends WorkingGroupsCommandBase {
  7. static description = 'Updates the worker/lead role account. Requires member controller account to be selected'
  8. static args = [
  9. {
  10. name: 'accountAddress',
  11. required: false,
  12. description: 'New role 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(WorkingGroupsUpdateRoleAccount)
  20. const account = await this.getRequiredSelectedAccount()
  21. const worker = await this.getRequiredWorkerByMemberController()
  22. const cliAccounts = await this.fetchAccounts()
  23. let newRoleAccount: string = args.accountAddress
  24. if (!newRoleAccount) {
  25. newRoleAccount = (await this.promptForAccount(cliAccounts, undefined, 'Choose the new role account')).address
  26. }
  27. validateAddress(newRoleAccount)
  28. await this.requestAccountDecoding(account)
  29. await this.sendAndFollowExtrinsic(account, apiModuleByGroup[this.group], 'updateRoleAccount', [
  30. worker.workerId,
  31. new GenericAccountId(newRoleAccount),
  32. ])
  33. this.log(chalk.green(`Succesfully updated the role account to: ${chalk.white(newRoleAccount)})`))
  34. const matchingAccount = cliAccounts.find((account) => account.address === newRoleAccount)
  35. if (matchingAccount) {
  36. const switchAccount = await this.simplePrompt({
  37. type: 'confirm',
  38. message: 'Do you want to switch the currenly selected CLI account to the new role account?',
  39. default: false,
  40. })
  41. if (switchAccount) {
  42. await this.setSelectedAccount(matchingAccount)
  43. this.log(
  44. chalk.green('Account switched to: ') +
  45. chalk.white(`${matchingAccount.meta.name} (${matchingAccount.address})`)
  46. )
  47. }
  48. }
  49. }
  50. }