Fix calculation of fiat token rates

This commit is contained in:
Martin Boehm 2023-01-15 11:31:21 +01:00 committed by Martin
parent c7a711c1f1
commit 778c071d12
2 changed files with 24 additions and 1 deletions

View File

@ -12,10 +12,28 @@ type CurrencyRatesTicker struct {
TokenRates map[string]float32 `json:"tokenRates"` // rates of the tokens (identified by the address of the contract) against the base currency
}
var (
// TickerRecalculateTokenRate signals if it is necessary to recalculate token rate to base rate
// this happens when token rates are downloaded in TokenVsCurrency different from the base currency
TickerRecalculateTokenRate bool
// TickerTokenVsCurrency is the currency in which the token rates are downloaded
TickerTokenVsCurrency string
)
// Convert returns token rate in base currency
func (t *CurrencyRatesTicker) GetTokenRate(token string) (float32, bool) {
if t.TokenRates != nil {
rate, found := t.TokenRates[strings.ToLower(token)]
if !found {
return 0, false
}
if TickerRecalculateTokenRate {
vsRate, found := t.Rates[TickerTokenVsCurrency]
if !found || vsRate == 0 {
return 0, false
}
rate = rate / vsRate
}
return rate, found
}
return 0, false

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"strings"
"time"
"github.com/golang/glog"
@ -58,11 +59,15 @@ func NewFiatRatesDownloader(db *db.RocksDB, apiType string, params string, allow
rd.db = db
rd.callbackOnNewTicker = callback
rd.downloadTokens = rdParams.PlatformIdentifier != "" && rdParams.PlatformVsCurrency != ""
if rd.downloadTokens {
common.TickerRecalculateTokenRate = strings.ToLower(db.GetInternalState().CoinShortcut) != rdParams.PlatformVsCurrency
common.TickerTokenVsCurrency = rdParams.PlatformVsCurrency
}
is := rd.db.GetInternalState()
if apiType == "coingecko" {
throttle := true
if callback == nil {
// a small hack - in tests the callback is not used, therefore there is no delay slowing the test
// a small hack - in tests the callback is not used, therefore there is no delay slowing down the test
throttle = false
}
rd.downloader = NewCoinGeckoDownloader(db, rdParams.URL, rdParams.Coin, rdParams.PlatformIdentifier, rdParams.PlatformVsCurrency, allowedVsCurrencies, rd.timeFormat, throttle)