Skip to content

Commit e735ac0

Browse files
committed
feat(ratios): always return 24h change in GetRelative service
1 parent 1ebd04e commit e735ac0

File tree

1 file changed

+13
-39
lines changed

1 file changed

+13
-39
lines changed

services/ratios/service.go

+13-39
Original file line numberDiff line numberDiff line change
@@ -169,49 +169,23 @@ func (s *Service) GetRelative(
169169
updated = time.Now()
170170
}
171171

172-
if duration != "1d" {
173-
// fill change with 0s ( it's unused for multiple coinIDs and we will overwrite for single )
174-
out := map[string]map[string]decimal.Decimal{}
175-
for k, v := range *rates {
176-
innerOut := map[string]decimal.Decimal{}
177-
for kk, vv := range v {
178-
if !strings.HasSuffix(kk, "_24h_change") {
179-
innerOut[kk+"_timeframe_change"] = decimal.Zero
180-
innerOut[kk] = vv
181-
}
172+
// Transform rates to copy 24h change values to timeframe change
173+
out := map[string]map[string]decimal.Decimal{}
174+
for k, v := range *rates {
175+
innerOut := map[string]decimal.Decimal{}
176+
for kk, vv := range v {
177+
if strings.HasSuffix(kk, "_24h_change") {
178+
// Copy 24h change to timeframe change
179+
innerOut[strings.TrimSuffix(kk, "_24h_change")+"_timeframe_change"] = vv
182180
}
183-
out[k] = innerOut
181+
innerOut[kk] = vv
184182
}
185-
186-
if len(coinIDs) == 1 {
187-
// request history for duration to calculate change
188-
chart, _, err := s.coingecko.FetchMarketChart(
189-
ctx,
190-
coinIDs[0].String(),
191-
vsCurrencies[0].String(),
192-
duration.ToDays(),
193-
duration.ToGetHistoryCacheDurationSeconds(),
194-
)
195-
if err != nil {
196-
logger.Error().Err(err).Msg("failed to fetch chart from coingecko")
197-
return nil, fmt.Errorf("failed to fetch chart from coingecko: %w", err)
198-
}
199-
200-
current := out[coinIDs[0].String()][vsCurrencies[0].String()]
201-
previous := chart.Prices[0][1]
202-
change := decimal.Zero
203-
// division by error when previous is zero
204-
if !previous.IsZero() {
205-
change = current.Sub(previous).Div(previous).Mul(decimal.NewFromFloat(100))
206-
}
207-
208-
out[coinIDs[0].String()][vsCurrencies[0].String()+"_timeframe_change"] = change
209-
}
210-
211-
tmp := coingecko.SimplePriceResponse(out)
212-
rates = &tmp
183+
out[k] = innerOut
213184
}
214185

186+
tmp := coingecko.SimplePriceResponse(out)
187+
rates = &tmp
188+
215189
return &ratiosclient.RelativeResponse{
216190
Payload: mapSimplePriceResponse(ctx, *rates, duration, coinIDs, vsCurrencies),
217191
LastUpdated: updated,

0 commit comments

Comments
 (0)