Skip to content

Commit d422119

Browse files
committed
fixup! Hack support for non-USD targetFiat
1 parent 0094167 commit d422119

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/v3/router.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,37 @@ const getNonUsdRates = async (
138138
// Loop over the USD rates and store them in a map. At the same time,
139139
// save the date strings we'll need to query the original requested fiat for
140140
const usdCryptoRatesMap = new Map<string, number | undefined>()
141-
const fiatUsdDateExchangeRateMap = new Map<string, number | undefined>()
141+
const dateSet = new Set<string>()
142142
for (const crypto of usdResult.crypto) {
143143
usdCryptoRatesMap.set(toDatedCryptoKey(crypto), crypto.rate)
144-
fiatUsdDateExchangeRateMap.set(crypto.isoDate.toISOString(), undefined)
144+
dateSet.add(crypto.isoDate.toISOString())
145145
}
146146
const usdFiatRatesMap = new Map<string, number | undefined>()
147147
for (const fiat of usdResult.fiat) {
148148
usdFiatRatesMap.set(toDatedFiatKey(fiat), fiat.rate)
149-
fiatUsdDateExchangeRateMap.set(fiat.isoDate.toISOString(), undefined)
149+
dateSet.add(fiat.isoDate.toISOString())
150150
}
151151

152152
// Get fiat/USD rates
153-
const fiatParams = {
154-
targetFiat: 'USD',
155-
crypto: [],
156-
fiat: [...fiatUsdDateExchangeRateMap.keys()].map(date => ({
157-
isoDate: new Date(date),
158-
fiatCode: initialParams.targetFiat,
159-
rate: undefined
160-
}))
161-
}
162-
const fiatResult = await getRates(fiatParams, rightNow)
163-
for (const fiat of fiatResult.fiat) {
164-
fiatUsdDateExchangeRateMap.set(fiat.isoDate.toISOString(), fiat.rate)
153+
// The number of unique dates across crypto and fiat could exceed 256 so we need to chunk them
154+
const dateArray = Array.from(dateSet)
155+
const chunkSize = FIAT_LIMIT
156+
const fiatUsdDateExchangeRateMap = new Map<string, number | undefined>()
157+
for (let i = 0; i < dateArray.length; i += chunkSize) {
158+
const chunk = dateArray.slice(i, i + chunkSize)
159+
const fiatParams = {
160+
targetFiat: 'USD',
161+
crypto: [],
162+
fiat: chunk.map(date => ({
163+
isoDate: new Date(date),
164+
fiatCode: initialParams.targetFiat,
165+
rate: undefined
166+
}))
167+
}
168+
const fiatResult = await getRates(fiatParams, rightNow)
169+
for (const fiat of fiatResult.fiat) {
170+
fiatUsdDateExchangeRateMap.set(fiat.isoDate.toISOString(), fiat.rate)
171+
}
165172
}
166173

167174
// Loop over the initial request and bridge the rates

0 commit comments

Comments
 (0)