2018-04-04 17:39:38 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-01-14 11:17:31 +00:00
|
|
|
"math/big"
|
2018-04-04 17:39:38 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2022-01-14 11:17:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2018-04-04 17:39:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-04-04 17:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-04-09 08:18:22 +00:00
|
|
|
// ErrInvalidSendTxArgs is returned when the structure of SendTxArgs is ambigious.
|
2018-08-16 11:37:53 +00:00
|
|
|
ErrInvalidSendTxArgs = errors.New("transaction arguments are invalid")
|
|
|
|
// ErrUnexpectedArgs is returned when args are of unexpected length.
|
2018-04-09 08:18:22 +00:00
|
|
|
ErrUnexpectedArgs = errors.New("unexpected args")
|
2019-08-29 08:06:22 +00:00
|
|
|
//ErrInvalidTxSender is returned when selected account is different than From field.
|
2018-08-16 11:37:53 +00:00
|
|
|
ErrInvalidTxSender = errors.New("transaction can only be send by its creator")
|
2019-08-29 08:06:22 +00:00
|
|
|
//ErrAccountDoesntExist is sent when provided sub-account is not stored in database.
|
|
|
|
ErrAccountDoesntExist = errors.New("account doesn't exist")
|
2018-04-04 17:39:38 +00:00
|
|
|
)
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// PendingNonceProvider provides information about nonces.
|
|
|
|
type PendingNonceProvider interface {
|
|
|
|
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
2018-04-04 17:39:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// GasCalculator provides methods for estimating and pricing gas.
|
|
|
|
type GasCalculator interface {
|
|
|
|
ethereum.GasEstimator
|
|
|
|
ethereum.GasPricer
|
2018-04-04 17:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTxArgs represents the arguments to submit a new transaction into the transaction pool.
|
|
|
|
// This struct is based on go-ethereum's type in internal/ethapi/api.go, but we have freedom
|
|
|
|
// over the exact layout of this struct.
|
|
|
|
type SendTxArgs struct {
|
2021-07-14 07:48:01 +00:00
|
|
|
From types.Address `json:"from"`
|
|
|
|
To *types.Address `json:"to"`
|
|
|
|
Gas *hexutil.Uint64 `json:"gas"`
|
|
|
|
GasPrice *hexutil.Big `json:"gasPrice"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
Nonce *hexutil.Uint64 `json:"nonce"`
|
|
|
|
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
|
|
|
|
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
|
2018-04-04 17:39:38 +00:00
|
|
|
// We keep both "input" and "data" for backward compatibility.
|
|
|
|
// "input" is a preferred field.
|
|
|
|
// see `vendor/github.com/ethereum/go-ethereum/internal/ethapi/api.go:1107`
|
2019-12-19 18:27:27 +00:00
|
|
|
Input types.HexBytes `json:"input"`
|
|
|
|
Data types.HexBytes `json:"data"`
|
2018-04-04 17:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid checks whether this structure is filled in correctly.
|
|
|
|
func (args SendTxArgs) Valid() bool {
|
|
|
|
// if at least one of the fields is empty, it is a valid struct
|
|
|
|
if isNilOrEmpty(args.Input) || isNilOrEmpty(args.Data) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only allow both fields to present if they have the same data
|
|
|
|
return bytes.Equal(args.Input, args.Data)
|
|
|
|
}
|
|
|
|
|
2021-07-14 07:48:01 +00:00
|
|
|
// IsDynamicFeeTx checks whether dynamic fee parameters are set for the tx
|
|
|
|
func (args SendTxArgs) IsDynamicFeeTx() bool {
|
|
|
|
return args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:39:38 +00:00
|
|
|
// GetInput returns either Input or Data field's value dependent on what is filled.
|
2019-12-19 18:27:27 +00:00
|
|
|
func (args SendTxArgs) GetInput() types.HexBytes {
|
2018-04-04 17:39:38 +00:00
|
|
|
if !isNilOrEmpty(args.Input) {
|
|
|
|
return args.Input
|
|
|
|
}
|
|
|
|
|
|
|
|
return args.Data
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:17:31 +00:00
|
|
|
func (args SendTxArgs) ToTransactOpts(signerFn bind.SignerFn) *bind.TransactOpts {
|
|
|
|
var gasFeeCap *big.Int
|
|
|
|
if args.MaxFeePerGas != nil {
|
|
|
|
gasFeeCap = (*big.Int)(args.MaxFeePerGas)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gasTipCap *big.Int
|
|
|
|
if args.MaxPriorityFeePerGas != nil {
|
|
|
|
gasTipCap = (*big.Int)(args.MaxPriorityFeePerGas)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nonce *big.Int
|
|
|
|
if args.Nonce != nil {
|
|
|
|
nonce = new(big.Int).SetUint64((uint64)(*args.Nonce))
|
|
|
|
}
|
|
|
|
|
|
|
|
var gasPrice *big.Int
|
|
|
|
if args.GasPrice != nil {
|
|
|
|
gasPrice = (*big.Int)(args.GasPrice)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gasLimit uint64
|
|
|
|
if args.Gas != nil {
|
|
|
|
gasLimit = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bind.TransactOpts{
|
|
|
|
From: common.Address(args.From),
|
|
|
|
Signer: signerFn,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
GasLimit: gasLimit,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
Nonce: nonce,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
func isNilOrEmpty(bytes types.HexBytes) bool {
|
2018-04-04 17:39:38 +00:00
|
|
|
return bytes == nil || len(bytes) == 0
|
|
|
|
}
|