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 #46

Merged
merged 3 commits into from
Jun 9, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"api-changelly": "git://github.com/changelly/api-changelly.git#8e350f3",
"await-sleep": "0.0.1",
"axios": "^0.19.2",
"biggystring": "^3.0.1",
"changelly_api": "git://github.com/changelly/api-changelly.git#8e350f3",
"csvtojson": "^2.0.10",
Expand Down
101 changes: 72 additions & 29 deletions src/simplex.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow
import type { StandardTx, SwapFuncParams } from './checkSwapService.js'
const js = require('jsonfile')
const fs = require('fs')
const confFileName = './config.json'
const CONFIG = js.readFileSync(confFileName)
const { checkSwapService } = require('./checkSwapService.js')
const csv = require('csvtojson')
const axios = require('axios')

const SIMPLEX_CACHE = './cache/simRaw.json'
const SIMPLEX_FOLDER = './cache/simplex'

async function doSimplex (swapFuncParams: SwapFuncParams) {
return checkSwapService(fetchSimplex,
Expand All @@ -18,47 +18,90 @@ async function doSimplex (swapFuncParams: SwapFuncParams) {

async function fetchSimplex (swapFuncParams: SwapFuncParams) {
if (!swapFuncParams.useCache) {
console.log('Fetching Simplex from CSV...')
console.log('Fetching Simplex...')
}
let diskCache = { txs: [] }
let diskCache = { txs: [], offset: 0 }

const transactionMap = {}
const ssFormatTxs: Array<StandardTx> = []

try {
diskCache = js.readFileSync(SIMPLEX_CACHE)
// console.log('diskCache: ', diskCache)
} catch (e) {}

const files = await fs.readdirSync(SIMPLEX_FOLDER)
// console.log('files: ', files)
// 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 = ``

for (const fileName of files) {
const filePath = `./cache/simplex/${fileName}`
// console.log('filePath is: ', filePath)
const csvData = await csv().fromFile(filePath)
try {
while (1 && !swapFuncParams.useCache) {
// console.log('----------------')
// console.log('initialOffset: ', initialOffset)
// 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`
console.log('url: ', url)
const csvData = await axios({
url,
headers: {
'X-API-KEY': CONFIG.simplex.apiKey
}
})

for (const order of csvData) {
if (!order.total_amount_usd || !order.total_amount_crypto) {
continue
const responseTxs = csvData.data.data

for (const order of responseTxs) {
if (!order.fiat_total_amount || !order.amount_crypto) {
continue
}
const timestamp = order.created_at
const uniqueIdentifier = order.transaction_id
const ssTx: StandardTx = {
status: 'complete',
inputTXID: uniqueIdentifier,
inputAddress: '',
inputCurrency: order.currency,
inputAmount: parseFloat(order.fiat_total_amount.replace('$', '').replace(',', '')),
outputAddress: '',
outputCurrency: order.crypto_currency,
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
}
const date = new Date(order.processed_at_utc + ':00.000Z')
const timestamp = date.getTime() / 1000
const uniqueIdentifier = `${timestamp}-${order.total_amount_crypto.replace('.', '')}`
const ssTx: StandardTx = {
status: 'complete',
inputTXID: uniqueIdentifier,
inputAddress: '',
inputCurrency: order.currency,
inputAmount: parseFloat(order.total_amount_usd.replace('$', '').replace(',', '')),
outputAddress: '',
outputCurrency: order.crypto_currency,
outputAmount: order.total_amount_crypto,
timestamp: timestamp
if (responseTxs.length < 1000) {
console.log('responseTxs.length: ', responseTxs.length)
diskCache.offset = offset
break
}
// console.log('ssTx: ', ssTx)
transactionMap[uniqueIdentifier] = ssTx
}
} catch (error) {
console.log('error: ', error)
}

for (const id in transactionMap) {
ssFormatTxs.push(transactionMap[id])
ssFormatTxs.sort((a, b) => a.timestamp - b.timestamp)
Expand Down
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==

axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

babel-code-frame@^6.22.0, babel-code-frame@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
Expand Down Expand Up @@ -849,6 +856,13 @@ debug@2.6.9, debug@^2.2.0, debug@^2.6.8, debug@^2.6.9:
dependencies:
ms "2.0.0"

debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

decode-uri-component@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
Expand Down Expand Up @@ -1564,6 +1578,13 @@ flow-remove-types@^1.2.3:
babylon "^6.15.0"
vlq "^0.2.1"

follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
Expand Down