Skip to content

Commit

Permalink
make supply endpoint use supply module
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-bowman committed Dec 30, 2024
1 parent 2d744e0 commit c966986
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1,407 deletions.
26 changes: 21 additions & 5 deletions conf.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
lcd_endpoint: "https://lcd.quicksilver.zone"
rpc_endpoint: "https://rpc.quicksilver.zone:443"
chain_rpc_endpoint: "https://rpc.%s.quicksilver.zone:443"
lcd_endpoint: "http://quicksilver-archive-svc.default.svc.cluster.local:1317"
supply_lcd_endpoint: "http://quicksilver-archive-svc.default.svc.cluster.local:1317"
rpc_endpoint: "http://quicksilver-archive-svc.default.svc.cluster.local:26657"
chain_rpc_endpoint: "https://%s.rpc.quicksilver.zone:443"
chains:
- quicksilver
- cosmoshub
Expand All @@ -14,10 +15,15 @@ chains:
- saga
- celestia
- agoric
- xion
- archway
- omniflixhub

apr_url: "https://chains.cosmos.directory"
apr_cache_minutes: 15
supply_cache_minutes: 180
defi_apis:
osmo_assets: https://celatone-api-prod.alleslabs.dev/v1/osmosis/osmosis-1/assets?with_prices=true
shade: https://na36v10ce3.execute-api.us-east-1.amazonaws.com/API-mainnet-STAGE/shadeswap/pairs
osmosis: https://app.osmosis.zone/api/pools?page=1&limit=500&min_liquidity=500
osmosis_apy: https://public-osmosis-api.numia.xyz/pools_apr
Expand All @@ -28,6 +34,11 @@ defi:
action: Add Liquidity
link: https://app.osmosis.zone/pool/944
id: 944
- assetPair: qATOM/ATOM
provider: osmosis
action: Add Liquidity
link: https://app.osmosis.zone/pool/1589
id: 1589
- assetPair: qSTARS/STARS
provider: osmosis
action: Add Liquidity
Expand All @@ -43,6 +54,11 @@ defi:
action: Add Liquidity
link: https://app.osmosis.zone/pool/956
id: 956
- assetPair: qOSMO/OSMO
provider: osmosis
action: Add Liquidity
link: https://app.osmosis.zone/pool/1590
id: 1590
- assetPair: qSOMM/SOMM
provider: osmosis
action: Add Liquidity
Expand All @@ -57,13 +73,13 @@ defi:
provider: shade
action: Add Liquidity
apy: 0.459
tvl: 100400
tvl: 2160
link: https://app.shadeprotocol.io/swap/pools
id: 17dddb72-5719-4328-a3a7-1c84b4f7a932
- assetPair: qATOM/ATOM
provider: shade
action: Add Liquidity
apy: 0.121
tvl: 75760
tvl: 3030
link: https://app.shadeprotocol.io/swap/pools
id: 44c1658d-8626-4bf9-835f-d13aefec4c09
73 changes: 37 additions & 36 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *Service) ConfigureRoutes() {

data, found := s.Cache.Get(key)
if !found {
return s.getTotalSupply(ctx, key)
return s.getSupply(ctx, key)
}
return ctx.JSONBlob(http.StatusOK, data.([]byte))
})
Expand Down Expand Up @@ -354,68 +354,67 @@ func (s *Service) getAPR(ctx echov4.Context, key string) error {
return ctx.JSONBlob(http.StatusOK, respdata)
}

func (s *Service) getTotalSupply(ctx echov4.Context, key string) error {
s.Echo.Logger.Infof("getTotalSupply")
func (s *Service) getSupply(ctx echov4.Context, key string) error {
s.Echo.Logger.Infof("getSupply")

totalSupply, err := getTotalSupply(s.Config.LcdEndpoint + "/cosmos/bank/v1beta1/supply")
supply, _, err := getSupply(s.Config.SupplyLcdEndpoint + "/quicksilver/supply/v1/supply")
if err != nil {
s.Echo.Logger.Errorf("getTotalSupply: %v - %v", ErrUnableToGetTotalSupply, err)
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrUnableToGetTotalSupply, err)
return ErrUnableToGetTotalSupply
}
s.Echo.Logger.Info("totalSupply", " -> ", totalSupply)
respData, err := json.Marshal(float64(totalSupply.Int64()) / 1_000_000)

respData, err := json.Marshal(supply.Quo(sdkmath.NewInt(1_000_000)).Int64())
if err != nil {
s.Echo.Logger.Errorf("getTotalSupply: %v - %v", ErrMarshalResponse, err)
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrMarshalResponse, err)
return ErrMarshalResponse
}
s.Cache.SetWithTTL(key, respData, 1, time.Duration(s.Config.SupplyCacheTime)*time.Minute)

return ctx.JSONBlob(http.StatusOK, respData)
}

func (s *Service) getCirculatingSupply(ctx echov4.Context, key string) error {
s.Echo.Logger.Infof("getCirculatingSupply")

var CirculatingSupplyResponse int64

totalLockedTokens := sdkmath.ZeroInt()

for _, address := range VESTING_ACCOUNTS {
lockedTokensForAddress, err := getVestingAccountLocked(s.Config.LcdEndpoint+"/cosmos/auth/v1beta1/accounts/", address)
if err != nil {
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrUnableToGetLockedTokens, err)
return ErrUnableToGetLockedTokens
}
totalLockedTokens = totalLockedTokens.Add(lockedTokensForAddress)
s.Echo.Logger.Info("lockedTokensFor", address, " -> ", lockedTokensForAddress)
}

