2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2021-03-23 19:04:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-06 13:55:51 +00:00
|
|
|
"github.com/google/uuid"
|
2022-08-01 12:27:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-03-23 19:04:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2021-03-23 19:04:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-04-23 14:32:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2021-03-23 19:04:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP11Decimals invokes `decimals` NEP-11 method on the specified contract.
|
2021-03-23 19:04:34 +00:00
|
|
|
func (c *Client) NEP11Decimals(tokenHash util.Uint160) (int64, error) {
|
|
|
|
return c.nepDecimals(tokenHash)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP11Symbol invokes `symbol` NEP-11 method on the specified contract.
|
2021-03-23 19:04:34 +00:00
|
|
|
func (c *Client) NEP11Symbol(tokenHash util.Uint160) (string, error) {
|
|
|
|
return c.nepSymbol(tokenHash)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP11TotalSupply invokes `totalSupply` NEP-11 method on the specified contract.
|
2021-03-23 19:04:34 +00:00
|
|
|
func (c *Client) NEP11TotalSupply(tokenHash util.Uint160) (int64, error) {
|
|
|
|
return c.nepTotalSupply(tokenHash)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP11BalanceOf invokes `balanceOf` NEP-11 method on the specified contract.
|
2021-03-23 19:04:34 +00:00
|
|
|
func (c *Client) NEP11BalanceOf(tokenHash, owner util.Uint160) (int64, error) {
|
|
|
|
return c.nepBalanceOf(tokenHash, owner, nil)
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP11TokenInfo returns full NEP-11 token info.
|
2021-04-22 14:44:09 +00:00
|
|
|
func (c *Client) NEP11TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
|
2021-04-23 14:32:48 +00:00
|
|
|
return c.nepTokenInfo(tokenHash, manifest.NEP11StandardName)
|
2021-04-22 14:44:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 19:04:34 +00:00
|
|
|
// TransferNEP11 creates an invocation transaction that invokes 'transfer' method
|
2022-04-20 18:30:09 +00:00
|
|
|
// on the given token to move the whole NEP-11 token with the specified token ID to
|
|
|
|
// the given account and sends it to the network returning just a hash of it.
|
2021-03-23 19:04:34 +00:00
|
|
|
func (c *Client) TransferNEP11(acc *wallet.Account, to util.Uint160,
|
2021-05-05 10:22:26 +00:00
|
|
|
tokenHash util.Uint160, tokenID string, data interface{}, gas int64, cosigners []SignerAccount) (util.Uint256, error) {
|
|
|
|
tx, err := c.CreateNEP11TransferTx(acc, tokenHash, gas, cosigners, to, tokenID, data)
|
2021-03-23 19:04:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
|
|
|
}
|
|
|
|
|
2021-04-21 14:39:18 +00:00
|
|
|
return c.SignAndPushTx(tx, acc, cosigners)
|
2021-03-23 19:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:06:36 +00:00
|
|
|
// CreateNEP11TransferTx creates an invocation transaction for the 'transfer'
|
2022-04-20 18:30:09 +00:00
|
|
|
// method of the given contract (token) to move the whole (or the specified amount
|
|
|
|
// of) NEP-11 token with the specified token ID to the given account and returns it.
|
2021-04-26 13:06:36 +00:00
|
|
|
// The returned transaction is not signed. CreateNEP11TransferTx is also a
|
|
|
|
// helper for TransferNEP11 and TransferNEP11D.
|
2021-05-05 10:22:26 +00:00
|
|
|
// `args` for TransferNEP11: to util.Uint160, tokenID string, data interface{};
|
|
|
|
// `args` for TransferNEP11D: from, to util.Uint160, amount int64, tokenID string, data interface{}.
|
2021-04-26 13:06:36 +00:00
|
|
|
func (c *Client) CreateNEP11TransferTx(acc *wallet.Account, tokenHash util.Uint160,
|
2021-04-21 14:39:18 +00:00
|
|
|
gas int64, cosigners []SignerAccount, args ...interface{}) (*transaction.Transaction, error) {
|
2022-07-25 17:04:43 +00:00
|
|
|
script, err := smartcontract.CreateCallWithAssertScript(tokenHash, "transfer", args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create NEP-11 transfer script: %w", err)
|
2021-03-23 19:04:34 +00:00
|
|
|
}
|
|
|
|
from, err := address.StringToUint160(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("bad account address: %w", err)
|
|
|
|
}
|
2022-07-25 17:04:43 +00:00
|
|
|
return c.CreateTxFromScript(script, acc, -1, gas, append([]SignerAccount{{
|
2021-03-23 19:04:34 +00:00
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: from,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
2021-04-21 14:39:18 +00:00
|
|
|
}}, cosigners...))
|
2021-03-23 19:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 13:55:51 +00:00
|
|
|
// NEP11TokensOf returns iterator over token IDs for the specified owner of the
|
|
|
|
// specified NFT token. First return value is the session ID, the second one is
|
|
|
|
// Iterator itself, the third one is an error. Use TraverseIterator method to
|
|
|
|
// traverse iterator values or TerminateSession to terminate opened iterator
|
|
|
|
// session. See TraverseIterator and TerminateSession documentation for more details.
|
|
|
|
func (c *Client) NEP11TokensOf(tokenHash util.Uint160, owner util.Uint160) (uuid.UUID, result.Iterator, error) {
|
2022-08-01 10:37:07 +00:00
|
|
|
res, err := c.reader.Call(tokenHash, "tokensOf", owner)
|
2022-07-06 13:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(res)
|
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
iter, err := topIteratorFromStack(res.Stack)
|
|
|
|
return res.Session, iter, err
|
|
|
|
}
|
|
|
|
|
2022-07-06 15:15:17 +00:00
|
|
|
// NEP11UnpackedTokensOf returns an array of token IDs for the specified owner of the specified NFT token
|
|
|
|
// (config.DefaultMaxIteratorResultItems at max). It differs from NEP11TokensOf in that no iterator session
|
|
|
|
// is used to retrieve values from iterator. Instead, unpacking VM script is created and invoked via
|
|
|
|
// `invokescript` JSON-RPC call.
|
2022-07-06 13:55:51 +00:00
|
|
|
func (c *Client) NEP11UnpackedTokensOf(tokenHash util.Uint160, owner util.Uint160) ([][]byte, error) {
|
2022-08-01 12:27:36 +00:00
|
|
|
result, err := c.reader.CallAndExpandIterator(tokenHash, "tokensOf", config.DefaultMaxIteratorResultItems, owner)
|
2021-04-28 14:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-09 08:55:07 +00:00
|
|
|
arr, err := topIterableFromStack(result.Stack, []byte{})
|
2021-04-28 14:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get token IDs from stack: %w", err)
|
|
|
|
}
|
2022-02-09 08:55:07 +00:00
|
|
|
ids := make([][]byte, len(arr))
|
2021-04-28 14:47:44 +00:00
|
|
|
for i := range ids {
|
2022-02-09 08:55:07 +00:00
|
|
|
ids[i] = arr[i].([]byte)
|
2021-04-28 14:47:44 +00:00
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:04:34 +00:00
|
|
|
// Non-divisible NFT methods section start.
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP11NDOwnerOf invokes `ownerOf` non-divisible NEP-11 method with the
|
|
|
|
// specified token ID on the specified contract.
|
2022-02-09 08:55:07 +00:00
|
|
|
func (c *Client) NEP11NDOwnerOf(tokenHash util.Uint160, tokenID []byte) (util.Uint160, error) {
|
2022-08-01 10:37:07 +00:00
|
|
|
result, err := c.reader.Call(tokenHash, "ownerOf", tokenID)
|
2021-03-23 19:04:34 +00:00
|
|
|
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'
|
2022-04-20 18:30:09 +00:00
|
|
|
// method on the given token to move the specified amount of divisible NEP-11 assets
|
|
|
|
// (in FixedN format using contract's number of decimals) to the given account and
|
2021-03-23 19:04:34 +00:00
|
|
|
// sends it to the network returning just a hash of it.
|
|
|
|
func (c *Client) TransferNEP11D(acc *wallet.Account, to util.Uint160,
|
2022-02-09 08:55:07 +00:00
|
|
|
tokenHash util.Uint160, amount int64, tokenID []byte, data interface{}, gas int64, cosigners []SignerAccount) (util.Uint256, error) {
|
2021-03-23 19:04:34 +00:00
|
|
|
from, err := address.StringToUint160(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("bad account address: %w", err)
|
|
|
|
}
|
2021-05-05 10:22:26 +00:00
|
|
|
tx, err := c.CreateNEP11TransferTx(acc, tokenHash, gas, cosigners, from, to, amount, tokenID, data)
|
2021-03-23 19:04:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
|
|
|
}
|
|
|
|
|
2021-04-21 14:39:18 +00:00
|
|
|
return c.SignAndPushTx(tx, acc, cosigners)
|
2021-03-23 19:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP11DBalanceOf invokes `balanceOf` divisible NEP-11 method on a
|
2021-03-23 19:04:34 +00:00
|
|
|
// specified contract.
|
2022-02-09 08:55:07 +00:00
|
|
|
func (c *Client) NEP11DBalanceOf(tokenHash, owner util.Uint160, tokenID []byte) (int64, error) {
|
|
|
|
return c.nepBalanceOf(tokenHash, owner, tokenID)
|
2021-03-23 19:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 13:55:51 +00:00
|
|
|
// NEP11DOwnerOf returns iterator over the specified NEP-11 divisible token owners. First return value
|
|
|
|
// is the session ID, the second one is Iterator itself, the third one is an error. Use TraverseIterator
|
|
|
|
// method to traverse iterator values or TerminateSession to terminate opened iterator session. See
|
|
|
|
// TraverseIterator and TerminateSession documentation for more details.
|
|
|
|
func (c *Client) NEP11DOwnerOf(tokenHash util.Uint160, tokenID []byte) (uuid.UUID, result.Iterator, error) {
|
2022-08-01 10:37:07 +00:00
|
|
|
res, err := c.reader.Call(tokenHash, "ownerOf", tokenID)
|
2022-07-06 13:55:51 +00:00
|
|
|
sessID := res.Session
|
|
|
|
if err != nil {
|
|
|
|
return sessID, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(res)
|
|
|
|
if err != nil {
|
|
|
|
return sessID, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
arr, err := topIteratorFromStack(res.Stack)
|
|
|
|
return sessID, arr, err
|
|
|
|
}
|
|
|
|
|
2022-07-06 15:15:17 +00:00
|
|
|
// NEP11DUnpackedOwnerOf returns list of the specified NEP-11 divisible token owners
|
|
|
|
// (config.DefaultMaxIteratorResultItems at max). It differs from NEP11DOwnerOf in that no
|
|
|
|
// iterator session is used to retrieve values from iterator. Instead, unpacking VM
|
|
|
|
// script is created and invoked via `invokescript` JSON-RPC call.
|
2022-07-06 13:55:51 +00:00
|
|
|
func (c *Client) NEP11DUnpackedOwnerOf(tokenHash util.Uint160, tokenID []byte) ([]util.Uint160, error) {
|
2022-08-01 12:27:36 +00:00
|
|
|
result, err := c.reader.CallAndExpandIterator(tokenHash, "ownerOf", config.DefaultMaxIteratorResultItems, tokenID)
|
2021-04-28 14:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, err := topIterableFromStack(result.Stack, util.Uint160{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get token IDs from stack: %w", err)
|
|
|
|
}
|
|
|
|
owners := make([]util.Uint160, len(arr))
|
|
|
|
for i := range owners {
|
|
|
|
owners[i] = arr[i].(util.Uint160)
|
|
|
|
}
|
|
|
|
return owners, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:04:34 +00:00
|
|
|
// Divisible NFT methods section end.
|
|
|
|
|
|
|
|
// Optional NFT methods section start.
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP11Properties invokes `properties` optional NEP-11 method on the
|
2021-03-23 19:04:34 +00:00
|
|
|
// specified contract.
|
2022-02-09 08:55:07 +00:00
|
|
|
func (c *Client) NEP11Properties(tokenHash util.Uint160, tokenID []byte) (*stackitem.Map, error) {
|
2022-08-01 10:37:07 +00:00
|
|
|
result, err := c.reader.Call(tokenHash, "properties", tokenID)
|
2021-03-23 19:04:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return topMapFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:55:51 +00:00
|
|
|
// NEP11Tokens returns iterator over the tokens minted by the contract. First return
|
|
|
|
// value is the session ID, the second one is Iterator itself, the third one is an
|
|
|
|
// error. Use TraverseIterator method to traverse iterator values or
|
|
|
|
// TerminateSession to terminate opened iterator session. See TraverseIterator and
|
|
|
|
// TerminateSession documentation for more details.
|
|
|
|
func (c *Client) NEP11Tokens(tokenHash util.Uint160) (uuid.UUID, result.Iterator, error) {
|
2022-08-01 10:37:07 +00:00
|
|
|
res, err := c.reader.Call(tokenHash, "tokens")
|
2022-07-06 13:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(res)
|
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, result.Iterator{}, err
|
|
|
|
}
|
|
|
|
iter, err := topIteratorFromStack(res.Stack)
|
|
|
|
return res.Session, iter, err
|
|
|
|
}
|
|
|
|
|
2022-07-06 15:15:17 +00:00
|
|
|
// NEP11UnpackedTokens returns list of the tokens minted by the contract
|
|
|
|
// (config.DefaultMaxIteratorResultItems at max). It differs from NEP11Tokens in that no
|
|
|
|
// iterator session is used to retrieve values from iterator. Instead, unpacking
|
|
|
|
// VM script is created and invoked via `invokescript` JSON-RPC call.
|
2022-07-06 13:55:51 +00:00
|
|
|
func (c *Client) NEP11UnpackedTokens(tokenHash util.Uint160) ([][]byte, error) {
|
2022-08-01 12:27:36 +00:00
|
|
|
result, err := c.reader.CallAndExpandIterator(tokenHash, "tokens", config.DefaultMaxIteratorResultItems)
|
2021-04-28 14:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-09 08:55:07 +00:00
|
|
|
arr, err := topIterableFromStack(result.Stack, []byte{})
|
2021-04-28 14:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get token IDs from stack: %w", err)
|
|
|
|
}
|
2022-02-09 08:55:07 +00:00
|
|
|
tokens := make([][]byte, len(arr))
|
2021-04-28 14:47:44 +00:00
|
|
|
for i := range tokens {
|
2022-02-09 08:55:07 +00:00
|
|
|
tokens[i] = arr[i].([]byte)
|
2021-04-28 14:47:44 +00:00
|
|
|
}
|
|
|
|
return tokens, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:04:34 +00:00
|
|
|
// Optional NFT methods section end.
|