Skip to content

Commit

Permalink
standardizes currency output information for currency prompting, for …
Browse files Browse the repository at this point in the history
…verified and unverified (#5594)
  • Loading branch information
jowparks authored Oct 29, 2024
1 parent 569de29 commit 78d5ad0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
5 changes: 2 additions & 3 deletions ironfish-cli/src/commands/wallet/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,10 @@ This will destroy tokens and decrease supply for a given asset.`
amount = await promptCurrency({
client: client,
required: true,
text: 'Enter the amount of the custom asset to burn',
text: 'Enter the amount to burn',
minimum: 1n,
logger: this.logger,
assetId: assetId,
assetVerification: assetData.verification,
assetData,
balance: {
account,
confirmations: flags.confirmations,
Expand Down
5 changes: 2 additions & 3 deletions ironfish-cli/src/commands/wallet/chainport/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,10 @@ export class BridgeCommand extends IronfishCommand {
amount = await promptCurrency({
client: client,
required: true,
text: 'Enter the amount in the major denomination',
text: 'Enter the amount',
minimum: 1n,
logger: this.logger,
assetId: assetId,
assetVerification: assetData.verification,
assetData,
balance: {
account: from,
},
Expand Down
5 changes: 2 additions & 3 deletions ironfish-cli/src/commands/wallet/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ This will create tokens and increase supply for a given asset.`
amount = await promptCurrency({
client: client,
required: true,
text: 'Enter the amount',
text: 'Enter the amount to mint',
minimum: 0n,
logger: this.logger,
assetId: assetId,
assetVerification: assetData?.verification,
assetData,
})
}

Expand Down
5 changes: 2 additions & 3 deletions ironfish-cli/src/commands/wallet/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ export class Send extends IronfishCommand {
amount = await promptCurrency({
client: client,
required: true,
text: 'Enter the amount in the major denomination',
text: 'Enter the amount',
minimum: 1n,
logger: this.logger,
assetId: assetId,
assetVerification: assetData.verification,
assetData,
balance: {
account: from,
confirmations: flags.confirmations,
Expand Down
46 changes: 17 additions & 29 deletions ironfish-cli/src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,46 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Asset } from '@ironfish/rust-nodejs'
import { Assert, CurrencyUtils, Logger, RpcAssetVerification, RpcClient } from '@ironfish/sdk'
import { Assert, CurrencyUtils, Logger, RpcAsset, RpcClient } from '@ironfish/sdk'
import { inputPrompt } from '../ui'
import { renderAssetWithVerificationStatus } from './asset'

/**
* This prompts the user to enter an amount of currency in the major
* denomination and returns the value in the minor denomination
*/
export async function promptCurrency(options: {
client: Pick<RpcClient, 'wallet'>
text: string
logger: Logger
required: true
minimum?: bigint
assetId?: string
assetVerification?: RpcAssetVerification
balance?: {
account?: string
confirmations?: number
}
}): Promise<bigint>

export async function promptCurrency(options: {
client: Pick<RpcClient, 'wallet'>
text: string
logger: Logger
required?: boolean
minimum?: bigint
assetId?: string
assetVerification?: RpcAssetVerification
assetData?: RpcAsset
balance?: {
account?: string
confirmations?: number
}
}): Promise<bigint | null> {
}): Promise<bigint> {
let text = options.text
if (options.assetData) {
const assetName = Buffer.from(options.assetData.name, 'hex').toString('utf-8')
text += ` in ${renderAssetWithVerificationStatus(assetName, {
verification: options.assetData.verification,
})}`
}

if (options.balance) {
const balance = await options.client.wallet.getAccountBalance({
account: options.balance.account,
assetId: options.assetId ?? Asset.nativeId().toString('hex'),
assetId: options.assetData?.id ?? Asset.nativeId().toString('hex'),
confirmations: options.balance.confirmations,
})

const renderedAvailable = CurrencyUtils.render(
balance.content.available,
false,
options.assetId,
options.assetVerification,
options.assetData?.id,
options.assetData?.verification,
)
text += ` (balance ${renderedAvailable})`
}
Expand All @@ -59,14 +51,10 @@ export async function promptCurrency(options: {
while (true) {
const input = await inputPrompt(text, options.required)

if (!input) {
return null
}

const [amount, error] = CurrencyUtils.tryMajorToMinor(
input,
options.assetId,
options.assetVerification,
options.assetData?.id,
options.assetData?.verification,
)

if (error) {
Expand All @@ -80,8 +68,8 @@ export async function promptCurrency(options: {
const renderedMinimum = CurrencyUtils.render(
options.minimum,
false,
options.assetId,
options.assetVerification,
options.assetData?.id,
options.assetData?.verification,
)
options.logger.error(`Error: Minimum is ${renderedMinimum}`)
continue
Expand Down
2 changes: 1 addition & 1 deletion ironfish-cli/src/utils/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function selectFee(options: {
const fee = await promptCurrency({
client: options.client,
required: true,
text: 'Enter the fee amount in $IRON',
text: 'Enter the fee in $IRON',
logger: options.logger,
balance: {
account: options.account,
Expand Down

0 comments on commit 78d5ad0

Please sign in to comment.