From d737915843b9bbed01a358e6fb2a5315afdbf0d9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 11 Jan 2021 15:02:17 +0300 Subject: [PATCH] cli: output nep17 balance using decimals Also fix balance tests to match full line, not just prefix. --- cli/nep17_test.go | 10 +++++----- cli/wallet/nep17.go | 13 +++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cli/nep17_test.go b/cli/nep17_test.go index 35c60e253..768ecb145 100644 --- a/cli/nep17_test.go +++ b/cli/nep17_test.go @@ -30,7 +30,7 @@ func TestNEP17Balance(t *testing.T) { checkResult := func(t *testing.T) { e.checkNextLine(t, "^\\s*Account\\s+"+validatorAddr) e.checkNextLine(t, "^\\s*NEO:\\s+NeoToken \\("+e.Chain.GoverningTokenHash().StringLE()+"\\)") - e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+b.String()) + e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+b.String()+"$") e.checkNextLine(t, "^\\s*Updated\\s*:\\s*"+strconv.FormatUint(uint64(index), 10)) e.checkEOF(t) } @@ -48,7 +48,7 @@ func TestNEP17Balance(t *testing.T) { e.checkNextLine(t, "^\\s*Account\\s+"+validatorAddr) e.checkNextLine(t, "^\\s*GAS:\\s+GasToken \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)") b := e.Chain.GetUtilityTokenBalance(validatorHash) - e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(b.Int64()).String()) + e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(b.Int64()).String()+"$") }) t.Run("all accounts", func(t *testing.T) { e.Run(t, cmdbase...) @@ -57,7 +57,7 @@ func TestNEP17Balance(t *testing.T) { e.checkNextLine(t, "^Account "+address.Uint160ToString(addr1)) e.checkNextLine(t, "^\\s*GAS:\\s+GasToken \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)") balance := e.Chain.GetUtilityTokenBalance(addr1) - e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String()) + e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String()+"$") e.checkNextLine(t, "^\\s*Updated:") e.checkNextLine(t, "^\\s*$") @@ -75,12 +75,12 @@ func TestNEP17Balance(t *testing.T) { if strings.Contains(line, "GAS") { e.checkLine(t, line, "^\\s*GAS:\\s+GasToken \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)") balance = e.Chain.GetUtilityTokenBalance(addr3) - e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String()) + e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String()+"$") e.checkNextLine(t, "^\\s*Updated:") } else { balance, index := e.Chain.GetGoverningTokenBalance(validatorHash) e.checkLine(t, line, "^\\s*NEO:\\s+NeoToken \\("+e.Chain.GoverningTokenHash().StringLE()+"\\)") - e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+balance.String()) + e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+balance.String()+"$") e.checkNextLine(t, "^\\s*Updated\\s*:\\s*"+strconv.FormatUint(uint64(index), 10)) } } diff --git a/cli/wallet/nep17.go b/cli/wallet/nep17.go index 80269f06e..9a9e0b65c 100644 --- a/cli/wallet/nep17.go +++ b/cli/wallet/nep17.go @@ -3,6 +3,7 @@ package wallet import ( "errors" "fmt" + "math/big" "strings" "github.com/nspcc-dev/neo-go/cli/flags" @@ -179,7 +180,7 @@ func getNEP17Balance(ctx *cli.Context) error { for i := range balances.Balances { var tokenName, tokenSymbol string - + tokenDecimals := 0 asset := balances.Balances[i].Asset token, err := getMatchingToken(ctx, wall, asset.StringLE()) if err != nil { @@ -191,6 +192,7 @@ func getNEP17Balance(ctx *cli.Context) error { } tokenName = token.Name tokenSymbol = token.Symbol + tokenDecimals = int(token.Decimals) } else { if name != "" { continue @@ -198,7 +200,14 @@ func getNEP17Balance(ctx *cli.Context) error { tokenSymbol = "UNKNOWN" } fmt.Fprintf(ctx.App.Writer, "%s: %s (%s)\n", tokenSymbol, tokenName, asset.StringLE()) - fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", balances.Balances[i].Amount) + amount := balances.Balances[i].Amount + if tokenDecimals != 0 { + b, ok := new(big.Int).SetString(amount, 10) + if ok { + amount = fixedn.ToString(b, tokenDecimals) + } + } + fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", amount) fmt.Fprintf(ctx.App.Writer, "\tUpdated: %d\n", balances.Balances[i].LastUpdated) } }