-
Notifications
You must be signed in to change notification settings - Fork 0
/
egpp.go
37 lines (29 loc) · 1009 Bytes
/
egpp.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
package egpp
import (
"errors"
"github.com/OsoianMarcel/egpp/common"
)
// The method requests the gas price from specified providers in order and returns the first successful result
func GetGasPriceWithFallback(providers []common.Provider) (common.GasPrice, error) {
for _, provider := range providers {
gasPrice, err := provider.Request()
if err == nil {
return gasPrice, nil
}
}
return common.GasPrice{}, errors.New("all providers failed")
}
// The method requests gas prices from all specified providers in parallel and returns a result with average values
func GetGasPriceAverage(providers []common.Provider) (common.GasPrice, error) {
results := common.BatchRequests(providers)
gasPrices := make([]common.GasPrice, 0, len(results))
for _, gpr := range results {
if gpr.Err == nil {
gasPrices = append(gasPrices, gpr.GasPrice)
}
}
if len(gasPrices) == 0 {
return common.GasPrice{}, errors.New("all providers failed")
}
return common.AverageGasPrice(gasPrices), nil
}