-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy patheth_client_utils.go
More file actions
121 lines (104 loc) · 5.54 KB
/
eth_client_utils.go
File metadata and controls
121 lines (104 loc) · 5.54 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package utils
import (
"context"
"math/big"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
eigentypes "github.com/Layr-Labs/eigensdk-go/types"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
retry "github.com/yetanotherco/aligned_layer/core"
)
// WaitForTransactionReceiptRetryable repeatedly attempts to fetch the transaction receipt for a given transaction hash.
// If the receipt is not found, the function will retry with exponential backoff until the specified `waitTimeout` duration is reached.
// If the receipt is still unavailable after `waitTimeout`, it will return an error.
//
// Note: The `time.Second * 2` is set as the max interval in the retry mechanism because we can't reliably measure the specific time the tx will be included in a block.
// Setting a higher value will imply doing less retries across the waitTimeout, and so we might lose the receipt
// All errors are considered Transient Errors
// - Retry times: 0.5s, 1s, 2s, 2s, 2s, ... until it reaches waitTimeout
func WaitForTransactionReceiptRetryable(client eth.InstrumentedClient, fallbackClient eth.InstrumentedClient, txHash gethcommon.Hash, config *retry.RetryParams) (*types.Receipt, error) {
receipt_func := func() (*types.Receipt, error) {
receipt, err := client.TransactionReceipt(context.Background(), txHash)
if err != nil {
receipt, err = fallbackClient.TransactionReceipt(context.Background(), txHash)
if err != nil {
return nil, err
}
return receipt, nil
}
return receipt, nil
}
return receipt_func
}
// WaitForTransactionReceiptRetryable repeatedly attempts to fetch the transaction receipt for a given transaction hash.
// If the receipt is not found, the function will retry with exponential backoff until the specified `waitTimeout` duration is reached.
// If the receipt is still unavailable after `waitTimeout`, it will return an error.
//
// Note: The `time.Second * 2` is set as the max interval in the retry mechanism because we can't reliably measure the specific time the tx will be included in a block.
// Setting a higher value will imply doing less retries across the waitTimeout, and so we might lose the receipt
// All errors are considered Transient Errors
// - Retry times: 0.5s, 1s, 2s, 2s, 2s, ... until it reaches waitTimeout
func WaitForTransactionReceiptRetryable(client eth.InstrumentedClient, fallbackClient eth.InstrumentedClient, txHash gethcommon.Hash, config *retry.RetryConfig) (*types.Receipt, error) {
return retry.RetryWithData(WaitForTransactionReceipt(client, fallbackClient, txHash, config), config)
}
func BytesToQuorumNumbers(quorumNumbersBytes []byte) eigentypes.QuorumNums {
quorumNums := make(eigentypes.QuorumNums, len(quorumNumbersBytes))
for i, quorumNumberByte := range quorumNumbersBytes {
quorumNums[i] = eigentypes.QuorumNum(quorumNumberByte)
}
return quorumNums
}
func BytesToQuorumThresholdPercentages(quorumThresholdPercentagesBytes []byte) eigentypes.QuorumThresholdPercentages {
quorumThresholdPercentages := make(eigentypes.QuorumThresholdPercentages, len(quorumThresholdPercentagesBytes))
for i, quorumNumberByte := range quorumThresholdPercentagesBytes {
quorumThresholdPercentages[i] = eigentypes.QuorumThresholdPercentage(quorumNumberByte)
}
return quorumThresholdPercentages
}
func WeiToEth(wei *big.Int) float64 {
weiToEth := new(big.Float).SetFloat64(1e18)
weiFloat := new(big.Float).SetInt(wei)
result := new(big.Float).Quo(weiFloat, weiToEth)
eth, _ := result.Float64()
return eth
}
// Simple algorithm to calculate the gasPrice bump based on:
// the currentGasPrice, a base bump percentage, a retry percentage, and the retry count.
// Formula: currentGasPrice + (currentGasPrice * (baseBumpPercentage + retryCount * incrementalRetryPercentage) / 100)
func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, baseBumpPercentage uint, retryAttemptPercentage uint, bumpPercentageLimit uint, retryCount int) *big.Int {
// Incremental percentage increase for each retry attempt (i*retryAttemptPercentage)
incrementalRetryPercentage := new(big.Int).Mul(big.NewInt(int64(retryAttemptPercentage)), big.NewInt(int64(retryCount)))
// Total bump percentage: base bump + incremental retry percentage
totalBumpPercentage := new(big.Int).Add(big.NewInt(int64(baseBumpPercentage)), incrementalRetryPercentage)
// Make sure the percentage to bump isn't higher than the limit
bumpPercentageLimitAsBigInt := big.NewInt(int64(bumpPercentageLimit))
if totalBumpPercentage.Cmp(bumpPercentageLimitAsBigInt) > 0 {
totalBumpPercentage = bumpPercentageLimitAsBigInt
}
// Calculate the bump amount: currentGasPrice * totalBumpPercentage / 100
bumpAmount := new(big.Int).Mul(currentGasPrice, totalBumpPercentage)
bumpAmount = new(big.Int).Div(bumpAmount, big.NewInt(100))
// Final bumped gas price: currentGasPrice + bumpAmount
bumpedGasPrice := new(big.Int).Add(currentGasPrice, bumpAmount)
return bumpedGasPrice
}
//TODO: move to retryable function file
/*
GetGasPriceRetryable
Get the gas price from the client with retry logic.
- All errors are considered Transient Errors
- Retry times: 1 sec, 2 sec, 4 sec
*/
func GetGasPriceRetryable(client eth.InstrumentedClient, fallbackClient eth.InstrumentedClient, config *retry.RetryParams) (*big.Int, error) {
respondToTaskV2_func := func() (*big.Int, error) {
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
gasPrice, err = fallbackClient.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
}
return gasPrice, nil
}
return retry.RetryWithData(respondToTaskV2_func, config)
}