Overview
src/services/currencyService.ts retrieves the current USD → COP (Colombian peso) exchange rate from the Open Exchange Rates API and applies a 12-hour in-memory cache to avoid redundant network calls.
The rate is used by both the daily cron broadcast and the /deals command to display prices in COP alongside the USD price.
Exported function
getExchangeRate
number.
- If a cached rate exists and was fetched within the last 12 hours, it is returned immediately without hitting the network.
- If the API call fails for any reason (network error, unexpected response shape, rate out of range), the function returns the last known cached rate if available, or the hardcoded fallback of 4,000 COP/USD.
The exchange rate is never a required dependency for deal delivery. If the API is unavailable, the fallback rate ensures the bot keeps running and prices are still shown — just with a potentially stale rate.
Cache behavior
The in-memory cache is module-level:| Condition | Behavior |
|---|---|
| Cache is empty and API succeeds | Fetches rate, caches it, returns it |
| Cache is fresh (< 12 hours old) | Returns cached rate immediately |
| Cache is stale and API succeeds | Refreshes cache and returns new rate |
| Cache is stale and API fails | Returns cachedRate if set, else fallback 4000 |
| Cache is empty and API fails | Returns fallback 4000 |
Fallback rate
The hardcoded fallback is4000 COP per USD:
External API
The function calls the Open Exchange Rates API:Timeout: 10 seconds The response shape used:
COP field is validated to be a finite positive number before it is accepted. Any other value triggers the fallback path.
Full source
src/services/currencyService.ts