Skip to content

Commit

Permalink
Merge pull request #3382 from iron-fish/staging
Browse files Browse the repository at this point in the history
STAGING -> MASTER (2023-02-10)
  • Loading branch information
rohanjadvani authored Feb 10, 2023
2 parents 12abe5b + d7e74d1 commit bba0453
Show file tree
Hide file tree
Showing 29 changed files with 702 additions and 302 deletions.
4 changes: 2 additions & 2 deletions ironfish-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ironfish",
"version": "0.1.66",
"version": "0.1.67",
"description": "CLI for running and interacting with an Iron Fish node",
"author": "Iron Fish <[email protected]> (https://ironfish.network)",
"main": "build/src/index.js",
Expand Down Expand Up @@ -60,7 +60,7 @@
"@aws-sdk/client-cognito-identity": "3.215.0",
"@aws-sdk/client-s3": "3.127.0",
"@ironfish/rust-nodejs": "0.1.26",
"@ironfish/sdk": "0.0.43",
"@ironfish/sdk": "0.0.44",
"@oclif/core": "1.23.1",
"@oclif/plugin-help": "5.1.12",
"@oclif/plugin-not-found": "2.3.1",
Expand Down
22 changes: 4 additions & 18 deletions ironfish-cli/src/commands/wallet/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { CliUx, Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'

// TODO(rohanjadvani): Remove this after assets are added to the wallet
type Balance = GetBalancesResponse['balances'][number] & {
name: string
}

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

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

let columns: CliUx.Table.table.Columns<Balance> = {
name: {
let columns: CliUx.Table.table.Columns<GetBalancesResponse['balances'][number]> = {
assetName: {
header: 'Asset Name',
get: (row) => BufferUtils.toHuman(Buffer.from(row.assetName, 'hex')),
},
assetId: {
header: 'Asset Id',
Expand Down Expand Up @@ -82,16 +78,6 @@ export class BalancesCommand extends IronfishCommand {
}
}

const balancesWithNames = []
// TODO(rohanjadvani) We currently fetch the asset from the blockchain to
// populate the name when rendering balance. This can be refactored once
// the wallet persists assets.
for (const balance of response.content.balances) {
const assetResponse = await client.getAsset({ id: balance.assetId })
const name = BufferUtils.toHuman(Buffer.from(assetResponse.content.name, 'hex'))
balancesWithNames.push({ ...balance, name })
}

CliUx.ux.table(balancesWithNames, columns)
CliUx.ux.table(response.content.balances, columns)
}
}
4 changes: 3 additions & 1 deletion ironfish-cli/src/commands/wallet/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ export class Burn extends IronfishCommand {
let assetId = flags.assetId

if (assetId == null) {
assetId = await selectAsset(client, account, {
const asset = await selectAsset(client, account, {
action: 'burn',
showNativeAsset: false,
showSingleAssetChoice: true,
confirmations,
})

assetId = asset?.id
}

if (assetId == null) {
Expand Down
6 changes: 4 additions & 2 deletions ironfish-cli/src/commands/wallet/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ export class Mint extends IronfishCommand {
})
}
} else if (!assetId) {
assetId = await selectAsset(client, account, {
const asset = await selectAsset(client, account, {
action: 'mint',
showNativeAsset: false,
showSingleAssetChoice: true,
confirmations: confirmations,
})

if (!assetId) {
if (!asset) {
this.error(`You must have an existing asset. Try creating a new one.`)
}

assetId = asset.id
}

let amount
Expand Down
4 changes: 3 additions & 1 deletion ironfish-cli/src/commands/wallet/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ export class Send extends IronfishCommand {
}

if (assetId == null) {
assetId = await selectAsset(client, from, {
const asset = await selectAsset(client, from, {
action: 'send',
showNativeAsset: true,
showSingleAssetChoice: false,
confirmations: confirmations,
})

assetId = asset?.id
}

if (!assetId) {
Expand Down
57 changes: 36 additions & 21 deletions ironfish-cli/src/utils/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,63 @@ export async function selectAsset(
showSingleAssetChoice: boolean
confirmations?: number
},
): Promise<string | undefined> {
): Promise<
| {
id: string
name: string
}
| undefined
> {
const balancesResponse = await client.getAccountBalances({
account: account,
confirmations: options.confirmations,
})
const assetOptions = []

let balances = balancesResponse.content.balances

if (!options.showNativeAsset) {
balances = balances.filter(
(balance) => balance.assetId !== Asset.nativeId().toString('hex'),
)
balances = balances.filter((b) => b.assetId !== Asset.nativeId().toString('hex'))
}

if (balances.length === 0) {
return undefined
} else if (balances.length === 1 && !options.showSingleAssetChoice) {
}

if (balances.length === 1 && !options.showSingleAssetChoice) {
// If there's only one available asset, showing the choices is unnecessary
return balances[0].assetId
return {
id: balances[0].assetId,
name: balances[0].assetName,
}
}

// Get the asset name from the chain DB to populate the display choices
for (const { assetId, confirmed } of balances) {
const assetResponse = await client.getAsset({ id: assetId })
const choices = balances.map((balance) => {
const assetName = BufferUtils.toHuman(Buffer.from(balance.assetName, 'hex'))
const name = `${balance.assetId} (${assetName}) (${CurrencyUtils.renderIron(
balance.confirmed,
)})`

if (assetResponse.content.name) {
const displayName = BufferUtils.toHuman(Buffer.from(assetResponse.content.name, 'hex'))
assetOptions.push({
value: assetId,
name: `${assetId} (${displayName}) (${CurrencyUtils.renderIron(confirmed)})`,
})
const value = {
id: balance.assetId,
name: balance.assetName,
}
}

const response: { assetId: string } = await inquirer.prompt<{ assetId: string }>([
return { value, name }
})

const response = await inquirer.prompt<{
asset: {
id: string
name: string
}
}>([
{
name: 'assetId',
name: 'asset',
message: `Select the asset you wish to ${options.action}`,
type: 'list',
choices: assetOptions,
choices,
},
])
return response.assetId

return response.asset
}
2 changes: 1 addition & 1 deletion ironfish/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/sdk",
"version": "0.0.43",
"version": "0.0.44",
"description": "SDK for running and interacting with an Iron Fish node",
"author": "Iron Fish <[email protected]> (https://ironfish.network)",
"main": "build/src/index.js",
Expand Down
12 changes: 12 additions & 0 deletions ironfish/src/genesis/__fixtures__/genesis.test.slow.ts.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Create genesis block Can generate a valid genesis block": [
{
"id": "1cefb303-7565-40a7-a1db-962186fa8170",
"name": "test",
"spendingKey": "6e6df28f097908c071657bc98c04ad74254069b1cb14005ad62474cdd8bc6f48",
"incomingViewKey": "3d94de8f3864e0c48e52069e93e20d856bbbe3b6787f7411500c847d006c4e00",
"outgoingViewKey": "81e80cc79ea8c2c866177eeabeb1d8c894279cbcfa5c700e6040b2d25bc43cd3",
"publicAddress": "babb76a29b30928c61d7984479ace53bf4739a02628c82f792f2cd9b6deca957"
}
]
}
4 changes: 2 additions & 2 deletions ironfish/src/genesis/genesis.test.slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Asset, generateKey } from '@ironfish/rust-nodejs'
import { BlockSerde, SerializedBlock } from '../primitives/block'
import { Target } from '../primitives/target'
import { IJSON } from '../serde'
import { createNodeTest } from '../testUtilities'
import { createNodeTest, useAccountFixture } from '../testUtilities'
import { acceptsAllTarget } from '../testUtilities/helpers/blockchain'
import { GenesisBlockInfo, makeGenesisBlock } from './makeGenesisBlock'

Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Create genesis block', () => {
const amountBigint = BigInt(amountNumber)

// Construct parameters for the genesis block
const account = await node.wallet.createAccount('test', true)
const account = await useAccountFixture(node.wallet, 'test')
const info: GenesisBlockInfo = {
timestamp: Date.now(),
target: Target.maxTarget(),
Expand Down
4 changes: 4 additions & 0 deletions ironfish/src/migrations/data/018-backfill-wallet-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class Migration018 extends Migration {
tx: IDatabaseTransaction | undefined,
logger: Logger,
): Promise<void> {
// Ensure there are no corrupted records for users who might have failed
// running this migration
await node.wallet.walletDb.assets.clear()

const accounts = []
for await (const accountValue of node.wallet.walletDb.loadAccounts(tx)) {
accounts.push(
Expand Down
65 changes: 65 additions & 0 deletions ironfish/src/migrations/data/020-backfill-null-asset-supplies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* 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 { Logger } from '../../logger'
import { IronfishNode } from '../../node'
import { IDatabase, IDatabaseTransaction } from '../../storage'
import { BufferUtils } from '../../utils'
import { Account } from '../../wallet'
import { Migration } from '../migration'

export class Migration020 extends Migration {
path = __filename

prepare(node: IronfishNode): IDatabase {
return node.wallet.walletDb.db
}

async forward(
node: IronfishNode,
_db: IDatabase,
tx: IDatabaseTransaction | undefined,
logger: Logger,
): Promise<void> {
const accounts = []
for await (const accountValue of node.wallet.walletDb.loadAccounts(tx)) {
accounts.push(
new Account({
...accountValue,
walletDb: node.wallet.walletDb,
}),
)
}

logger.info(`Backfilling assets for ${accounts.length} accounts`)

for (const account of accounts) {
logger.info('')

let assetCount = 0
logger.info(` Clearing assets for account ${account.name}`)
for await (const asset of account.getAssets(tx)) {
if (asset.owner.toString('hex') !== account.publicAddress) {
continue
}

logger.info(` Re-syncing asset ${BufferUtils.toHuman(asset.name)}`)
await node.wallet.walletDb.deleteAsset(account, asset.id, tx)
assetCount++
}

for await (const transactionValue of account.getTransactionsOrderedBySequence(tx)) {
await account.saveMintsToAssetsStore(transactionValue, tx)
await account.saveConnectedBurnsToAssetsStore(transactionValue.transaction, tx)
}

const assetsString = assetCount === 1 ? `${assetCount} asset` : `${assetCount} assets`
logger.info(` Completed backfilling ${assetsString} for account ${account.name}`)
}

logger.info('')
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
async backward(): Promise<void> {}
}
2 changes: 2 additions & 0 deletions ironfish/src/migrations/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Migration016 } from './016-sequence-to-tx'
import { Migration017 } from './017-sequence-encoding'
import { Migration018 } from './018-backfill-wallet-assets'
import { Migration019 } from './019-backfill-wallet-assets-from-chain'
import { Migration020 } from './020-backfill-null-asset-supplies'

export const MIGRATIONS = [
Migration014,
Expand All @@ -16,4 +17,5 @@ export const MIGRATIONS = [
Migration017,
Migration018,
Migration019,
Migration020,
]
Loading

0 comments on commit bba0453

Please sign in to comment.