validation.ts 678 B

12345678910111213141516171819
  1. import BN from 'bn.js'
  2. import ExitCodes from '../ExitCodes'
  3. import { decodeAddress } from '@polkadot/util-crypto'
  4. import { DerivedBalances } from '@polkadot/api-derive/types'
  5. import { CLIError } from '@oclif/errors'
  6. export function validateAddress(address: string, errorMessage = 'Invalid address'): void {
  7. try {
  8. decodeAddress(address)
  9. } catch (e) {
  10. throw new CLIError(errorMessage, { exit: ExitCodes.InvalidInput })
  11. }
  12. }
  13. export function checkBalance(accBalances: DerivedBalances, requiredBalance: BN): void {
  14. if (requiredBalance.gt(accBalances.availableBalance)) {
  15. throw new CLIError('Not enough balance available', { exit: ExitCodes.InvalidInput })
  16. }
  17. }