Skip to content

Commit

Permalink
Merge pull request #37 from 0xPolygonHermez/feature/wait-receipt
Browse files Browse the repository at this point in the history
Do for loop to wait for the tx receipt
  • Loading branch information
agnusmor authored May 28, 2024
2 parents de59b1d + 2006c5f commit 5713258
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions ethtxmanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ type Config struct {
FrequencyToMonitorTxs types.Duration `mapstructure:"FrequencyToMonitorTxs"`
// WaitTxToBeMined time to wait after transaction was sent to the ethereum
WaitTxToBeMined types.Duration `mapstructure:"WaitTxToBeMined"`
// WaitReceiptToBeGenerated time to wait after transaction was mined to get the receipt
WaitReceiptToBeGenerated types.Duration `mapstructure:"WaitReceiptToBeGenerated"`
// GetReceiptMaxTime is the max time to wait to get the receipt of the mined transaction
GetReceiptMaxTime types.Duration `mapstructure:"WaitReceiptMaxTime"`
// GetReceiptWaitInterval is the time to sleep before trying to get the receipt of the mined transaction
GetReceiptWaitInterval types.Duration `mapstructure:"WaitReceiptCheckInterval"`
// ConsolidationL1ConfirmationBlocks is the number of blocks to wait for a L1 tx to be consolidated
ConsolidationL1ConfirmationBlocks uint64 `mapstructure:"ConsolidationL1ConfirmationBlocks"`
// FinalizationL1ConfirmationBlocks is the number of blocks to wait for a L1 tx to be finalized
Expand Down
23 changes: 15 additions & 8 deletions ethtxmanager/ethtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,16 +699,23 @@ func (c *Client) monitorTx(ctx context.Context, mTx monitoredTx, logger *log.Log
return
}

// Wait for the receipt to be available
time.Sleep(c.cfg.WaitReceiptToBeGenerated.Duration)

// get tx receipt
var txReceipt *types.Receipt
txReceipt, err = c.etherman.GetTxReceipt(ctx, signedTx.Hash())
if err != nil {
logger.Warnf("failed to get tx receipt for tx %v: %v", signedTx.Hash().String(), err)
return
waitingReceiptTimeout := time.Now().Add(c.cfg.GetReceiptMaxTime.Duration)
// get tx receipt
for {
txReceipt, err = c.etherman.GetTxReceipt(ctx, signedTx.Hash())
if err != nil {
if waitingReceiptTimeout.After(time.Now()) {
time.Sleep(c.cfg.GetReceiptWaitInterval.Duration)
} else {
logger.Warnf("failed to get tx receipt for tx %v after %v: %v", signedTx.Hash().String(), c.cfg.GetReceiptMaxTime, err)
return
}
} else {
break
}
}

lastReceiptChecked = *txReceipt
}

Expand Down
3 changes: 2 additions & 1 deletion test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func main() {
config := ethtxmanager.Config{
FrequencyToMonitorTxs: types.Duration{Duration: 1 * time.Second},
WaitTxToBeMined: types.Duration{Duration: 2 * time.Minute},
WaitReceiptToBeGenerated: types.Duration{Duration: 10 * time.Second},
GetReceiptMaxTime: types.Duration{Duration: 10 * time.Second},
GetReceiptWaitInterval: types.Duration{Duration: 250 * time.Millisecond},
ConsolidationL1ConfirmationBlocks: 5,
FinalizationL1ConfirmationBlocks: 10,
ForcedGas: 0,
Expand Down

0 comments on commit 5713258

Please sign in to comment.