Skip to content

Commit

Permalink
Merge pull request #52 from ingenuity-build/fix/somm-APR
Browse files Browse the repository at this point in the history
APR support for SOMM added
  • Loading branch information
ajansari95 authored Aug 21, 2023
2 parents bd95695 + 32b8d02 commit 2ecaf03
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
52 changes: 42 additions & 10 deletions apr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
)

type Chain struct {
Expand All @@ -19,13 +22,21 @@ 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"`
}

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
Expand All @@ -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

}
1 change: 1 addition & 0 deletions conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ chains:
- "stargaze"
- "osmosis"
- "regen"
- "sommelier"
aprurl: "https://chains.cosmos.directory"
aprcachetime: 15 #minutes
lcdendpoint: "https://lcd.quicksilver.zone"
Expand Down

0 comments on commit 2ecaf03

Please sign in to comment.