1
1
package rates
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/csv"
6
7
"encoding/json"
@@ -70,11 +71,11 @@ type Market struct {
70
71
UsdPrice float64
71
72
URL string
72
73
// Converter for calculating the TON to USD price
73
- TonPriceConverter func (closer io. ReadCloser ) (float64 , error )
74
+ TonPriceConverter func (respBody [] byte ) (float64 , error )
74
75
// Converter for calculating fiat prices
75
- FiatPriceConverter func (closer io. ReadCloser ) (map [string ]float64 , error )
76
+ FiatPriceConverter func (respBody [] byte ) (map [string ]float64 , error )
76
77
// Converter for calculating jetton prices within pools
77
- PoolResponseConverter func (closer io. ReadCloser ) ([]Assets , []LpAsset , error )
78
+ PoolResponseConverter func (respBody [] byte ) ([]Assets , []LpAsset , error )
78
79
DateUpdate time.Time
79
80
}
80
81
@@ -149,12 +150,11 @@ func (m *Mock) GetCurrentMarketsTonPrice() ([]Market, error) {
149
150
return markets , nil
150
151
}
151
152
152
- func convertedTonGateIOResponse (respBody io.ReadCloser ) (float64 , error ) {
153
- defer respBody .Close ()
153
+ func convertedTonGateIOResponse (respBody []byte ) (float64 , error ) {
154
154
var data []struct {
155
155
Last string `json:"last"`
156
156
}
157
- if err := json .NewDecoder (respBody ). Decode ( & data ); err != nil {
157
+ if err := json .Unmarshal (respBody , & data ); err != nil {
158
158
return 0 , fmt .Errorf ("[convertedTonGateIOResponse] failed to decode response: %v" , err )
159
159
}
160
160
if len (data ) == 0 {
@@ -167,8 +167,7 @@ func convertedTonGateIOResponse(respBody io.ReadCloser) (float64, error) {
167
167
return price , nil
168
168
}
169
169
170
- func convertedTonBybitResponse (respBody io.ReadCloser ) (float64 , error ) {
171
- defer respBody .Close ()
170
+ func convertedTonBybitResponse (respBody []byte ) (float64 , error ) {
172
171
var data struct {
173
172
RetMsg string `json:"retMsg"`
174
173
Result struct {
@@ -177,7 +176,7 @@ func convertedTonBybitResponse(respBody io.ReadCloser) (float64, error) {
177
176
} `json:"list"`
178
177
} `json:"result"`
179
178
}
180
- if err := json .NewDecoder (respBody ). Decode ( & data ); err != nil {
179
+ if err := json .Unmarshal (respBody , & data ); err != nil {
181
180
return 0 , fmt .Errorf ("[convertedTonBybitResponse] failed to decode response: %v" , err )
182
181
}
183
182
if data .RetMsg != "OK" {
@@ -193,10 +192,9 @@ func convertedTonBybitResponse(respBody io.ReadCloser) (float64, error) {
193
192
return price , nil
194
193
}
195
194
196
- func convertedTonBitFinexResponse (respBody io.ReadCloser ) (float64 , error ) {
197
- defer respBody .Close ()
195
+ func convertedTonBitFinexResponse (respBody []byte ) (float64 , error ) {
198
196
var prices []float64
199
- if err := json .NewDecoder (respBody ). Decode ( & prices ); err != nil {
197
+ if err := json .Unmarshal (respBody , & prices ); err != nil {
200
198
return 0 , fmt .Errorf ("[convertedTonBitFinexResponse] failed to decode response: %v" , err )
201
199
}
202
200
if len (prices ) == 0 {
@@ -209,15 +207,14 @@ func convertedTonBitFinexResponse(respBody io.ReadCloser) (float64, error) {
209
207
return prices [0 ], nil
210
208
}
211
209
212
- func convertedTonKuCoinResponse (respBody io.ReadCloser ) (float64 , error ) {
213
- defer respBody .Close ()
210
+ func convertedTonKuCoinResponse (respBody []byte ) (float64 , error ) {
214
211
var data struct {
215
212
Success bool `json:"success"`
216
213
Data []struct {
217
214
LastTradedPrice string `json:"lastTradedPrice"`
218
215
} `json:"data"`
219
216
}
220
- if err := json .NewDecoder (respBody ). Decode ( & data ); err != nil {
217
+ if err := json .Unmarshal (respBody , & data ); err != nil {
221
218
return 0 , fmt .Errorf ("[convertedTonKuCoinResponse] failed to decode response: %v" , err )
222
219
}
223
220
if ! data .Success {
@@ -233,15 +230,14 @@ func convertedTonKuCoinResponse(respBody io.ReadCloser) (float64, error) {
233
230
return price , nil
234
231
}
235
232
236
- func convertedTonOKXResponse (respBody io.ReadCloser ) (float64 , error ) {
237
- defer respBody .Close ()
233
+ func convertedTonOKXResponse (respBody []byte ) (float64 , error ) {
238
234
var data struct {
239
235
Code string `json:"code"`
240
236
Data []struct {
241
237
Last string `json:"last"`
242
238
} `json:"data"`
243
239
}
244
- if err := json .NewDecoder (respBody ). Decode ( & data ); err != nil {
240
+ if err := json .Unmarshal (respBody , & data ); err != nil {
245
241
return 0 , fmt .Errorf ("[convertedTonOKXResponse] failed to decode response: %v" , err )
246
242
}
247
243
if data .Code != "0" {
@@ -259,8 +255,7 @@ func convertedTonOKXResponse(respBody io.ReadCloser) (float64, error) {
259
255
return price , nil
260
256
}
261
257
262
- func convertedTonHuobiResponse (respBody io.ReadCloser ) (float64 , error ) {
263
- defer respBody .Close ()
258
+ func convertedTonHuobiResponse (respBody []byte ) (float64 , error ) {
264
259
var data struct {
265
260
Status string `json:"status"`
266
261
Tick struct {
@@ -271,7 +266,7 @@ func convertedTonHuobiResponse(respBody io.ReadCloser) (float64, error) {
271
266
} `json:"data"`
272
267
} `json:"tick"`
273
268
}
274
- if err := json .NewDecoder (respBody ). Decode ( & data ); err != nil {
269
+ if err := json .Unmarshal (respBody , & data ); err != nil {
275
270
return 0 , fmt .Errorf ("[convertedTonHuobiResponse] failed to decode response: %v" , err )
276
271
}
277
272
if data .Status != "ok" {
@@ -316,14 +311,13 @@ func getFiatPrices(tonPrice float64) map[string]float64 {
316
311
return prices
317
312
}
318
313
319
- func convertedCoinBaseFiatPricesResponse (respBody io.ReadCloser ) (map [string ]float64 , error ) {
320
- defer respBody .Close ()
314
+ func convertedCoinBaseFiatPricesResponse (respBody []byte ) (map [string ]float64 , error ) {
321
315
var data struct {
322
316
Data struct {
323
317
Rates map [string ]string `json:"rates"`
324
318
} `json:"data"`
325
319
}
326
- if err := json .NewDecoder (respBody ). Decode ( & data ); err != nil {
320
+ if err := json .Unmarshal (respBody , & data ); err != nil {
327
321
return map [string ]float64 {}, fmt .Errorf ("[convertedCoinBaseFiatPricesResponse] failed to decode response: %v" , err )
328
322
}
329
323
prices := make (map [string ]float64 )
@@ -432,9 +426,8 @@ func (m *Mock) getJettonPricesFromDex(pools map[ton.AccountID]float64) map[ton.A
432
426
return pools
433
427
}
434
428
435
- func convertedStonFiPoolResponse (respBody io.ReadCloser ) ([]Assets , []LpAsset , error ) {
436
- defer respBody .Close ()
437
- reader := csv .NewReader (respBody )
429
+ func convertedStonFiPoolResponse (respBody []byte ) ([]Assets , []LpAsset , error ) {
430
+ reader := csv .NewReader (bytes .NewReader (respBody ))
438
431
records , err := reader .ReadAll ()
439
432
if err != nil {
440
433
return nil , nil , err
@@ -546,9 +539,8 @@ func convertedStonFiPoolResponse(respBody io.ReadCloser) ([]Assets, []LpAsset, e
546
539
return actualAssets , maps .Values (actualLpAssets ), nil
547
540
}
548
541
549
- func convertedDeDustPoolResponse (respBody io.ReadCloser ) ([]Assets , []LpAsset , error ) {
550
- defer respBody .Close ()
551
- reader := csv .NewReader (respBody )
542
+ func convertedDeDustPoolResponse (respBody []byte ) ([]Assets , []LpAsset , error ) {
543
+ reader := csv .NewReader (bytes .NewReader (respBody ))
552
544
records , err := reader .ReadAll ()
553
545
if err != nil {
554
546
return nil , nil , err
@@ -795,8 +787,7 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
795
787
return calculatedAccount , price
796
788
}
797
789
798
- // Note: You must close resp.Body in the handler function; here, it is closed ONLY in case of a bad status_code
799
- func sendRequest (url , token string ) (io.ReadCloser , error ) {
790
+ func sendRequest (url , token string ) ([]byte , error ) {
800
791
ctx , cancel := context .WithTimeout (context .Background (), time .Second * 15 )
801
792
defer cancel ()
802
793
@@ -812,10 +803,14 @@ func sendRequest(url, token string) (io.ReadCloser, error) {
812
803
if err != nil {
813
804
return nil , err
814
805
}
806
+ defer resp .Body .Close ()
815
807
if resp .StatusCode != http .StatusOK {
816
- resp .Body .Close ()
817
808
return nil , fmt .Errorf ("unexpected status code: %d" , resp .StatusCode )
818
809
}
810
+ body , err := io .ReadAll (resp .Body )
811
+ if err != nil {
812
+ return nil , fmt .Errorf ("failed to read response body: %w" , err )
813
+ }
819
814
820
- return resp . Body , nil
815
+ return body , nil
821
816
}
0 commit comments