Skip to content

Commit

Permalink
fix: wait for tx result for evm properly
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Dec 16, 2024
1 parent f76b1da commit cb9b50e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
5 changes: 2 additions & 3 deletions relayer/chains/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
)

const (
DefaultPollingInterval = time.Second * 30
MaximumPollTry = 15
DefaultCreateTimeout = time.Second * 10
DefaultTxConfirmationTimeout = time.Second * 45
DefaultCreateTimeout = time.Second * 10
)

func newClient(ctx context.Context, connectionContract, XcallContract common.Address, rpcUrl, websocketUrl string, l *zap.Logger) (IClient, error) {
Expand Down
31 changes: 20 additions & 11 deletions relayer/chains/evm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,35 @@ func (p *Provider) FinalityBlock(ctx context.Context) uint64 {
}

func (p *Provider) WaitForResults(ctx context.Context, tx *ethTypes.Transaction) (*coreTypes.Receipt, error) {
ticker := time.NewTicker(DefaultPollingInterval)
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
counter := 0
startTime := time.Now()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
if counter >= MaximumPollTry {
return nil, fmt.Errorf("failed to get receipt after %d tries", counter)
txReceipt, err := p.client.TransactionReceipt(ctx, tx.Hash())
if err != nil && !errors.Is(err, ethereum.NotFound) {
return txReceipt, err
}
counter++
txr, err := p.client.TransactionReceipt(ctx, tx.Hash())
if err == nil {
return txr, nil

if txReceipt != nil {
if txReceipt.Status == ethTypes.ReceiptStatusFailed {
return txReceipt, fmt.Errorf("txn failed [tx hash: %s]", tx.Hash())
} else {
return txReceipt, nil
}
}
if errors.Is(err, ethereum.NotFound) {
continue

// handle txn not found case:
if time.Since(startTime) > DefaultTxConfirmationTimeout {
if err != nil {
return txReceipt, err
} else {
return txReceipt, fmt.Errorf("tx confirmation timed out [tx hash: %s]", tx.Hash())
}
}
return txr, err
}
}
}
Expand Down

0 comments on commit cb9b50e

Please sign in to comment.