-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathtypes.go
90 lines (74 loc) · 1.92 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package relayer
import (
"context"
"fmt"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
var (
ZeroHash = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
)
// IsInSlice determines whether v is in slice s
func IsInSlice[T comparable](v T, s []T) bool {
for _, e := range s {
if v == e {
return true
}
}
return false
}
type confirmer interface {
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
BlockNumber(ctx context.Context) (uint64, error)
}
// WaitReceipt keeps waiting until the given transaction has an execution
// receipt to know whether it was reverted or not.
func WaitReceipt(ctx context.Context, confirmer confirmer, txHash common.Hash) (*types.Receipt, error) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
receipt, err := confirmer.TransactionReceipt(ctx, txHash)
if err != nil {
continue
}
if receipt.Status != types.ReceiptStatusSuccessful {
return nil, fmt.Errorf("transaction reverted, hash: %s", txHash)
}
return receipt, nil
}
}
}
// WaitConfirmations won't return before N blocks confirmations have been seen
// on destination chain.
func WaitConfirmations(ctx context.Context, confirmer confirmer, confirmations uint64, txHash common.Hash) error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
receipt, err := confirmer.TransactionReceipt(ctx, txHash)
if err != nil {
if err == ethereum.NotFound {
continue
}
return err
}
latest, err := confirmer.BlockNumber(ctx)
if err != nil {
return err
}
if latest < receipt.BlockNumber.Uint64()+confirmations {
continue
}
return nil
}
}
}