Skip to content

Commit a3257a8

Browse files
fix the func that parses assets
1 parent 960e9a2 commit a3257a8

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

pkg/rates/market.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ func (m *Mock) GetCurrentMarketsTonPrice() ([]Market, error) {
127127
},
128128
}
129129
for idx, market := range markets {
130-
respBody, err := sendRequest(market.URL, "")
130+
headers := http.Header{"Content-Type": {"application/json"}}
131+
respBody, err := sendRequest(market.URL, "", headers)
131132
if err != nil {
132133
slog.Error("[GetCurrentMarketsTonPrice] failed to send request", slog.Any("error", err))
133134
errorsCounter.WithLabelValues(market.Name).Inc()
@@ -290,7 +291,8 @@ func getFiatPrices(tonPrice float64) map[string]float64 {
290291
}
291292
prices := make(map[string]float64)
292293
for _, market := range markets {
293-
respBody, err := sendRequest(market.URL, "")
294+
headers := http.Header{"Content-Type": {"application/json"}}
295+
respBody, err := sendRequest(market.URL, "", headers)
294296
if err != nil {
295297
slog.Error("[getFiatPrices] failed to send request", slog.Any("error", err))
296298
errorsCounter.WithLabelValues(market.Name).Inc()
@@ -372,7 +374,8 @@ func (m *Mock) getJettonPricesFromDex(pools map[ton.AccountID]float64) map[ton.A
372374
var actualLpAssets []LpAsset
373375
// Fetch and parse pool data from each market
374376
for _, market := range markets {
375-
respBody, err := sendRequest(market.URL, "")
377+
headers := http.Header{"Accept": {"text/csv"}}
378+
respBody, err := sendRequest(market.URL, "", headers)
376379
if err != nil {
377380
slog.Error("[getJettonPricesFromDex] failed to send request", slog.Any("error", err), slog.String("url", market.URL))
378381
errorsCounter.WithLabelValues(market.Name).Inc()
@@ -451,23 +454,27 @@ func convertedStonFiPoolResponse(respBody []byte) ([]Assets, []LpAsset, error) {
451454
return Assets{}, err
452455
}
453456
firstMeta := make(map[string]any)
454-
if err = json.Unmarshal([]byte(record[4]), &firstMeta); err != nil {
455-
return Assets{}, err
457+
if record[4] != "NULL" {
458+
if err = json.Unmarshal([]byte(record[4]), &firstMeta); err != nil {
459+
return Assets{}, err
460+
}
456461
}
457462
value, ok := firstMeta["decimals"]
458-
if !ok {
463+
if !ok || value != "NaN" {
459464
value = fmt.Sprintf("%d", defaultDecimals)
460465
}
461466
firstAsset.Decimals, err = strconv.Atoi(value.(string))
462467
if err != nil {
463468
return Assets{}, err
464469
}
465470
secondMeta := make(map[string]any)
466-
if err = json.Unmarshal([]byte(record[5]), &secondMeta); err != nil {
467-
return Assets{}, err
471+
if record[5] != "NULL" {
472+
if err = json.Unmarshal([]byte(record[5]), &secondMeta); err != nil {
473+
return Assets{}, err
474+
}
468475
}
469476
value, ok = secondMeta["decimals"]
470-
if !ok {
477+
if !ok || value != "NaN" {
471478
value = fmt.Sprintf("%d", defaultDecimals)
472479
}
473480
secondAsset.Decimals, err = strconv.Atoi(value.(string))
@@ -515,7 +522,7 @@ func convertedStonFiPoolResponse(respBody []byte) ([]Assets, []LpAsset, error) {
515522
}
516523
assets, err := parseAssets(record)
517524
if err != nil {
518-
slog.Error("failed to parse assets", slog.Any("error", err))
525+
slog.Error("failed to parse assets", slog.Any("error", err), slog.Any("assets", record))
519526
continue
520527
}
521528
firstAsset, secondAsset := assets.Assets[0], assets.Assets[1]
@@ -554,8 +561,8 @@ func convertedDeDustPoolResponse(respBody []byte) ([]Assets, []LpAsset, error) {
554561
return 0, err
555562
}
556563
value, ok := converted["decimals"]
557-
if !ok {
558-
value = "9"
564+
if !ok || value == "NaN" {
565+
value = fmt.Sprintf("%d", defaultDecimals)
559566
}
560567
decimals, err := strconv.Atoi(value.(string))
561568
if err != nil {
@@ -787,15 +794,15 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
787794
return calculatedAccount, price
788795
}
789796

790-
func sendRequest(url, token string) ([]byte, error) {
797+
func sendRequest(url, token string, headers http.Header) ([]byte, error) {
791798
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
792799
defer cancel()
793800

794801
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
795802
if err != nil {
796803
return nil, err
797804
}
798-
req.Header.Set("Content-Type", "application/json")
805+
req.Header = headers
799806
if token != "" {
800807
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
801808
}

pkg/rates/sources.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"math/big"
8+
"net/http"
89
"sort"
910
"strconv"
1011
"strings"
@@ -141,7 +142,8 @@ type ExecutionResult struct {
141142
// getBemoPrice fetches BEMO price by smart contract data
142143
func (m *Mock) getBemoPrice(_ float64, _ map[ton.AccountID]float64) (map[ton.AccountID]float64, error) {
143144
url := fmt.Sprintf("https://tonapi.io/v2/blockchain/accounts/%v/methods/get_full_data", references.BemoAccount.ToRaw())
144-
respBody, err := sendRequest(url, m.TonApiToken)
145+
headers := http.Header{"Content-Type": {"application/json"}}
146+
respBody, err := sendRequest(url, m.TonApiToken, headers)
145147
if err != nil {
146148
return nil, err
147149
}
@@ -168,7 +170,8 @@ func (m *Mock) getBemoPrice(_ float64, _ map[ton.AccountID]float64) (map[ton.Acc
168170
// getTonstakersPrice fetches Tonstakers price by smart contract data
169171
func (m *Mock) getTonstakersPrice(_ float64, _ map[ton.AccountID]float64) (map[ton.AccountID]float64, error) {
170172
url := fmt.Sprintf("https://tonapi.io/v2/blockchain/accounts/%v/methods/get_pool_full_data", references.TonstakersAccountPool.ToRaw())
171-
respBody, err := sendRequest(url, m.TonApiToken)
173+
headers := http.Header{"Content-Type": {"application/json"}}
174+
respBody, err := sendRequest(url, m.TonApiToken, headers)
172175
if err != nil {
173176
return nil, err
174177
}
@@ -204,7 +207,8 @@ func (m *Mock) getBeetrootPrice(tonPrice float64, _ map[ton.AccountID]float64) (
204207
account := ton.MustParseAccountID("EQAFGhmx199oH6kmL78PGBHyAx4d5CiJdfXwSjDK5F5IFyfC")
205208

206209
url := fmt.Sprintf("https://tonapi.io/v2/blockchain/accounts/%v/methods/get_price_data", contract)
207-
respBody, err := sendRequest(url, m.TonApiToken)
210+
headers := http.Header{"Content-Type": {"application/json"}}
211+
respBody, err := sendRequest(url, m.TonApiToken, headers)
208212
if err != nil {
209213
return nil, err
210214
}
@@ -234,7 +238,8 @@ func (m *Mock) getTsUSDePrice(tonPrice float64, _ map[ton.AccountID]float64) (ma
234238
refShare := decimal.NewFromInt(1_000_000_000)
235239

236240
url := fmt.Sprintf("https://tonapi.io/v2/blockchain/accounts/%v/methods/convertToAssets?args=%v", contract, refShare)
237-
respBody, err := sendRequest(url, m.TonApiToken)
241+
headers := http.Header{"Content-Type": {"application/json"}}
242+
respBody, err := sendRequest(url, m.TonApiToken, headers)
238243
if err != nil {
239244
return nil, err
240245
}
@@ -262,7 +267,8 @@ func (m *Mock) getUsdEPrice(tonPrice float64, _ map[ton.AccountID]float64) (map[
262267
account := ton.MustParseAccountID("EQAIb6KmdfdDR7CN1GBqVJuP25iCnLKCvBlJ07Evuu2dzP5f")
263268

264269
url := "https://api.bybit.com/v5/market/tickers?category=spot&symbol=USDEUSDT"
265-
respBody, err := sendRequest(url, "")
270+
headers := http.Header{"Content-Type": {"application/json"}}
271+
respBody, err := sendRequest(url, "", headers)
266272
if err != nil {
267273
return nil, err
268274
}
@@ -298,7 +304,8 @@ func (m *Mock) getSlpTokensPrice(tonPrice float64, pools map[ton.AccountID]float
298304
result := make(map[tongo.AccountID]float64)
299305
for slpType, account := range references.SlpAccounts {
300306
url := fmt.Sprintf("https://tonapi.io/v2/blockchain/accounts/%v/methods/get_vault_data", account.ToRaw())
301-
respBody, err := sendRequest(url, m.TonApiToken)
307+
headers := http.Header{"Content-Type": {"application/json"}}
308+
respBody, err := sendRequest(url, m.TonApiToken, headers)
302309
if err != nil {
303310
continue
304311
}

0 commit comments

Comments
 (0)