Skip to content

Commit 8bf3d52

Browse files
committed
Add coinmarketcap historical lookup maximum months
1 parent dc0ffb9 commit 8bf3d52

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
COIN_GECKO_API_KEY = '',
2121
COIN_MARKET_CAP_API_KEY = '',
2222
COIN_MARKET_CAP_HISTORICAL_API_KEY = '',
23+
COIN_MARKET_CAP_HISTORICAL_MAX_MONTHS = 60,
2324
SLACK_WEBHOOK_URL = '',
2425
OPEN_EXCHANGE_RATES_API_KEY = '',
2526
DEFAULT_FIAT = 'iso:USD'
@@ -39,7 +40,8 @@ const providerDefaults = {
3940
},
4041
coinMarketCapHistorical: {
4142
uri: 'https://pro-api.coinmarketcap.com',
42-
apiKey: COIN_MARKET_CAP_HISTORICAL_API_KEY
43+
apiKey: COIN_MARKET_CAP_HISTORICAL_API_KEY,
44+
maxHistoricalMonths: Number(COIN_MARKET_CAP_HISTORICAL_MAX_MONTHS)
4345
},
4446
openExchangeRates: {
4547
uri: 'https://openexchangerates.org',
@@ -146,7 +148,8 @@ export const asConfig = asObject({
146148
coinMarketCapHistorical: asMaybe(
147149
asObject({
148150
uri: asString,
149-
apiKey: asString
151+
apiKey: asString,
152+
maxHistoricalMonths: asOptional(asNumber, 60)
150153
}),
151154
providerDefaults.coinMarketCapHistorical
152155
),

src/v3/providers/coinmarketcap/coinmarketcap.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ const getHistoricalRates = async (
253253
rightNow: Date
254254
): Promise<NumberMap> => {
255255
const out: NumberMap = {}
256+
257+
const { maxHistoricalMonths } = config.providers.coinMarketCapHistorical
258+
const oldestAllowed = new Date(Date.now())
259+
oldestAllowed.setMonth(oldestAllowed.getMonth() - maxHistoricalMonths)
260+
if (new Date(date) < oldestAllowed) {
261+
return out
262+
}
263+
256264
const days = daysBetween(new Date(date), rightNow)
257265

258266
// If we're querying a date more than 3 months in the past, use

0 commit comments

Comments
 (0)