From 8ac0f7f55b7aef5b3822b732df8e7a8ae05095b8 Mon Sep 17 00:00:00 2001 From: kylanhurt Date: Mon, 15 Jun 2020 13:42:59 -0700 Subject: [PATCH 1/4] Add comments for Simplex routine --- src/simplex.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/simplex.js b/src/simplex.js index bade744..6e84e60 100644 --- a/src/simplex.js +++ b/src/simplex.js @@ -83,10 +83,16 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { } // 1567388220 = first transaction // console.log('timestamp: ', timestamp) + // if not fetching from scratch if (initialOffset) { + // then update minimum if timestamp lower than minimum if (timestamp < minTimestamp) minTimestamp = timestamp } + // if it's from scratch and this is first tx + // then set the minimum timestamp to current tx if (!initialOffset) minTimestamp = timestamp + // if timestamp is greater than max + // then set new max to current tx if (timestamp > maxTimestamp) maxTimestamp = timestamp offset = maxTimestamp // console.log('ssTx: ', ssTx) @@ -94,6 +100,7 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { } if (responseTxs.length < 1000) { console.log('responseTxs.length: ', responseTxs.length) + // set the offset for the cache to diskCache.offset = offset break } From c52a81a361954fbc6155c968b6397997cddc7ab0 Mon Sep 17 00:00:00 2001 From: kylanhurt Date: Mon, 15 Jun 2020 18:18:17 -0700 Subject: [PATCH 2/4] Allow non-camel case --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 5eadfb6daf977a53b421e55c5428a6b48033dbb0 Mon Sep 17 00:00:00 2001 From: kylanhurt Date: Mon, 15 Jun 2020 18:26:07 -0700 Subject: [PATCH 3/4] Change Simplex to use page cursor during fetch calls --- src/simplex.js | 52 ++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/simplex.js b/src/simplex.js index 6e84e60..1be16c7 100644 --- a/src/simplex.js +++ b/src/simplex.js @@ -32,10 +32,10 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { // 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 = `` + let maxTimestamp = diskCache.offset || 0 + let continueFromSyntax = '' + let has_more_pages = false + let next_page_cursor = '' try { while (1 && !swapFuncParams.useCache) { @@ -44,16 +44,8 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { // console.log('offset: ', offset) // 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,27 +76,22 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { outputAmount: order.amount_crypto, timestamp: timestamp } - // 1567388220 = first transaction - // console.log('timestamp: ', timestamp) - // if not fetching from scratch - if (initialOffset) { - // then update minimum if timestamp lower than minimum - if (timestamp < minTimestamp) minTimestamp = timestamp - } - // if it's from scratch and this is first tx - // then set the minimum timestamp to current tx - if (!initialOffset) minTimestamp = timestamp - // if timestamp is greater than max - // then set new max to current tx + 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 < initialOffset) { + has_more_pages = false + } } - if (responseTxs.length < 1000) { + if (has_more_pages === false) { console.log('responseTxs.length: ', responseTxs.length) - // set the offset for the cache to - diskCache.offset = offset + // set the offset for the cache to two weeks before latest tx + diskCache.offset = maxTimestamp - 60 * 60 * 24 * 14 break } } From 3e1987520a3d142e1bcb1a85b09492db6d7bc8a5 Mon Sep 17 00:00:00 2001 From: kylanhurt Date: Mon, 15 Jun 2020 21:46:56 -0700 Subject: [PATCH 4/4] Change 'offset' parameter to lastTxTimestamp for Simplex --- src/simplex.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/simplex.js b/src/simplex.js index 1be16c7..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,8 +31,8 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { } catch (e) {} // flag for fresh vs already-populated cache - const initialOffset = diskCache.offset || 0 - let maxTimestamp = diskCache.offset || 0 + const initialLastTxTimestamp = diskCache.lastTxTimestamp || 0 + let maxTimestamp = diskCache.lastTxTimestamp || 0 let continueFromSyntax = '' let has_more_pages = false let next_page_cursor = '' @@ -40,8 +40,8 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { 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 (next_page_cursor) continueFromSyntax = `continue_from=${next_page_cursor}&` @@ -84,14 +84,14 @@ async function fetchSimplex (swapFuncParams: SwapFuncParams) { // if transaction is before the cutoff timestamp // then stop the loop - if (timestamp < initialOffset) { + if (timestamp < initialLastTxTimestamp) { has_more_pages = false } } if (has_more_pages === false) { console.log('responseTxs.length: ', responseTxs.length) - // set the offset for the cache to two weeks before latest tx - diskCache.offset = maxTimestamp - 60 * 60 * 24 * 14 + // set the lastTxTimestamp for the cache to two weeks before latest tx + diskCache.lastTxTimestamp = maxTimestamp - 60 * 60 * 24 * 7 break } }