github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/rpcclient/neptoken/info.go (about)

     1  package neptoken
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/core/state"
     7  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
     8  	"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
     9  	"github.com/nspcc-dev/neo-go/pkg/util"
    10  	"github.com/nspcc-dev/neo-go/pkg/wallet"
    11  )
    12  
    13  // InfoClient is a set of RPC methods required to get all of the NEP-11/NEP-17
    14  // token data.
    15  type InfoClient interface {
    16  	invoker.RPCInvoke
    17  
    18  	GetContractStateByHash(hash util.Uint160) (*state.Contract, error)
    19  }
    20  
    21  // Info allows to get basic token info using RPC client.
    22  func Info(c InfoClient, hash util.Uint160) (*wallet.Token, error) {
    23  	cs, err := c.GetContractStateByHash(hash)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	var standard string
    28  	for _, st := range cs.Manifest.SupportedStandards {
    29  		if st == manifest.NEP17StandardName || st == manifest.NEP11StandardName {
    30  			standard = st
    31  			break
    32  		}
    33  	}
    34  	if standard == "" {
    35  		return nil, fmt.Errorf("contract %s is not NEP-11/NEP17", hash.StringLE())
    36  	}
    37  	b := New(invoker.New(c, nil), hash)
    38  	symbol, err := b.Symbol()
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	decimals, err := b.Decimals()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return wallet.NewToken(hash, cs.Manifest.Name, symbol, int64(decimals), standard), nil
    47  }