Skip to content

Commit fd379f6

Browse files
committed
Add max new transactions for letsexchange
1 parent 2f72472 commit fd379f6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/partners/letsexchange.ts

Lines changed: 19 additions & 0 deletions
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 = 20000
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,6 +393,9 @@ 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
}
@@ -398,6 +409,14 @@ export async function queryLetsExchange(
398409

399410
page++
400411
retry = 0
412+
if (standardTxs.length - newTxStart >= MAX_NEW_TRANSACTIONS) {
413+
log.warn(
414+
`Max new transactions reached, saving progress at ${latestIsoDate}`
415+
)
416+
latestIsoDate = windowStartIso
417+
done = true
418+
break
419+
}
401420
} catch (e) {
402421
log.error(String(e))
403422
// Retry a few times with time delay to prevent throttling

0 commit comments

Comments
 (0)