info.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import AccountsCommandBase from '../../base/AccountsCommandBase'
  2. import ExitCodes from '../../ExitCodes'
  3. import { validateAddress } from '../../helpers/validation'
  4. import { NameValueObj } from '../../Types'
  5. import { displayHeader, displayNameValueTable } from '../../helpers/display'
  6. import { formatBalance } from '@polkadot/util'
  7. import moment from 'moment'
  8. export default class AccountInfo extends AccountsCommandBase {
  9. static description = 'Display detailed information about specified account'
  10. static aliases = ['account:inspect']
  11. static args = [
  12. { name: 'address', required: false, description: 'An address to inspect (can also be provided interavtively)' },
  13. ]
  14. async run(): Promise<void> {
  15. let { address } = this.parse(AccountInfo).args
  16. if (!address) {
  17. address = await this.promptForAnyAddress()
  18. } else if (validateAddress(address) !== true) {
  19. this.error('Invalid address', { exit: ExitCodes.InvalidInput })
  20. }
  21. const summary = await this.getApi().getAccountSummary(address)
  22. displayHeader('Account information')
  23. const accountRows: NameValueObj[] = [{ name: 'Address:', value: address }]
  24. if (this.isKeyAvailable(address)) {
  25. const pair = this.getPair(address)
  26. accountRows.push({ name: 'Account name', value: pair.meta.name })
  27. accountRows.push({ name: 'Type', value: pair.type })
  28. const creationDate = pair.meta.whenCreated
  29. ? moment(pair.meta.whenCreated as string | number).format('YYYY-MM-DD HH:mm:ss')
  30. : null
  31. if (creationDate) {
  32. accountRows.push({ name: 'Creation date', value: creationDate })
  33. }
  34. }
  35. displayNameValueTable(accountRows)
  36. displayHeader('Balances')
  37. const balances = summary.balances
  38. const balancesRows: NameValueObj[] = [
  39. { name: 'Total balance:', value: formatBalance(balances.votingBalance) },
  40. { name: 'Transferable balance:', value: formatBalance(balances.availableBalance) },
  41. ]
  42. if (balances.lockedBalance.gtn(0)) {
  43. balancesRows.push({ name: 'Locked balance:', value: formatBalance(balances.lockedBalance) })
  44. }
  45. if (balances.reservedBalance.gtn(0)) {
  46. balancesRows.push({ name: 'Reserved balance:', value: formatBalance(balances.reservedBalance) })
  47. }
  48. displayNameValueTable(balancesRows)
  49. }
  50. }