diff --git a/common/currencyrateticker.go b/common/currencyrateticker.go index dae0f1b2..f69fc25e 100644 --- a/common/currencyrateticker.go +++ b/common/currencyrateticker.go @@ -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 diff --git a/fiat/fiat_rates.go b/fiat/fiat_rates.go index ee92ea9c..4719bece 100644 --- a/fiat/fiat_rates.go +++ b/fiat/fiat_rates.go @@ -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)