Skip to content

Commit 659342a

Browse files
authored
ethclient: add SubscribeTransactionReceipts (#32869)
Add `SubscribeTransactionReceipts` for ethclient. This is a complement to #32697.
1 parent de24450 commit 659342a

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
@@ -299,15 +299,27 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
299299
return rpcSub, nil
300300
}
301301

302-
// TransactionReceiptsFilter defines criteria for transaction receipts subscription.
303-
// If TransactionHashes is nil or empty, receipts for all transactions included in new blocks will be delivered.
304-
// Otherwise, only receipts for the specified transactions will be delivered.
305-
type TransactionReceiptsFilter struct {
306-
TransactionHashes []common.Hash `json:"transactionHashes,omitempty"`
302+
// TransactionReceiptsQuery defines criteria for transaction receipts subscription.
303+
// Same as ethereum.TransactionReceiptsQuery but with UnmarshalJSON() method.
304+
type TransactionReceiptsQuery ethereum.TransactionReceiptsQuery
305+
306+
// UnmarshalJSON sets *args fields with given data.
307+
func (args *TransactionReceiptsQuery) UnmarshalJSON(data []byte) error {
308+
type input struct {
309+
TransactionHashes []common.Hash `json:"transactionHashes"`
310+
}
311+
312+
var raw input
313+
if err := json.Unmarshal(data, &raw); err != nil {
314+
return err
315+
}
316+
317+
args.TransactionHashes = raw.TransactionHashes
318+
return nil
307319
}
308320

309321
// TransactionReceipts creates a subscription that fires transaction receipts when transactions are included in blocks.
310-
func (api *FilterAPI) TransactionReceipts(ctx context.Context, filter *TransactionReceiptsFilter) (*rpc.Subscription, error) {
322+
func (api *FilterAPI) TransactionReceipts(ctx context.Context, filter *TransactionReceiptsQuery) (*rpc.Subscription, error) {
311323
notifier, supported := rpc.NotifierFromContext(ctx)
312324
if !supported {
313325
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported

ethclient/ethclient.go

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

353+
// SubscribeTransactionReceipts subscribes to notifications about transaction receipts.
354+
func (ec *Client) SubscribeTransactionReceipts(ctx context.Context, q *ethereum.TransactionReceiptsQuery, ch chan<- []*types.Receipt) (ethereum.Subscription, error) {
355+
sub, err := ec.c.EthSubscribe(ctx, ch, "transactionReceipts", q)
356+
if err != nil {
357+
return nil, err
358+
}
359+
return sub, nil
360+
}
361+
353362
// SyncProgress retrieves the current progress of the sync algorithm. If there's
354363
// no sync currently running, it returns nil.
355364
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)