Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NayNay] Balance --all #310

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- TUI
- animation on tui load (while entropy loads) [#288](https://github.com/entropyxyz/cli/pull/288)

- Programmatic CLI
- added option for users to view all balances of admin accounts in the config [#310](https://github.com/entropyxyz/cli/pull/310)

### Changes

- Shared
Expand Down
29 changes: 22 additions & 7 deletions src/balance/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,40 @@ import { EntropyBalance } from "./main";
import { endpointOption, cliWrite, loadEntropy } from "../common/utils-cli";
import { findAccountByAddressOrName } from "../common/utils";
import * as config from "../config";
import { EntropyAccountConfig } from "src/config/types";
import { formattedBalances } from "./utils";

export function entropyBalanceCommand () {
const balanceCommand = new Command('balance')
// account can no longer be a required argument if a user wishes to
// view the balances of all accounts
balanceCommand
.description('Command to retrieive the balance of an account on the Entropy Network')
.argument('<account>', [
.argument('[account]', [
'The address an account address whose balance you want to query.',
'Can also be the human-readable name of one of your accounts'
].join(' '))
.addOption(endpointOption())
.option('--all', 'Get balances for all admin accounts in the config')
.action(async (account, opts) => {
const entropy: Entropy = await loadEntropy(account, opts.endpoint)
const BalanceService = new EntropyBalance(entropy, opts.endpoint)

const { accounts } = await config.get()
const address = findAccountByAddressOrName(accounts, account)?.address
// when trying to get the balance of all accounts, need to use a temporary address
// to initialize entropy in order to use substrate
const tempAddress = account || accounts[0].address
const entropy: Entropy = await loadEntropy(tempAddress, opts.endpoint)
const BalanceService = new EntropyBalance(entropy, opts.endpoint)

const balance = await BalanceService.getBalance(address)
cliWrite(`${balance.toLocaleString('en-US')} BITS`)
if (opts.all) {
// Balances for all admin accounts
const addresses: string[] = accounts.map((acct: EntropyAccountConfig) => acct.address)
const balances = formattedBalances(await BalanceService.getBalances(addresses))
cliWrite(balances)
} else {
// Balance for singular account
const address = findAccountByAddressOrName(accounts, account)?.address
const balance = await BalanceService.getBalance(address)
cliWrite(`${balance.toLocaleString('en-US')} BITS`)
}
process.exit(0)
})

Expand Down
10 changes: 9 additions & 1 deletion src/balance/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export const hexToBigInt = (hexString: string) => BigInt(hexString)
import { BalanceInfo } from "./types"

export const hexToBigInt = (hexString: string) => BigInt(hexString)

export function formattedBalances (balanceInfo: BalanceInfo) {
const balances = {}
Object.keys(balanceInfo).forEach(key => balances[key] = !balanceInfo[key].error ? `${balanceInfo[key].balance.toLocaleString('en-us')} BITS` : null)
return balances
}
Loading