Skip to content

Commit 32d9328

Browse files
10gicmanav2401
authored andcommitted
ethclient: add SubscribeTransactionReceipts (#32869)
Add `SubscribeTransactionReceipts` for ethclient. This is a complement to ethereum/go-ethereum#32697.
1 parent 8af34f1 commit 32d9328

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

eth/filters/api.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,27 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
309309
return rpcSub, nil
310310
}
311311

312-
// TransactionReceiptsFilter defines criteria for transaction receipts subscription.
313-
// If TransactionHashes is nil or empty, receipts for all transactions included in new blocks will be delivered.
314-
// Otherwise, only receipts for the specified transactions will be delivered.
315-
type TransactionReceiptsFilter struct {
316-
TransactionHashes []common.Hash `json:"transactionHashes,omitempty"`
312+
// TransactionReceiptsQuery defines criteria for transaction receipts subscription.
313+
// Same as ethereum.TransactionReceiptsQuery but with UnmarshalJSON() method.
314+
type TransactionReceiptsQuery ethereum.TransactionReceiptsQuery
315+
316+
// UnmarshalJSON sets *args fields with given data.
317+
func (args *TransactionReceiptsQuery) UnmarshalJSON(data []byte) error {
318+
type input struct {
319+
TransactionHashes []common.Hash `json:"transactionHashes"`
320+
}
321+
322+
var raw input
323+
if err := json.Unmarshal(data, &raw); err != nil {
324+
return err
325+
}
326+
327+
args.TransactionHashes = raw.TransactionHashes
328+
return nil
317329
}
318330

319331
// TransactionReceipts creates a subscription that fires transaction receipts when transactions are included in blocks.
320-
func (api *FilterAPI) TransactionReceipts(ctx context.Context, filter *TransactionReceiptsFilter) (*rpc.Subscription, error) {
332+
func (api *FilterAPI) TransactionReceipts(ctx context.Context, filter *TransactionReceiptsQuery) (*rpc.Subscription, error) {
321333
notifier, supported := rpc.NotifierFromContext(ctx)
322334
if !supported {
323335
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported

ethclient/ethclient.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*
390390
return r, err
391391
}
392392

393+
// SubscribeTransactionReceipts subscribes to notifications about transaction receipts.
394+
func (ec *Client) SubscribeTransactionReceipts(ctx context.Context, q *ethereum.TransactionReceiptsQuery, ch chan<- []*types.Receipt) (ethereum.Subscription, error) {
395+
sub, err := ec.c.EthSubscribe(ctx, ch, "transactionReceipts", q)
396+
if err != nil {
397+
return nil, err
398+
}
399+
return sub, nil
400+
}
401+
393402
// SyncProgress retrieves the current progress of the sync algorithm. If there's
394403
// no sync currently running, it returns nil.
395404
func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {

interfaces.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ type ChainReader interface {
6262
SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (Subscription, error)
6363
}
6464

65+
// TransactionReceiptsQuery defines criteria for transaction receipts subscription.
66+
// If TransactionHashes is empty, receipts for all transactions included in new blocks will be delivered.
67+
// Otherwise, only receipts for the specified transactions will be delivered.
68+
type TransactionReceiptsQuery struct {
69+
TransactionHashes []common.Hash
70+
}
71+
6572
// TransactionReader provides access to past transactions and their receipts.
6673
// Implementations may impose arbitrary restrictions on the transactions and receipts that
6774
// can be retrieved. Historic transactions may not be available.
@@ -81,6 +88,11 @@ type TransactionReader interface {
8188
// transaction may not be included in the current canonical chain even if a receipt
8289
// exists.
8390
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
91+
// SubscribeTransactionReceipts subscribes to notifications about transaction receipts.
92+
// The receipts are delivered in batches when transactions are included in blocks.
93+
// If q is nil or has empty TransactionHashes, all receipts from new blocks will be delivered.
94+
// Otherwise, only receipts for the specified transaction hashes will be delivered.
95+
SubscribeTransactionReceipts(ctx context.Context, q *TransactionReceiptsQuery, ch chan<- []*types.Receipt) (Subscription, error)
8496
}
8597

8698
// ChainStateReader wraps access to the state trie of the canonical blockchain. Note that

0 commit comments

Comments
 (0)