forked from TrueCloudLab/neoneo-go
rpc: allow client to send nep17 transfer with data
This commit is contained in:
parent
59ad0e5e04
commit
7896ef0640
4 changed files with 29 additions and 18 deletions
|
@ -468,7 +468,7 @@ func transferNEP17(ctx *cli.Context) error {
|
||||||
func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recipients []client.TransferTarget) error {
|
func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recipients []client.TransferTarget) error {
|
||||||
gas := flags.Fixed8FromContext(ctx, "gas")
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
||||||
|
|
||||||
tx, err := c.CreateNEP17MultiTransferTx(acc, int64(gas), recipients...)
|
tx, err := c.CreateNEP17MultiTransferTx(acc, int64(gas), recipients, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,7 +239,7 @@ func claimGas(ctx *cli.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0)
|
hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,27 +104,38 @@ func (c *Client) NEP17TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
|
||||||
// method of a given contract (token) to move specified amount of NEP17 assets
|
// method of a given contract (token) to move specified amount of NEP17 assets
|
||||||
// (in FixedN format using contract's number of decimals) to given account and
|
// (in FixedN format using contract's number of decimals) to given account and
|
||||||
// returns it. The returned transaction is not signed.
|
// returns it. The returned transaction is not signed.
|
||||||
func (c *Client) CreateNEP17TransferTx(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64) (*transaction.Transaction, error) {
|
func (c *Client) CreateNEP17TransferTx(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64, data interface{}) (*transaction.Transaction, error) {
|
||||||
return c.CreateNEP17MultiTransferTx(acc, gas, TransferTarget{
|
return c.CreateNEP17MultiTransferTx(acc, gas, []TransferTarget{
|
||||||
Token: token,
|
{Token: token,
|
||||||
Address: to,
|
Address: to,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
})
|
},
|
||||||
|
}, []interface{}{data})
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateNEP17MultiTransferTx creates an invocation transaction for performing NEP17 transfers
|
// CreateNEP17MultiTransferTx creates an invocation transaction for performing NEP17 transfers
|
||||||
// from a single sender to multiple recipients.
|
// from a single sender to multiple recipients with the given data.
|
||||||
func (c *Client) CreateNEP17MultiTransferTx(acc *wallet.Account, gas int64, recipients ...TransferTarget) (*transaction.Transaction, error) {
|
func (c *Client) CreateNEP17MultiTransferTx(acc *wallet.Account, gas int64, recipients []TransferTarget, data []interface{}) (*transaction.Transaction, error) {
|
||||||
from, err := address.StringToUint160(acc.Address)
|
from, err := address.StringToUint160(acc.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("bad account address: %w", err)
|
return nil, fmt.Errorf("bad account address: %w", err)
|
||||||
}
|
}
|
||||||
|
if data == nil {
|
||||||
|
data = make([]interface{}, len(recipients))
|
||||||
|
} else {
|
||||||
|
if len(data) != len(recipients) {
|
||||||
|
return nil, fmt.Errorf("data and recipients number mismatch: %d vs %d", len(data), len(recipients))
|
||||||
|
}
|
||||||
|
}
|
||||||
w := io.NewBufBinWriter()
|
w := io.NewBufBinWriter()
|
||||||
for i := range recipients {
|
for i := range recipients {
|
||||||
emit.AppCall(w.BinWriter, recipients[i].Token, "transfer",
|
emit.AppCall(w.BinWriter, recipients[i].Token, "transfer",
|
||||||
callflag.WriteStates|callflag.AllowCall|callflag.AllowNotify, from, recipients[i].Address, recipients[i].Amount, nil)
|
callflag.WriteStates|callflag.AllowCall|callflag.AllowNotify, from, recipients[i].Address, recipients[i].Amount, data[i])
|
||||||
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
||||||
}
|
}
|
||||||
|
if w.Err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create transfer script: %w", w.Err)
|
||||||
|
}
|
||||||
accAddr, err := address.StringToUint160(acc.Address)
|
accAddr, err := address.StringToUint160(acc.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("bad account address: %v", err)
|
return nil, fmt.Errorf("bad account address: %v", err)
|
||||||
|
@ -178,10 +189,10 @@ func (c *Client) CreateTxFromScript(script []byte, acc *wallet.Account, sysFee,
|
||||||
|
|
||||||
// TransferNEP17 creates an invocation transaction that invokes 'transfer' method
|
// TransferNEP17 creates an invocation transaction that invokes 'transfer' method
|
||||||
// on a given token to move specified amount of NEP17 assets (in FixedN format
|
// on a given token to move specified amount of NEP17 assets (in FixedN format
|
||||||
// using contract's number of decimals) to given account and sends it to the
|
// using contract's number of decimals) to given account with data specified and
|
||||||
// network returning just a hash of it.
|
// sends it to the network returning just a hash of it.
|
||||||
func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64) (util.Uint256, error) {
|
func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64, data interface{}) (util.Uint256, error) {
|
||||||
tx, err := c.CreateNEP17TransferTx(acc, to, token, amount, gas)
|
tx, err := c.CreateNEP17TransferTx(acc, to, token, amount, gas, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.Uint256{}, err
|
return util.Uint256{}, err
|
||||||
}
|
}
|
||||||
|
@ -194,8 +205,8 @@ func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients.
|
// MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients.
|
||||||
func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients ...TransferTarget) (util.Uint256, error) {
|
func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients []TransferTarget, data []interface{}) (util.Uint256, error) {
|
||||||
tx, err := c.CreateNEP17MultiTransferTx(acc, gas, recipients...)
|
tx, err := c.CreateNEP17MultiTransferTx(acc, gas, recipients, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.Uint256{}, err
|
return util.Uint256{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -272,7 +272,7 @@ func TestCreateNEP17TransferTx(t *testing.T) {
|
||||||
gasContractHash, err := c.GetNativeContractHash(nativenames.Gas)
|
gasContractHash, err := c.GetNativeContractHash(nativenames.Gas)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0)
|
tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, acc.SignTx(tx))
|
require.NoError(t, acc.SignTx(tx))
|
||||||
require.NoError(t, chain.VerifyTx(tx))
|
require.NoError(t, chain.VerifyTx(tx))
|
||||||
|
|
Loading…
Reference in a new issue