@@ -69,6 +69,10 @@ const QUERY_LOOKBACK = 1000 * 60 * 60 * 24 * 5 // 5 days
6969const LIMIT = 200
7070const MAX_ERROR_TEXT_LENGTH = 500
7171
72+ let nexchangeCurrencyMapPromise :
73+ | Promise < NexchangeCurrencyInfoMap >
74+ | undefined
75+
7276const statusMap : { [ key : string ] : Status } = {
7377 released : 'complete' ,
7478 complete : 'complete' ,
@@ -192,6 +196,27 @@ export async function fetchNexchangeCurrencyMap(): Promise<
192196 return map
193197}
194198
199+ async function getNexchangeCurrencyMap (
200+ pluginParams : PluginParams
201+ ) : Promise < NexchangeCurrencyInfoMap > {
202+ if ( nexchangeCurrencyMapPromise == null ) {
203+ nexchangeCurrencyMapPromise = fetchNexchangeCurrencyMap ( )
204+ . then ( currencyMap => {
205+ pluginParams . log (
206+ `Nexchange currency map loaded with ${
207+ Object . keys ( currencyMap ) . length
208+ } entries`
209+ )
210+ return currencyMap
211+ } )
212+ . catch ( error => {
213+ nexchangeCurrencyMapPromise = undefined
214+ throw error
215+ } )
216+ }
217+ return await nexchangeCurrencyMapPromise
218+ }
219+
195220/**
196221 * Returned by `resolveNexchangeAsset`. The shape is consistent across all
197222 * exit branches so callers can rely on the field set. `chainPluginId`,
@@ -311,12 +336,6 @@ export async function queryNexchange(
311336 let offset = 0
312337
313338 try {
314- // The currency catalog supplies the network/contract metadata that the
315- // audit-orders endpoint omits, so it is required for chain/token
316- // enrichment. Fetch it up front; a failure aborts the run (saving
317- // nothing) rather than persisting a batch of unenriched transactions.
318- const currencyMap = await fetchNexchangeCurrencyMap ( )
319-
320339 while ( true ) {
321340 const params : string [ ] = [
322341 `dateFrom=${ encodeURIComponent ( queryDateFrom ) } ` ,
@@ -341,7 +360,7 @@ export async function queryNexchange(
341360 const { orders, nextCursor, hasMore } = asNexchangeOrdersResponse ( json )
342361
343362 for ( const rawOrder of orders ) {
344- const standardTx = processNexchangeTx ( rawOrder , currencyMap )
363+ const standardTx = await processNexchangeTx ( rawOrder , pluginParams )
345364 txByOrderId . set ( standardTx . orderId , standardTx )
346365 if ( standardTx . isoDate > latestIsoDate ) {
347366 latestIsoDate = standardTx . isoDate
@@ -351,14 +370,14 @@ export async function queryNexchange(
351370
352371 if ( ! hasMore || orders . length === 0 ) break
353372
373+ offset += orders . length
354374 if ( nextCursor != null && nextCursor !== '' ) {
355375 cursor = nextCursor
356376 } else {
357377 // Reset cursor when falling back to offset, otherwise the previous
358378 // cursor value would re-pin pagination to the wrong position next
359379 // iteration.
360380 cursor = undefined
361- offset += orders . length
362381 }
363382 }
364383 } catch ( e ) {
@@ -383,6 +402,15 @@ export const nexchange: PartnerPlugin = {
383402}
384403
385404export function processNexchangeTx (
405+ rawTx : unknown ,
406+ pluginParams : PluginParams
407+ ) : Promise < StandardTx > {
408+ return getNexchangeCurrencyMap ( pluginParams ) . then ( currencyMap =>
409+ standardizeNexchangeOrder ( rawTx , currencyMap )
410+ )
411+ }
412+
413+ export function standardizeNexchangeOrder (
386414 rawTx : unknown ,
387415 currencyMap : NexchangeCurrencyInfoMap
388416) : StandardTx {
0 commit comments