Skip to content

Commit e4548fa

Browse files
authored
Add new useAccount helper an upgrade commands (#5311)
* Add new helper to select a user account * Upgrade commands to use useAccount
1 parent 8a213b7 commit e4548fa

File tree

14 files changed

+88
-129
lines changed

14 files changed

+88
-129
lines changed

ironfish-cli/src/commands/wallet/assets.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
PUBLIC_ADDRESS_LENGTH,
1010
} from '@ironfish/rust-nodejs'
1111
import { BufferUtils } from '@ironfish/sdk'
12-
import { Args, Flags } from '@oclif/core'
12+
import { Flags } from '@oclif/core'
1313
import { IronfishCommand } from '../../command'
1414
import { RemoteFlags } from '../../flags'
1515
import { table, TableFlags } from '../../ui'
16-
import { renderAssetWithVerificationStatus } from '../../utils'
16+
import { renderAssetWithVerificationStatus, useAccount } from '../../utils'
1717
import { TableCols } from '../../utils/table'
1818

1919
const MAX_ASSET_METADATA_COLUMN_WIDTH = ASSET_METADATA_LENGTH + 1
@@ -25,13 +25,6 @@ const MIN_ASSET_NAME_COLUMN_WIDTH = ASSET_NAME_LENGTH / 2 + 1
2525
export class AssetsCommand extends IronfishCommand {
2626
static description = `list the account's assets`
2727

28-
static args = {
29-
account: Args.string({
30-
required: false,
31-
description: 'Name of the account. DEPRECATED: use --account flag',
32-
}),
33-
}
34-
3528
static flags = {
3629
...RemoteFlags,
3730
...TableFlags,
@@ -42,11 +35,12 @@ export class AssetsCommand extends IronfishCommand {
4235
}
4336

4437
async start(): Promise<void> {
45-
const { flags, args } = await this.parse(AssetsCommand)
46-
// TODO: remove account arg
47-
const account = flags.account ? flags.account : args.account
38+
const { flags } = await this.parse(AssetsCommand)
4839

4940
const client = await this.connectRpc()
41+
42+
const account = await useAccount(client, flags.account)
43+
5044
const response = client.wallet.getAssets({
5145
account,
5246
})

ironfish-cli/src/commands/wallet/balance.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
import { CurrencyUtils, GetBalanceResponse, isNativeIdentifier, RpcAsset } from '@ironfish/sdk'
5-
import { Args, Flags } from '@oclif/core'
5+
import { Flags } from '@oclif/core'
66
import { IronfishCommand } from '../../command'
77
import { RemoteFlags } from '../../flags'
88
import * as ui from '../../ui'
9-
import { renderAssetWithVerificationStatus } from '../../utils'
9+
import { renderAssetWithVerificationStatus, useAccount } from '../../utils'
1010

1111
export class BalanceCommand extends IronfishCommand {
1212
static description = `show the account's balance for an asset
@@ -27,13 +27,6 @@ Balance is your coins from all of your transactions, even if they are on forks o
2727
},
2828
]
2929

30-
static args = {
31-
account: Args.string({
32-
required: false,
33-
description: 'Name of the account to get balance for. DEPRECATED: use --account flag',
34-
}),
35-
}
36-
3730
static flags = {
3831
...RemoteFlags,
3932
account: Flags.string({
@@ -59,12 +52,12 @@ Balance is your coins from all of your transactions, even if they are on forks o
5952
}
6053

6154
async start(): Promise<void> {
62-
const { flags, args } = await this.parse(BalanceCommand)
63-
// TODO: remove account arg
64-
const account = flags.account ? flags.account : args.account
55+
const { flags } = await this.parse(BalanceCommand)
6556

6657
const client = await this.connectRpc()
6758

59+
const account = await useAccount(client, flags.account)
60+
6861
const response = await client.wallet.getAccountBalance({
6962
account,
7063
assetId: flags.assetId,

ironfish-cli/src/commands/wallet/balances.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
import { BufferUtils, CurrencyUtils, GetBalancesResponse, RpcAsset } from '@ironfish/sdk'
5-
import { Args, Flags } from '@oclif/core'
5+
import { Flags } from '@oclif/core'
66
import { IronfishCommand } from '../../command'
77
import { RemoteFlags } from '../../flags'
88
import { table, TableColumns, TableFlags } from '../../ui'
9-
import { compareAssets, renderAssetWithVerificationStatus } from '../../utils'
9+
import { compareAssets, renderAssetWithVerificationStatus, useAccount } from '../../utils'
1010

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

1313
export class BalancesCommand extends IronfishCommand {
1414
static description = `show the account's balance for all assets`
1515

16-
static args = {
17-
account: Args.string({
18-
required: false,
19-
description: 'Name of the account to get balances for. DEPRECATED: use --account flag',
20-
}),
21-
}
22-
2316
static flags = {
2417
...RemoteFlags,
2518
...TableFlags,
@@ -38,11 +31,11 @@ export class BalancesCommand extends IronfishCommand {
3831
}
3932

4033
async start(): Promise<void> {
41-
const { flags, args } = await this.parse(BalancesCommand)
34+
const { flags } = await this.parse(BalancesCommand)
4235
const client = await this.connectRpc()
4336

44-
// TODO: remove account arg
45-
const account = flags.account ? flags.account : args.account
37+
const account = await useAccount(client, flags.account)
38+
4639
const response = await client.wallet.getAccountBalances({
4740
account,
4841
confirmations: flags.confirmations,

ironfish-cli/src/commands/wallet/burn.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Flags, ux } from '@oclif/core'
1414
import { IronfishCommand } from '../../command'
1515
import { IronFlag, RemoteFlags, ValueFlag } from '../../flags'
1616
import * as ui from '../../ui'
17+
import { useAccount } from '../../utils'
1718
import { promptCurrency } from '../../utils/currency'
1819
import { promptExpiration } from '../../utils/expiration'
1920
import { getExplorer } from '../../utils/explorer'
@@ -108,19 +109,7 @@ This will destroy tokens and decrease supply for a given asset.`
108109
}
109110
}
110111

111-
let account = flags.account
112-
if (!account) {
113-
const response = await client.wallet.getDefaultAccount()
114-
115-
if (!response.content.account) {
116-
this.error(
117-
`No account is currently active.
118-
Use ironfish wallet:create <name> to first create an account`,
119-
)
120-
}
121-
122-
account = response.content.account.name
123-
}
112+
const account = await useAccount(client, flags.account)
124113

125114
let assetId = flags.assetId
126115

ironfish-cli/src/commands/wallet/export.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
import { AccountFormat, ErrorUtils, LanguageUtils } from '@ironfish/sdk'
5-
import { Args, Flags } from '@oclif/core'
5+
import { Flags } from '@oclif/core'
66
import fs from 'fs'
77
import path from 'path'
88
import { IronfishCommand } from '../../command'
99
import { EnumLanguageKeyFlag, JsonFlags, RemoteFlags } from '../../flags'
1010
import { confirmOrQuit } from '../../ui'
11+
import { useAccount } from '../../utils'
1112

1213
export class ExportCommand extends IronfishCommand {
1314
static description = `export an account`
1415
static enableJsonFlag = true
1516

16-
static args = {
17-
account: Args.string({
18-
required: false,
19-
description: 'Name of the account to export',
20-
}),
21-
}
22-
2317
static flags = {
2418
...RemoteFlags,
2519
...JsonFlags,
20+
account: Flags.string({
21+
char: 'a',
22+
description: 'Name of the account to export',
23+
}),
2624
local: Flags.boolean({
2725
default: false,
2826
description: 'Export an account without an online node',
@@ -47,9 +45,8 @@ export class ExportCommand extends IronfishCommand {
4745
}
4846

4947
async start(): Promise<unknown> {
50-
const { flags, args } = await this.parse(ExportCommand)
48+
const { flags } = await this.parse(ExportCommand)
5149
const { local, path: exportPath, viewonly: viewOnly } = flags
52-
const { account } = args
5350

5451
if (flags.language) {
5552
flags.mnemonic = true
@@ -62,6 +59,9 @@ export class ExportCommand extends IronfishCommand {
6259
: AccountFormat.Base64Json
6360

6461
const client = await this.connectRpc(local)
62+
63+
const account = await useAccount(client, flags.account)
64+
6565
const response = await client.wallet.exportAccount({
6666
account,
6767
viewOnly,

ironfish-cli/src/commands/wallet/mint.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Flags, ux } from '@oclif/core'
1818
import { IronfishCommand } from '../../command'
1919
import { IronFlag, RemoteFlags, ValueFlag } from '../../flags'
2020
import * as ui from '../../ui'
21+
import { useAccount } from '../../utils'
2122
import { promptCurrency } from '../../utils/currency'
2223
import { promptExpiration } from '../../utils/expiration'
2324
import { getExplorer } from '../../utils/explorer'
@@ -129,19 +130,7 @@ This will create tokens and increase supply for a given asset.`
129130
}
130131
}
131132

132-
let account = flags.account
133-
if (!account) {
134-
const response = await client.wallet.getDefaultAccount()
135-
136-
if (!response.content.account) {
137-
this.error(
138-
`No account is currently active.
139-
Use ironfish wallet:create <name> to first create an account`,
140-
)
141-
}
142-
143-
account = response.content.account.name
144-
}
133+
const account = await useAccount(client, flags.account)
145134

146135
const publicKeyResponse = await client.wallet.getAccountPublicKey({ account })
147136
const accountPublicKey = publicKeyResponse.content.publicKey

ironfish-cli/src/commands/wallet/notes/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
import { CurrencyUtils, RpcAsset } from '@ironfish/sdk'
5-
import { Args, Flags } from '@oclif/core'
5+
import { Flags } from '@oclif/core'
66
import { IronfishCommand } from '../../../command'
77
import { RemoteFlags } from '../../../flags'
88
import { table, TableFlags } from '../../../ui'
9+
import { useAccount } from '../../../utils'
910
import { TableCols } from '../../../utils/table'
1011

1112
const { sort: _, ...tableFlags } = TableFlags
1213
export class NotesCommand extends IronfishCommand {
1314
static description = `list the account's notes`
1415

15-
static args = {
16-
account: Args.string({
17-
required: false,
18-
description: 'Name of the account to get notes for. DEPRECATED: use --account flag',
19-
}),
20-
}
21-
2216
static flags = {
2317
...RemoteFlags,
2418
...tableFlags,
@@ -29,14 +23,14 @@ export class NotesCommand extends IronfishCommand {
2923
}
3024

3125
async start(): Promise<void> {
32-
const { flags, args } = await this.parse(NotesCommand)
33-
// TODO: remove account arg
34-
const account = flags.account ? flags.account : args.account
26+
const { flags } = await this.parse(NotesCommand)
3527

3628
const assetLookup: Map<string, RpcAsset> = new Map()
3729

3830
const client = await this.connectRpc()
3931

32+
const account = await useAccount(client, flags.account)
33+
4034
const response = client.wallet.getAccountNotesStream({ account })
4135

4236
let showHeader = !flags['no-header']

ironfish-cli/src/commands/wallet/reset.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

5-
import { Args, Flags } from '@oclif/core'
5+
import { Flags } from '@oclif/core'
66
import { IronfishCommand } from '../../command'
77
import { RemoteFlags } from '../../flags'
88
import { confirmOrQuit } from '../../ui'
9+
import { useAccount } from '../../utils'
910

1011
export class ResetCommand extends IronfishCommand {
1112
static description = `resets an account's balance and rescans`
1213

13-
static args = {
14-
account: Args.string({
15-
required: true,
16-
description: 'Name of the account to reset',
17-
}),
18-
}
19-
2014
static flags = {
2115
...RemoteFlags,
16+
account: Flags.string({
17+
char: 'a',
18+
description: 'Name of the account to reset',
19+
}),
2220
resetCreated: Flags.boolean({
2321
default: false,
2422
description: 'Reset the accounts birthday',
@@ -34,8 +32,11 @@ export class ResetCommand extends IronfishCommand {
3432
}
3533

3634
async start(): Promise<void> {
37-
const { args, flags } = await this.parse(ResetCommand)
38-
const { account } = args
35+
const { flags } = await this.parse(ResetCommand)
36+
37+
const client = await this.connectRpc()
38+
39+
const account = await useAccount(client, flags.account)
3940

4041
await confirmOrQuit(
4142
`Are you sure you want to reset the account '${account}'?` +
@@ -45,8 +46,6 @@ export class ResetCommand extends IronfishCommand {
4546
flags.confirm,
4647
)
4748

48-
const client = await this.connectRpc()
49-
5049
await client.wallet.resetAccount({
5150
account,
5251
resetCreatedAt: flags.resetCreated,

ironfish-cli/src/commands/wallet/send.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Flags } from '@oclif/core'
1616
import { IronfishCommand } from '../../command'
1717
import { HexFlag, IronFlag, RemoteFlags, ValueFlag } from '../../flags'
1818
import * as ui from '../../ui'
19+
import { useAccount } from '../../utils'
1920
import { promptCurrency } from '../../utils/currency'
2021
import { promptExpiration } from '../../utils/expiration'
2122
import { getExplorer } from '../../utils/explorer'
@@ -121,7 +122,6 @@ export class Send extends IronfishCommand {
121122
const { flags } = await this.parse(Send)
122123
let assetId = flags.assetId
123124
let to = flags.to
124-
let from = flags.account
125125

126126
const client = await this.connectRpc()
127127

@@ -135,6 +135,8 @@ export class Send extends IronfishCommand {
135135
}
136136
}
137137

138+
const from = await useAccount(client, flags.account, 'Select an account to send from')
139+
138140
if (assetId == null) {
139141
const asset = await ui.assetPrompt(client, from, {
140142
action: 'send',
@@ -190,19 +192,6 @@ export class Send extends IronfishCommand {
190192
})
191193
}
192194

193-
if (!from) {
194-
const response = await client.wallet.getDefaultAccount()
195-
196-
if (!response.content.account) {
197-
this.error(
198-
`No account is currently active.
199-
Use ironfish wallet:create <name> to first create an account`,
200-
)
201-
}
202-
203-
from = response.content.account.name
204-
}
205-
206195
if (!to) {
207196
to = await ui.inputPrompt('Enter the public address of the recipient', true)
208197
}

0 commit comments

Comments
 (0)