|
8 | 8 | "github.com/0xsequence/ethkit/ethrpc" |
9 | 9 | "github.com/0xsequence/ethkit/go-ethereum" |
10 | 10 | "github.com/0xsequence/ethkit/go-ethereum/common" |
| 11 | + "github.com/0xsequence/ethkit/go-ethereum/common/hexutil" |
11 | 12 | "github.com/0xsequence/ethkit/go-ethereum/core" |
12 | 13 | "github.com/0xsequence/ethkit/go-ethereum/core/types" |
13 | 14 | ) |
@@ -49,6 +50,12 @@ type TransactionRequest struct { |
49 | 50 |
|
50 | 51 | type WaitReceipt func(ctx context.Context) (*types.Receipt, error) |
51 | 52 |
|
| 53 | +// Logger logs ethtxn request/response events. |
| 54 | +type Logger interface { |
| 55 | + Info(msg string, args ...any) |
| 56 | + Error(msg string, args ...any) |
| 57 | +} |
| 58 | + |
52 | 59 | // NewTransaction prepares a transaction for delivery, however the transaction still needs to be signed |
53 | 60 | // before it can be sent. |
54 | 61 | func NewTransaction(ctx context.Context, provider *ethrpc.Provider, txnRequest *TransactionRequest) (*types.Transaction, error) { |
@@ -181,15 +188,38 @@ func NewTransaction(ctx context.Context, provider *ethrpc.Provider, txnRequest * |
181 | 188 | } |
182 | 189 |
|
183 | 190 | func SendTransaction(ctx context.Context, provider *ethrpc.Provider, signedTxn *types.Transaction) (*types.Transaction, WaitReceipt, error) { |
| 191 | + return SendTransactionWithLogger(ctx, provider, signedTxn, nil) |
| 192 | +} |
| 193 | + |
| 194 | +// SendTransactionWithLogger sends a signed transaction and logs request/response details. |
| 195 | +func SendTransactionWithLogger(ctx context.Context, provider *ethrpc.Provider, signedTxn *types.Transaction, log Logger) (*types.Transaction, WaitReceipt, error) { |
184 | 196 | if provider == nil { |
185 | 197 | return nil, nil, fmt.Errorf("ethtxn (SendTransaction): provider is not set") |
186 | 198 | } |
187 | 199 |
|
| 200 | + if log != nil { |
| 201 | + rawTx, err := signedTxn.MarshalBinary() |
| 202 | + if err != nil { |
| 203 | + log.Error("ethtxn request marshal failed", "method", "eth_sendRawTransaction", "error", err) |
| 204 | + } else { |
| 205 | + log.Info("ethtxn request", "method", "eth_sendRawTransaction", "txHash", signedTxn.Hash().Hex(), "rawTx", hexutil.Encode(rawTx)) |
| 206 | + } |
| 207 | + } |
| 208 | + |
188 | 209 | waitFn := func(ctx context.Context) (*types.Receipt, error) { |
189 | 210 | return ethrpc.WaitForTxnReceipt(ctx, provider, signedTxn.Hash()) |
190 | 211 | } |
191 | 212 |
|
192 | | - return signedTxn, waitFn, provider.SendTransaction(ctx, signedTxn) |
| 213 | + response, err := provider.Do(ctx, ethrpc.SendTransaction(signedTxn)) |
| 214 | + if log != nil { |
| 215 | + if err != nil { |
| 216 | + log.Error("ethtxn response", "method", "eth_sendRawTransaction", "txHash", signedTxn.Hash().Hex(), "response", string(response), "error", err) |
| 217 | + } else { |
| 218 | + log.Info("ethtxn response", "method", "eth_sendRawTransaction", "txHash", signedTxn.Hash().Hex(), "response", string(response)) |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return signedTxn, waitFn, err |
193 | 223 | } |
194 | 224 |
|
195 | 225 | var zeroBigInt = big.NewInt(0) |
|
0 commit comments