forked from somatic-labs/meteorite
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.go
More file actions
117 lines (101 loc) · 2.72 KB
/
lib.go
File metadata and controls
117 lines (101 loc) · 2.72 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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"strconv"
"time"
"github.com/BurntSushi/toml"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func getInitialSequence(address string, config Config) (int64, int64) {
resp, err := httpGet(config.Nodes.API + "/cosmos/auth/v1beta1/accounts/" + address)
if err != nil {
log.Printf("Failed to get initial sequence: %v", err)
return 0, 0
}
var accountRes AccountResult
err = json.Unmarshal(resp, &accountRes)
if err != nil {
log.Printf("Failed to unmarshal account result: %v", err)
return 0, 0
}
seqint, err := strconv.ParseInt(accountRes.Account.Sequence, 10, 64)
if err != nil {
log.Printf("Failed to convert sequence to int: %v", err)
return 0, 0
}
accnum, err := strconv.ParseInt(accountRes.Account.AccountNumber, 10, 64)
if err != nil {
log.Printf("Failed to convert account number to int: %v", err)
return 0, 0
}
return seqint, accnum
}
func getChainID(nodeURL string) (string, error) {
resp, err := httpGet(nodeURL + "/status")
if err != nil {
log.Printf("Failed to get node status: %v", err)
return "", err
}
var statusRes NodeStatusResponse
err = json.Unmarshal(resp, &statusRes)
if err != nil {
log.Printf("Failed to unmarshal node status result: %v", err)
return "", err
}
return statusRes.Result.NodeInfo.Network, nil
}
func httpGet(url string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
netErr, ok := err.(net.Error)
if ok && netErr.Timeout() {
log.Printf("Request to %s timed out, continuing...", url)
return nil, nil
}
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// This function will load our nodes from nodes.toml.
func loadNodes() []string {
var config Config
if _, err := toml.DecodeFile("nodes.toml", &config); err != nil {
log.Fatalf("Failed to load nodes.toml: %v", err)
}
return config.Nodes.RPC
}
func getGasPrice(value interface{}, denom string) sdk.DecCoin {
var decValue sdk.Dec
switch v := value.(type) {
case int64:
decValue = sdk.NewDec(v)
case float64:
var err error
decValue, err = sdk.NewDecFromStr(fmt.Sprintf("%f", v))
if err != nil {
log.Fatalf("Failed to convert float64 to Dec: %v", err)
}
default:
log.Fatalf("Invalid type for gas price: %T", v)
}
// Assuming the gas price needs to be scaled to a specific precision
decCoin := sdk.NewDecCoinFromDec(denom, decValue.Quo(sdk.NewDec(10000))) // Adjust the scaling factor as needed
return decCoin
}