forked from TrueCloudLab/neoneo-go
cli/wallet: use token data from getnepXXbalances
We have this data available since 0.99.1 while all public networks require at least 0.99.2 for compatibility and NeoFS setups use 0.99.2+ too. This data can simplify account handling considerably making additional requests unneccessary in many cases.
This commit is contained in:
parent
e28bf55ebb
commit
5f1fe72504
1 changed files with 23 additions and 30 deletions
|
@ -210,28 +210,12 @@ func getNEP17Balance(ctx *cli.Context) error {
|
||||||
|
|
||||||
var tokenFound bool
|
var tokenFound bool
|
||||||
for i := range balances.Balances {
|
for i := range balances.Balances {
|
||||||
var tokenName, tokenSymbol string
|
token := tokenFromNEP17Balance(&balances.Balances[i])
|
||||||
tokenDecimals := 0
|
|
||||||
asset := balances.Balances[i].Asset
|
|
||||||
token, err := getMatchingToken(ctx, wall, asset.StringLE(), manifest.NEP17StandardName)
|
|
||||||
if err != nil {
|
|
||||||
token, err = getTokenWithStandard(c, asset, manifest.NEP17StandardName)
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
if name != "" && !(token.Name == name || token.Symbol == name || token.Address() == name || token.Hash.StringLE() == name) {
|
if name != "" && !(token.Name == name || token.Symbol == name || token.Address() == name || token.Hash.StringLE() == name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tokenName = token.Name
|
printAssetBalance(ctx, balances.Balances[i])
|
||||||
tokenSymbol = token.Symbol
|
|
||||||
tokenDecimals = int(token.Decimals)
|
|
||||||
tokenFound = true
|
tokenFound = true
|
||||||
} else {
|
|
||||||
if name != "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
tokenSymbol = "UNKNOWN"
|
|
||||||
}
|
|
||||||
printAssetBalance(ctx, asset, tokenName, tokenSymbol, tokenDecimals, balances.Balances[i])
|
|
||||||
}
|
}
|
||||||
if name == "" || tokenFound {
|
if name == "" || tokenFound {
|
||||||
continue
|
continue
|
||||||
|
@ -274,22 +258,25 @@ func getNEP17Balance(ctx *cli.Context) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printAssetBalance(ctx, token.Hash, token.Name, token.Symbol, int(token.Decimals), result.NEP17Balance{
|
printAssetBalance(ctx, result.NEP17Balance{
|
||||||
Asset: token.Hash,
|
Asset: token.Hash,
|
||||||
Amount: "0",
|
Amount: "0",
|
||||||
|
Decimals: int(token.Decimals),
|
||||||
LastUpdated: 0,
|
LastUpdated: 0,
|
||||||
|
Name: token.Name,
|
||||||
|
Symbol: token.Symbol,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printAssetBalance(ctx *cli.Context, asset util.Uint160, tokenName, tokenSymbol string, tokenDecimals int, balance result.NEP17Balance) {
|
func printAssetBalance(ctx *cli.Context, balance result.NEP17Balance) {
|
||||||
fmt.Fprintf(ctx.App.Writer, "%s: %s (%s)\n", tokenSymbol, tokenName, asset.StringLE())
|
fmt.Fprintf(ctx.App.Writer, "%s: %s (%s)\n", balance.Symbol, balance.Name, balance.Asset.StringLE())
|
||||||
amount := balance.Amount
|
amount := balance.Amount
|
||||||
if tokenDecimals != 0 {
|
if balance.Decimals != 0 {
|
||||||
b, ok := new(big.Int).SetString(amount, 10)
|
b, ok := new(big.Int).SetString(amount, 10)
|
||||||
if ok {
|
if ok {
|
||||||
amount = fixedn.ToString(b, tokenDecimals)
|
amount = fixedn.ToString(b, balance.Decimals)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", amount)
|
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", amount)
|
||||||
|
@ -302,6 +289,14 @@ func getMatchingToken(ctx *cli.Context, w *wallet.Wallet, name string, standard
|
||||||
}, len(w.Extra.Tokens), name, standard)
|
}, len(w.Extra.Tokens), name, standard)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tokenFromNEP17Balance(bal *result.NEP17Balance) *wallet.Token {
|
||||||
|
return wallet.NewToken(bal.Asset, bal.Name, bal.Symbol, int64(bal.Decimals), manifest.NEP17StandardName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tokenFromNEP11Balance(bal *result.NEP11AssetBalance) *wallet.Token {
|
||||||
|
return wallet.NewToken(bal.Asset, bal.Name, bal.Symbol, int64(bal.Decimals), manifest.NEP11StandardName)
|
||||||
|
}
|
||||||
|
|
||||||
func getMatchingTokenRPC(ctx *cli.Context, c *rpcclient.Client, addr util.Uint160, name string, standard string) (*wallet.Token, error) {
|
func getMatchingTokenRPC(ctx *cli.Context, c *rpcclient.Client, addr util.Uint160, name string, standard string) (*wallet.Token, error) {
|
||||||
switch standard {
|
switch standard {
|
||||||
case manifest.NEP17StandardName:
|
case manifest.NEP17StandardName:
|
||||||
|
@ -310,8 +305,7 @@ func getMatchingTokenRPC(ctx *cli.Context, c *rpcclient.Client, addr util.Uint16
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
get := func(i int) *wallet.Token {
|
get := func(i int) *wallet.Token {
|
||||||
t, _ := getTokenWithStandard(c, bs.Balances[i].Asset, standard)
|
return tokenFromNEP17Balance(&bs.Balances[i])
|
||||||
return t
|
|
||||||
}
|
}
|
||||||
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
||||||
case manifest.NEP11StandardName:
|
case manifest.NEP11StandardName:
|
||||||
|
@ -320,8 +314,7 @@ func getMatchingTokenRPC(ctx *cli.Context, c *rpcclient.Client, addr util.Uint16
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
get := func(i int) *wallet.Token {
|
get := func(i int) *wallet.Token {
|
||||||
t, _ := getTokenWithStandard(c, bs.Balances[i].Asset, standard)
|
return tokenFromNEP11Balance(&bs.Balances[i])
|
||||||
return t
|
|
||||||
}
|
}
|
||||||
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue