From 65d8cd0273fcfad353d1f9bec488b9cfbff0403a Mon Sep 17 00:00:00 2001 From: Gergely Szilagyi Date: Fri, 9 Oct 2020 14:07:14 +0700 Subject: [PATCH] Add SideShift.ai reporter Also edited README.md: corrected outdated instructions. --- README.md | 2 +- config.json.sample | 4 +- src/reporter.js | 15 +++++++ src/sideshift.js | 104 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 src/sideshift.js diff --git a/README.md b/README.md index 6a438cc..0eca1f3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ - `yarn build` or `npm run build` - Rename `config.json.sample` to `config.json` and enter relevant API keys - If only interested in submitting your own exchange reporting plugin: -- Come up with a prefix for your organization (eg ShapeShift = "SS", "BitRefill" = "BR"), and create a [prefix]Raw.json file in the `cache` folder and enter an empty object. +- Come up with a prefix for your organization (eg ShapeShift = "SS", "BitRefill" = "BR"), and create a [prefix]Raw.json file in the `cache` folder and enter an object with an empty `"txs": []` array. - In the `src` folder create a file `myOrganization.js` with the code to fetch transactions from your API and format transactions to match the `StandardTx` type (can view in `checkSwapService.js` file) - In `reporter.js` import the transaction fetching procedure from your `myOrganization.js` file and include near the top of the `main` function. Also make sure you have your organization's transactions printed out with `printTxDataMap` in the `report` method (in `reporter.js`). - When in doubt, look at how other teams have implemented their reporting procedure. If you have any further questions please feel free to reach out to the Edge team at our `dev` Slack channel: diff --git a/config.json.sample b/config.json.sample index bd8dd15..eb85553 100644 --- a/config.json.sample +++ b/config.json.sample @@ -44,5 +44,7 @@ "switchainApiKey" : "", "transak_api_secret": "xxx", "coinMarketCapExcludeLookup": ["USD", "EUR", "GBP"], - "coinApiExcludeLookup": ["USD", "EUR", "GBP"] + "coinApiExcludeLookup": ["USD", "EUR", "GBP"], + "sideShiftAffiliateId": "", + "sideShiftAffiliateSecret": "" } diff --git a/src/reporter.js b/src/reporter.js index d69ef40..7833c0d 100644 --- a/src/reporter.js +++ b/src/reporter.js @@ -20,6 +20,7 @@ const { doBanxa } = require('./banxa.js') const { doBity } = require('./bity.js') const { doSwitchain } = require('./switchain.js') const { doPaytrie } = require('./paytrie.js') +const { doSideShift } = require('./sideshift.js') const { bns } = require('biggystring') const config = require('../config.json') const { sprintf } = require('sprintf-js') @@ -45,6 +46,10 @@ async function main (swapFuncParams: SwapFuncParams) { console.error('doShapeShift failed') return {} }) + const rXai = await doSideShift(swapFuncParams).catch(e => { + console.error('doSideShift failed') + return {} + }) const rLbx = await doLibertyX(swapFuncParams).catch(e => { console.error('doLibertyX failed') return {} @@ -116,6 +121,7 @@ async function main (swapFuncParams: SwapFuncParams) { printTxDataMap('CHA', rCha) printTxDataMap('FAA', rFaa) printTxDataMap('SSH', rSsh) + printTxDataMap('XAI', rXai) printTxDataMap('LBX', rLbx) printTxDataMap('BIT', rBit) printTxDataMap('TOT', rTl) @@ -274,6 +280,9 @@ async function report (argv: Array) { const ssResults = config.shapeShiftToken ? await doSummaryFunction(doShapeShift) : {} + const xaiResults = config.sideShiftAffiliateId + ? await doSummaryFunction(doSideShift) + : {} const faResults = config.faastAffiliateId ? await doSummaryFunction(doFaast) : {} @@ -329,6 +338,7 @@ async function report (argv: Array) { combineResults(results, chResults) combineResults(results, faResults) combineResults(results, ssResults) + combineResults(results, xaiResults) combineResults(results, tlResults) combineResults(results, foxResults) combineResults(results, csResults) @@ -360,6 +370,11 @@ async function report (argv: Array) { console.log('\n***** Shapeshift Monthly *****') printTxDataMap('SSH', ssResults.monthly) + console.log('\n***** SideShift.ai Daily *****') + printTxDataMap('XAI', xaiResults.daily) + console.log('\n***** SideShift.ai Monthly *****') + printTxDataMap('XAI', xaiResults.monthly) + console.log('\n***** Coinswitch Daily *****') printTxDataMap('CS', csResults.daily) console.log('\n***** Coinswitch Monthly *****') diff --git a/src/sideshift.js b/src/sideshift.js new file mode 100644 index 0000000..3cc93f5 --- /dev/null +++ b/src/sideshift.js @@ -0,0 +1,104 @@ +// @flow + +import type {StandardTx, SwapFuncParams} from './checkSwapService' + +const {checkSwapService} = require('./checkSwapService.js') +const js = require('jsonfile') +const fetch = require('node-fetch') +const crypto = require('crypto') + +const confFileName = './config.json' +const config = js.readFileSync(confFileName) + +const SIDESHIFT_CACHE = './cache/xaiRaw.json' +const PAGE_LIMIT = 500 +const TRANSACTIONS_TO_FETCH = 1500 + +type SideShiftTransaction = { + id: string, + depositAddress: { + address: string + }, + depositAsset: string, + depositMin: number, + depositMax: number, + settleAddress: { + address: string + }, + settleAsset: string, + settleAmount: number, + createdAt: string +} + +async function doSideShift (swapFuncParams: SwapFuncParams) { + return checkSwapService(fetchSideShift, + SIDESHIFT_CACHE, + 'XAI', + swapFuncParams) +} + +function affiliateSignature (affiliateId: string, affiliateSecret: string, time: number): string { + return crypto.createHmac('sha1', affiliateSecret) + .update(affiliateId + time) + .digest('hex') +} + +async function fetchSideShift (swapFuncParams: SwapFuncParams) { + if (!swapFuncParams.useCache) { + console.log('Fetching SideShift.ai...') + } + let diskCache = {txs: []} + try { + diskCache = js.readFileSync(SIDESHIFT_CACHE) + } catch (e) { + console.log(e) + } + + const newTransactions: StandardTx[] = [] + let offset = 0 + + while (1 && !swapFuncParams.useCache) { + const time = Date.now() + const signature = affiliateSignature(config.sideShiftAffiliateId, config.sideShiftAffiliateSecret, time) + + try { + const url = `https://sideshift.ai/api/affiliate/completedOrders?limit=${PAGE_LIMIT}&offset=${offset}&affiliateId=${config.sideShiftAffiliateId}&time=${time}&signature=${signature}` + const transactions: SideShiftTransaction[] = await fetch(url) + .then(response => response.json()) + + for (const tx of transactions) { + const timestamp = new Date(tx.createdAt).getTime() / 1000 + const xaiTx: StandardTx = { + status: 'complete', + inputTXID: tx.id, + inputAddress: tx.depositAddress.address, + inputCurrency: tx.depositAsset.toUpperCase(), + inputAmount: tx.depositMin, + outputAddress: tx.settleAddress.address, + outputCurrency: tx.settleAsset.toUpperCase(), + outputAmount: tx.settleAmount.toString(), + timestamp + } + newTransactions.push(xaiTx) + } + + if (transactions.length < PAGE_LIMIT) { + break + } + } catch (e) { + console.log(e) + break + } + if (offset > TRANSACTIONS_TO_FETCH) { + break + } + offset += PAGE_LIMIT + } + + return { + diskCache, + newTransactions + } +} + +module.exports = {doSideShift}