-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 'accounts balances' sub commands
Add 'accounts balances get' and 'accounts balances send' sub-commands, including support for stargate clients and signing clients.
- Loading branch information
Showing
33 changed files
with
1,777 additions
and
1,458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Args } from '@oclif/core'; | ||
|
||
import { parseAmount } from '@/utils/coin'; | ||
|
||
const AmountArgumentDescription = 'Token amount'; | ||
|
||
/** | ||
* Check if string is a valid coin | ||
* | ||
* @param value - User input to be validated | ||
* @returns The same value without any changes | ||
*/ | ||
const validateCoin = async (value: string): Promise<string> => { | ||
parseAmount(value); | ||
|
||
return value; | ||
}; | ||
|
||
/** | ||
* Contract name argument | ||
*/ | ||
export const amountRequired = Args.string({ | ||
required: true, | ||
description: AmountArgumentDescription, | ||
parse: validateCoin, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { BaseCommand } from '@/lib/base'; | ||
import { accountRequired } from '@/arguments/account'; | ||
import { Accounts } from '@/domain/Accounts'; | ||
import { KeyringFlags } from '@/flags/keyring'; | ||
import { Config } from '@/domain/Config'; | ||
import { bold, darkGreen, green, yellow } from '@/utils/style'; | ||
import { showSpinner } from '@/ui/Spinner'; | ||
|
||
import { BackendType } from '@/types/Account'; | ||
|
||
/** | ||
* Command 'accounts balances get' | ||
* Access the bank module to query the balance of an address or account. | ||
*/ | ||
export default class AccountsBalancesGet extends BaseCommand<typeof AccountsBalancesGet> { | ||
static summary = 'Query the balance of an address or account'; | ||
static args = { | ||
account: accountRequired, | ||
}; | ||
|
||
static flags = { | ||
...KeyringFlags, | ||
}; | ||
|
||
/** | ||
* Runs the command. | ||
* | ||
* @returns Empty promise | ||
*/ | ||
public async run(): Promise<void> { | ||
const accountsDomain = await Accounts.init(this.flags['keyring-backend'] as BackendType, { filesPath: this.flags['keyring-path'] }); | ||
const config = await Config.open(); | ||
|
||
const result = await showSpinner(async () => { | ||
const client = await config.getStargateClient(); | ||
|
||
return accountsDomain.queryBalance(client, this.args.account); | ||
}, 'Querying balance'); | ||
|
||
if (this.jsonEnabled()) { | ||
this.logJson(result); | ||
} else { | ||
this.log(`Balances for account ${green(result.account.name)} (${darkGreen(result.account.address)})\n`); | ||
if (result.account.balances.length === 0) this.log(`- ${yellow('Empty wallet')}`); | ||
for (const item of result.account.balances) this.log(`- ${bold(item.amount)} ${item.denom}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Command } from '@oclif/core'; | ||
|
||
import Help from '@/plugins/help-plugin/help'; | ||
|
||
/** | ||
* Command 'accounts balances' | ||
* Displays the help info for the 'accounts balances' command | ||
*/ | ||
export default class AccountsBalances extends Command { | ||
static summary = 'Manage the balances of an account.'; | ||
|
||
public async run(): Promise<void> { | ||
const help = new Help(this.config, { all: true }); | ||
await help.showCommandHelp(AccountsBalances); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Flags } from '@oclif/core'; | ||
|
||
import { BaseCommand } from '@/lib/base'; | ||
import { accountRequired } from '@/arguments/account'; | ||
import { Accounts } from '@/domain/Accounts'; | ||
import { KeyringFlags } from '@/flags/keyring'; | ||
import { Config } from '@/domain/Config'; | ||
import { amountRequired } from '@/arguments/amount'; | ||
import { showSpinner } from '@/ui/Spinner'; | ||
import { bold, darkGreen, green, white } from '@/utils/style'; | ||
import { askForConfirmation } from '@/flags/force'; | ||
import { parseAmount } from '@/utils/coin'; | ||
|
||
import { AccountWithMnemonic, BackendType } from '@/types/Account'; | ||
|
||
/** | ||
* Command 'accounts balances send' | ||
* This command will use the bank module to send coins from one account to another. | ||
*/ | ||
export default class AccountsBalancesSend extends BaseCommand<typeof AccountsBalancesSend> { | ||
static summary = 'Send tokens from an address or account to another'; | ||
static args = { | ||
account: accountRequired, | ||
amount: amountRequired, | ||
}; | ||
|
||
static flags = { | ||
to: Flags.string({ description: 'Destination of the funds', required: true }), | ||
...KeyringFlags, | ||
}; | ||
|
||
/** | ||
* Runs the command. | ||
* | ||
* @returns Empty promise | ||
*/ | ||
public async run(): Promise<void> { | ||
const accountsDomain = await Accounts.init(this.flags['keyring-backend'] as BackendType, { filesPath: this.flags['keyring-path'] }); | ||
const fromAccount: AccountWithMnemonic = await accountsDomain.getWithMnemonic(this.args.account); | ||
const toAccount = await accountsDomain.accountBaseFromAddress(this.flags.to); | ||
const config = await Config.open(); | ||
const amount = parseAmount(this.args.amount); | ||
|
||
this.log(`Sending ${bold(amount.plainText)}`); | ||
this.log(`From ${green(fromAccount.name)} (${darkGreen(fromAccount.address)})`); | ||
this.log(`To ${toAccount.name ? `${green(toAccount.name)} (${darkGreen(toAccount.address)})` : green(toAccount.address)}\n`); | ||
|
||
await askForConfirmation(); | ||
this.log(); | ||
|
||
try { | ||
await showSpinner(async () => { | ||
const signingClient = await config.getSigningArchwayClient(fromAccount); | ||
|
||
fromAccount.mnemonic = ''; | ||
|
||
await signingClient.sendTokens(fromAccount.address, toAccount.address, [amount.coin], 'auto'); | ||
}, 'Sending tokens'); | ||
} catch (error: Error | any) { | ||
if (error?.message?.toString?.()?.includes('insufficient funds')) | ||
throw new Error(`Insufficient funds to send ${white.reset.bold(amount.plainText)} from ${green(fromAccount.name)}`); | ||
} | ||
|
||
this.success( | ||
darkGreen( | ||
`Sent ${white.reset.bold(amount.plainText)} from ${green(fromAccount.name)} to ${green(toAccount.name || toAccount.address)}` | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.