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

Adds the possibility to list a single account. #1

Merged
merged 1 commit into from
Jan 2, 2022
Merged
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
22 changes: 17 additions & 5 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ var listCmd = &cobra.Command{
Use: "list",
Short: "List wallets or accounts within a wallet",
Run: func(cmd *cobra.Command, args []string) {
if walletIndex < 0 {
if walletAccount != "" {
// For when a specific account is specified. A single account is returned.
rpcClient := rpc.Client{URL: rpcURL}
getBalanceAndPrint(walletAccount, rpcClient)
} else if walletIndex < 0 {
// For when nothing is specified, shows the number of accounts in each wallet.
for i, wi := range wallets {
n := len(wi.Accounts)
switch n {
Expand All @@ -26,6 +31,8 @@ var listCmd = &cobra.Command{
}
}
} else {
// For when a specific wallet is specified, shows the balance of all accounts
// in that wallet.
checkWalletIndex()
var accounts []string
for address := range wallets[walletIndex].Accounts {
Expand All @@ -35,12 +42,9 @@ var listCmd = &cobra.Command{
rpcClient := rpc.Client{URL: rpcURL}
var balanceSum, pendingSum big.Int
for _, address := range accounts {
balance, pending, err := rpcClient.AccountBalance(address)
fatalIf(err)
balance, pending := getBalanceAndPrint(address, rpcClient)
balanceSum.Add(&balanceSum, &balance.Int)
pendingSum.Add(&pendingSum, &pending.Int)
fmt.Print(address)
printAmounts(&balance.Int, &pending.Int)
}
if len(accounts) > 1 {
fmt.Print(strings.Repeat(" ", 61), "Sum:")
Expand All @@ -50,6 +54,14 @@ var listCmd = &cobra.Command{
},
}

func getBalanceAndPrint(account string, rpcClient rpc.Client) (balance, pending *rpc.RawAmount) {
balance, pending, err := rpcClient.AccountBalance(account)
fatalIf(err)
fmt.Print(account)
printAmounts(&balance.Int, &pending.Int)
return balance, pending
}

func printAmounts(balance, pending *big.Int) {
if balance.Sign() > 0 {
fmt.Printf(" %s", util.NanoAmount{Raw: balance})
Expand Down