diff --git a/src/bity/config.js b/src/bity/config.js index b3a4da6..0cb729a 100644 --- a/src/bity/config.js +++ b/src/bity/config.js @@ -76,5 +76,6 @@ export default { active: true } }, - encryptionKey: process.env.BITY_ENC_KEY || "" + encryptionKey: process.env.BITY_ENC_KEY || "", + disabledPairs: [] }; diff --git a/src/bity/methods/getEstimate.js b/src/bity/methods/getEstimate.js index 83a2504..682fb4e 100644 --- a/src/bity/methods/getEstimate.js +++ b/src/bity/methods/getEstimate.js @@ -10,6 +10,24 @@ export default body => { const pairOptions = [...Object.keys(configs.orderValues), ...Object.keys(configs.fiatValues)]; let notSupported = true; + if(configs.disabledPairs.includes(body.params.pair)){ + return resolve( + success({ + jsonrpc: '2.0', + result: { + input:{ + amount: 1, + minimum_amount: 0 + }, + output: { + amount: 0 + } + }, + id: body.id + }) + ); + } + if(pairOptions.includes(body.params.pair)){ if(configs.orderValues[body.params.pair]){ notSupported = !configs.orderValues[body.params.pair].active diff --git a/src/changelly/index.js b/src/changelly/index.js index 69fc405..cbdad71 100644 --- a/src/changelly/index.js +++ b/src/changelly/index.js @@ -1,9 +1,12 @@ -import { error, success } from "../response"; -import changellyConfigs from "./config"; -import allowedMethods from "./validMethods"; -import request from "../request"; -import crypto from "crypto"; +import {error, success} from '../response'; +import changellyConfigs from './config'; +import allowedMethods from './validMethods'; +import request from '../request'; +import crypto from 'crypto'; + export default (req, logger) => { + let alternativeHandlingMethods = ['getFixRate', 'getExchangeAmount']; + let activeMethod = false; // getFixRate getExchangeAmount return new Promise((resolve, reject) => { if (req.body) { let body = req.body; @@ -14,24 +17,37 @@ export default (req, logger) => { if (allowedMethods.indexOf(body.method) === -1) reject(error(`Invalid Method - ${body.method}`)); else { + activeMethod = alternativeHandlingMethods.includes(body.method); const req = { url: changellyConfigs.API_URL, headers: { - "api-key": changellyConfigs.CHANGELLY_API_KEY, + 'api-key': changellyConfigs.CHANGELLY_API_KEY, sign: crypto - .createHmac("sha512", changellyConfigs.CHANGELLY_SECRET) + .createHmac('sha512', changellyConfigs.CHANGELLY_SECRET) .update(JSON.stringify(body)) - .digest("hex") + .digest('hex') } }; request(req, body) .then(result => { + if(!Array.isArray(result)){ + if(result.error){ + if (logger) console.log('CHANGELLY ERROR', result); + resolve(success({result: [{ + min: 0, + max: 0, + result: 0, + id: 0 + }]})); + return; + } + } resolve(success(result)); }) .catch(err => { - if(logger) console.log('CHANGELLY ERROR', error); - if(logger) logger.errorReporter('changelly'); - reject(error(err, "")); + if (logger) console.log('CHANGELLY ERROR', err); + if (logger) logger.errorReporter('changelly'); + reject(error(err, '')); }); } } diff --git a/src/dexAg/config.js b/src/dexAg/config.js new file mode 100644 index 0000000..ef873ee --- /dev/null +++ b/src/dexAg/config.js @@ -0,0 +1,19 @@ +require("dotenv").config(); + +const SUPPORTED_DEXES = [ + 'uniswap', //ETH to Token -> OK, Token to Token -> + 'bancor', //ETH to Token -> , Token to Token -> + 'kyber', //ETH to Token -> , Token to Token -> + 'zero_x', //ETH to Token -> OK , Token to Token -> | (did have 1 eth-> token fail revert) + 'radar-relay', //ETH to Token -> , Token to Token -> + 'oasis', //ETH to Token -> OK, Token to Token -> | (did have 1 eth-> token fail revert) + 'synthetix', //ETH to Token -> , Token to Token -> + 'curvefi', //ETH to Token -> , Token to Token -> + 'ag' +]; + +export default { + SUPPORTED_DEXES, + PROXY_CONTRACT_ADDRESS: process.env.DEX_AG_PROXY_CONTRACT_ADDRESS || "", + API_URL: "https://api-v2.dex.ag/" +}; diff --git a/src/dexAg/index.js b/src/dexAg/index.js new file mode 100644 index 0000000..4df27c3 --- /dev/null +++ b/src/dexAg/index.js @@ -0,0 +1,58 @@ +import {error, success} from '../response'; +import changellyConfigs from './config'; +import allowedMethods from './validMethods'; +import request from '../request'; +import crypto from 'crypto'; +import { + createTransaction, + getPrice, + getSupportedCurrencies, + supportedDexes +} from './methods'; + +export default (req, logger) => { + return new Promise((resolve, reject) => { + + const errorLogging = error => { + if (logger) console.log('DEX_AG ERROR', error); + if (logger) logger.errorReporter('dexAg'); + reject(error); + }; + + if (req.body) { + let body = req.body; + if (logger) logger.process(body); + if (Array.isArray(body)) { + reject(error(`Invalid Request - ${body}`)); + } else { + if (allowedMethods.indexOf(body.method) === -1) + reject(error(`Invalid Method - ${body.method}`)); + else { + switch (body.method) { + case 'getPrice': + getPrice(body) + .then(resolve) + .catch(errorLogging); + break; + case 'createTransaction': + createTransaction(body) + .then(resolve) + .catch(errorLogging); + break; + case 'getSupportedCurrencies': + getSupportedCurrencies(body) + .then(resolve) + .catch(errorLogging); + break; + case 'supportedDexes': + supportedDexes(body) + .then(resolve) + .catch(errorLogging); + break; + + } + } + } + } + }); +}; diff --git a/src/dexAg/methods/createTransaction.js b/src/dexAg/methods/createTransaction.js new file mode 100644 index 0000000..258425b --- /dev/null +++ b/src/dexAg/methods/createTransaction.js @@ -0,0 +1,31 @@ +import configs from '../config'; +import request from '../../request'; +import {error, success} from '../../response'; + + +export default body => { + return new Promise((resolve, reject) => { + if (!body.params.transactionParams) { + reject(error('Missing body parameter', '')); + return; + } + const transactionParams = body.params.transactionParams; + const req = { + url: `${configs.API_URL}tradeAndSend?from=${transactionParams.fromCurrency}&to=${transactionParams.toCurrency}&fromAmount=${transactionParams.fromValue}&dex=${transactionParams.dex}&recipient=${transactionParams.toAddress}&proxy=${configs.PROXY_CONTRACT_ADDRESS}`, + method: 'GET' + }; + request(req) + .then(result => { + resolve( + success({ + jsonrpc: '2.0', + result: JSON.parse(result), + id: body.id + }) + ); + }) + .catch(err => { + reject(error(err, '')); + }); + }); +}; diff --git a/src/dexAg/methods/getPrice.js b/src/dexAg/methods/getPrice.js new file mode 100644 index 0000000..53224d0 --- /dev/null +++ b/src/dexAg/methods/getPrice.js @@ -0,0 +1,30 @@ +import configs from '../config'; +import request from '../../request'; +import {error, success} from '../../response'; + + +export default body => { + return new Promise((resolve, reject) => { + if (!body.params.fromToken || !body.params.toToken || !body.params.fromValue) { + reject(error('Missing body parameter', '')); + return; + } + const req = { + url: `${configs.API_URL}price?from=${body.params.fromToken}&to=${body.params.toToken}&fromAmount=${body.params.fromValue}&dex=all`, + method: 'GET' + }; + request(req) + .then(result => { + resolve( + success({ + jsonrpc: '2.0', + result: JSON.parse(result), + id: body.id + }) + ); + }) + .catch(err => { + reject(error(err, '')); + }); + }); +}; diff --git a/src/dexAg/methods/getSupportedCurrencies.js b/src/dexAg/methods/getSupportedCurrencies.js new file mode 100644 index 0000000..0448043 --- /dev/null +++ b/src/dexAg/methods/getSupportedCurrencies.js @@ -0,0 +1,26 @@ +import configs from '../config'; +import request from '../../request'; +import {error, success} from '../../response'; + + +export default body => { + return new Promise((resolve, reject) => { + const req = { + url: `${configs.API_URL}token-list-full`, + method: 'GET' + }; + request(req) + .then(result => { + resolve( + success({ + jsonrpc: '2.0', + result: JSON.parse(result), + id: body.id + }) + ); + }) + .catch(err => { + reject(error(err, '')); + }); + }); +}; diff --git a/src/dexAg/methods/index.js b/src/dexAg/methods/index.js new file mode 100644 index 0000000..e8ca47d --- /dev/null +++ b/src/dexAg/methods/index.js @@ -0,0 +1,11 @@ +import createTransaction from './createTransaction'; +import getPrice from './getPrice'; +import getSupportedCurrencies from './getSupportedCurrencies' +import supportedDexes from './supportedDexes'; + +export { + createTransaction, + getPrice, + getSupportedCurrencies, + supportedDexes +} \ No newline at end of file diff --git a/src/dexAg/methods/supportedDexes.js b/src/dexAg/methods/supportedDexes.js new file mode 100644 index 0000000..08cd0ba --- /dev/null +++ b/src/dexAg/methods/supportedDexes.js @@ -0,0 +1,15 @@ +import configs from '../config'; +import {error, success} from '../../response'; + + +export default body => { + return new Promise((resolve, reject) => { + resolve( + success({ + jsonrpc: '2.0', + result: configs.SUPPORTED_DEXES, + id: body.id + }) + ); + }); +}; diff --git a/src/dexAg/validMethods.js b/src/dexAg/validMethods.js new file mode 100644 index 0000000..cd1e916 --- /dev/null +++ b/src/dexAg/validMethods.js @@ -0,0 +1,6 @@ +export default [ + 'getPrice', + 'getSupportedCurrencies', + 'createTransaction', + 'supportedDexes' +]; diff --git a/src/index.js b/src/index.js index 4851767..47639be 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import api from "./api"; import changelly from "./changelly"; import bity from "./bity"; import kyber from './kyber' +import dexAg from './dexAg' import proxy from './proxy' import { cloudWatchLogger } from "./loggers"; @@ -20,6 +21,11 @@ api.post("/kyber", request => { return kyber(request, cloudwatch); }); +api.post("/dexag", request => { + const cloudwatch = new cloudWatchLogger("DEXAG"); + return dexAg(request, cloudwatch); +}); + api.get("/proxy", request => { const cloudwatch = new cloudWatchLogger("PROXY"); return proxy(request, cloudwatch); diff --git a/src/proxy/config.js b/src/proxy/config.js index 283d333..bca9d91 100644 --- a/src/proxy/config.js +++ b/src/proxy/config.js @@ -1,4 +1,5 @@ require("dotenv").config(); export default [ - 'api.stateofthedapps.com' + 'api.stateofthedapps.com', + 'api-v2.dex.ag' ]