diff --git a/.eslintrc.json b/.eslintrc.json index 9916294..561c99c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,7 +3,7 @@ "parser": "babel-eslint", "plugins": ["flowtype", "standard"], "rules": { - "camelcase": "error", + "camelcase": "off", "flowtype/generic-spacing": "off", "no-throw-literal": "error", "no-var": "error", diff --git a/src/simplex.js b/src/simplex.js index bade744..9c4d890 100644 --- a/src/simplex.js +++ b/src/simplex.js @@ -20,7 +20,7 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { if (!swapFuncParams.useCache) { console.log('Fetching Simplex...') } - let diskCache = { txs: [], offset: 0 } + let diskCache = { txs: [], lastTxTimestamp: 0 } const transactionMap = {} const ssFormatTxs: Array = [] @@ -31,29 +31,21 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { } catch (e) {} // flag for fresh vs already-populated cache - const initialOffset = diskCache.offset || 0 - let offset = diskCache.offset || 0 - let minTimestamp = initialOffset ? (initialOffset - 10) : 0 - let maxTimestamp = 0 - let offsetSyntax = `` + const initialLastTxTimestamp = diskCache.lastTxTimestamp || 0 + let maxTimestamp = diskCache.lastTxTimestamp || 0 + let continueFromSyntax = '' + let has_more_pages = false + let next_page_cursor = '' try { while (1 && !swapFuncParams.useCache) { // console.log('----------------') - // console.log('initialOffset: ', initialOffset) - // console.log('offset: ', offset) + // console.log('initiallastTxTimestamp: ', initiallastTxTimestamp) + // console.log('lastTxTimestamp: ', lastTxTimestamp) // console.log('maxTimestamp: ', maxTimestamp) // console.log('minTimestamp: ', minTimestamp) - if (initialOffset) { // if continuing - offsetSyntax = `starting_at=${maxTimestamp}&` - } else { // if from fresh / empty tx set - if (offset === 0) { // if first time in loop - offsetSyntax = '' - } else { // otherwise - offsetSyntax = `ending_at=${minTimestamp}&` - } - } - const url = `https://turnkey.api.simplex.com/transactions?${offsetSyntax}limit=1000` + if (next_page_cursor) continueFromSyntax = `continue_from=${next_page_cursor}&` + const url = `https://turnkey.api.simplex.com/transactions?${continueFromSyntax}limit=1000` console.log('url: ', url) const csvData = await axios({ url, @@ -62,6 +54,9 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { } }) + has_more_pages = csvData.data.has_more_pages + next_page_cursor = csvData.data.next_page_cursor + const responseTxs = csvData.data.data for (const order of responseTxs) { @@ -81,20 +76,22 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { outputAmount: order.amount_crypto, timestamp: timestamp } - // 1567388220 = first transaction - // console.log('timestamp: ', timestamp) - if (initialOffset) { - if (timestamp < minTimestamp) minTimestamp = timestamp - } - if (!initialOffset) minTimestamp = timestamp + if (timestamp > maxTimestamp) maxTimestamp = timestamp - offset = maxTimestamp - // console.log('ssTx: ', ssTx) + transactionMap[uniqueIdentifier] = ssTx + + // if transaction is before the cutoff timestamp + // then stop the loop + + if (timestamp < initialLastTxTimestamp) { + has_more_pages = false + } } - if (responseTxs.length < 1000) { + if (has_more_pages === false) { console.log('responseTxs.length: ', responseTxs.length) - diskCache.offset = offset + // set the lastTxTimestamp for the cache to two weeks before latest tx + diskCache.lastTxTimestamp = maxTimestamp - 60 * 60 * 24 * 7 break } }