Skip to content

Commit a483eaa

Browse files
committed
ethwallet: Wallet.SendTransactionWithLogger
1 parent c335c85 commit a483eaa

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

ethtxn/ethtxn.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/0xsequence/ethkit/ethrpc"
99
"github.com/0xsequence/ethkit/go-ethereum"
1010
"github.com/0xsequence/ethkit/go-ethereum/common"
11+
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
1112
"github.com/0xsequence/ethkit/go-ethereum/core"
1213
"github.com/0xsequence/ethkit/go-ethereum/core/types"
1314
)
@@ -49,6 +50,12 @@ type TransactionRequest struct {
4950

5051
type WaitReceipt func(ctx context.Context) (*types.Receipt, error)
5152

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+
5259
// NewTransaction prepares a transaction for delivery, however the transaction still needs to be signed
5360
// before it can be sent.
5461
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 *
181188
}
182189

183190
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) {
184196
if provider == nil {
185197
return nil, nil, fmt.Errorf("ethtxn (SendTransaction): provider is not set")
186198
}
187199

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+
188209
waitFn := func(ctx context.Context) (*types.Receipt, error) {
189210
return ethrpc.WaitForTxnReceipt(ctx, provider, signedTxn.Hash())
190211
}
191212

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
193223
}
194224

195225
var zeroBigInt = big.NewInt(0)

ethwallet/ethwallet.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,14 @@ func (w *Wallet) NewTransaction(ctx context.Context, txnRequest *ethtxn.Transact
358358
}
359359

360360
func (w *Wallet) SendTransaction(ctx context.Context, signedTxn *types.Transaction) (*types.Transaction, ethtxn.WaitReceipt, error) {
361+
return w.SendTransactionWithLogger(ctx, signedTxn, nil)
362+
}
363+
364+
// SendTransactionWithLogger sends a signed transaction and optionally logs request/response details.
365+
func (w *Wallet) SendTransactionWithLogger(ctx context.Context, signedTxn *types.Transaction, log ethtxn.Logger) (*types.Transaction, ethtxn.WaitReceipt, error) {
361366
provider := w.GetProvider()
362367
if provider == nil {
363368
return nil, nil, fmt.Errorf("ethwallet: provider is not set")
364369
}
365-
return ethtxn.SendTransaction(ctx, provider, signedTxn)
370+
return ethtxn.SendTransactionWithLogger(ctx, provider, signedTxn, log)
366371
}

0 commit comments

Comments
 (0)