Skip to content

Commit c0d9cf8

Browse files
committed
errors in price getter
1 parent 9d3d39c commit c0d9cf8

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

pkg/rates/sources.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/prometheus/client_golang/prometheus"
8+
"github.com/prometheus/client_golang/prometheus/promauto"
79
"math"
810
"net/http"
911
"strconv"
12+
"time"
1013

1114
"github.com/labstack/gommon/log"
1215
"github.com/tonkeeper/opentonapi/pkg/references"
@@ -15,6 +18,10 @@ import (
1518
"github.com/tonkeeper/tongo/ton"
1619
)
1720

21+
var errorsCounter = promauto.NewCounterVec(prometheus.CounterOpts{
22+
Name: "rates_getter_errors_total",
23+
}, []string{"source"})
24+
1825
type storage interface {
1926
GetJettonMasterMetadata(ctx context.Context, master tongo.AccountID) (tep64.Metadata, error)
2027
}
@@ -31,18 +38,30 @@ func (m *Mock) GetCurrentRates() (map[string]float64, error) {
3138
tonPriceOKX := getTonOKXPrice()
3239
tonPriceHuobi := getTonHuobiPrice()
3340
if tonPriceOKX == 0 && tonPriceHuobi == 0 {
41+
errorsCounter.WithLabelValues("ton_price").Inc()
3442
return nil, fmt.Errorf("failed to get ton price")
3543
}
3644
meanTonPriceToUSD := (tonPriceHuobi + tonPriceOKX) / 2
3745

38-
pools := m.getDedustPool()
46+
pools, err := m.getDedustPool()
47+
if err != nil {
48+
log.Errorf("dedust pool %v", err)
49+
errorsCounter.WithLabelValues("dedust").Inc()
50+
}
51+
3952
stonfiPools := getStonFiPool(meanTonPriceToUSD)
53+
if len(stonfiPools) == 0 {
54+
errorsCounter.WithLabelValues("stonfi").Inc()
55+
}
4056
for address, price := range stonfiPools {
4157
if _, ok := pools[address]; !ok {
4258
pools[address] = price
4359
}
4460
}
4561
megatonPool := getMegatonPool(meanTonPriceToUSD)
62+
if len(megatonPool) == 0 {
63+
errorsCounter.WithLabelValues("megaton").Inc()
64+
}
4665
for address, price := range megatonPool {
4766
if _, ok := pools[address]; !ok {
4867
pools[address] = price
@@ -51,6 +70,14 @@ func (m *Mock) GetCurrentRates() (map[string]float64, error) {
5170
tonstakersJetton, tonstakersPrice, err := getTonstakersPrice(references.TonstakersAccountPool)
5271
if err == nil {
5372
pools[tonstakersJetton] = tonstakersPrice
73+
} else {
74+
log.Errorf("tonstakers price: %v", err)
75+
errorsCounter.WithLabelValues("tonstakers").Inc()
76+
time.Sleep(time.Second * 5)
77+
tonstakersJetton, tonstakersPrice, err = getTonstakersPrice(references.TonstakersAccountPool)
78+
if err == nil {
79+
pools[tonstakersJetton] = tonstakersPrice
80+
}
5481
}
5582

5683
// All data is displayed to the ratio to TON
@@ -138,11 +165,11 @@ func getStonFiPool(tonPrice float64) map[ton.AccountID]float64 {
138165
return mapOfPool
139166
}
140167

141-
func (m *Mock) getDedustPool() map[ton.AccountID]float64 {
168+
func (m *Mock) getDedustPool() (map[ton.AccountID]float64, error) {
142169
resp, err := http.Get("https://api.dedust.io/v2/pools")
143170
if err != nil {
144171
log.Errorf("failed to fetch dedust rates: %v", err)
145-
return map[ton.AccountID]float64{}
172+
return map[ton.AccountID]float64{}, err
146173
}
147174
defer resp.Body.Close()
148175

@@ -161,7 +188,7 @@ func (m *Mock) getDedustPool() map[ton.AccountID]float64 {
161188
var respBody []Pool
162189
if err = json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
163190
log.Errorf("failed to decode response: %v", err)
164-
return map[ton.AccountID]float64{}
191+
return map[ton.AccountID]float64{}, err
165192
}
166193

167194
mapOfPool := make(map[ton.AccountID]float64)
@@ -209,7 +236,7 @@ func (m *Mock) getDedustPool() map[ton.AccountID]float64 {
209236
mapOfPool[account.ID] = price
210237
}
211238

212-
return mapOfPool
239+
return mapOfPool, nil
213240
}
214241

215242
func getTonHuobiPrice() float64 {

0 commit comments

Comments
 (0)