rpc: add NEP11 API support to the RPC client

This commit is contained in:
Anna Shaleva 2021-03-23 22:04:34 +03:00
parent 2bb3ff2aff
commit f955589370
5 changed files with 317 additions and 44 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
@ -75,3 +76,22 @@ func topStringFromStack(st []stackitem.Item) (string, error) {
}
return string(bs), nil
}
// topUint160FromStack returns the top util.Uint160 from stack.
func topUint160FromStack(st []stackitem.Item) (util.Uint160, error) {
index := len(st) - 1 // top stack element is last in the array
bs, err := st[index].TryBytes()
if err != nil {
return util.Uint160{}, err
}
return util.Uint160DecodeBytesBE(bs)
}
// topMapFromStack returns the top stackitem.Map from stack.
func topMapFromStack(st []stackitem.Item) (*stackitem.Map, error) {
index := len(st) - 1 // top stack element is last in the array
if t := st[index].Type(); t != stackitem.MapT {
return nil, fmt.Errorf("invalid return stackitem type: %s", t.String())
}
return st[index].(*stackitem.Map), nil
}

72
pkg/rpc/client/nep.go Normal file
View file

@ -0,0 +1,72 @@
package client
import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// 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.
func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID *string) (int64, error) {
params := []smartcontract.Parameter{{
Type: smartcontract.Hash160Type,
Value: acc,
}}
if tokenID != nil {
params = append(params, smartcontract.Parameter{
Type: smartcontract.StringType,
Value: *tokenID,
})
}
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)
}

165
pkg/rpc/client/nep11.go Normal file
View file

