Skip to content

Commit

Permalink
Try fiat rater first, then fallback to crypto & Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
altafan committed Mar 6, 2024
1 parent 678ad90 commit 5889525
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions pkg/rater/exchange_rates_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,21 @@ func (e *exchangeRateWrapper) ConvertCurrency(
return decimal.NewFromFloat(1), nil
}

isCryptoSymbol, err := e.isCryptoSymbol(ctx, e.coinGeckoWaitDuration, source)
isFiatSymbol, err := e.IsFiatSymbolSupported(target)
if err != nil {
return decimal.Zero, err
}

if isCryptoSymbol {
return e.getCryptoToFiatRate(ctx, source, target)
if isFiatSymbol {
return e.getFiatToFiatRate(source, target)
}

sourceValidFiatSymbol, err := e.IsFiatSymbolSupported(target)
if err != nil {
return decimal.Zero, err
}

if !isCryptoSymbol && !sourceValidFiatSymbol {
isCryptoSymbol, _ := e.isCryptoSymbol(ctx, e.coinGeckoWaitDuration, source)
if !isCryptoSymbol {
return decimal.Zero, fmt.Errorf("%s is not a supported fiat nor crypto symbol", source)
}

return e.getFiatToFiatRate(source, target)
return e.getCryptoToFiatRate(ctx, source, target)
}

func (e *exchangeRateWrapper) IsFiatSymbolSupported(symbol string) (bool, error) {
Expand Down Expand Up @@ -262,10 +258,6 @@ func (e *exchangeRateWrapper) getFiatToFiatRate(
) (decimal.Decimal, error) {
e.ratesLock.Lock()
defer e.ratesLock.Unlock()

source = strings.ToUpper(source)
target = strings.ToUpper(target)

// Update cache once a day
if cache, ok := e.ratesCache[target]; !ok || time.Since(cache.lastUpdate).Hours() >= 24 {
data, err := fetchRates(e.httpClient, target)
Expand Down

0 comments on commit 5889525

Please sign in to comment.