Skip to content

Commit

Permalink
Add invariant helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwartz10 committed Sep 25, 2024
1 parent ca1bd6d commit 251c3bd
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
69 changes: 69 additions & 0 deletions econ/glifapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,72 @@ func GetBaseFisFromAPI(agentAddr common.Address, eventsURL string) (miners []add

return miners, baseFis, nil
}

func GetPoolMetricsFromAPI(eventsURL string) (*PoolMetrics, error) {
url := fmt.Sprintf("%s/metrics", eventsURL)

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 baseFis
if resp.StatusCode == http.StatusNotFound {
return nil, nil
}

return nil, fmt.Errorf("error fetching collateral stats. Status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var response PoolsMetricsJSON
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}

poolTotalAssets := big.NewInt(0)
poolTotalAssets.SetString(response.PoolTotalAssets, 10)
poolTotalBorrowed := big.NewInt(0)
poolTotalBorrowed.SetString(response.PoolTotalBorrowed, 10)
poolTotalBorrowableAssets := big.NewInt(0)
poolTotalBorrowableAssets.SetString(response.PoolTotalBorrowableAssets, 10)
poolExitReserve := big.NewInt(0)
poolExitReserve.SetString(response.PoolExitReserve, 10)
totalMinerCollaterals := big.NewInt(0)
totalMinerCollaterals.SetString(response.TotalMinerCollaterals, 10)
totalValueLocked := big.NewInt(0)
totalValueLocked.SetString(response.TotalValueLocked, 10)
totalMinersSectors := big.NewInt(0)
totalMinersSectors.SetString(response.TotalMinersSectors, 10)
totalMinerQAP := big.NewInt(0)
totalMinerQAP.SetString(response.TotalMinerQAP, 10)
totalMinerRBP := big.NewInt(0)
totalMinerRBP.SetString(response.TotalMinerRBP, 10)
totalMinerEDR := big.NewInt(0)
totalMinerEDR.SetString(response.TotalMinerEDR, 10)

result := PoolMetrics{
Height: response.Height,
Timestamp: response.Timestamp,
PoolTotalAssets: poolTotalAssets,
PoolTotalBorrowed: poolTotalBorrowed,
PoolTotalBorrowableAssets: poolTotalBorrowableAssets,
PoolExitReserve: poolExitReserve,
TotalAgentCount: response.TotalAgentCount,
TotalMinerCollaterals: totalMinerCollaterals,
TotalMinersCount: response.TotalMinersCount,
TotalValueLocked: totalValueLocked,
TotalMinersSectors: totalMinersSectors,
TotalMinerQAP: totalMinerQAP,
TotalMinerRBP: totalMinerRBP,
TotalMinerEDR: totalMinerEDR,
}
return &result, nil
}
6 changes: 6 additions & 0 deletions econ/glifapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ func TestFetchBaseFis(t *testing.T) {
})
}
}

func TestGetPoolMetricsFromAPI(t *testing.T) {
metrics, err := GetPoolMetricsFromAPI(deploy.StagingEventsURL)
assert.NoError(t, err)
assert.NotNil(t, metrics)
}
34 changes: 34 additions & 0 deletions econ/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,37 @@ type MinerDetailsJSON struct {
TerminationFee string `json:"terminationFee"`
LiquidationValue string `json:"liquidationValue"`
}

type PoolMetrics struct {
Height uint64
Timestamp uint64
PoolTotalAssets *big.Int
PoolTotalBorrowed *big.Int
PoolTotalBorrowableAssets *big.Int
PoolExitReserve *big.Int
TotalAgentCount uint64
TotalMinerCollaterals *big.Int
TotalMinersCount uint64
TotalValueLocked *big.Int
TotalMinersSectors *big.Int
TotalMinerQAP *big.Int
TotalMinerRBP *big.Int
TotalMinerEDR *big.Int
}

type PoolsMetricsJSON struct {
Height uint64 `json:"height"`
Timestamp uint64 `json:"timestamp"`
PoolTotalAssets string `json:"poolTotalAssets"`
PoolTotalBorrowed string `json:"poolTotalBorrowed"`
PoolTotalBorrowableAssets string `json:"poolTotalBorrowableAssets"`
PoolExitReserve string `json:"poolExitReserve"`
TotalAgentCount uint64 `json:"totalAgentCount"`
TotalMinerCollaterals string `json:"totalMinerCollaterals"`
TotalMinersCount uint64 `json:"totalMinersCount"`
TotalValueLocked string `json:"totalValueLocked"`
TotalMinersSectors string `json:"totalMinersSectors"`
TotalMinerQAP string `json:"totalMinerQAP"`
TotalMinerRBP string `json:"totalMinerRBP"`
TotalMinerEDR string `json:"totalMinerEDR"`
}

0 comments on commit 251c3bd

Please sign in to comment.