Skip to content

Commit

Permalink
adding custom waitMined
Browse files Browse the repository at this point in the history
  • Loading branch information
yashnevatia committed Dec 6, 2024
1 parent aa868d5 commit 1188149
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions deployment/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -142,12 +143,12 @@ func DeployContract[C any](
lggr.Errorw("Failed to deploy contract", "err", contractDeploy.Err)
return nil, contractDeploy.Err
}
// _, err := chain.Confirm(contractDeploy.Tx)
// if err != nil {
// lggr.Errorw("Failed to confirm deployment", "err", err)
// return nil, err
// }
err := addressBook.Save(chain.Selector, contractDeploy.Address.String(), contractDeploy.Tv)
err := waitForMined(context.Background(), chain.Client, contractDeploy.TxHash, true)
if err != nil {
lggr.Errorw("Failed to confirm deployment", "err", err)
return nil, err
}
err = addressBook.Save(chain.Selector, contractDeploy.Address.String(), contractDeploy.Tv)
if err != nil {
lggr.Errorw("Failed to save contract address", "err", err)
return nil, err
Expand All @@ -165,3 +166,30 @@ func IsValidChainSelector(cs uint64) error {
}
return nil
}

const (
RetryTiming = 5 * time.Second
CrossChainTimout = 5 * time.Minute
TxInclusionTimout = 3 * time.Minute
)

func waitForMined(ctx context.Context, client bind.DeployBackend, hash common.Hash, shouldSucceed bool) error {
maxIterations := TxInclusionTimout / RetryTiming
for i := 0; i < int(maxIterations); i++ {
receipt, _ := client.TransactionReceipt(context.Background(), hash)

if receipt != nil {
if shouldSucceed && receipt.Status == 0 {
// GetLogger(ctx).Debug().Msgf("[MINING] ERROR tx reverted %s", hash.Hex())
return errors.New("tx reverted")
} else if !shouldSucceed && receipt.Status != 0 {
// GetLogger(ctx).Debug().Msgf("[MINING] ERROR expected tx to revert %s", hash.Hex())
return errors.New("tx did not revert")
}
return nil
}

time.Sleep(RetryTiming)
}
return errors.New("no tx found within the given timeout")
}

0 comments on commit 1188149

Please sign in to comment.