Optimize fetch of current token rates

This commit is contained in:
Martin Boehm 2023-01-06 12:00:32 +01:00 committed by Martin
parent 7186f73da8
commit 25950dbc98

View File

@ -271,21 +271,24 @@ func (cg *Coingecko) CurrentTickers() (*common.CurrencyRatesTicker, error) {
} }
} }
newTickers.TokenRates = make(map[string]float32) newTickers.TokenRates = make(map[string]float32)
const platformIdsGroup = 200 from := 0
for from := 0; from < len(platformIds); from += platformIdsGroup { const maxRequestLen = 6000
to := from + platformIdsGroup requestLen := 0
if to > len(platformIds) { for to := 0; to < len(platformIds); to++ {
to = len(platformIds) requestLen += len(platformIds[to]) + 3 // 3 characters for the comma separator %2C
} if requestLen > maxRequestLen || to+1 >= len(platformIds) {
tokenPrices, err := cg.simplePrice(platformIds[from:to], []string{cg.platformVsCurrency}) tokenPrices, err := cg.simplePrice(platformIds[from:to+1], []string{cg.platformVsCurrency})
if err != nil || tokenPrices == nil { if err != nil || tokenPrices == nil {
return nil, err return nil, err
}
for id, v := range *tokenPrices {
t, found := platformIdsToTokens[id]
if found {
newTickers.TokenRates[t] = v[cg.platformVsCurrency]
} }
for id, v := range *tokenPrices {
t, found := platformIdsToTokens[id]
if found {
newTickers.TokenRates[t] = v[cg.platformVsCurrency]
}
}
from = to + 1
requestLen = 0
} }
} }
} }