forked from flashbots/mev-boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
54 lines (45 loc) · 1.12 KB
/
common.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
package common
import (
"math/big"
"os"
"strconv"
"github.com/flashbots/go-boost-utils/types"
)
const (
SlotTimeSecMainnet = 12
)
func GetEnv(key, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}
func GetEnvInt(key string, defaultValue int) int {
if value, ok := os.LookupEnv(key); ok {
val, err := strconv.Atoi(value)
if err == nil {
return val
}
}
return defaultValue
}
func GetEnvFloat64(key string, defaultValue float64) float64 {
if value, ok := os.LookupEnv(key); ok {
val, err := strconv.ParseFloat(value, 64)
if err == nil {
return val
}
}
return defaultValue
}
// FloatEthTo256Wei converts a float (precision 10) denominated in eth to a U256Str denominated in wei
func FloatEthTo256Wei(val float64) (*types.U256Str, error) {
weiFloat := new(big.Float)
weiFloatLessPrecise := new(big.Float)
weiFloat.Mul(new(big.Float).SetFloat64(val), big.NewFloat(1e18))
weiFloatLessPrecise.SetString(weiFloat.String())
weiInt, _ := weiFloatLessPrecise.Int(nil)
weiU256 := new(types.U256Str)
err := weiU256.FromBig(weiInt)
return weiU256, err
}