@ -0,0 +1,165 @@
package client
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)
// NEP11Decimals invokes `decimals` NEP11 method on a specified contract.
func (c *Client) NEP11Decimals(tokenHash util.Uint160) (int64, error) {
return c.nepDecimals(tokenHash)
}
// NEP11Symbol invokes `symbol` NEP11 method on a specified contract.
func (c *Client) NEP11Symbol(tokenHash util.Uint160) (string, error) {
return c.nepSymbol(tokenHash)
}
// NEP11TotalSupply invokes `totalSupply` NEP11 method on a specified contract.
func (c *Client) NEP11TotalSupply(tokenHash util.Uint160) (int64, error) {
return c.nepTotalSupply(tokenHash)
}
// NEP11BalanceOf invokes `balanceOf` NEP11 method on a specified contract.
func (c *Client) NEP11BalanceOf(tokenHash, owner util.Uint160) (int64, error) {
return c.nepBalanceOf(tokenHash, owner, nil)
}
// TransferNEP11 creates an invocation transaction that invokes 'transfer' method
// on a given token to move the whole NEP11 token with the specified token ID to
// given account and sends it to the network returning just a hash of it.
func (c *Client) TransferNEP11(acc *wallet.Account, to util.Uint160,
tokenHash util.Uint160, tokenID string, gas int64) (util.Uint256, error) {
if !c.initDone {
return util.Uint256{}, errNetworkNotInitialized
}
tx, err := c.createNEP11TransferTx(acc, tokenHash, gas, to, tokenID)
if err != nil {
return util.Uint256{}, err
}
if err := acc.SignTx(c.GetNetwork(), tx); err != nil {
return util.Uint256{}, fmt.Errorf("can't sign NEP11 transfer tx: %w", err)
}
return c.SendRawTransaction(tx)
}
// createNEP11TransferTx is an internal helper for TransferNEP11 and
// TransferNEP11D which creates an invocation transaction for the
// 'transfer' method of a given contract (token) to move the whole (or the
// specified amount of) NEP11 token with the specified token ID to given account
// and returns it. The returned transaction is not signed.
// `args` for TransferNEP11: to util.Uint160, tokenID string;
// `args` for TransferNEP11D: from, to util.Uint160, amount int64, tokenID string.
func (c *Client) createNEP11TransferTx(acc *wallet.Account, tokenHash util.Uint160,
gas int64, args ...interface{}) (*transaction.Transaction, error) {
w := io.NewBufBinWriter()
emit.AppCall(w.BinWriter, tokenHash, "transfer", callflag.All, args...)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
if w.Err != nil {
return nil, fmt.Errorf("failed to create NEP11 transfer script: %w", w.Err)
}
from, err := address.StringToUint160(acc.Address)
if err != nil {
return nil, fmt.Errorf("bad account address: %w", err)
}
return c.CreateTxFromScript(w.Bytes(), acc, -1, gas, []SignerAccount{{
Signer: transaction.Signer{
Account: from,
Scopes: transaction.CalledByEntry,
},
Account: acc,
}})
}
// Non-divisible NFT methods section start.
// NEP11NDOwnerOf invokes `ownerOf` non-devisible NEP11 method with the
// specified token ID on a specified contract.
func (c *Client) NEP11NDOwnerOf(tokenHash util.Uint160, tokenID string) (util.Uint160, error) {
result, err := c.InvokeFunction(tokenHash, "ownerOf", []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: tokenID,
},
}, nil)
if err != nil {
return util.Uint160{}, err
}
err = getInvocationError(result)
if err != nil {
return util.Uint160{}, err
}
return topUint160FromStack(result.Stack)
}
// Non-divisible NFT methods section end.
// Divisible NFT methods section start.
// TransferNEP11D creates an invocation transaction that invokes 'transfer'
// method on a given token to move specified amount of divisible NEP11 assets
// (in FixedN format using contract's number of decimals) to given account and
// sends it to the network returning just a hash of it.
func (c *Client) TransferNEP11D(acc *wallet.Account, to util.Uint160,
tokenHash util.Uint160, amount int64, tokenID string, gas int64) (util.Uint256, error) {
if !c.initDone {
return util.Uint256{}, errNetworkNotInitialized
}
from, err := address.StringToUint160(acc.Address)
if err != nil {
return util.Uint256{}, fmt.Errorf("bad account address: %w", err)
}
tx, err := c.createNEP11TransferTx(acc, tokenHash, gas, acc.Address, from, to, amount, tokenID)
if err != nil {
return util.Uint256{}, err
}
if err := acc.SignTx(c.GetNetwork(), tx); err != nil {
return util.Uint256{}, fmt.Errorf("can't sign NEP11 divisible transfer tx: %w", err)
}
return c.SendRawTransaction(tx)
}
// NEP11DBalanceOf invokes `balanceOf` divisible NEP11 method on a
// specified contract.
func (c *Client) NEP11DBalanceOf(tokenHash, owner util.Uint160, tokenID string) (int64, error) {
return c.nepBalanceOf(tokenHash, owner, &tokenID)
}
// Divisible NFT methods section end.
// Optional NFT methods section start.
// NEP11Properties invokes `properties` optional NEP11 method on a
// specified contract.
func (c *Client) NEP11Properties(tokenHash util.Uint160, tokenID string) (*stackitem.Map, error) {
result, err := c.InvokeFunction(tokenHash, "properties", []smartcontract.Parameter{{
Type: smartcontract.StringType,
Value: tokenID,
}}, nil)
if err != nil {
return nil, err
}
err = getInvocationError(result)
if err != nil {
return nil, err
}
return topMapFromStack(result.Stack)
}
// Optional NFT methods section end.

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
@ -30,61 +29,22 @@ type SignerAccount struct {
// NEP17Decimals invokes `decimals` NEP17 method on a specified contract.
func (c *Client) NEP17Decimals(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, fmt.Errorf("failed to get NEP17 decimals: %w", err)
}
return topIntFromStack(result.Stack)
return c.nepDecimals(tokenHash)
}
// NEP17Symbol invokes `symbol` NEP17 method on a specified contract.
func (c *Client) NEP17Symbol(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 "", fmt.Errorf("failed to get NEP17 symbol: %w", err)
}
return topStringFromStack(result.Stack)
return c.nepSymbol(tokenHash)
}
// NEP17TotalSupply invokes `totalSupply` NEP17 method on a specified contract.
func (c *Client) NEP17TotalSupply(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, fmt.Errorf("failed to get NEP17 total supply: %w", err)
}
return topIntFromStack(result.Stack)
return c.nepTotalSupply(tokenHash)
}
// NEP17BalanceOf invokes `balanceOf` NEP17 method on a specified contract.
func (c *Client) NEP17BalanceOf(tokenHash, acc util.Uint160) (int64, error) {
result, err := c.InvokeFunction(tokenHash, "balanceOf", []smartcontract.Parameter{{
Type: smartcontract.Hash160Type,
Value: acc,
}}, nil)
if err != nil {
return 0, err
}
err = getInvocationError(result)
if err != nil {
return 0, fmt.Errorf("failed to get NEP17 balance: %w", err)
}
return topIntFromStack(result.Stack)
return c.nepBalanceOf(tokenHash, acc, nil)
}
// NEP17TokenInfo returns full NEP17 token info.

