-
Notifications
You must be signed in to change notification settings - Fork 2
/
apr.go
154 lines (126 loc) · 3.33 KB
/
apr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
sdkmath "cosmossdk.io/math"
)
type Chain struct {
ChainID string `json:"chain_id"`
Params Params `json:"params"`
}
type Params struct {
EstimatedApr float64 `json:"estimated_apr"`
}
type APRResponse struct {
Chains []ChainAPR `json:"chains"`
}
type ChainAPR struct {
ChainID string `json:"chain_id"`
APR float64 `json:"apr"`
}
func getAPRquery(cfg Config, chainName string) (ChainAPR, error) {
var apr float64
var chainID string
var err error
switch chainName {
case "sommelier":
chainID, apr, err = SommelierApr(cfg, chainName)
case "stargaze":
chainID, apr, err = StargazeApr(cfg, chainName)
default:
chainID, apr, err = BasicApr(cfg, chainName)
}
if err != nil {
return ChainAPR{}, err
}
if chainName != "quicksilver" {
feeadjustedAPR := (apr) * (0.965)
compoundedAPR := math.Pow(1+feeadjustedAPR/121.66, 121.66) - 1
return ChainAPR{ChainID: chainID, APR: compoundedAPR}, nil
}
return ChainAPR{ChainID: chainID, APR: apr}, nil
}
func BasicApr(cfg Config, chainName string) (string, float64, error) {
url := fmt.Sprintf("%s/%s", cfg.APRURL, chainName)
resp, err := http.Get(url)
if err != nil {
return "", 0, err
}
defer resp.Body.Close()
var result map[string]json.RawMessage
var chain Chain
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", 0, err
}
err = json.Unmarshal(result["chain"], &chain)
if err != nil {
return "", 0, err
}
return chain.ChainID, chain.Params.EstimatedApr, nil
}
func StargazeApr(cfg Config, chainname string) (string, float64, error) {
provisionsUrl := "https://lcd.stargaze-1.quicksilver.zone/stargaze/mint/v1beta1/annual_provisions"
bondedUrl := "https://lcd.stargaze-1.quicksilver.zone/cosmos/staking/v1beta1/pool"
provisionQuery, err := http.Get(provisionsUrl)
if err != nil {
return "", 0, err
}
defer provisionQuery.Body.Close()
var provisionsResult map[string]sdkmath.LegacyDec
provisionsResponse, err := io.ReadAll(provisionQuery.Body)
if err != nil {
fmt.Println("Error:", err)
return "", 0, err
}
err = json.Unmarshal(provisionsResponse, &provisionsResult)
if err != nil {
return "", 0, err
}
provisions, err := provisionsResult["annual_provisions"].Float64()
if err != nil {
return "", 0, err
}
bondedQuery, err := http.Get(bondedUrl)
if err != nil {
return "", 0, err
}
defer bondedQuery.Body.Close()
var bondedResult map[string]map[string]sdkmath.Int
bondedResponse, err := io.ReadAll(bondedQuery.Body)
if err != nil {
fmt.Println("Error:", err)
return "", 0, err
}
err = json.Unmarshal(bondedResponse, &bondedResult)
if err != nil {
return "", 0, err
}
bonded := float64(bondedResult["pool"]["bonded_tokens"].Int64())
return "stargaze-1", provisions / bonded, nil
}
func SommelierApr(cfg Config, chainname string) (string, float64, error) {
url := "https://lcd.sommelier-3.quicksilver.zone/sommelier/incentives/v1/apy"
query, err := http.Get(url)
if err != nil {
return "", 0, err
}
var result map[string]sdkmath.LegacyDec
response, err := io.ReadAll(query.Body)
if err != nil {
fmt.Println("Error:", err)
return "", 0, err
}
err = json.Unmarshal(response, &result)
if err != nil {
return "", 0, err
}
apy, err := result["apy"].Float64()
if err != nil {
return "", 0, err
}
return "sommelier-3", apy, nil
}