2021-03-23 19:04:34 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-04-23 14:47:43 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-03-23 19:04:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-04-22 14:44:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-03-23 19:04:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// nepDecimals invokes `decimals` NEP* method on a specified contract.
|
|
|
|
func (c *Client) nepDecimals(tokenHash util.Uint160) (int64, error) {
|
|
|
|
result, err := c.InvokeFunction(tokenHash, "decimals", []smartcontract.Parameter{}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nepSymbol invokes `symbol` NEP* method on a specified contract.
|
|
|
|
func (c *Client) nepSymbol(tokenHash util.Uint160) (string, error) {
|
|
|
|
result, err := c.InvokeFunction(tokenHash, "symbol", []smartcontract.Parameter{}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return topStringFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nepTotalSupply invokes `totalSupply` NEP* method on a specified contract.
|
|
|
|
func (c *Client) nepTotalSupply(tokenHash util.Uint160) (int64, error) {
|
|
|
|
result, err := c.InvokeFunction(tokenHash, "totalSupply", []smartcontract.Parameter{}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nepBalanceOf invokes `balanceOf` NEP* method on a specified contract.
|
2022-02-09 08:55:07 +00:00
|
|
|
func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID []byte) (int64, error) {
|
2021-03-23 19:04:34 +00:00
|
|
|
params := []smartcontract.Parameter{{
|
|
|
|
Type: smartcontract.Hash160Type,
|
|
|
|
Value: acc,
|
|
|
|
}}
|
|
|
|
if tokenID != nil {
|
|
|
|
params = append(params, smartcontract.Parameter{
|
2022-02-09 08:55:07 +00:00
|
|
|
Type: smartcontract.ByteArrayType,
|
|
|
|
Value: tokenID,
|
2021-03-23 19:04:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
result, err := c.InvokeFunction(tokenHash, "balanceOf", params, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
2021-04-22 14:44:09 +00:00
|
|
|
|
|
|
|
// nepTokenInfo returns full NEP* token info.
|
2021-04-23 14:32:48 +00:00
|
|
|
func (c *Client) nepTokenInfo(tokenHash util.Uint160, standard string) (*wallet.Token, error) {
|
2021-04-22 14:44:09 +00:00
|
|
|
cs, err := c.GetContractStateByHash(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-23 14:47:43 +00:00
|
|
|
var isStandardOK bool
|
|
|
|
for _, st := range cs.Manifest.SupportedStandards {
|
|
|
|
if st == standard {
|
|
|
|
isStandardOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isStandardOK {
|
|
|
|
return nil, fmt.Errorf("token %s does not support %s standard", tokenHash.StringLE(), standard)
|
|
|
|
}
|
2021-04-22 14:44:09 +00:00
|
|
|
symbol, err := c.nepSymbol(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decimals, err := c.nepDecimals(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-23 14:32:48 +00:00
|
|
|
return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals, standard), nil
|
2021-04-22 14:44:09 +00:00
|
|
|
}
|