Skip to content

Commit

Permalink
Show nicer parsing errors in promptCurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
NullSoldier committed Feb 26, 2023
1 parent 815b64b commit f41c164
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ironfish-cli/src/commands/wallet/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class Burn extends IronfishCommand {
required: true,
text: 'Enter the amount of the custom asset to burn',
minimum: 1n,
logger: this.logger,
balance: {
account,
confirmations: flags.confirmations,
Expand Down Expand Up @@ -166,6 +167,7 @@ export class Burn extends IronfishCommand {
raw = await selectFee({
client,
transaction: params,
logger: this.logger,
})
} else {
const response = await client.createTransaction(params)
Expand Down
2 changes: 2 additions & 0 deletions ironfish-cli/src/commands/wallet/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export class Mint extends IronfishCommand {
required: true,
text: 'Enter the amount',
minimum: 1n,
logger: this.logger,
})
}

Expand All @@ -203,6 +204,7 @@ export class Mint extends IronfishCommand {
raw = await selectFee({
client,
transaction: params,
logger: this.logger,
})
} else {
const response = await client.createTransaction(params)
Expand Down
2 changes: 2 additions & 0 deletions ironfish-cli/src/commands/wallet/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class Send extends IronfishCommand {
required: true,
text: 'Enter the amount',
minimum: 1n,
logger: this.logger,
balance: {
account: from,
confirmations: flags.confirmations,
Expand Down Expand Up @@ -201,6 +202,7 @@ export class Send extends IronfishCommand {
raw = await selectFee({
client,
transaction: params,
logger: this.logger,
})
} else {
const response = await client.createTransaction(params)
Expand Down
14 changes: 12 additions & 2 deletions ironfish-cli/src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Asset } from '@ironfish/rust-nodejs'
import { CurrencyUtils, RpcClient } from '@ironfish/sdk'
import { Assert, CurrencyUtils, Logger, RpcClient } from '@ironfish/sdk'
import { CliUx } from '@oclif/core'

export async function promptCurrency(options: {
client: RpcClient
text: string
logger: Logger
required: true
minimum?: bigint
balance?: {
Expand All @@ -21,6 +22,7 @@ export async function promptCurrency(options: {
export async function promptCurrency(options: {
client: RpcClient
text: string
logger: Logger
required?: boolean
minimum?: bigint
balance?: {
Expand Down Expand Up @@ -50,9 +52,17 @@ export async function promptCurrency(options: {
return null
}

const amount = CurrencyUtils.decodeIron(input)
const [amount, error] = CurrencyUtils.decodeIronTry(input)

if (error) {
options.logger.error(`Error: ${error.reason}`)
continue
}

Assert.isNotNull(amount)

if (options.minimum != null && amount < options.minimum) {
options.logger.error(`Error: Minimum is ${CurrencyUtils.renderIron(options.minimum)}`)
continue
}

Expand Down
3 changes: 3 additions & 0 deletions ironfish-cli/src/utils/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CreateTransactionRequest,
CurrencyUtils,
ERROR_CODES,
Logger,
RawTransaction,
RawTransactionSerde,
RpcClient,
Expand All @@ -21,6 +22,7 @@ export async function selectFee(options: {
transaction: CreateTransactionRequest
account?: string
confirmations?: number
logger: Logger
}): Promise<RawTransaction> {
const feeRates = await options.client.estimateFeeRates()

Expand Down Expand Up @@ -68,6 +70,7 @@ export async function selectFee(options: {
client: options.client,
required: true,
text: 'Enter the fee amount in $IRON',
logger: options.logger,
balance: {
account: options.account,
confirmations: options.confirmations,
Expand Down
30 changes: 30 additions & 0 deletions ironfish/src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { formatFixed, parseFixed } from '@ethersproject/bignumber'
import { isNativeIdentifier } from './asset'
import { BigIntUtils } from './bigint'
import { ErrorUtils } from './error'
import { FixedNumberUtils } from './fixedNumber'

export class CurrencyUtils {
Expand All @@ -24,6 +25,21 @@ export class CurrencyUtils {
return parseFixed(amount.toString(), 8).toBigInt()
}

/**
* Parses iron into ore but returns the error if parsing fails
*/
static decodeIronTry(amount: string | number): [bigint, null] | [null, ParseFixedError] {
try {
const parsed = parseFixed(amount.toString(), 8).toBigInt()
return [parsed, null]
} catch (e) {
if (isParseFixedError(e)) {
return [null, e]
}
throw e
}
}

/**
* Deserialize ore back into bigint
*/
Expand Down Expand Up @@ -85,6 +101,20 @@ export class CurrencyUtils {
}
}

export interface ParseFixedError extends Error {
reason: string
code: 'INVALID_ARGUMENT' | 'NUMERIC_FAULT'
}

export function isParseFixedError(error: unknown): error is ParseFixedError {
return (
ErrorUtils.isNodeError(error) &&
(error['code'] === 'INVALID_ARGUMENT' || error['code'] === 'NUMERIC_FAULT') &&
'reason' in error &&
typeof error['reason'] === 'string'
)
}

export const ORE_TO_IRON = 100000000
export const MINIMUM_ORE_AMOUNT = 0n
export const MAXIMUM_ORE_AMOUNT = 2n ** 64n
Expand Down

0 comments on commit f41c164

Please sign in to comment.