2018-04-09 08:18:22 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
2018-08-16 11:37:53 +00:00
|
|
|
"bytes"
|
2018-04-09 08:18:22 +00:00
|
|
|
"context"
|
2019-03-01 13:49:30 +00:00
|
|
|
"errors"
|
2019-02-15 11:31:20 +00:00
|
|
|
"fmt"
|
2018-04-09 08:18:22 +00:00
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-21 09:53:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-11-23 17:57:05 +00:00
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
2018-04-09 08:18:22 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-11-02 11:35:28 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-04-09 08:18:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sendTxTimeout defines how many seconds to wait before returning result in sentTransaction().
|
2018-04-10 10:02:54 +00:00
|
|
|
sendTxTimeout = 300 * time.Second
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
defaultGas = 90000
|
2019-03-01 13:49:30 +00:00
|
|
|
|
2023-09-29 17:56:27 +00:00
|
|
|
ValidSignatureSize = 65
|
2018-04-09 08:18:22 +00:00
|
|
|
)
|
|
|
|
|
2019-03-01 13:49:30 +00:00
|
|
|
// ErrInvalidSignatureSize is returned if a signature is not 65 bytes to avoid panic from go-ethereum
|
|
|
|
var ErrInvalidSignatureSize = errors.New("signature size must be 65")
|
|
|
|
|
2019-02-15 11:31:20 +00:00
|
|
|
type ErrBadNonce struct {
|
2019-02-21 09:53:39 +00:00
|
|
|
nonce uint64
|
|
|
|
expectedNonce uint64
|
2019-02-15 11:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrBadNonce) Error() string {
|
2019-02-21 09:53:39 +00:00
|
|
|
return fmt.Sprintf("bad nonce. expected %d, got %d", e.expectedNonce, e.nonce)
|
2019-02-15 11:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// Transactor validates, signs transactions.
|
|
|
|
// It uses upstream to propagate transactions to the Ethereum network.
|
|
|
|
type Transactor struct {
|
2022-06-09 13:09:56 +00:00
|
|
|
rpcWrapper *rpcWrapper
|
|
|
|
sendTxTimeout time.Duration
|
|
|
|
rpcCallTimeout time.Duration
|
|
|
|
networkID uint64
|
2022-12-19 12:37:37 +00:00
|
|
|
nonce *Nonce
|
|
|
|
log log.Logger
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransactor returns a new Manager.
|
2018-08-16 11:37:53 +00:00
|
|
|
func NewTransactor() *Transactor {
|
2018-04-09 08:18:22 +00:00
|
|
|
return &Transactor{
|
2018-08-16 11:37:53 +00:00
|
|
|
sendTxTimeout: sendTxTimeout,
|
2022-12-19 12:37:37 +00:00
|
|
|
nonce: NewNonce(),
|
2018-08-16 11:37:53 +00:00
|
|
|
log: log.New("package", "status-go/transactions.Manager"),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNetworkID selects a correct network.
|
|
|
|
func (t *Transactor) SetNetworkID(networkID uint64) {
|
|
|
|
t.networkID = networkID
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:09:55 +00:00
|
|
|
func (t *Transactor) NetworkID() uint64 {
|
|
|
|
return t.networkID
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// SetRPC sets RPC params, a client and a timeout
|
|
|
|
func (t *Transactor) SetRPC(rpcClient *rpc.Client, timeout time.Duration) {
|
2022-06-09 13:09:56 +00:00
|
|
|
t.rpcWrapper = newRPCWrapper(rpcClient, rpcClient.UpstreamChainID)
|
2018-04-10 10:02:54 +00:00
|
|
|
t.rpcCallTimeout = timeout
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 09:26:02 +00:00
|
|
|
func (t *Transactor) NextNonce(rpcClient *rpc.Client, chainID uint64, from types.Address) (uint64, UnlockNonceFunc, error) {
|
2022-12-19 12:37:37 +00:00
|
|
|
wrapper := newRPCWrapper(rpcClient, chainID)
|
|
|
|
return t.nonce.Next(wrapper, from)
|
|
|
|
}
|
2023-11-06 09:26:02 +00:00
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
func (t *Transactor) EstimateGas(network *params.Network, from common.Address, to common.Address, value *big.Int, input []byte) (uint64, error) {
|
|
|
|
rpcWrapper := newRPCWrapper(t.rpcWrapper.RPCClient, network.ChainID)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: from,
|
|
|
|
To: &to,
|
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcWrapper.EstimateGas(ctx, msg)
|
|
|
|
}
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// SendTransaction is an implementation of eth_sendTransaction. It queues the tx to the sign queue.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (t *Transactor) SendTransaction(sendArgs SendTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
|
2022-06-09 13:09:56 +00:00
|
|
|
hash, err = t.validateAndPropagate(t.rpcWrapper, verifiedAccount, sendArgs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) SendTransactionWithChainID(chainID uint64, sendArgs SendTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
|
|
|
|
wrapper := newRPCWrapper(t.rpcWrapper.RPCClient, chainID)
|
|
|
|
hash, err = t.validateAndPropagate(wrapper, verifiedAccount, sendArgs)
|
2018-08-16 11:37:53 +00:00
|
|
|
return
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 09:26:02 +00:00
|
|
|
func (t *Transactor) ValidateAndBuildTransaction(chainID uint64, sendArgs SendTxArgs) (tx *gethtypes.Transaction, unlock UnlockNonceFunc, err error) {
|
2023-09-29 17:56:27 +00:00
|
|
|
wrapper := newRPCWrapper(t.rpcWrapper.RPCClient, chainID)
|
2023-11-06 09:26:02 +00:00
|
|
|
tx, unlock, err = t.validateAndBuildTransaction(wrapper, sendArgs)
|
2023-09-29 17:56:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:39:36 +00:00
|
|
|
func (t *Transactor) AddSignatureToTransaction(chainID uint64, tx *gethtypes.Transaction, sig []byte) (*gethtypes.Transaction, error) {
|
2023-09-29 17:56:27 +00:00
|
|
|
if len(sig) != ValidSignatureSize {
|
2023-11-24 15:39:36 +00:00
|
|
|
return nil, ErrInvalidSignatureSize
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpcWrapper := newRPCWrapper(t.rpcWrapper.RPCClient, chainID)
|
|
|
|
chID := big.NewInt(int64(rpcWrapper.chainID))
|
|
|
|
|
|
|
|
signer := gethtypes.NewLondonSigner(chID)
|
2023-11-24 15:39:36 +00:00
|
|
|
txWithSignature, err := tx.WithSignature(signer, sig)
|
2023-09-29 17:56:27 +00:00
|
|
|
if err != nil {
|
2023-11-24 15:39:36 +00:00
|
|
|
return nil, err
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 15:39:36 +00:00
|
|
|
return txWithSignature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) SendRawTransaction(chainID uint64, rawTx string) error {
|
|
|
|
rpcWrapper := newRPCWrapper(t.rpcWrapper.RPCClient, chainID)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return rpcWrapper.SendRawTransaction(ctx, rawTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) SendTransactionWithSignature(tx *gethtypes.Transaction) (hash types.Hash, err error) {
|
|
|
|
rpcWrapper := newRPCWrapper(t.rpcWrapper.RPCClient, tx.ChainId().Uint64())
|
|
|
|
|
2023-09-29 17:56:27 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-11-24 15:39:36 +00:00
|
|
|
if err := rpcWrapper.SendTransaction(ctx, tx); err != nil {
|
2023-09-29 17:56:27 +00:00
|
|
|
return hash, err
|
|
|
|
}
|
2023-11-24 15:39:36 +00:00
|
|
|
return types.Hash(tx.Hash()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) AddSignatureToTransactionAndSend(chainID uint64, tx *gethtypes.Transaction, sig []byte) (hash types.Hash, err error) {
|
|
|
|
txWithSignature, err := t.AddSignatureToTransaction(chainID, tx, sig)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.SendTransactionWithSignature(txWithSignature)
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 15:39:36 +00:00
|
|
|
// BuildTransactionAndSendWithSignature receive a transaction and a signature, serialize them together and propage it to the network.
|
2019-02-15 11:31:20 +00:00
|
|
|
// It's different from eth_sendRawTransaction because it receives a signature and not a serialized transaction with signature.
|
|
|
|
// Since the transactions is already signed, we assume it was validated and used the right nonce.
|
2023-11-24 15:39:36 +00:00
|
|
|
func (t *Transactor) BuildTransactionAndSendWithSignature(chainID uint64, args SendTxArgs, sig []byte) (hash types.Hash, err error) {
|
2019-02-15 11:31:20 +00:00
|
|
|
if !args.Valid() {
|
|
|
|
return hash, ErrInvalidSendTxArgs
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:56:27 +00:00
|
|
|
if len(sig) != ValidSignatureSize {
|
2019-03-01 13:49:30 +00:00
|
|
|
return hash, ErrInvalidSignatureSize
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
tx := t.buildTransaction(args)
|
2023-09-29 17:56:27 +00:00
|
|
|
rpcWrapper := newRPCWrapper(t.rpcWrapper.RPCClient, chainID)
|
|
|
|
expectedNonce, unlock, err := t.nonce.Next(rpcWrapper, args.From)
|
2019-02-15 11:31:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
2023-11-17 11:38:12 +00:00
|
|
|
if unlock != nil {
|
|
|
|
defer func() {
|
|
|
|
unlock(err == nil, expectedNonce)
|
|
|
|
}()
|
|
|
|
}
|
2019-02-15 11:31:20 +00:00
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
if tx.Nonce() != expectedNonce {
|
|
|
|
return hash, &ErrBadNonce{tx.Nonce(), expectedNonce}
|
2019-02-15 11:31:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 17:59:23 +00:00
|
|
|
hash, err = t.AddSignatureToTransactionAndSend(chainID, tx, sig)
|
|
|
|
return hash, err
|
2019-02-15 11:31:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (t *Transactor) HashTransaction(args SendTxArgs) (validatedArgs SendTxArgs, hash types.Hash, err error) {
|
2019-02-21 09:53:39 +00:00
|
|
|
if !args.Valid() {
|
|
|
|
return validatedArgs, hash, ErrInvalidSendTxArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
validatedArgs = args
|
|
|
|
|
2022-12-19 12:37:37 +00:00
|
|
|
nonce, unlock, err := t.nonce.Next(t.rpcWrapper, args.From)
|
2019-02-21 09:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return validatedArgs, hash, err
|
|
|
|
}
|
2023-11-17 11:38:12 +00:00
|
|
|
if unlock != nil {
|
|
|
|
defer unlock(false, 0)
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
|
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
2021-08-10 08:48:11 +00:00
|
|
|
gasFeeCap := (*big.Int)(args.MaxFeePerGas)
|
|
|
|
gasTipCap := (*big.Int)(args.MaxPriorityFeePerGas)
|
|
|
|
if args.GasPrice == nil && args.MaxFeePerGas == nil {
|
2019-02-21 09:53:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2022-06-09 13:09:56 +00:00
|
|
|
gasPrice, err = t.rpcWrapper.SuggestGasPrice(ctx)
|
2019-02-21 09:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return validatedArgs, hash, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := big.NewInt(int64(t.networkID))
|
|
|
|
value := (*big.Int)(args.Value)
|
|
|
|
|
|
|
|
var gas uint64
|
|
|
|
if args.Gas == nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2019-12-19 18:27:27 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
gethTo common.Address
|
|
|
|
gethToPtr *common.Address
|
|
|
|
)
|
|
|
|
if args.To != nil {
|
|
|
|
gethTo = common.Address(*args.To)
|
|
|
|
gethToPtr = &gethTo
|
|
|
|
}
|
2021-08-10 08:48:11 +00:00
|
|
|
if args.GasPrice == nil {
|
2022-06-09 13:09:56 +00:00
|
|
|
gas, err = t.rpcWrapper.EstimateGas(ctx, ethereum.CallMsg{
|
2021-08-10 08:48:11 +00:00
|
|
|
From: common.Address(args.From),
|
|
|
|
To: gethToPtr,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
})
|
|
|
|
} else {
|
2022-06-09 13:09:56 +00:00
|
|
|
gas, err = t.rpcWrapper.EstimateGas(ctx, ethereum.CallMsg{
|
2021-08-10 08:48:11 +00:00
|
|
|
From: common.Address(args.From),
|
|
|
|
To: gethToPtr,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
})
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return validatedArgs, hash, err
|
|
|
|
}
|
|
|
|
if gas < defaultGas {
|
|
|
|
t.log.Info("default gas will be used because estimated is lower", "estimated", gas, "default", defaultGas)
|
|
|
|
gas = defaultGas
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
|
|
|
|
newNonce := hexutil.Uint64(nonce)
|
|
|
|
newGas := hexutil.Uint64(gas)
|
|
|
|
validatedArgs.Nonce = &newNonce
|
2021-08-10 08:48:11 +00:00
|
|
|
if args.GasPrice != nil {
|
|
|
|
validatedArgs.GasPrice = (*hexutil.Big)(gasPrice)
|
|
|
|
} else {
|
|
|
|
validatedArgs.MaxPriorityFeePerGas = (*hexutil.Big)(gasTipCap)
|
|
|
|
validatedArgs.MaxPriorityFeePerGas = (*hexutil.Big)(gasFeeCap)
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
validatedArgs.Gas = &newGas
|
|
|
|
|
|
|
|
tx := t.buildTransaction(validatedArgs)
|
2021-07-07 06:11:09 +00:00
|
|
|
hash = types.Hash(gethtypes.NewLondonSigner(chainID).Hash(tx))
|
2019-02-21 09:53:39 +00:00
|
|
|
|
|
|
|
return validatedArgs, hash, nil
|
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// make sure that only account which created the tx can complete it
|
|
|
|
func (t *Transactor) validateAccount(args SendTxArgs, selectedAccount *account.SelectedExtKey) error {
|
|
|
|
if selectedAccount == nil {
|
|
|
|
return account.ErrNoAccountSelected
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
if !bytes.Equal(args.From.Bytes(), selectedAccount.Address.Bytes()) {
|
|
|
|
return ErrInvalidTxSender
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 09:26:02 +00:00
|
|
|
func (t *Transactor) validateAndBuildTransaction(rpcWrapper *rpcWrapper, args SendTxArgs) (tx *gethtypes.Transaction, unlock UnlockNonceFunc, err error) {
|
2018-04-09 08:18:22 +00:00
|
|
|
if !args.Valid() {
|
2023-11-06 09:26:02 +00:00
|
|
|
return tx, nil, ErrInvalidSendTxArgs
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2023-11-17 11:38:12 +00:00
|
|
|
var nonce uint64
|
2022-12-19 12:37:37 +00:00
|
|
|
if args.Nonce != nil {
|
|
|
|
nonce = uint64(*args.Nonce)
|
2023-11-17 11:38:12 +00:00
|
|
|
} else {
|
|
|
|
nonce, unlock, err = t.nonce.Next(rpcWrapper, args.From)
|
|
|
|
if err != nil {
|
|
|
|
return tx, nil, err
|
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2023-11-17 11:38:12 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2020-11-20 08:57:22 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
2021-07-14 07:48:01 +00:00
|
|
|
if !args.IsDynamicFeeTx() && args.GasPrice == nil {
|
2022-06-09 13:09:56 +00:00
|
|
|
gasPrice, err = rpcWrapper.SuggestGasPrice(ctx)
|
2018-04-09 08:18:22 +00:00
|
|
|
if err != nil {
|
2023-11-06 09:26:02 +00:00
|
|
|
return tx, unlock, err
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
value := (*big.Int)(args.Value)
|
|
|
|
var gas uint64
|
2021-07-14 07:48:01 +00:00
|
|
|
if args.Gas != nil {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
} else if args.Gas == nil && !args.IsDynamicFeeTx() {
|
2018-04-09 08:18:22 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2019-12-19 18:27:27 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
gethTo common.Address
|
|
|
|
gethToPtr *common.Address
|
|
|
|
)
|
|
|
|
if args.To != nil {
|
|
|
|
gethTo = common.Address(*args.To)
|
|
|
|
gethToPtr = &gethTo
|
|
|
|
}
|
2022-06-09 13:09:56 +00:00
|
|
|
gas, err = rpcWrapper.EstimateGas(ctx, ethereum.CallMsg{
|
2019-12-19 18:27:27 +00:00
|
|
|
From: common.Address(args.From),
|
|
|
|
To: gethToPtr,
|
2018-04-09 08:18:22 +00:00
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-11-06 09:26:02 +00:00
|
|
|
return tx, unlock, err
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
if gas < defaultGas {
|
2018-08-16 11:37:53 +00:00
|
|
|
t.log.Info("default gas will be used because estimated is lower", "estimated", gas, "default", defaultGas)
|
2018-04-09 08:18:22 +00:00
|
|
|
gas = defaultGas
|
|
|
|
}
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
tx = t.buildTransactionWithOverrides(nonce, value, gas, gasPrice, args)
|
2023-11-06 09:26:02 +00:00
|
|
|
return tx, unlock, nil
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) validateAndPropagate(rpcWrapper *rpcWrapper, selectedAccount *account.SelectedExtKey, args SendTxArgs) (hash types.Hash, err error) {
|
|
|
|
if err = t.validateAccount(args, selectedAccount); err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2023-11-06 09:26:02 +00:00
|
|
|
tx, unlock, err := t.validateAndBuildTransaction(rpcWrapper, args)
|
|
|
|
defer func() {
|
|
|
|
if unlock != nil {
|
|
|
|
unlock(err == nil, tx.Nonce())
|
|
|
|
}
|
|
|
|
}()
|
2023-09-29 17:56:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := big.NewInt(int64(rpcWrapper.chainID))
|
2021-07-07 06:11:09 +00:00
|
|
|
signedTx, err := gethtypes.SignTx(tx, gethtypes.NewLondonSigner(chainID), selectedAccount.AccountKey.PrivateKey)
|
2018-04-09 08:18:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2018-06-06 07:35:32 +00:00
|
|
|
|
2022-06-09 13:09:56 +00:00
|
|
|
if err := rpcWrapper.SendTransaction(ctx, signedTx); err != nil {
|
2018-04-09 08:18:22 +00:00
|
|
|
return hash, err
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
return types.Hash(signedTx.Hash()), nil
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (t *Transactor) buildTransaction(args SendTxArgs) *gethtypes.Transaction {
|
2023-11-28 17:59:23 +00:00
|
|
|
var (
|
|
|
|
nonce uint64
|
|
|
|
value *big.Int
|
|
|
|
gas uint64
|
|
|
|
gasPrice *big.Int
|
|
|
|
)
|
|
|
|
if args.Nonce != nil {
|
|
|
|
nonce = uint64(*args.Nonce)
|
|
|
|
}
|
|
|
|
if args.Value != nil {
|
|
|
|
value = (*big.Int)(args.Value)
|
|
|
|
}
|
|
|
|
if args.Gas != nil {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
if args.GasPrice != nil {
|
|
|
|
gasPrice = (*big.Int)(args.GasPrice)
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
return t.buildTransactionWithOverrides(nonce, value, gas, gasPrice, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) buildTransactionWithOverrides(nonce uint64, value *big.Int, gas uint64, gasPrice *big.Int, args SendTxArgs) *gethtypes.Transaction {
|
2019-11-23 17:57:05 +00:00
|
|
|
var tx *gethtypes.Transaction
|
2019-02-21 09:53:39 +00:00
|
|
|
|
|
|
|
if args.To != nil {
|
2021-07-14 07:48:01 +00:00
|
|
|
to := common.Address(*args.To)
|
|
|
|
var txData gethtypes.TxData
|
|
|
|
|
|
|
|
if args.IsDynamicFeeTx() {
|
|
|
|
gasTipCap := (*big.Int)(args.MaxPriorityFeePerGas)
|
|
|
|
gasFeeCap := (*big.Int)(args.MaxFeePerGas)
|
|
|
|
|
|
|
|
txData = &gethtypes.DynamicFeeTx{
|
|
|
|
Nonce: nonce,
|
|
|
|
Gas: gas,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
To: &to,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
txData = &gethtypes.LegacyTx{
|
|
|
|
Nonce: nonce,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Gas: gas,
|
|
|
|
To: &to,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx = gethtypes.NewTx(txData)
|
2019-02-21 09:53:39 +00:00
|
|
|
t.logNewTx(args, gas, gasPrice, value)
|
|
|
|
} else {
|
2021-08-19 10:28:58 +00:00
|
|
|
if args.IsDynamicFeeTx() {
|
|
|
|
gasTipCap := (*big.Int)(args.MaxPriorityFeePerGas)
|
|
|
|
gasFeeCap := (*big.Int)(args.MaxFeePerGas)
|
|
|
|
|
|
|
|
txData := &gethtypes.DynamicFeeTx{
|
|
|
|
Nonce: nonce,
|
|
|
|
Value: value,
|
|
|
|
Gas: gas,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
}
|
|
|
|
tx = gethtypes.NewTx(txData)
|
|
|
|
} else {
|
|
|
|
tx = gethtypes.NewContractCreation(nonce, value, gas, gasPrice, args.GetInput())
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
t.logNewContract(args, gas, gasPrice, value, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) logNewTx(args SendTxArgs, gas uint64, gasPrice *big.Int, value *big.Int) {
|
|
|
|
t.log.Info("New transaction",
|
|
|
|
"From", args.From,
|
|
|
|
"To", *args.To,
|
|
|
|
"Gas", gas,
|
|
|
|
"GasPrice", gasPrice,
|
|
|
|
"Value", value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) logNewContract(args SendTxArgs, gas uint64, gasPrice *big.Int, value *big.Int, nonce uint64) {
|
|
|
|
t.log.Info("New contract",
|
|
|
|
"From", args.From,
|
|
|
|
"Gas", gas,
|
|
|
|
"GasPrice", gasPrice,
|
|
|
|
"Value", value,
|
|
|
|
"Contract address", crypto.CreateAddress(args.From, nonce),
|
|
|
|
)
|
|
|
|
}
|