View file

@ -19,6 +19,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
)
@ -476,3 +477,58 @@ func TestClient_GetNativeContracts(t *testing.T) {
require.NoError(t, err)
require.Equal(t, chain.GetNatives(), cs)
}
func TestClient_NEP11(t *testing.T) {
chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t)
defer chain.Close()
defer rpcSrv.Shutdown()
c, err := client.New(context.Background(), httpSrv.URL, client.Options{})
require.NoError(t, err)
require.NoError(t, c.Init())
h, err := chain.GetNativeContractScriptHash(nativenames.NameService)
require.NoError(t, err)
acc := testchain.PrivateKeyByID(0).GetScriptHash()
t.Run("Decimals", func(t *testing.T) {
d, err := c.NEP11Decimals(h)
require.NoError(t, err)
require.EqualValues(t, 0, d) // non-divisible
})
t.Run("TotalSupply", func(t *testing.T) {
s, err := c.NEP11TotalSupply(h)
require.NoError(t, err)
require.EqualValues(t, 1, s) // the only `neo.com` of acc0
})
t.Run("Symbol", func(t *testing.T) {
sym, err := c.NEP11Symbol(h)
require.NoError(t, err)
require.Equal(t, "NNS", sym)
})
t.Run("BalanceOf", func(t *testing.T) {
b, err := c.NEP11BalanceOf(h, acc)
require.NoError(t, err)
require.EqualValues(t, 1, b)
})
t.Run("OwnerOf", func(t *testing.T) {
b, err := c.NEP11NDOwnerOf(h, "neo.com")
require.NoError(t, err)
require.EqualValues(t, acc, b)
})
t.Run("Properties", func(t *testing.T) {
p, err := c.NEP11Properties(h, "neo.com")
require.NoError(t, err)
blockRegisterDomain, err := chain.GetBlock(chain.GetHeaderHash(13)) // `neo.com` domain was registered in 13th block
require.NoError(t, err)
require.Equal(t, 1, len(blockRegisterDomain.Transactions))
expected := stackitem.NewMap()
expected.Add(stackitem.Make([]byte("name")), stackitem.Make([]byte("neo.com")))
expected.Add(stackitem.Make([]byte("expiration")), stackitem.Make(blockRegisterDomain.Timestamp/1000+365*24*3600)) // expiration formula
require.EqualValues(t, expected, p)
})
t.Run("Transfer", func(t *testing.T) {
_, err := c.TransferNEP11(wallet.NewAccountFromPrivateKey(testchain.PrivateKeyByID(0)), testchain.PrivateKeyByID(1).GetScriptHash(), h, "neo.com", 0)
require.NoError(t, err)
})
}