Skip to content

Commit

Permalink
removing deprecated field usage in wallet transactions and asset utils
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Sep 22, 2023
1 parent f1347c1 commit 9200d18
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 25 deletions.
33 changes: 23 additions & 10 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,
RpcClient,
TransactionType,
} from '@ironfish/sdk'
import { CliUx, Flags } from '@oclif/core'
Expand Down Expand Up @@ -84,8 +85,8 @@ export class TransactionsCommand extends IronfishCommand {

for await (const transaction of response.contentStream()) {
const transactionRows = flags.notes
? this.getTransactionRowsByNote(transaction, format)
: this.getTransactionRows(transaction, format)
? await this.getTransactionRowsByNote(client, transaction, format)
: await this.getTransactionRows(client, transaction, format)

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

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

const assetBalanceDeltas = transaction.assetBalanceDeltas.sort((d) =>
Expand All @@ -118,7 +120,11 @@ export class TransactionsCommand extends IronfishCommand {

let assetCount = assetBalanceDeltas.length

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

let amount = BigInt(delta)

if (assetId === Asset.nativeId().toString('hex')) {
Expand All @@ -142,15 +148,15 @@ export class TransactionsCommand extends IronfishCommand {
...transaction,
group,
assetId,
assetName,
assetName: asset.content.name,
amount,
feePaid,
})
} else {
transactionRows.push({
group,
assetId,
assetName,
assetName: asset.content.name,
amount,
})
}
Expand All @@ -159,10 +165,11 @@ export class TransactionsCommand extends IronfishCommand {
return transactionRows
}

getTransactionRowsByNote(
async getTransactionRowsByNote(
client: RpcClient,
transaction: GetAccountTransactionsResponse,
format: Format,
): PartialRecursive<TransactionRow>[] {
): Promise<PartialRecursive<TransactionRow>[]> {
Assert.isNotUndefined(transaction.notes)
const transactionRows = []

Expand All @@ -177,7 +184,13 @@ export class TransactionsCommand extends IronfishCommand {
for (const [index, note] of notes.entries()) {
const amount = BigInt(note.value)
const assetId = note.assetId
const assetName = note.assetName

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

const assetName = asset.content.name

const sender = note.sender
const recipient = note.owner

Expand Down
54 changes: 39 additions & 15 deletions ironfish-cli/src/utils/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Asset } from '@ironfish/rust-nodejs'
import { BufferUtils, CurrencyUtils, RpcClient, StringUtils } from '@ironfish/sdk'
import { AssetVerification } from '@ironfish/sdk'
import {
AssetVerification,
BufferUtils,
CurrencyUtils,
RpcClient,
StringUtils,
} from '@ironfish/sdk'
import chalk from 'chalk'
import inquirer from 'inquirer'

Expand Down Expand Up @@ -102,7 +107,15 @@ export async function selectAsset(
account: account,
})

balances = balances.filter((b) => b.assetCreator === accountResponse.content.publicKey)
balances = await Promise.all(
balances.filter(async (b) => {
const asset = await client.wallet.getAsset({
id: b.assetId,
})

return asset.content.creator === accountResponse.content.publicKey
}),
)
}

if (balances.length === 0) {
Expand All @@ -111,25 +124,36 @@ export async function selectAsset(

if (balances.length === 1 && !options.showSingleAssetChoice) {
// If there's only one available asset, showing the choices is unnecessary

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

return {
id: balances[0].assetId,
name: balances[0].assetName,
name: asset.content.name,
}
}

const choices = balances.map((balance) => {
const assetName = BufferUtils.toHuman(Buffer.from(balance.assetName, 'hex'))
const name = `${balance.assetId} (${assetName}) (${CurrencyUtils.renderIron(
balance.available,
)})`
const choices = Promise.all(
balances.map(async (balance) => {
const asset = await client.wallet.getAsset({
id: balance.assetId,
})

const value = {
id: balance.assetId,
name: balance.assetName,
}
const assetName = BufferUtils.toHuman(Buffer.from(asset.content.name, 'hex'))
const name = `${balance.assetId} (${assetName}) (${CurrencyUtils.renderIron(
balance.available,
)})`

return { value, name }
})
const value = {
id: balance.assetId,
name: asset.content.name,
}

return { value, name }
}),
)

const response = await inquirer.prompt<{
asset: {
Expand Down

0 comments on commit 9200d18

Please sign in to comment.