Skip to content

Commit 971e6d1

Browse files
committed
Add max new transactions for letsexchange
1 parent 206c09b commit 971e6d1

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/partners/letsexchange.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const MAX_RETRIES = 5
2727
const QUERY_INTERVAL_MS = 1000 * 60 * 60 * 24 * 30 // 30 days in milliseconds
2828
const LETSEXCHANGE_START_DATE = '2022-02-01T00:00:00.000Z'
2929

30+
/**
31+
* Max number of new transactions to save. This is to prevent overloading the db
32+
* write and potentially causing a timeout or failure. The query will be retried
33+
* starting from where it left off.
34+
*/
35+
const MAX_NEW_TRANSACTIONS = 100000
36+
3037
export const asLetsExchangePluginParams = asObject({
3138
settings: asObject({
3239
latestIsoDate: asOptional(asString, LETSEXCHANGE_START_DATE)
@@ -350,6 +357,7 @@ export async function queryLetsExchange(
350357
let windowStart = new Date(latestIsoDate).getTime() - QUERY_INTERVAL_MS
351358
const now = Date.now()
352359
let done = false
360+
let newTxStart: number = 0
353361

354362
// Outer loop: iterate over 30-day windows
355363
while (windowStart < now && !done) {
@@ -385,11 +393,17 @@ export async function queryLetsExchange(
385393
const standardTx = await processLetsExchangeTx(rawTx, pluginParams)
386394
standardTxs.push(standardTx)
387395
if (standardTx.isoDate > latestIsoDate) {
396+
if (newTxStart === 0) {
397+
newTxStart = standardTxs.length
398+
}
388399
latestIsoDate = standardTx.isoDate
389400
}
390401
}
391402

392-
log(`page ${page}/${lastPage} latestIsoDate ${latestIsoDate}`)
403+
const newTxs = standardTxs.length - newTxStart
404+
log(
405+
`page ${page}/${lastPage} latestIsoDate ${latestIsoDate} newTxs: ${newTxs}/${MAX_NEW_TRANSACTIONS}`
406+
)
393407

394408
// Check if we've reached the last page for this window
395409
if (currentPage >= lastPage || txs.length === 0) {
@@ -398,6 +412,14 @@ export async function queryLetsExchange(
398412

399413
page++
400414
retry = 0
415+
if (newTxs >= MAX_NEW_TRANSACTIONS) {
416+
latestIsoDate = windowStartIso
417+
log.warn(
418+
`Max new transactions reached, saving progress at ${latestIsoDate}`
419+
)
420+
done = true
421+
break
422+
}
401423
} catch (e) {
402424
log.error(String(e))
403425
// Retry a few times with time delay to prevent throttling

0 commit comments

Comments
 (0)