From 966ad8a0b15279bb77eb7a051e86f8ffebca35c7 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 6 Jul 2020 10:49:24 +0300 Subject: [PATCH] rpc: fix adding extra network fee to tx --- pkg/rpc/client/nep5.go | 23 ++++++++--------------- pkg/rpc/client/rpc.go | 9 +++++---- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/pkg/rpc/client/nep5.go b/pkg/rpc/client/nep5.go index b8b84722d..e921871cc 100644 --- a/pkg/rpc/client/nep5.go +++ b/pkg/rpc/client/nep5.go @@ -115,17 +115,6 @@ func (c *Client) CreateNEP5TransferTx(acc *wallet.Account, to util.Uint160, toke emit.Opcode(w.BinWriter, opcode.ASSERT) script := w.Bytes() - tx := transaction.New(c.opts.Network, script, gas) - tx.Sender = from - tx.Cosigners = []transaction.Cosigner{ - { - Account: from, - Scopes: transaction.CalledByEntry, - AllowedContracts: nil, - AllowedGroups: nil, - }, - } - result, err := c.InvokeScript(script, []transaction.Cosigner{ { Account: from, @@ -135,16 +124,20 @@ func (c *Client) CreateNEP5TransferTx(acc *wallet.Account, to util.Uint160, toke if err != nil { return nil, fmt.Errorf("can't add system fee to transaction: %v", err) } - if result.GasConsumed > 0 { - tx.SystemFee = result.GasConsumed + tx := transaction.New(c.opts.Network, script, result.GasConsumed) + tx.Sender = from + tx.Cosigners = []transaction.Cosigner{ + { + Account: from, + Scopes: transaction.CalledByEntry, + }, } - tx.ValidUntilBlock, err = c.CalculateValidUntilBlock() if err != nil { return nil, fmt.Errorf("can't calculate validUntilBlock: %v", err) } - err = c.AddNetworkFee(tx, acc) + err = c.AddNetworkFee(tx, gas, acc) if err != nil { return nil, fmt.Errorf("can't add network fee to transaction: %v", err) } diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index 0bb161b96..d734d1104 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -449,7 +449,7 @@ func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sys } tx.Sender = addr - err = c.AddNetworkFee(tx, acc) + err = c.AddNetworkFee(tx, int64(netfee), acc) if err != nil { return txHash, errors.Wrapf(err, "failed to add network fee") } @@ -512,8 +512,9 @@ func (c *Client) CalculateValidUntilBlock() (uint32, error) { return blockCount + validatorsCount, nil } -// AddNetworkFee adds network fee for each witness script to transaction. -func (c *Client) AddNetworkFee(tx *transaction.Transaction, acc *wallet.Account) error { +// AddNetworkFee adds network fee for each witness script and optional extra +// network fee to transaction. +func (c *Client) AddNetworkFee(tx *transaction.Transaction, extraFee int64, acc *wallet.Account) error { size := io.GetVarSize(tx) if acc.Contract != nil { netFee, sizeDelta := core.CalculateNetworkFee(acc.Contract.Script) @@ -540,6 +541,6 @@ func (c *Client) AddNetworkFee(tx *transaction.Transaction, acc *wallet.Account) if err != nil { return err } - tx.NetworkFee += int64(size) * fee + tx.NetworkFee += int64(size)*fee + extraFee return nil }