Skip to content

Commit

Permalink
cleaner lookup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Sep 25, 2023
1 parent 872547b commit f0c47f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
7 changes: 4 additions & 3 deletions ironfish-cli/src/commands/wallet/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { compareAssets, renderAssetNameFromHex } from '../../utils'

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

export class BalancesCommand extends IronfishCommand {
static description = `Display the account's balances for all assets`

Expand Down Expand Up @@ -45,7 +43,10 @@ export class BalancesCommand extends IronfishCommand {
})
this.log(`Account: ${response.content.account}`)

const assetBalancePairs: AssetBalancePairs[] = []
const assetBalancePairs: {
asset: RpcAsset
balance: GetBalancesResponse['balances'][number]
}[] = []

for (const balance of response.content.balances) {
const asset = await client.wallet.getAsset({
Expand Down
72 changes: 54 additions & 18 deletions ironfish-cli/src/commands/wallet/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CurrencyUtils,
GetAccountTransactionsResponse,
PartialRecursive,
RpcAsset,
RpcClient,
TransactionType,
} from '@ironfish/sdk'
Expand Down Expand Up @@ -84,9 +85,21 @@ export class TransactionsCommand extends IronfishCommand {
let hasTransactions = false

for await (const transaction of response.contentStream()) {
const transactionRows = flags.notes
? await this.getTransactionRowsByNote(client, transaction, format)
: await this.getTransactionRows(client, transaction, format)
let transactionRows: PartialRecursive<TransactionRow>[]

if (flags.notes) {
const assetLookup = await this.fetchAssetsFromTransaction(client, transaction, 'notes')

transactionRows = this.getTransactionRowsByNote(assetLookup, transaction, format)
} else {
const assetLookup = await this.fetchAssetsFromTransaction(
client,
transaction,
'assetBalanceDeltas',
)

transactionRows = this.getTransactionRows(assetLookup, transaction, format)
}

CliUx.ux.table(transactionRows, columns, {
printLine: this.log.bind(this),
Expand All @@ -103,11 +116,11 @@ export class TransactionsCommand extends IronfishCommand {
}
}

async getTransactionRows(
client: RpcClient,
getTransactionRows(
assetLookup: { [key: string]: RpcAsset },
transaction: GetAccountTransactionsResponse,
format: Format,
): Promise<PartialRecursive<TransactionRow>[]> {
): PartialRecursive<TransactionRow>[] {
const nativeAssetId = Asset.nativeId().toString('hex')

const assetBalanceDeltas = transaction.assetBalanceDeltas.sort((d) =>
Expand All @@ -121,9 +134,7 @@ export class TransactionsCommand extends IronfishCommand {
let assetCount = assetBalanceDeltas.length

for (const [index, { assetId, delta }] of assetBalanceDeltas.entries()) {
const asset = await client.wallet.getAsset({
id: assetId,
})
const asset = assetLookup[assetId]

let amount = BigInt(delta)

Expand All @@ -148,15 +159,15 @@ export class TransactionsCommand extends IronfishCommand {
...transaction,
group,
assetId,
assetName: asset.content.name,
assetName: asset.name,
amount,
feePaid,
})
} else {
transactionRows.push({
group,
assetId,
assetName: asset.content.name,
assetName: asset.name,
amount,
})
}
Expand All @@ -165,11 +176,40 @@ export class TransactionsCommand extends IronfishCommand {
return transactionRows
}

async getTransactionRowsByNote(
async fetchAssetsFromTransaction(
client: RpcClient,
transaction: GetAccountTransactionsResponse,
key: 'notes' | 'assetBalanceDeltas',
): Promise<{ [key: string]: RpcAsset }> {
const iterate = transaction[key]
Assert.isNotUndefined(iterate)

const assetIdSet = new Set<string>()

for (const { assetId } of iterate) {
assetIdSet.add(assetId)
}

const assetIds = Array.from(assetIdSet)

const assets: { [key: string]: RpcAsset } = {}

for (const assetId of assetIds) {
const asset = await client.wallet.getAsset({
id: assetId,
})

assets[assetId] = asset.content
}

return assets
}

getTransactionRowsByNote(
assetLookup: { [key: string]: RpcAsset },
transaction: GetAccountTransactionsResponse,
format: Format,
): Promise<PartialRecursive<TransactionRow>[]> {
): PartialRecursive<TransactionRow>[] {
Assert.isNotUndefined(transaction.notes)
const transactionRows = []

Expand All @@ -185,11 +225,7 @@ export class TransactionsCommand extends IronfishCommand {
const amount = BigInt(note.value)
const assetId = note.assetId

const asset = await client.wallet.getAsset({
id: assetId,
})

const assetName = asset.content.name
const assetName = assetLookup[note.assetId].name

const sender = note.sender
const recipient = note.owner
Expand Down

0 comments on commit f0c47f8

Please sign in to comment.