2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2018-01-18 16:55:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-12-19 18:27:27 +00:00
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
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-09 08:18:22 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-01-18 16:55:17 +00:00
|
|
|
)
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// rpcWrapper wraps provides convenient interface for ethereum RPC APIs we need for sending transactions
|
|
|
|
type rpcWrapper struct {
|
2024-06-27 21:27:09 +00:00
|
|
|
RPCClient rpc.ClientInterface
|
2022-06-09 13:09:56 +00:00
|
|
|
chainID uint64
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 21:27:09 +00:00
|
|
|
func newRPCWrapper(client rpc.ClientInterface, chainID uint64) *rpcWrapper {
|
2022-06-09 13:09:56 +00:00
|
|
|
return &rpcWrapper{RPCClient: client, chainID: chainID}
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PendingNonceAt returns the account nonce of the given account in the pending state.
|
|
|
|
// This is the nonce that should be used for the next transaction.
|
2018-04-09 08:18:22 +00:00
|
|
|
func (w *rpcWrapper) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
2018-01-18 16:55:17 +00:00
|
|
|
var result hexutil.Uint64
|
2022-06-09 13:09:56 +00:00
|
|
|
err := w.RPCClient.CallContext(ctx, &result, w.chainID, "eth_getTransactionCount", account, "pending")
|
2018-01-18 16:55:17 +00:00
|
|
|
return uint64(result), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
|
|
|
|
// execution of a transaction.
|
2018-04-09 08:18:22 +00:00
|
|
|
func (w *rpcWrapper) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
2018-01-18 16:55:17 +00:00
|
|
|
var hex hexutil.Big
|
2022-06-09 13:09:56 +00:00
|
|
|
if err := w.RPCClient.CallContext(ctx, &hex, w.chainID, "eth_gasPrice"); err != nil {
|
2018-01-18 16:55:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*big.Int)(&hex), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
|
|
|
|
// the current pending state of the backend blockchain. There is no guarantee that this is
|
|
|
|
// the true gas limit requirement as other transactions may be added or removed by miners,
|
|
|
|
// but it should provide a basis for setting a reasonable default.
|
2018-04-09 08:18:22 +00:00
|
|
|
func (w *rpcWrapper) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
|
2018-02-27 10:39:30 +00:00
|
|
|
var hex hexutil.Uint64
|
2022-06-09 13:09:56 +00:00
|
|
|
err := w.RPCClient.CallContext(ctx, &hex, w.chainID, "eth_estimateGas", toCallArg(msg))
|
2018-01-18 16:55:17 +00:00
|
|
|
if err != nil {
|
2018-02-27 10:39:30 +00:00
|
|
|
return 0, err
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2018-02-27 10:39:30 +00:00
|
|
|
return uint64(hex), nil
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 15:39:36 +00:00
|
|
|
// Does the `eth_sendRawTransaction` call with the given raw transaction hex string.
|
|
|
|
func (w *rpcWrapper) SendRawTransaction(ctx context.Context, rawTx string) error {
|
|
|
|
return w.RPCClient.CallContext(ctx, nil, w.chainID, "eth_sendRawTransaction", rawTx)
|
|
|
|
}
|
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
// SendTransaction injects a signed transaction into the pending pool for execution.
|
|
|
|
//
|
|
|
|
// If the transaction was a contract creation use the TransactionReceipt method to get the
|
|
|
|
// contract address after the transaction has been mined.
|
2019-12-19 18:27:27 +00:00
|
|
|
func (w *rpcWrapper) SendTransaction(ctx context.Context, tx *gethtypes.Transaction) error {
|
2021-07-14 11:05:12 +00:00
|
|
|
data, err := tx.MarshalBinary()
|
2018-01-18 16:55:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-24 15:39:36 +00:00
|
|
|
return w.SendRawTransaction(ctx, types.EncodeHex(data))
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toCallArg(msg ethereum.CallMsg) interface{} {
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"from": msg.From,
|
|
|
|
"to": msg.To,
|
|
|
|
}
|
|
|
|
if len(msg.Data) > 0 {
|
2019-12-19 18:27:27 +00:00
|
|
|
arg["data"] = types.HexBytes(msg.Data)
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
if msg.Value != nil {
|
|
|
|
arg["value"] = (*hexutil.Big)(msg.Value)
|
|
|
|
}
|
2018-02-27 10:39:30 +00:00
|
|
|
if msg.Gas != 0 {
|
|
|
|
arg["gas"] = hexutil.Uint64(msg.Gas)
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
if msg.GasPrice != nil {
|
|
|
|
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|