Skip to content

Commit

Permalink
client: check for zero fiat rates and response status code
Browse files Browse the repository at this point in the history
This increases the request interval for fiat rates to 12min due to a
possibility of hitting source's request limits. This can be considered a
temporary solution since we need to adjust this interval for every added
asset.

This also checks the HTTP response status code instead of assuming
the response is the expected payload.
  • Loading branch information
ukane-philemon authored and chappjc committed Dec 15, 2022
1 parent b87d960 commit 2ae50bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -8031,7 +8031,7 @@ func (c *Core) fiatConversions() map[uint32]float64 {
var sources int
for _, source := range c.fiatRateSources {
rateInfo := source.assetRate(assetID)
if rateInfo != nil && time.Since(rateInfo.lastUpdate) < fiatRateDataExpiry {
if rateInfo != nil && time.Since(rateInfo.lastUpdate) < fiatRateDataExpiry && rateInfo.rate > 0 {
sources++
rateSum += rateInfo.rate
}
Expand Down
19 changes: 18 additions & 1 deletion client/core/exchangeratefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// DefaultFiatCurrency is the currency for displaying assets fiat value.
DefaultFiatCurrency = "USD"
// fiatRateRequestInterval is the amount of time between calls to the exchange API.
fiatRateRequestInterval = 5 * time.Minute
fiatRateRequestInterval = 8 * time.Minute
// fiatRateDataExpiry : Any data older than fiatRateDataExpiry will be discarded.
fiatRateDataExpiry = 60 * time.Minute

Expand Down Expand Up @@ -96,6 +96,9 @@ func (source *commonRateSource) refreshRates(ctx context.Context, logger dex.Log
source.mtx.Lock()
defer source.mtx.Unlock()
for assetID, fiatRate := range fiatRates {
if fiatRate <= 0 {
continue
}
source.fiatRates[assetID] = &fiatRateInfo{
rate: fiatRate,
lastUpdate: now,
Expand Down Expand Up @@ -148,6 +151,11 @@ func fetchCoinpaprikaRates(ctx context.Context, log dex.Logger, assets map[uint3
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Errorf("unexpected response, got status code %d", resp.StatusCode)
continue
}

// Read the raw bytes and close the response.
reader := io.LimitReader(resp.Body, 1<<20)
err = json.NewDecoder(reader).Decode(res)
Expand Down Expand Up @@ -191,6 +199,10 @@ func fetchDcrdataRates(ctx context.Context, log dex.Logger, assets map[uint32]*S
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Errorf("unexpected response, got status code %d", resp.StatusCode)
return nil
}
// Read the raw bytes and close the response.
reader := io.LimitReader(resp.Body, 1<<20)
err = json.NewDecoder(reader).Decode(res)
Expand Down Expand Up @@ -244,6 +256,11 @@ func fetchMessariRates(ctx context.Context, log dex.Logger, assets map[uint32]*S
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Errorf("unexpected response, got status code %d", resp.StatusCode)
continue
}

// Read the raw bytes and close the response.
reader := io.LimitReader(resp.Body, 1<<20)
err = json.NewDecoder(reader).Decode(res)
Expand Down

0 comments on commit 2ae50bb

Please sign in to comment.