-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
90 lines (77 loc) · 2.21 KB
/
utils.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 gohubble
import (
"crypto/ecdsa"
"errors"
"fmt"
"math"
"math/big"
"net/url"
"os"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
rpcEndpoint string
webSocketEndpoint string
)
func FloatToBigInt(float float64, scale int) *big.Int {
// Create a big.Float value from the input float64 value
bigFloat := new(big.Float).SetFloat64(float)
// Create the scale as a big.Float
scaleFactor := new(big.Float).SetFloat64(math.Pow10(scale))
// Multiply the input big.Float value with the scale
bigFloat.Mul(bigFloat, scaleFactor)
// Convert big.Float to big.Int
bigInt := new(big.Int)
bigFloat.Int(bigInt)
return bigInt
}
func StringToFloat(str string) float64 {
float, err := strconv.ParseFloat(str, 64)
if err != nil {
panic(fmt.Sprintf("can't convert string to float - string = %s", str))
}
return float
}
func getAddressFromPrivateKey(privateKey *ecdsa.PrivateKey) (common.Address, error) {
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return common.Address{}, errors.New("unable to get address from private key")
}
address := crypto.PubkeyToAddress(*publicKeyECDSA)
return address, nil
}
func getRPCEndpoint() string {
if rpcEndpoint == "" {
rpcHost := os.Getenv("HUBBLE_RPC_HOST")
if rpcHost == "" {
panic("HUBBLE_RPC_HOST environment variable not set")
}
blockchainID := os.Getenv("HUBBLE_BLOCKCHAIN_ID")
if rpcHost == "" {
panic("HUBBLE_BLOCKCHAIN_ID environment variable not set")
}
path := fmt.Sprintf("/ext/bc/%s/rpc", blockchainID)
rpcURL := url.URL{Scheme: "https", Host: rpcHost, Path: path}
rpcEndpoint = rpcURL.String()
}
return rpcEndpoint
}
func getWebSocketEndpoint() string {
if webSocketEndpoint == "" {
rpcHost := os.Getenv("HUBBLE_RPC_HOST")
if rpcHost == "" {
panic("HUBBLE_RPC_HOST environment variable not set")
}
blockchainID := os.Getenv("HUBBLE_BLOCKCHAIN_ID")
if rpcHost == "" {
panic("HUBBLE_BLOCKCHAIN_ID environment variable not set")
}
path := fmt.Sprintf("/ext/bc/%s/ws", blockchainID)
websocketURL := url.URL{Scheme: "wss", Host: rpcHost, Path: path}
webSocketEndpoint = websocketURL.String()
}
return webSocketEndpoint
}