2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2020-03-06 11:56:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2022-07-25 17:04:43 +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"
|
2020-03-06 11:56:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-03-06 13:20:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-03-06 11:56:55 +00:00
|
|
|
)
|
|
|
|
|
2021-04-16 10:54:23 +00:00
|
|
|
// TransferTarget represents target address, token amount and data for transfer.
|
2020-08-04 07:35:47 +00:00
|
|
|
type TransferTarget struct {
|
|
|
|
Token util.Uint160
|
2020-07-24 13:34:46 +00:00
|
|
|
Address util.Uint160
|
|
|
|
Amount int64
|
2021-04-16 10:54:23 +00:00
|
|
|
Data interface{}
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 12:43:09 +00:00
|
|
|
// SignerAccount represents combination of the transaction.Signer and the
|
|
|
|
// corresponding wallet.Account.
|
|
|
|
type SignerAccount struct {
|
|
|
|
Signer transaction.Signer
|
|
|
|
Account *wallet.Account
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP17Decimals invokes `decimals` NEP-17 method on the specified contract.
|
2022-08-12 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package, this method will be removed in future
|
|
|
|
// versions.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (c *Client) NEP17Decimals(tokenHash util.Uint160) (int64, error) {
|
2021-03-23 19:04:34 +00:00
|
|
|
return c.nepDecimals(tokenHash)
|
2020-03-06 11:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP17Symbol invokes `symbol` NEP-17 method on the specified contract.
|
2022-08-12 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package, this method will be removed in future
|
|
|
|
// versions.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (c *Client) NEP17Symbol(tokenHash util.Uint160) (string, error) {
|
2021-03-23 19:04:34 +00:00
|
|
|
return c.nepSymbol(tokenHash)
|
2020-03-06 11:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP17TotalSupply invokes `totalSupply` NEP-17 method on the specified contract.
|
2022-08-12 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package, this method will be removed in future
|
|
|
|
// versions. This method is also wrong since tokens can return values overflowing
|
|
|
|
// int64.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (c *Client) NEP17TotalSupply(tokenHash util.Uint160) (int64, error) {
|
2021-03-23 19:04:34 +00:00
|
|
|
return c.nepTotalSupply(tokenHash)
|
2020-03-06 11:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEP17BalanceOf invokes `balanceOf` NEP-17 method on the specified contract.
|
2022-08-12 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package, this method will be removed in future
|
|
|
|
// versions. This method is also wrong since tokens can return values overflowing
|
|
|
|
// int64.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (c *Client) NEP17BalanceOf(tokenHash, acc util.Uint160) (int64, error) {
|
2021-03-23 19:04:34 +00:00
|
|
|
return c.nepBalanceOf(tokenHash, acc, nil)
|
2020-03-06 11:56:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP17TokenInfo returns full NEP-17 token info.
|
2022-08-26 19:56:18 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use Info method from the neptoken subpackage. This method
|
|
|
|
// will be removed in future versions.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (c *Client) NEP17TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
|
2021-04-23 14:32:48 +00:00
|
|
|
return c.nepTokenInfo(tokenHash, manifest.NEP17StandardName)
|
2020-03-06 13:20:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// CreateNEP17TransferTx creates an invocation transaction for the 'transfer'
|
2022-04-20 18:30:09 +00:00
|
|
|
// method of the given contract (token) to move the specified amount of NEP-17 assets
|
|
|
|
// (in FixedN format using contract's number of decimals) to the given account and
|
2020-06-01 18:46:01 +00:00
|
|
|
// returns it. The returned transaction is not signed.
|
2022-08-12 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package, this method will be removed in future
|
|
|
|
// versions.
|
2021-04-21 09:43:30 +00:00
|
|
|
func (c *Client) CreateNEP17TransferTx(acc *wallet.Account, to util.Uint160,
|
|
|
|
token util.Uint160, amount int64, gas int64, data interface{}, cosigners []SignerAccount) (*transaction.Transaction, error) {
|
2021-02-09 08:12:44 +00:00
|
|
|
return c.CreateNEP17MultiTransferTx(acc, gas, []TransferTarget{
|
|
|
|
{Token: token,
|
|
|
|
Address: to,
|
|
|
|
Amount: amount,
|
2021-04-16 10:54:23 +00:00
|
|
|
Data: data,
|
2021-02-09 08:12:44 +00:00
|
|
|
},
|
2021-04-21 09:43:30 +00:00
|
|
|
}, cosigners)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 09:43:30 +00:00
|
|
|
// CreateNEP17MultiTransferTx creates an invocation transaction for performing
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP-17 transfers from a single sender to multiple recipients with the given
|
2022-04-20 18:30:09 +00:00
|
|
|
// data and cosigners. The transaction sender is included with the CalledByEntry
|
2021-04-21 09:43:30 +00:00
|
|
|
// scope by default.
|
2022-08-15 07:55:13 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package (when transferring the same token) or
|
|
|
|
// [smartcontract.Builder] (when transferring multiple tokens), this method will
|
|
|
|
// be removed in future versions.
|
2021-04-21 09:43:30 +00:00
|
|
|
func (c *Client) CreateNEP17MultiTransferTx(acc *wallet.Account, gas int64,
|
|
|
|
recipients []TransferTarget, cosigners []SignerAccount) (*transaction.Transaction, error) {
|
2020-03-16 14:48:57 +00:00
|
|
|
from, err := address.StringToUint160(acc.Address)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("bad account address: %w", err)
|
2020-03-16 14:48:57 +00:00
|
|
|
}
|
2022-07-25 17:04:43 +00:00
|
|
|
b := smartcontract.NewBuilder()
|
2020-08-14 09:16:24 +00:00
|
|
|
for i := range recipients {
|
2022-07-25 17:04:43 +00:00
|
|
|
b.InvokeWithAssert(recipients[i].Token, "transfer",
|
2021-04-16 10:54:23 +00:00
|
|
|
from, recipients[i].Address, recipients[i].Amount, recipients[i].Data)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2022-07-25 17:04:43 +00:00
|
|
|
script, err := b.Script()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create transfer script: %w", err)
|
2021-02-09 08:12:44 +00:00
|
|
|
}
|
2022-07-25 17:04:43 +00:00
|
|
|
return c.CreateTxFromScript(script, acc, -1, gas, append([]SignerAccount{{
|
2021-03-02 12:43:09 +00:00
|
|
|
Signer: transaction.Signer{
|
2021-03-23 19:05:14 +00:00
|
|
|
Account: from,
|
2021-03-02 12:43:09 +00:00
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
2021-04-21 09:43:30 +00:00
|
|
|
}}, cosigners...))
|
2020-08-04 09:35:04 +00:00
|
|
|
}
|
2020-03-16 14:48:57 +00:00
|
|
|
|
2020-08-04 09:35:04 +00:00
|
|
|
// CreateTxFromScript creates transaction and properly sets cosigners and NetworkFee.
|
2020-10-14 15:13:20 +00:00
|
|
|
// If sysFee <= 0, it is determined via result of `invokescript` RPC. You should
|
|
|
|
// initialize network magic with Init before calling CreateTxFromScript.
|
2022-08-07 19:21:03 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use actor.Actor API, this method will be removed in future
|
|
|
|
// versions.
|
2020-08-17 13:49:47 +00:00
|
|
|
func (c *Client) CreateTxFromScript(script []byte, acc *wallet.Account, sysFee, netFee int64,
|
2021-03-02 12:43:09 +00:00
|
|
|
cosigners []SignerAccount) (*transaction.Transaction, error) {
|
|
|
|
signers, accounts, err := getSigners(acc, cosigners)
|
2020-08-04 09:35:04 +00:00
|
|
|
if err != nil {
|
2021-03-02 12:43:09 +00:00
|
|
|
return nil, fmt.Errorf("failed to construct tx signers: %w", err)
|
2020-08-04 09:35:04 +00:00
|
|
|
}
|
2020-08-17 13:49:47 +00:00
|
|
|
if sysFee < 0 {
|
|
|
|
result, err := c.InvokeScript(script, signers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't add system fee to transaction: %w", err)
|
|
|
|
}
|
2020-10-01 10:43:28 +00:00
|
|
|
if result.State != "HALT" {
|
2020-10-05 13:33:20 +00:00
|
|
|
return nil, fmt.Errorf("can't add system fee to transaction: bad vm state: %s due to an error: %s", result.State, result.FaultException)
|
2020-10-01 10:43:28 +00:00
|
|
|
}
|
2020-08-17 13:49:47 +00:00
|
|
|
sysFee = result.GasConsumed
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
2020-08-17 13:49:47 +00:00
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New(script, sysFee)
|
2020-08-17 13:49:47 +00:00
|
|
|
tx.Signers = signers
|
|
|
|
|
2020-04-17 10:52:23 +00:00
|
|
|
tx.ValidUntilBlock, err = c.CalculateValidUntilBlock()
|
|
|
|
if err != nil {
|
2020-08-17 13:49:47 +00:00
|
|
|
return nil, fmt.Errorf("failed to add validUntilBlock to transaction: %w", err)
|
2020-04-17 10:52:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 12:43:09 +00:00
|
|
|
err = c.AddNetworkFee(tx, netFee, accounts...)
|
2020-05-08 17:54:24 +00:00
|
|
|
if err != nil {
|
2020-08-17 13:49:47 +00:00
|
|
|
return nil, fmt.Errorf("failed to add network fee: %w", err)
|
2020-06-01 18:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// TransferNEP17 creates an invocation transaction that invokes 'transfer' method
|
2022-04-20 18:30:09 +00:00
|
|
|
// on the given token to move the specified amount of NEP-17 assets (in FixedN format
|
|
|
|
// using contract's number of decimals) to the given account with the data specified and
|
2021-04-21 09:43:30 +00:00
|
|
|
// sends it to the network returning just a hash of it. Cosigners argument
|
|
|
|
// specifies a set of the transaction cosigners (may be nil or may include sender)
|
2022-04-20 18:30:09 +00:00
|
|
|
// with a proper scope and the accounts to cosign the transaction. If cosigning is
|
2021-04-21 09:43:30 +00:00
|
|
|
// impossible (e.g. due to locked cosigner's account) an error is returned.
|
2022-08-12 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package, this method will be removed in future
|
|
|
|
// versions.
|
2021-04-21 09:43:30 +00:00
|
|
|
func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160,
|
|
|
|
amount int64, gas int64, data interface{}, cosigners []SignerAccount) (util.Uint256, error) {
|
|
|
|
tx, err := c.CreateNEP17TransferTx(acc, to, token, amount, gas, data, cosigners)
|
2020-06-01 18:46:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 09:43:30 +00:00
|
|
|
return c.SignAndPushTx(tx, acc, cosigners)
|
2020-03-16 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients.
|
2022-08-15 07:55:13 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nep17 package (when transferring the same token) or
|
|
|
|
// [smartcontract.Builder] (when transferring multiple tokens), this method will
|
|
|
|
// be removed in future versions.
|
2021-04-21 09:43:30 +00:00
|
|
|
func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients []TransferTarget, cosigners []SignerAccount) (util.Uint256, error) {
|
|
|
|
tx, err := c.CreateNEP17MultiTransferTx(acc, gas, recipients, cosigners)
|
2020-07-24 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
|
|
|
}
|
|
|
|
|
2021-04-21 09:43:30 +00:00
|
|
|
return c.SignAndPushTx(tx, acc, cosigners)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|