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. (#11) #3

Merged
merged 1 commit into from
Jan 3, 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
23 changes: 17 additions & 6 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ 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.
getBalanceAndPrint(walletAccount)
} 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,21 +30,19 @@ 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 {
accounts = append(accounts, address)
}
sort.Strings(accounts)
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)
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 +52,15 @@ var listCmd = &cobra.Command{
},
}

func getBalanceAndPrint(account string) (balance, pending *rpc.RawAmount) {
rpcClient := rpc.Client{URL: rpcURL}
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