-
Notifications
You must be signed in to change notification settings - Fork 7
/
Receipt.go
73 lines (68 loc) · 1.96 KB
/
Receipt.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
package zksync
import (
"github.com/pkg/errors"
"time"
)
type TransactionReceiptProcessor struct {
provider Provider
checkInterval time.Duration
timeout time.Duration
}
func NewTransactionReceiptProcessor(provider Provider) *TransactionReceiptProcessor {
return &TransactionReceiptProcessor{
provider: provider,
checkInterval: time.Second,
timeout: 5 * time.Minute,
}
}
func NewTransactionReceiptProcessorDurations(provider Provider,
checkInterval time.Duration, timeout time.Duration) *TransactionReceiptProcessor {
return &TransactionReceiptProcessor{
provider: provider,
checkInterval: checkInterval,
timeout: timeout,
}
}
func (rp *TransactionReceiptProcessor) WaitForTransaction(txHash string, status TransactionStatus) (*TransactionDetails, error) {
checkTransactionDetails := func(td *TransactionDetails, err error) (*TransactionDetails, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to get transaction details by txHash")
}
if td.Executed {
switch status {
case TransactionStatusSent:
if td.Block != nil {
return td, nil
}
case TransactionStatusCommitted:
if td.Block != nil && td.Block.Committed {
return td, nil
}
case TransactionStatusVerified:
if td.Block != nil && td.Block.Verified {
return td, nil
}
}
}
return nil, nil
}
td, err := checkTransactionDetails(rp.provider.GetTransactionDetails(txHash))
if err != nil {
return nil, errors.Wrap(err, "failed to get transaction details by txHash")
} else if td != nil {
return td, nil
}
for {
select {
case <-time.After(rp.checkInterval):
td, err = checkTransactionDetails(rp.provider.GetTransactionDetails(txHash))
if err != nil {
return nil, errors.Wrap(err, "failed to get transaction details by txHash")
} else if td != nil {
return td, nil
}
case <-time.After(rp.timeout):
return nil, errors.New("Transaction was not generated, waiting timeout reached")
}
}
}