diff --git a/apr.go b/apr.go index 1102c31..6ed865e 100644 --- a/apr.go +++ b/apr.go @@ -2,8 +2,11 @@ package main import ( "encoding/json" + "fmt" + "io" "math" "net/http" + "strconv" ) type Chain struct { @@ -19,6 +22,10 @@ type APRResponse struct { Chains []ChainAPR `json:"chains"` } +type SOMMAPYResponse struct { + APY string `json:"apy"` +} + type ChainAPR struct { ChainID string `json:"chain_id"` APR float64 `json:"apr"` @@ -26,6 +33,10 @@ type ChainAPR struct { func getAPRquery(baseurl string, chainname string) (ChainAPR, error) { url := baseurl + chainname + if chainname == "sommelier" { + url = "https://lcd.sommelier-3.quicksilver.zone/sommelier/incentives/v1/apy" + } + resp, err := http.Get(url) if err != nil { return ChainAPR{}, err @@ -34,22 +45,43 @@ func getAPRquery(baseurl string, chainname string) (ChainAPR, error) { defer resp.Body.Close() var result map[string]json.RawMessage - err = json.NewDecoder(resp.Body).Decode(&result) - if err != nil { - return ChainAPR{}, err - } - + var apr float64 var chain Chain - err = json.Unmarshal(result["chain"], &chain) - if err != nil { - return ChainAPR{}, err + + if chainname == "sommelier" { + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error:", err) + return ChainAPR{}, err + } + var aprResp SOMMAPYResponse + err = json.Unmarshal(body, &aprResp) + if err != nil { + return ChainAPR{}, err + } + chain.ChainID = "sommelier-3" + apr, err = strconv.ParseFloat(aprResp.APY, 64) + if err != nil { + fmt.Println("Error:", err) + return ChainAPR{}, err + } + } else { + err = json.NewDecoder(resp.Body).Decode(&result) + if err != nil { + return ChainAPR{}, err + } + err = json.Unmarshal(result["chain"], &chain) + if err != nil { + return ChainAPR{}, err + } + apr = chain.Params.EstimatedApr } if chainname != "quicksilver" { - feeadjustedAPR := (chain.Params.EstimatedApr) * (0.965) + feeadjustedAPR := (apr) * (0.965) compoundedAPR := math.Pow(1+feeadjustedAPR/121.66, 121.66) - 1 return ChainAPR{ChainID: chain.ChainID, APR: compoundedAPR}, nil } - return ChainAPR{ChainID: chain.ChainID, APR: chain.Params.EstimatedApr}, nil + return ChainAPR{ChainID: chain.ChainID, APR: apr}, nil } diff --git a/conf.yaml b/conf.yaml index 3f419fe..8cc81d4 100644 --- a/conf.yaml +++ b/conf.yaml @@ -6,6 +6,7 @@ chains: - "stargaze" - "osmosis" - "regen" + - "sommelier" aprurl: "https://chains.cosmos.directory" aprcachetime: 15 #minutes lcdendpoint: "https://lcd.quicksilver.zone"