totalSupply, err := getTotalSupply(s.Config.LcdEndpoint + "/cosmos/bank/v1beta1/supply")
_, circulatingSupply, err := getSupply(s.Config.SupplyLcdEndpoint + "/quicksilver/supply/v1/supply")
if err != nil {
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrUnableToGetTotalSupply, err)
return ErrUnableToGetTotalSupply
}
s.Echo.Logger.Info("totalSupply", " -> ", totalSupply)

communityPoolBalance, err := getCommunityPool(s.Config.LcdEndpoint + "/cosmos/distribution/v1beta1/community_pool")
respData, err := json.Marshal(circulatingSupply.Quo(sdkmath.NewInt(1_000_000)).Int64())
if err != nil {
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrUnableToGetCommunityPool, err)
return ErrUnableToGetCommunityPool
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrMarshalResponse, err)
return ErrMarshalResponse
}
s.Cache.SetWithTTL(key, respData, 1, time.Duration(s.Config.SupplyCacheTime)*time.Minute)

s.Echo.Logger.Info("communityPoolBalance", " -> ", communityPoolBalance)
return ctx.JSONBlob(http.StatusOK, respData)
}

totalCirculatingSupply := totalSupply.Sub(totalLockedTokens).Sub(communityPoolBalance).Sub(sdkmath.NewInt(500_000_000_000)) // unknown account
CirculatingSupplyResponse = totalCirculatingSupply.Int64()
type Supply struct {
Supply sdkmath.Int `json:"supply"`
CirculatingSupply sdkmath.Int `json:"circulating_supply"`
}

respData, err := json.Marshal(float64(CirculatingSupplyResponse) / 1_000_000)
func getSupply(url string) (sdkmath.Int, sdkmath.Int, error) {
resp, err := http.Get(url)
if err != nil {
s.Echo.Logger.Errorf("getCirculatingSupply: %v - %v", ErrMarshalResponse, err)
return ErrMarshalResponse
return sdkmath.Int{}, sdkmath.Int{}, err
}
s.Cache.SetWithTTL(key, respData, 1, time.Duration(s.Config.SupplyCacheTime)*time.Minute)

return ctx.JSONBlob(http.StatusOK, respData)
var result json.RawMessage
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return sdkmath.Int{}, sdkmath.Int{}, err
}

var supply Supply
err = json.Unmarshal(result, &supply)
if err != nil {
return sdkmath.Int{}, sdkmath.Int{}, err
}

return supply.Supply, supply.CirculatingSupply, nil

}

Expand Down Expand Up @@ -671,6 +670,7 @@ func (s *Service) queryOsmo() (OsmosisPoolCacheResult, error) {
if err != nil {
return OsmosisPoolCacheResult{}, err
}
fmt.Printf("poolResult: %v\n", poolResult)

s.Logger.Info("querying osmosis apr api")

Expand All @@ -684,6 +684,7 @@ func (s *Service) queryOsmo() (OsmosisPoolCacheResult, error) {
if err != nil {
return OsmosisPoolCacheResult{}, err
}
fmt.Printf("poolResult.PoolAprs: %v\n", poolResult.PoolAprs)

s.Cache.SetWithTTL("defi.raw.osmosis", poolResult, 1, 3*time.Hour)
time.Sleep(time.Millisecond * 200)
Expand Down
19 changes: 10 additions & 9 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ type Service struct {
}

type Config struct {
RpcEndpoint string `yaml:"rpc_endpoint" json:"rpc_endpoint"`
LcdEndpoint string `yaml:"lcd_endpoint" json:"lcd_endpoint"`
ChainHost string `yaml:"chain_rpc_endpoint" json:"chain_rpc_endpoint"`
Chains []string `yaml:"chains" json:"chains"`
APRURL string `yaml:"apr_url" json:"apr_url"`
APRCacheTime int `yaml:"apr_cache_minutes" json:"apr_cache_minutes"`
SupplyCacheTime int `yaml:"supply_cache_minutes" json:"supply_cache_minutes"`
DefiInfo []DefiInfo `yaml:"defi" json:"defi"`
DefiApis DefiApis `yaml:"defi_apis" json:"defi_apis"`
RpcEndpoint string `yaml:"rpc_endpoint" json:"rpc_endpoint"`
LcdEndpoint string `yaml:"lcd_endpoint" json:"lcd_endpoint"`
SupplyLcdEndpoint string `yaml:"supply_lcd_endpoint" json:"supply_lcd_endpoint"`
ChainHost string `yaml:"chain_rpc_endpoint" json:"chain_rpc_endpoint"`
Chains []string `yaml:"chains" json:"chains"`
APRURL string `yaml:"apr_url" json:"apr_url"`
APRCacheTime int `yaml:"apr_cache_minutes" json:"apr_cache_minutes"`
SupplyCacheTime int `yaml:"supply_cache_minutes" json:"supply_cache_minutes"`
DefiInfo []DefiInfo `yaml:"defi" json:"defi"`
DefiApis DefiApis `yaml:"defi_apis" json:"defi_apis"`
}

type DefiInfo struct {
Expand Down
134 changes: 0 additions & 134 deletions supply.go

This file was deleted.

Loading

0 comments on commit c966986

Please sign in to comment.