Skip to content

Commit 50aed5c

Browse files
committed
Only save successful assets to the asset caches
Assets that cannot have not found a rate, like obscure or invalid tokens, shouldn't slow down the initial lookup on login Fix eslint error in exchangeRates.ts
1 parent 74e7e44 commit 50aed5c

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased (develop)
44

5+
- changed: Optimize exchange rate fetching and asset pair cache handling
56
- fixed: Fixed broken `logEvent` tracking calls by adding the needed `dispatch`.
67
- removed: Remove change quote tracking.
78

eslint.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ export default [
496496
'src/util/CurrencyInfoHelpers.ts',
497497
'src/util/CurrencyWalletHelpers.ts',
498498

499-
'src/util/exchangeRates.ts',
500499
'src/util/fake/FakeProviders.tsx',
501500
'src/util/FioAddressUtils.ts',
502501
'src/util/getAccountUsername.ts',

src/actions/ExchangeRateActions.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ThunkAction } from '../types/reduxTypes'
66
import {
77
asCryptoAsset,
88
asRatesParams,
9+
createRateKey,
910
type RatesParams
1011
} from '../util/exchangeRates'
1112
import { fetchRates } from '../util/network'
@@ -308,6 +309,8 @@ async function fetchExchangeRates(
308309
}
309310
}
310311

312+
const cryptoPairCache = new Map<string, CryptoFiatPair>()
313+
const fiatPairCache = new Map<string, FiatFiatPair>()
311314
const requests = convertToRatesParams(cryptoPairMap, fiatPairMap)
312315
const promises = requests.map(async query => {
313316
const options = {
@@ -362,6 +365,14 @@ async function fetchExchangeRates(
362365
}
363366

364367
rateObj.expiration = rateExpiration
368+
369+
// Save crypto assets with rates to asset cache
370+
cryptoPairCache.set(createRateKey(cryptoRate.asset, targetFiat), {
371+
asset: cryptoRate.asset,
372+
targetFiat,
373+
isoDate: undefined,
374+
expiration: pairExpiration
375+
})
365376
}
366377
for (const fiatRate of cleanedRates.fiat) {
367378
const { isoDate, rate } = fiatRate
@@ -398,6 +409,14 @@ async function fetchExchangeRates(
398409
}
399410

400411
rateObj.expiration = rateExpiration
412+
413+
// Save fiat assets with rates to asset cache
414+
fiatPairCache.set(createRateKey(fiatCode, targetFiat), {
415+
fiatCode,
416+
targetFiat,
417+
isoDate: undefined,
418+
expiration: pairExpiration
419+
})
401420
}
402421
}
403422
} catch (error: unknown) {
@@ -411,8 +430,8 @@ async function fetchExchangeRates(
411430
// Update the in-memory cache:
412431
exchangeRateCache = {
413432
rates,
414-
cryptoPairs: Array.from(cryptoPairMap.values()),
415-
fiatPairs: Array.from(fiatPairMap.values())
433+
cryptoPairs: Array.from(cryptoPairCache.values()),
434+
fiatPairs: Array.from(fiatPairCache.values())
416435
}
417436

418437
// Write the cache to disk:

src/util/exchangeRates.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const doQuery = async (doFetch?: EdgeFetchFunction): Promise<void> => {
127127

128128
clog(`${n} deleting ${key}`)
129129
resolverMap.delete(key)
130-
if (resolvers.length) {
130+
if (resolvers.length > 0) {
131131
resolvers.forEach((r, i) => {
132132
r(rate)
133133
})
@@ -163,7 +163,7 @@ const doQuery = async (doFetch?: EdgeFetchFunction): Promise<void> => {
163163

164164
clog(`${n} deleting ${key}`)
165165
resolverMap.delete(key)
166-
if (resolvers.length) {
166+
if (resolvers.length > 0) {
167167
resolvers.forEach((r, i) => {
168168
r(rate)
169169
})
@@ -205,7 +205,7 @@ const addToQueue = (
205205
resolve: Function,
206206
maxQuerySize: number,
207207
doFetch?: EdgeFetchFunction
208-
) => {
208+
): void => {
209209
const rateKeyResolver = resolverMap.get(rateKey)
210210
if (rateKeyResolver == null) {
211211
// Create a new entry in the map for this pair/date
@@ -229,7 +229,7 @@ const addToQueue = (
229229
}
230230
}
231231

232-
const createRateKey = (
232+
export const createRateKey = (
233233
asset: { pluginId: string; tokenId?: EdgeTokenId } | string,
234234
targetFiat: string,
235235
date?: string

0 commit comments

Comments
 (0)