2020-03-06 11:56:55 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-03-16 14:48:57 +00:00
|
|
|
"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"
|
2020-03-06 11:56:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-03-16 14:48:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-07-31 12:26:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-06 13:20:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-03-06 11:56:55 +00:00
|
|
|
)
|
|
|
|
|
2020-08-04 07:35:47 +00:00
|
|
|
// TransferTarget represents target address and token amount for transfer.
|
|
|
|
type TransferTarget struct {
|
|
|
|
Token util.Uint160
|
2020-07-24 13:34:46 +00:00
|
|
|
Address util.Uint160
|
|
|
|
Amount int64
|
|
|
|
}
|
|
|
|
|
2020-06-25 08:40:22 +00:00
|
|
|
var (
|
|
|
|
// NeoContractHash is a hash of the NEO native contract.
|
2020-07-22 16:03:05 +00:00
|
|
|
NeoContractHash, _ = util.Uint160DecodeStringBE("25059ecb4878d3a875f91c51ceded330d4575fde")
|
2020-06-25 08:40:22 +00:00
|
|
|
// GasContractHash is a hash of the GAS native contract.
|
2020-07-22 16:03:05 +00:00
|
|
|
GasContractHash, _ = util.Uint160DecodeStringBE("bcaf41d684c7d4ad6ee0d99da9707b9d1f0c8e66")
|
2020-06-25 08:40:22 +00:00
|
|
|
)
|
|
|
|
|
2020-03-06 11:56:55 +00:00
|
|
|
// NEP5Decimals invokes `decimals` NEP5 method on a specified contract.
|
|
|
|
func (c *Client) NEP5Decimals(tokenHash util.Uint160) (int64, error) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(tokenHash, "decimals", []smartcontract.Parameter{}, nil)
|
2020-03-06 11:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if result.State != "HALT" || len(result.Stack) == 0 {
|
|
|
|
return 0, errors.New("invalid VM state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEP5Name invokes `name` NEP5 method on a specified contract.
|
|
|
|
func (c *Client) NEP5Name(tokenHash util.Uint160) (string, error) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(tokenHash, "name", []smartcontract.Parameter{}, nil)
|
2020-03-06 11:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if result.State != "HALT" || len(result.Stack) == 0 {
|
|
|
|
return "", errors.New("invalid VM state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return topStringFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEP5Symbol invokes `symbol` NEP5 method on a specified contract.
|
|
|
|
func (c *Client) NEP5Symbol(tokenHash util.Uint160) (string, error) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(tokenHash, "symbol", []smartcontract.Parameter{}, nil)
|
2020-03-06 11:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if result.State != "HALT" || len(result.Stack) == 0 {
|
|
|
|
return "", errors.New("invalid VM state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return topStringFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEP5TotalSupply invokes `totalSupply` NEP5 method on a specified contract.
|
|
|
|
func (c *Client) NEP5TotalSupply(tokenHash util.Uint160) (int64, error) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(tokenHash, "totalSupply", []smartcontract.Parameter{}, nil)
|
2020-03-06 11:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if result.State != "HALT" || len(result.Stack) == 0 {
|
|
|
|
return 0, errors.New("invalid VM state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEP5BalanceOf invokes `balanceOf` NEP5 method on a specified contract.
|
|
|
|
func (c *Client) NEP5BalanceOf(tokenHash util.Uint160) (int64, error) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(tokenHash, "balanceOf", []smartcontract.Parameter{}, nil)
|
2020-03-06 11:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if result.State != "HALT" || len(result.Stack) == 0 {
|
|
|
|
return 0, errors.New("invalid VM state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:20:23 +00:00
|
|
|
// NEP5TokenInfo returns full NEP5 token info.
|
|
|
|
func (c *Client) NEP5TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
|
|
|
|
name, err := c.NEP5Name(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
symbol, err := c.NEP5Symbol(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decimals, err := c.NEP5Decimals(tokenHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wallet.NewToken(tokenHash, name, symbol, decimals), nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:46:01 +00:00
|
|
|
// CreateNEP5TransferTx creates an invocation transaction for the 'transfer'
|
|
|
|
// method of a given contract (token) to move specified amount of NEP5 assets
|
|
|
|
// (in FixedN format using contract's number of decimals) to given account and
|
|
|
|
// returns it. The returned transaction is not signed.
|
2020-06-23 14:15:35 +00:00
|
|
|
func (c *Client) CreateNEP5TransferTx(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64) (*transaction.Transaction, error) {
|
2020-08-04 07:35:47 +00:00
|
|
|
return c.CreateNEP5MultiTransferTx(acc, gas, TransferTarget{
|
|
|
|
Token: token,
|
2020-07-24 13:34:46 +00:00
|
|
|
Address: to,
|
|
|
|
Amount: amount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNEP5MultiTransferTx creates an invocation transaction for performing NEP5 transfers
|
|
|
|
// from a single sender to multiple recepients.
|
2020-08-04 07:35:47 +00:00
|
|
|
func (c *Client) CreateNEP5MultiTransferTx(acc *wallet.Account, gas int64, recepients ...TransferTarget) (*transaction.Transaction, error) {
|
2020-03-16 14:48:57 +00:00
|
|
|
from, err := address.StringToUint160(acc.Address)
|
|
|
|
if err != nil {
|
2020-06-01 18:46:01 +00:00
|
|
|
return nil, fmt.Errorf("bad account address: %v", err)
|
2020-03-16 14:48:57 +00:00
|
|
|
}
|
|
|
|
w := io.NewBufBinWriter()
|
2020-07-24 13:34:46 +00:00
|
|
|
for i := range recepients {
|
2020-08-04 07:35:47 +00:00
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, recepients[i].Token, "transfer", from,
|
2020-07-24 13:34:46 +00:00
|
|
|
recepients[i].Address, recepients[i].Amount)
|
|
|
|
emit.Opcode(w.BinWriter, opcode.ASSERT)
|
|
|
|
}
|
2020-03-16 14:48:57 +00:00
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
script := w.Bytes()
|
2020-07-29 16:57:38 +00:00
|
|
|
result, err := c.InvokeScript(script, []transaction.Signer{
|
2020-06-11 08:45:17 +00:00
|
|
|
{
|
|
|
|
Account: from,
|
2020-07-06 08:03:21 +00:00
|
|
|
Scopes: transaction.CalledByEntry,
|
2020-06-11 08:45:17 +00:00
|
|
|
},
|
|
|
|
})
|
2020-05-08 17:54:24 +00:00
|
|
|
if err != nil {
|
2020-06-01 18:46:01 +00:00
|
|
|
return nil, fmt.Errorf("can't add system fee to transaction: %v", err)
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
2020-07-06 07:49:24 +00:00
|
|
|
tx := transaction.New(c.opts.Network, script, result.GasConsumed)
|
2020-07-29 16:57:38 +00:00
|
|
|
tx.Signers = []transaction.Signer{
|
2020-07-06 07:49:24 +00:00
|
|
|
{
|
|
|
|
Account: from,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
2020-04-17 10:52:23 +00:00
|
|
|
tx.ValidUntilBlock, err = c.CalculateValidUntilBlock()
|
|
|
|
if err != nil {
|
2020-06-01 18:46:01 +00:00
|
|
|
return nil, fmt.Errorf("can't calculate validUntilBlock: %v", err)
|
2020-04-17 10:52:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 07:49:24 +00:00
|
|
|
err = c.AddNetworkFee(tx, gas, acc)
|
2020-05-08 17:54:24 +00:00
|
|
|
if err != nil {
|
2020-06-01 18:46:01 +00:00
|
|
|
return nil, fmt.Errorf("can't add network fee to transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransferNEP5 creates an invocation transaction that invokes 'transfer' method
|
|
|
|
// on a given token to move specified amount of NEP5 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.
|
2020-06-23 14:15:35 +00:00
|
|
|
func (c *Client) TransferNEP5(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64) (util.Uint256, error) {
|
2020-06-01 18:46:01 +00:00
|
|
|
tx, err := c.CreateNEP5TransferTx(acc, to, token, amount, gas)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 14:48:57 +00:00
|
|
|
if err := acc.SignTx(tx); err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("can't sign tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-07-21 07:31:45 +00:00
|
|
|
return c.SendRawTransaction(tx)
|
2020-03-16 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:34:46 +00:00
|
|
|
// MultiTransferNEP5 is similar to TransferNEP5, buf allows to have multiple recepients.
|
2020-08-04 07:35:47 +00:00
|
|
|
func (c *Client) MultiTransferNEP5(acc *wallet.Account, gas int64, recepients ...TransferTarget) (util.Uint256, error) {
|
|
|
|
tx, err := c.CreateNEP5MultiTransferTx(acc, gas, recepients...)
|
2020-07-24 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.SignTx(tx); err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("can't sign tx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendRawTransaction(tx)
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:26:28 +00:00
|
|
|
func topIntFromStack(st []stackitem.Item) (int64, error) {
|
2020-03-06 11:56:55 +00:00
|
|
|
index := len(st) - 1 // top stack element is last in the array
|
2020-07-31 12:26:28 +00:00
|
|
|
bi, err := st[index].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return bi.Int64(), nil
|
2020-03-06 11:56:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 12:26:28 +00:00
|
|
|
func topStringFromStack(st []stackitem.Item) (string, error) {
|
2020-03-06 11:56:55 +00:00
|
|
|
index := len(st) - 1 // top stack element is last in the array
|
2020-07-31 12:26:28 +00:00
|
|
|
bs, err := st[index].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(bs), nil
|
2020-03-06 11:56:55 +00:00
|
|
|
}
|