Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kylan/simplex #51

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
53 changes: 25 additions & 28 deletions src/simplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<StandardTx> = []
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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
}
}
Expand Down