Skip to content

Commit

Permalink
Render question mark for unknown assets (#4888)
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh authored Apr 9, 2024
1 parent d6856bc commit bc0a077
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
15 changes: 8 additions & 7 deletions ironfish-cli/src/commands/wallet/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { BufferUtils } from '@ironfish/sdk'
import { CliUx } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { renderAssetNameFromHex } from '../../utils'
import { renderAssetWithVerificationStatus } from '../../utils'
import { TableCols } from '../../utils/table'

const MAX_ASSET_METADATA_COLUMN_WIDTH = ASSET_METADATA_LENGTH + 1
Expand Down Expand Up @@ -64,12 +64,13 @@ export class AssetsCommand extends IronfishCommand {
header: 'Name',
width: assetNameWidth,
get: (row) =>
renderAssetNameFromHex(row.name, {
verification: row.verification,
outputType: flags.output,
verbose: !!flags.verbose,
logWarn: this.warn.bind(this),
}),
renderAssetWithVerificationStatus(
BufferUtils.toHuman(Buffer.from(row.name, 'hex')),
{
verification: row.verification,
outputType: flags.output,
},
),
}),
id: {
header: 'ID',
Expand Down
11 changes: 5 additions & 6 deletions ironfish-cli/src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CurrencyUtils, GetBalanceResponse, isNativeIdentifier } from '@ironfish
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { renderAssetName } from '../../utils'
import { renderAssetWithVerificationStatus } from '../../utils'

export class BalanceCommand extends IronfishCommand {
static description =
Expand Down Expand Up @@ -63,11 +63,10 @@ export class BalanceCommand extends IronfishCommand {
).content

const assetId = response.content.assetId
const assetName = renderAssetName(isNativeIdentifier(assetId) ? '$IRON' : assetId, {
verification: asset.verification,
verbose: !!flags.verbose,
logWarn: this.warn.bind(this),
})
const assetName = renderAssetWithVerificationStatus(
isNativeIdentifier(assetId) ? '$IRON' : assetId,
asset,
)

if (flags.explain) {
this.explainBalance(response.content, assetName)
Expand Down
17 changes: 9 additions & 8 deletions ironfish-cli/src/commands/wallet/balances.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { CurrencyUtils, GetBalancesResponse, RpcAsset } from '@ironfish/sdk'
import { BufferUtils, CurrencyUtils, GetBalancesResponse, RpcAsset } from '@ironfish/sdk'
import { CliUx, Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { compareAssets, renderAssetNameFromHex } from '../../utils'
import { compareAssets, renderAssetWithVerificationStatus } from '../../utils'

type AssetBalancePairs = { asset: RpcAsset; balance: GetBalancesResponse['balances'][number] }

Expand Down Expand Up @@ -63,12 +63,13 @@ export class BalancesCommand extends IronfishCommand {
assetName: {
header: 'Asset Name',
get: ({ asset }) =>
renderAssetNameFromHex(asset.name, {
verification: asset.verification,
outputType: flags.output,
verbose: !!flags.verbose,
logWarn: this.warn.bind(this),
}),
renderAssetWithVerificationStatus(
BufferUtils.toHuman(Buffer.from(asset.name, 'hex')),
{
verification: asset.verification,
outputType: flags.output,
},
),
},
'asset.id': {
header: 'Asset Id',
Expand Down
20 changes: 5 additions & 15 deletions ironfish-cli/src/utils/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import inquirer from 'inquirer'
type RenderAssetNameOptions = {
verification?: RpcAssetVerification
outputType?: string
verbose?: boolean
logWarn?: (msg: string) => void
}

export function renderAssetName(name: string, options?: RenderAssetNameOptions): string {
export function renderAssetWithVerificationStatus(
name: string,
options?: RenderAssetNameOptions,
): string {
if (options?.outputType) {
// User requested some machine-readable output (like CSV, JSON, or YAML).
// Do not alter the name in any way.
Expand All @@ -34,23 +35,12 @@ export function renderAssetName(name: string, options?: RenderAssetNameOptions):
case 'verified':
return chalk.green(name + '✓')
case 'unknown':
if (options?.verbose && options?.logWarn) {
options.logWarn(`Could not check whether ${name} is a verified asset`)
}
return name
return chalk.green(name + '?')
default:
return name
}
}

export function renderAssetNameFromHex(
hexName: string,
options?: RenderAssetNameOptions,
): string {
const name = BufferUtils.toHuman(Buffer.from(hexName, 'hex'))
return renderAssetName(name, options)
}

export function compareAssets(
leftName: string,
leftVerification: RpcAssetVerification,
Expand Down

0 comments on commit bc0a077

Please sign in to comment.