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

Add dex ag #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/bity/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ export default {
active: true
}
},
encryptionKey: process.env.BITY_ENC_KEY || ""
encryptionKey: process.env.BITY_ENC_KEY || "",
disabledPairs: []
};
18 changes: 18 additions & 0 deletions src/bity/methods/getEstimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 27 additions & 11 deletions src/changelly/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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, ''));
});
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/dexAg/config.js
Original file line number Diff line number Diff line change
@@ -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/"
};
58 changes: 58 additions & 0 deletions src/dexAg/index.js
Original file line number Diff line number Diff line change
@@ -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;

}
}
}
}
});
};
31 changes: 31 additions & 0 deletions src/dexAg/methods/createTransaction.js
Original file line number Diff line number Diff line change
@@ -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, ''));
});
});
};
30 changes: 30 additions & 0 deletions src/dexAg/methods/getPrice.js
Original file line number Diff line number Diff line change
@@ -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, ''));
});
});
};
26 changes: 26 additions & 0 deletions src/dexAg/methods/getSupportedCurrencies.js
Original file line number Diff line number Diff line change
@@ -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, ''));
});
});
};
11 changes: 11 additions & 0 deletions src/dexAg/methods/index.js
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions src/dexAg/methods/supportedDexes.js
Original file line number Diff line number Diff line change
@@ -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
})
);
});
};
6 changes: 6 additions & 0 deletions src/dexAg/validMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default [
'getPrice',
'getSupportedCurrencies',
'createTransaction',
'supportedDexes'
];
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/proxy/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("dotenv").config();
export default [
'api.stateofthedapps.com'
'api.stateofthedapps.com',
'api-v2.dex.ag'
]