-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.go
208 lines (196 loc) · 5.28 KB
/
api.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package godium
import (
"encoding/json"
"github.com/gagliardetto/solana-go"
"io/ioutil"
"net/http"
"sort"
)
// get pools data via raydium api
func GetPoolsViaApi() (PoolsV3Data, error) {
var poolsData PoolsV3Data
client := http.Client{}
request, err := http.NewRequest("GET", POOLS_V3_API, nil)
if err != nil {
return poolsData, err
}
request.Header.Set("Content-Type", "application/json")
// Make request
response, err := client.Do(request)
if err != nil {
return poolsData, err
}
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return poolsData, err
}
defer response.Body.Close()
err = json.Unmarshal(bodyBytes, &poolsData)
return poolsData, err
}
func GetTokens() (TokenList, error) {
var tokenData TokenList
client := http.Client{}
request, err := http.NewRequest("GET", TOKENS_API, nil)
if err != nil {
return tokenData, err
}
request.Header.Set("Content-Type", "application/json")
// Make request
response, err := client.Do(request)
if err != nil {
return tokenData, err
}
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return tokenData, err
}
defer response.Body.Close()
err = json.Unmarshal(bodyBytes, &tokenData)
return tokenData, err
}
type Pool struct {
ID string `json:"id"`
MintA string `json:"mintA"`
MintB string `json:"mintB"`
VaultA string `json:"vaultA"`
VaultB string `json:"vaultB"`
MintDecimalsA int `json:"mintDecimalsA"`
MintDecimalsB int `json:"mintDecimalsB"`
AmmConfig struct {
ID string `json:"id"`
Index int `json:"index"`
ProtocolFeeRate int `json:"protocolFeeRate"`
TradeFeeRate int `json:"tradeFeeRate"`
TickSpacing int `json:"tickSpacing"`
FundFeeRate int `json:"fundFeeRate"`
FundOwner string `json:"fundOwner"`
Description string `json:"description"`
} `json:"ammConfig"`
Tvl float64 `json:"tvl"`
Day struct {
Volume float64 `json:"volume"`
VolumeFee float64 `json:"volumeFee"`
FeeA float64 `json:"feeA"`
FeeB float64 `json:"feeB"`
FeeApr float64 `json:"feeApr"`
RewardApr struct {
A float64 `json:"A"`
B float64 `json:"B"`
C float64 `json:"C"`
} `json:"rewardApr"`
Apr float64 `json:"apr"`
PriceMin float64 `json:"priceMin"`
PriceMax float64 `json:"priceMax"`
} `json:"day"`
Week struct {
Volume float64 `json:"volume"`
VolumeFee float64 `json:"volumeFee"`
FeeA float64 `json:"feeA"`
FeeB float64 `json:"feeB"`
FeeApr float64 `json:"feeApr"`
RewardApr struct {
A float64 `json:"A"`
B float64 `json:"B"`
C float64 `json:"C"`
} `json:"rewardApr"`
Apr float64 `json:"apr"`
PriceMin float64 `json:"priceMin"`
PriceMax float64 `json:"priceMax"`
} `json:"week"`
Month struct {
Volume float64 `json:"volume"`
VolumeFee float64 `json:"volumeFee"`
FeeA float64 `json:"feeA"`
FeeB float64 `json:"feeB"`
FeeApr float64 `json:"feeApr"`
RewardApr struct {
A float64 `json:"A"`
B float64 `json:"B"`
C float64 `json:"C"`
} `json:"rewardApr"`
Apr float64 `json:"apr"`
PriceMin float64 `json:"priceMin"`
PriceMax float64 `json:"priceMax"`
} `json:"month"`
Price float64 `json:"price"`
LookupTableAccount string `json:"lookupTableAccount"`
}
type PoolsV3Data struct {
Data []Pool `json:"data"`
}
func (p Pool) HasMintKey(mint solana.PublicKey) bool {
if p.MintA == mint.String() || p.MintB == mint.String() {
return true
}
return false
}
func (p Pool) HasMintKeyAsString(mint string) bool {
if p.MintA == mint || p.MintB == mint {
return true
}
return false
}
// find pools with mints
func (p PoolsV3Data) GetPoolsViaMints(mintA, mintB solana.PublicKey) []Pool {
found := make([]Pool, 0)
for _, pool := range p.Data {
if pool.HasMintKey(mintA) && pool.HasMintKey(mintB) {
found = append(found, pool)
}
}
sort.SliceStable(found, func(i, j int) bool {
return int(found[i].Tvl) > int(found[j].Tvl)
})
return found
}
// find pools with symbola symbolb
func (p PoolsV3Data) GetPoolsViaMintsAsStrins(mintA, mintB string) []Pool {
found := make([]Pool, 0)
for _, pool := range p.Data {
if pool.HasMintKeyAsString(mintA) && pool.HasMintKeyAsString(mintB) {
found = append(found, pool)
}
}
sort.SliceStable(found, func(i, j int) bool {
return int(found[i].Tvl) > int(found[j].Tvl)
})
return found
}
type Token struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
Mint string `json:"mint"`
Decimals int `json:"decimals"`
Extensions struct {
CoingeckoID string `json:"coingeckoId"`
} `json:"extensions,omitempty"`
Icon string `json:"icon"`
HasFreeze int `json:"hasFreeze"`
}
type TokenList struct {
Name string `json:"name"`
Official []Token `json:"official"`
UnOfficial []Token `json:"unOfficial"`
UnNamed []struct {
Mint string `json:"mint"`
Decimals int `json:"decimals"`
HasFreeze int `json:"hasFreeze"`
} `json:"unNamed"`
Blacklist []interface{} `json:"blacklist"`
}
// find name and symbol
func (t TokenList) FindInfo(mintAsString string) Token {
var token Token
for _, token := range t.Official {
if token.Mint == mintAsString {
return token
}
}
for _, token := range t.UnOfficial {
if token.Mint == mintAsString {
return token
}
}
return token
}