Fix calculation of fiat token rates
This commit is contained in:
parent
c7a711c1f1
commit
778c071d12
@ -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
|
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
|
// Convert returns token rate in base currency
|
||||||
func (t *CurrencyRatesTicker) GetTokenRate(token string) (float32, bool) {
|
func (t *CurrencyRatesTicker) GetTokenRate(token string) (float32, bool) {
|
||||||
if t.TokenRates != nil {
|
if t.TokenRates != nil {
|
||||||
rate, found := t.TokenRates[strings.ToLower(token)]
|
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 rate, found
|
||||||
}
|
}
|
||||||
return 0, false
|
return 0, false
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@ -58,11 +59,15 @@ func NewFiatRatesDownloader(db *db.RocksDB, apiType string, params string, allow
|
|||||||
rd.db = db
|
rd.db = db
|
||||||
rd.callbackOnNewTicker = callback
|
rd.callbackOnNewTicker = callback
|
||||||
rd.downloadTokens = rdParams.PlatformIdentifier != "" && rdParams.PlatformVsCurrency != ""
|
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()
|
is := rd.db.GetInternalState()
|
||||||
if apiType == "coingecko" {
|
if apiType == "coingecko" {
|
||||||
throttle := true
|
throttle := true
|
||||||
if callback == nil {
|
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
|
throttle = false
|
||||||
}
|
}
|
||||||
rd.downloader = NewCoinGeckoDownloader(db, rdParams.URL, rdParams.Coin, rdParams.PlatformIdentifier, rdParams.PlatformVsCurrency, allowedVsCurrencies, rd.timeFormat, throttle)
|
rd.downloader = NewCoinGeckoDownloader(db, rdParams.URL, rdParams.Coin, rdParams.PlatformIdentifier, rdParams.PlatformVsCurrency, allowedVsCurrencies, rd.timeFormat, throttle)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user