@@ -4,9 +4,12 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "github.com/prometheus/client_golang/prometheus"
8
+ "github.com/prometheus/client_golang/prometheus/promauto"
7
9
"math"
8
10
"net/http"
9
11
"strconv"
12
+ "time"
10
13
11
14
"github.com/labstack/gommon/log"
12
15
"github.com/tonkeeper/opentonapi/pkg/references"
@@ -15,6 +18,10 @@ import (
15
18
"github.com/tonkeeper/tongo/ton"
16
19
)
17
20
21
+ var errorsCounter = promauto .NewCounterVec (prometheus.CounterOpts {
22
+ Name : "rates_getter_errors_total" ,
23
+ }, []string {"source" })
24
+
18
25
type storage interface {
19
26
GetJettonMasterMetadata (ctx context.Context , master tongo.AccountID ) (tep64.Metadata , error )
20
27
}
@@ -31,18 +38,30 @@ func (m *Mock) GetCurrentRates() (map[string]float64, error) {
31
38
tonPriceOKX := getTonOKXPrice ()
32
39
tonPriceHuobi := getTonHuobiPrice ()
33
40
if tonPriceOKX == 0 && tonPriceHuobi == 0 {
41
+ errorsCounter .WithLabelValues ("ton_price" ).Inc ()
34
42
return nil , fmt .Errorf ("failed to get ton price" )
35
43
}
36
44
meanTonPriceToUSD := (tonPriceHuobi + tonPriceOKX ) / 2
37
45
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
+
39
52
stonfiPools := getStonFiPool (meanTonPriceToUSD )
53
+ if len (stonfiPools ) == 0 {
54
+ errorsCounter .WithLabelValues ("stonfi" ).Inc ()
55
+ }
40
56
for address , price := range stonfiPools {
41
57
if _ , ok := pools [address ]; ! ok {
42
58
pools [address ] = price
43
59
}
44
60
}
45
61
megatonPool := getMegatonPool (meanTonPriceToUSD )
62
+ if len (megatonPool ) == 0 {
63
+ errorsCounter .WithLabelValues ("megaton" ).Inc ()
64
+ }
46
65
for address , price := range megatonPool {
47
66
if _ , ok := pools [address ]; ! ok {
48
67
pools [address ] = price
@@ -51,6 +70,14 @@ func (m *Mock) GetCurrentRates() (map[string]float64, error) {
51
70
tonstakersJetton , tonstakersPrice , err := getTonstakersPrice (references .TonstakersAccountPool )
52
71
if err == nil {
53
72
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
+ }
54
81
}
55
82
56
83
// All data is displayed to the ratio to TON
@@ -138,11 +165,11 @@ func getStonFiPool(tonPrice float64) map[ton.AccountID]float64 {
138
165
return mapOfPool
139
166
}
140
167
141
- func (m * Mock ) getDedustPool () map [ton.AccountID ]float64 {
168
+ func (m * Mock ) getDedustPool () ( map [ton.AccountID ]float64 , error ) {
142
169
resp , err := http .Get ("https://api.dedust.io/v2/pools" )
143
170
if err != nil {
144
171
log .Errorf ("failed to fetch dedust rates: %v" , err )
145
- return map [ton.AccountID ]float64 {}
172
+ return map [ton.AccountID ]float64 {}, err
146
173
}
147
174
defer resp .Body .Close ()
148
175
@@ -161,7 +188,7 @@ func (m *Mock) getDedustPool() map[ton.AccountID]float64 {
161
188
var respBody []Pool
162
189
if err = json .NewDecoder (resp .Body ).Decode (& respBody ); err != nil {
163
190
log .Errorf ("failed to decode response: %v" , err )
164
- return map [ton.AccountID ]float64 {}
191
+ return map [ton.AccountID ]float64 {}, err
165
192
}
166
193
167
194
mapOfPool := make (map [ton.AccountID ]float64 )
@@ -209,7 +236,7 @@ func (m *Mock) getDedustPool() map[ton.AccountID]float64 {
209
236
mapOfPool [account .ID ] = price
210
237
}
211
238
212
- return mapOfPool
239
+ return mapOfPool , nil
213
240
}
214
241
215
242
func getTonHuobiPrice () float64 {
0 commit comments