-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
215 lines (181 loc) · 5.59 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package terminate
import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"strings"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
lotusapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/glifio/go-pools/constants"
"github.com/ipfs/go-cid"
"golang.org/x/exp/constraints"
"golang.org/x/xerrors"
)
// From Lotus
func parseTipSetRef(ctx context.Context, api *lotusapi.FullNodeStruct, tss string) (*types.TipSet, error) {
if tss[0] == '@' {
if tss == "@head" {
return api.ChainHead(ctx)
}
var h uint64
if _, err := fmt.Sscanf(tss, "@%d", &h); err != nil {
return nil, xerrors.Errorf("parsing height tipset ref: %w", err)
}
ctxTimeout, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
return api.ChainGetTipSetByHeight(ctxTimeout, abi.ChainEpoch(h), types.EmptyTSK)
}
cids, err := parseTipSetString(tss)
if err != nil {
return nil, err
}
if len(cids) == 0 {
return nil, nil
}
k := types.NewTipSetKey(cids...)
ts, err := api.ChainGetTipSet(ctx, k)
if err != nil {
return nil, err
}
return ts, nil
}
func parseTipSetString(ts string) ([]cid.Cid, error) {
strs := strings.Split(ts, ",")
var cids []cid.Cid
for _, s := range strs {
c, err := cid.Parse(strings.TrimSpace(s))
if err != nil {
return nil, err
}
cids = append(cids, c)
}
return cids, nil
}
func parseTipSetAndHeight(ctx context.Context, api *lotusapi.FullNodeStruct, tipset string, vmHeight uint64) (h abi.ChainEpoch, ts *types.TipSet, err error) {
h = abi.ChainEpoch(vmHeight)
if tss := tipset; tss != "" {
ts, err = parseTipSetRef(ctx, api, tss)
} else if h > 0 {
ts, err = api.ChainGetTipSetByHeight(ctx, h, types.EmptyTSK)
} else {
ts, err = api.ChainHead(ctx)
}
if err != nil {
return 0, nil, err
}
if h == 0 {
h = ts.Height()
}
return h, ts, nil
}
// https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
func max[T constraints.Ordered](a, b T) T {
if a > b {
return a
}
return b
}
func FetchAgentCollateralStats(ctx context.Context, agentID *big.Int) (*AgentCollateralStats, error) {
url := fmt.Sprintf("%s/agent/%s/collateral-value", constants.EventsURL, agentID)
// Making an HTTP GET request
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// if the server can't find the agent we're asking for, it will return a 404
// we treat it as 0 collateral
if resp.StatusCode == http.StatusNotFound {
return &AgentCollateralStats{
AvailableBalance: big.NewInt(0),
TerminationPenalty: big.NewInt(0),
MinersTerminationStats: []*MinerCollateralStat{},
Epoch: 0,
}, nil
}
return nil, fmt.Errorf("error fetching collateral stats. Status code: %d", resp.StatusCode)
}
// Read and parse the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
type minerCollateralStat struct {
Address address.Address `json:"address"`
Total string `json:"total"`
Available string `json:"available"`
Pledged string `json:"pledged"`
Vesting string `json:"vesting"`
TerminationPenalty string `json:"terminationPenalty"`
}
type agentCollateralStats struct {
AvailableBalance string `json:"agentAvailableBalance"`
TerminationPenalty string `json:"terminationPenalty"`
MinersTerminationStats []*minerCollateralStat `json:"minersCollateralStats"`
Epoch abi.ChainEpoch
}
var response agentCollateralStats
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
minersStats := make([]*MinerCollateralStat, len(response.MinersTerminationStats))
for i, miner := range response.MinersTerminationStats {
// Convert string values to big.Int
total, ok := new(big.Int).SetString(miner.Total, 10)
if !ok {
return nil, fmt.Errorf("Error converting total to big.Int")
}
available, ok := new(big.Int).SetString(miner.Available, 10)
if !ok {
return nil, fmt.Errorf("Error converting available to big.Int")
}
pledged, ok := new(big.Int).SetString(miner.Pledged, 10)
if !ok {
return nil, fmt.Errorf("Error converting pledged to big.Int")
}
vesting, ok := new(big.Int).SetString(miner.Vesting, 10)
if !ok {
return nil, fmt.Errorf("Error converting vesting to big.Int")
}
terminationPenalty, ok := new(big.Int).SetString(miner.TerminationPenalty, 10)
if !ok {
return nil, fmt.Errorf("Error converting termination penalty to big.Int")
}
minersStats[i] = &MinerCollateralStat{
Address: miner.Address,
Total: total,
Available: available,
Pledged: pledged,
Vesting: vesting,
TerminationPenalty: terminationPenalty,
}
}
// Convert string values to big.Int
availableBalance, ok := new(big.Int).SetString(response.AvailableBalance, 10)
if !ok {
return nil, fmt.Errorf("Error converting available balance to big.Int")
}
terminationPenalty, ok := new(big.Int).SetString(response.TerminationPenalty, 10)
if !ok {
return nil, fmt.Errorf("Error converting termination penalty to big.Int")
}
return &AgentCollateralStats{
AvailableBalance: availableBalance,
TerminationPenalty: terminationPenalty,
MinersTerminationStats: minersStats,
Epoch: response.Epoch,
}, nil
}