Skip to content

Commit 251c3bd

Browse files
committed
Add invariant helpers
1 parent ca1bd6d commit 251c3bd

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

econ/glifapi.go

+69
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,72 @@ func GetBaseFisFromAPI(agentAddr common.Address, eventsURL string) (miners []add
217217

218218
return miners, baseFis, nil
219219
}
220+
221+
func GetPoolMetricsFromAPI(eventsURL string) (*PoolMetrics, error) {
222+
url := fmt.Sprintf("%s/metrics", eventsURL)
223+
224+
resp, err := http.Get(url)
225+
if err != nil {
226+
return nil, err
227+
}
228+
defer resp.Body.Close()
229+
230+
if resp.StatusCode != http.StatusOK {
231+
// if the server can't find the agent we're asking for, it will return a 404
232+
// we treat it as 0 baseFis
233+
if resp.StatusCode == http.StatusNotFound {
234+
return nil, nil
235+
}
236+
237+
return nil, fmt.Errorf("error fetching collateral stats. Status code: %d", resp.StatusCode)
238+
}
239+
240+
body, err := io.ReadAll(resp.Body)
241+
if err != nil {
242+
return nil, err
243+
}
244+
245+
var response PoolsMetricsJSON
246+
if err := json.Unmarshal(body, &response); err != nil {
247+
return nil, err
248+
}
249+
250+
poolTotalAssets := big.NewInt(0)
251+
poolTotalAssets.SetString(response.PoolTotalAssets, 10)
252+
poolTotalBorrowed := big.NewInt(0)
253+
poolTotalBorrowed.SetString(response.PoolTotalBorrowed, 10)
254+
poolTotalBorrowableAssets := big.NewInt(0)
255+
poolTotalBorrowableAssets.SetString(response.PoolTotalBorrowableAssets, 10)
256+
poolExitReserve := big.NewInt(0)
257+
poolExitReserve.SetString(response.PoolExitReserve, 10)
258+
totalMinerCollaterals := big.NewInt(0)
259+
totalMinerCollaterals.SetString(response.TotalMinerCollaterals, 10)
260+
totalValueLocked := big.NewInt(0)
261+
totalValueLocked.SetString(response.TotalValueLocked, 10)
262+
totalMinersSectors := big.NewInt(0)
263+
totalMinersSectors.SetString(response.TotalMinersSectors, 10)
264+
totalMinerQAP := big.NewInt(0)
265+
totalMinerQAP.SetString(response.TotalMinerQAP, 10)
266+
totalMinerRBP := big.NewInt(0)
267+
totalMinerRBP.SetString(response.TotalMinerRBP, 10)
268+
totalMinerEDR := big.NewInt(0)
269+
totalMinerEDR.SetString(response.TotalMinerEDR, 10)
270+
271+
result := PoolMetrics{
272+
Height: response.Height,
273+
Timestamp: response.Timestamp,
274+
PoolTotalAssets: poolTotalAssets,
275+
PoolTotalBorrowed: poolTotalBorrowed,
276+
PoolTotalBorrowableAssets: poolTotalBorrowableAssets,
277+
PoolExitReserve: poolExitReserve,
278+
TotalAgentCount: response.TotalAgentCount,
279+
TotalMinerCollaterals: totalMinerCollaterals,
280+
TotalMinersCount: response.TotalMinersCount,
281+
TotalValueLocked: totalValueLocked,
282+
TotalMinersSectors: totalMinersSectors,
283+
TotalMinerQAP: totalMinerQAP,
284+
TotalMinerRBP: totalMinerRBP,
285+
TotalMinerEDR: totalMinerEDR,
286+
}
287+
return &result, nil
288+
}

econ/glifapi_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ func TestFetchBaseFis(t *testing.T) {
138138
})
139139
}
140140
}
141+
142+
func TestGetPoolMetricsFromAPI(t *testing.T) {
143+
metrics, err := GetPoolMetricsFromAPI(deploy.StagingEventsURL)
144+
assert.NoError(t, err)
145+
assert.NotNil(t, metrics)
146+
}

econ/types.go

+34
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,37 @@ type MinerDetailsJSON struct {
101101
TerminationFee string `json:"terminationFee"`
102102
LiquidationValue string `json:"liquidationValue"`
103103
}
104+
105+
type PoolMetrics struct {
106+
Height uint64
107+
Timestamp uint64
108+
PoolTotalAssets *big.Int
109+
PoolTotalBorrowed *big.Int
110+
PoolTotalBorrowableAssets *big.Int
111+
PoolExitReserve *big.Int
112+
TotalAgentCount uint64
113+
TotalMinerCollaterals *big.Int
114+
TotalMinersCount uint64
115+
TotalValueLocked *big.Int
116+
TotalMinersSectors *big.Int
117+
TotalMinerQAP *big.Int
118+
TotalMinerRBP *big.Int
119+
TotalMinerEDR *big.Int
120+
}
121+
122+
type PoolsMetricsJSON struct {
123+
Height uint64 `json:"height"`
124+
Timestamp uint64 `json:"timestamp"`
125+
PoolTotalAssets string `json:"poolTotalAssets"`
126+
PoolTotalBorrowed string `json:"poolTotalBorrowed"`
127+
PoolTotalBorrowableAssets string `json:"poolTotalBorrowableAssets"`
128+
PoolExitReserve string `json:"poolExitReserve"`
129+
TotalAgentCount uint64 `json:"totalAgentCount"`
130+
TotalMinerCollaterals string `json:"totalMinerCollaterals"`
131+
TotalMinersCount uint64 `json:"totalMinersCount"`
132+
TotalValueLocked string `json:"totalValueLocked"`
133+
TotalMinersSectors string `json:"totalMinersSectors"`
134+
TotalMinerQAP string `json:"totalMinerQAP"`
135+
TotalMinerRBP string `json:"totalMinerRBP"`
136+
TotalMinerEDR string `json:"totalMinerEDR"`
137+
}

0 commit comments

Comments
 (0)