From 1878e522e0f18568cb4d3ad16a23ff3454fb0275 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Wed, 3 Jul 2024 14:47:33 -0400 Subject: [PATCH] feat: adds retry to BatchReceipts call --- x/contracts/txmonitor/eth_helper.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/x/contracts/txmonitor/eth_helper.go b/x/contracts/txmonitor/eth_helper.go index ca15db555..77113b3c4 100644 --- a/x/contracts/txmonitor/eth_helper.go +++ b/x/contracts/txmonitor/eth_helper.go @@ -2,6 +2,7 @@ package txmonitor import ( "context" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -111,13 +112,29 @@ func (e *evmHelper) BatchReceipts(ctx context.Context, txHashes []common.Hash) ( } } - // Execute the batch request - err := e.client.BatchCallContext(ctx, batch) + var receipts []Result + var err error + for retries := 0; retries < 3; retries++ { + // Execute the batch request + err = e.client.BatchCallContext(ctx, batch) + if err == nil { + break + } + + // Check if the error is a 429 (Too Many Requests) + if rpcErr, ok := err.(rpc.Error); ok && rpcErr.ErrorCode() == 429 { + time.Sleep(1 * time.Second) + continue + } + + return nil, err + } + if err != nil { return nil, err } - receipts := make([]Result, len(batch)) + receipts = make([]Result, len(batch)) for i, elem := range batch { receipts[i].Receipt = elem.Result.(*types.Receipt) if elem.Error != nil {