Skip to content

Commit ae80f98

Browse files
committed
print balances as a table
1 parent 8103142 commit ae80f98

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ _By setting this environment variable, you don't need to pass --rpc flag everyti
1717
#### Query account balance
1818

1919
```bash
20-
devd query balance [account addr] [optional ERC20 addr..] [--rpc http://localhost:8545]
20+
devd query balance [account addr] [optional ERC20 addr..] [--erc20] [--rpc http://localhost:8545]
2121
# devd q b 0xAccount
2222
# devd q b ethm1account
2323
# devd q b 0xAccount 0xErc20Contract
2424
# devd q b ethm1account 0xErc20Contract1 0xErc20Contract2
25+
# devd q b 0xAccount --erc20
2526
```
2627

2728
#### Query ERC20 token information

cmd/query/balance.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@ func GetQueryBalanceCommand() *cobra.Command {
1717
Short: "Get ERC-20 token information. If account address is provided, it will query the balance of the account (bech32 is accepted).",
1818
Args: cobra.MinimumNArgs(1),
1919
Run: func(cmd *cobra.Command, args []string) {
20-
ethClient8545, _ := mustGetEthClient(cmd, false)
21-
var bz []byte
22-
2320
evmAddrs, err := getEvmAddressFromAnyFormatAddress(args...)
2421
if err != nil {
2522
utils.PrintlnStdErr("ERR:", err)
2623
return
2724
}
2825

26+
//fetchErc20ModuleAndVfbc := cmd.Flags().Changed(flagErc20)
27+
28+
ethClient8545, _ := mustGetEthClient(cmd, false)
29+
var bz []byte
30+
2931
contextHeight := readContextHeightFromFlag(cmd)
3032

3133
accountAddr := evmAddrs[0]
3234
fmt.Println("Account", accountAddr)
3335

34-
if len(evmAddrs) == 1 {
35-
nativeBalance, err := ethClient8545.BalanceAt(context.Background(), accountAddr, contextHeight)
36-
utils.ExitOnErr(err, "failed to get account balance")
37-
38-
display, high, low, err := utils.ConvertNumberIntoDisplayWithExponent(nativeBalance, 18)
39-
utils.ExitOnErr(err, "failed to convert number into display with exponent")
40-
fmt.Println("> Native balance:")
41-
fmt.Println(" - Decimal:", 18)
42-
fmt.Println(" - Raw:", nativeBalance)
43-
fmt.Println(" - Display:", display)
44-
fmt.Println(" + High:", high)
45-
fmt.Println(" + Low:", low)
36+
printRow := func(colType, colContract, colSymbol, colBalance, colRaw, colDecimals, colHigh, colLow, extra string) {
37+
fmt.Printf("%-6s | %42s | %10s | %28s | %27s | %8s | %9s | %18s | %-1s\n", colType, colContract, colSymbol, colBalance, colRaw, colDecimals, colHigh, colLow, extra)
4638
}
4739

40+
printRow("Type", "Contract", "Symbol", "Balance", "Raw", "Decimals", "High", "Low", "Extra")
41+
42+
nativeBalance, err := ethClient8545.BalanceAt(context.Background(), accountAddr, contextHeight)
43+
utils.ExitOnErr(err, "failed to get account balance")
44+
45+
display, high, low, err := utils.ConvertNumberIntoDisplayWithExponent(nativeBalance, 18)
46+
utils.ExitOnErr(err, "failed to convert number into display with exponent")
47+
48+
printRow("Native", "-", "(native)", display, nativeBalance.String(), "18", high.String(), low.String(), "")
49+
4850
for i := 1; i < len(evmAddrs); i++ {
4951
contractAddr := evmAddrs[i]
5052

@@ -89,19 +91,14 @@ func GetQueryBalanceCommand() *cobra.Command {
8991
display, high, low, err := utils.ConvertNumberIntoDisplayWithExponent(tokenBalance, int(contractDecimals.Int64()))
9092
utils.ExitOnErr(err, "failed to convert number into display with exponent")
9193

92-
fmt.Printf("> ERC-20 %s\n", contractAddr)
93-
fmt.Println(" - Symbol:", contractSymbol)
94-
fmt.Println(" - Decimals:", contractDecimals.Uint64())
95-
fmt.Println(" - Raw:", tokenBalance)
96-
fmt.Println(" - Display:", display, contractSymbol)
97-
fmt.Println(" + High:", high)
98-
fmt.Println(" + Low:", low)
94+
printRow("Input", contractAddr.String(), contractSymbol, display, tokenBalance.String(), contractDecimals.String(), high.String(), low.String(), "")
9995
}
10096
},
10197
}
10298

10399
cmd.Flags().String(flagRpc, "", flagEvmRpcDesc)
104100
cmd.Flags().Int64(flagHeight, 0, "query balance at specific height")
101+
cmd.Flags().String(flagErc20, "", "query balance of ERC-20 contracts of `x/erc20` module and virtual frontier bank contracts")
105102

106103
return cmd
107104
}

cmd/query/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const (
1111
flagTracer = "tracer"
1212
flagHeight = "height"
1313
flagNoTranslate = "no-translate"
14+
flagErc20 = "erc20"
1415
)
1516

1617
const (

0 commit comments

Comments
 (0)