rpc: fix adding extra network fee to tx

This commit is contained in:
Anna Shaleva 2020-07-06 10:49:24 +03:00
parent 3c551788c9
commit 966ad8a0b1
2 changed files with 13 additions and 19 deletions

View file

@ -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)
}

View file

@ -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
}