Skip to content

Commit

Permalink
Merge pull request #51 from EdgeApp/kylan/simplex
Browse files Browse the repository at this point in the history
Kylan/simplex
  • Loading branch information
paullinator authored Jul 2, 2020
2 parents b9711b9 + 3e19875 commit b9e1126
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
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

0 comments on commit b9e1126

Please sign in to comment.