Skip to content

Commit 6c25e6a

Browse files
committed
Fix nexchange processor pagination
1 parent 130f101 commit 6c25e6a

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

src/partners/nexchange.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ const QUERY_LOOKBACK = 1000 * 60 * 60 * 24 * 5 // 5 days
6969
const LIMIT = 200
7070
const MAX_ERROR_TEXT_LENGTH = 500
7171

72+
let nexchangeCurrencyMapPromise:
73+
| Promise<NexchangeCurrencyInfoMap>
74+
| undefined
75+
7276
const 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

385404
export 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 {

test/nexchange.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
NEXCHANGE_NETWORK_TO_PLUGIN_ID,
66
NexchangeCurrencyInfoMap,
77
parseApiDate,
8-
processNexchangeTx,
98
resolveNexchangeAsset,
9+
standardizeNexchangeOrder,
1010
toQueryIsoDate
1111
} from '../src/partners/nexchange'
1212

@@ -115,9 +115,9 @@ function makeRawOrder(overrides: { [key: string]: any } = {}): unknown {
115115
}
116116

117117
describe('nexchange plugin', () => {
118-
describe('processNexchangeTx', () => {
118+
describe('standardizeNexchangeOrder', () => {
119119
it('maps Edge audit order payload into StandardTx with chain plugin and token ids', () => {
120-
const tx = processNexchangeTx(
120+
const tx = standardizeNexchangeOrder(
121121
makeRawOrder({ orderId: 'NEX-ABCD1234' }),
122122
currencyMap
123123
)
@@ -160,7 +160,7 @@ describe('nexchange plugin', () => {
160160
]
161161
for (const [rawStatus, expected] of statusCases) {
162162
it(`maps status "${rawStatus}" to "${expected}"`, () => {
163-
const tx = processNexchangeTx(
163+
const tx = standardizeNexchangeOrder(
164164
makeRawOrder({ status: rawStatus }),
165165
currencyMap
166166
)

0 commit comments

Comments
 (0)