@@ -3,7 +3,6 @@ package utils
33import (
44 "context"
55 "errors"
6- "fmt"
76 "time"
87
98 "github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -12,32 +11,30 @@ import (
1211 "github.com/rocket-pool/smartnode/bindings/rocketpool"
1312)
1413
15- // Wait for a transaction to get mined
16- func WaitForTransaction ( client rocketpool. ExecutionClient , hash common. Hash ) ( * types. Receipt , error ) {
17-
14+ // Wait for a transaction to get included, respecting the provided context for cancellation.
15+ // The transaction lookup retries indefinitely (with 1-second pauses) until found or ctx is done.
16+ func WaitForTransactionWithContext ( ctx context. Context , client rocketpool. ExecutionClient , hash common. Hash ) ( * types. Receipt , error ) {
1817 var tx * types.Transaction
19- var err error
2018
21- // Get the transaction from its hash, retrying for 30 sec if it wasn't found
22- for i := 0 ; i < 30 ; i ++ {
23- if i == 29 {
24- return nil , fmt .Errorf ("Transaction not found after 30 seconds." )
19+ // Get the transaction from its hash, retrying until found or ctx is cancelled.
20+ for {
21+ var err error
22+ tx , _ , err = client .TransactionByHash (ctx , hash )
23+ if err == nil {
24+ break
2525 }
26-
27- tx , _ , err = client .TransactionByHash (context .Background (), hash )
28- if err != nil {
29- if err .Error () == "not found" {
30- time .Sleep (1 * time .Second )
31- continue
32- }
26+ if err .Error () != "not found" {
3327 return nil , err
34- } else {
35- break
28+ }
29+ select {
30+ case <- ctx .Done ():
31+ return nil , ctx .Err ()
32+ case <- time .After (1 * time .Second ):
3633 }
3734 }
3835
3936 // Wait for transaction to be mined
40- txReceipt , err := bind .WaitMined (context . Background () , client , tx )
37+ txReceipt , err := bind .WaitMined (ctx , client , tx )
4138 if err != nil {
4239 return nil , err
4340 }
@@ -47,6 +44,10 @@ func WaitForTransaction(client rocketpool.ExecutionClient, hash common.Hash) (*t
4744 return txReceipt , errors .New ("Transaction failed with status 0" )
4845 }
4946
50- // Return
5147 return txReceipt , nil
5248}
49+
50+ // Wait for a transaction to get mined
51+ func WaitForTransaction (client rocketpool.ExecutionClient , hash common.Hash ) (* types.Receipt , error ) {
52+ return WaitForTransactionWithContext (context .Background (), client , hash )
53+ }
0 commit comments