2022-07-21 22:39:53 +03:00
|
|
|
package rpcclient
|
2021-03-23 22:04:34 +03:00
|
|
|
|
|
|
|
import (
|
2021-04-23 17:47:43 +03:00
|
|
|
"fmt"
|
|
|
|
|
2022-08-09 15:18:16 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-03-23 22:04:34 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-04-22 17:44:09 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-03-23 22:04:34 +03:00
|
|
|
)
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// nepDecimals invokes `decimals` NEP* method on the specified contract.
|
2021-03-23 22:04:34 +03:00
|
|
|
func (c *Client) nepDecimals(tokenHash util.Uint160) (int64, error) {
|
2022-08-09 15:18:16 +03:00
|
|
|
return unwrap.Int64(c.reader.Call(tokenHash, "decimals"))
|
2021-03-23 22:04:34 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// nepSymbol invokes `symbol` NEP* method on the specified contract.
|
2021-03-23 22:04:34 +03:00
|
|
|
func (c *Client) nepSymbol(tokenHash util.Uint160) (string, error) {
|
2022-08-09 15:18:16 +03:00
|
|
|
return unwrap.PrintableASCIIString(c.reader.Call(tokenHash, "symbol"))
|
2021-03-23 22:04:34 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// nepTotalSupply invokes `totalSupply` NEP* method on the specified contract.
|
2021-03-23 22:04:34 +03:00
|
|
|
func (c *Client) nepTotalSupply(tokenHash util.Uint160) (int64, error) {
|
2022-08-09 15:18:16 +03:00
|
|
|
return unwrap.Int64(c.reader.Call(tokenHash, "totalSupply"))
|
2021-03-23 22:04:34 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// nepBalanceOf invokes `balanceOf` NEP* method on the specified contract.
|
2022-02-09 11:55:07 +03:00
|
|
|
func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID []byte) (int64, error) {
|
2022-08-01 13:37:07 +03:00
|
|
|
params := []interface{}{acc}
|
2021-03-23 22:04:34 +03:00
|
|
|
if tokenID != nil {
|
2022-08-01 13:37:07 +03:00
|
|
|
params = append(params, tokenID)
|
2021-03-23 22:04:34 +03:00
|
|
|
}
|
2022-08-09 15:18:16 +03:00
|
|
|
return unwrap.Int64(c.reader.Call(tokenHash, "balanceOf", params...))
|
2021-03-23 22:04:34 +03:00
|
|
|
}
|
2021-04-22 17:44:09 +03:00
|
|
|
|
|
|
|
// nepTokenInfo returns full NEP* token info.
|
2021-04-23 17:32:48 +03:00
|
|
|
func (c *Client) nepTokenInfo(tokenHash util.Uint160, standard string) (*wallet.Token, error) {
|
2021-04-22 17:44:09 +03:00
|
|
|
cs, err := c.GetContractStateByHash(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-23 17:47:43 +03: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 17:44:09 +03: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 17:32:48 +03:00
|
|
|
return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals, standard), nil
|
2021-04-22 17:44:09 +03:00
|
|
|
}
|