Files
purple-explorer/lib/markets/xeggex.js
T
Joe Uhren 74ca66d44e Market improvements
-All external market apis have been normalized with a similar coding format, improved error handling with new wait times in between api calls to prevent abuse issues with sending too many requests too quickly
-All general market code has been reviewed and improved where necessary to help prevent sync issues
-Inactive markets are now removed from the markets collection on startup of the explorer to help prevent bloating the database
-The yobit api url has been changed to the new url
-The poloniex market has been updated to use the newest api
-The southxchange exchange trading link has been changed to the new url
-Miscellaneous locale string changes and reorganization
2023-10-27 18:33:52 -06:00

139 lines
5.4 KiB
JavaScript

const request = require('postman-request');
const base_url = 'https://api.xeggex.com/api/v2';
const market_url_template = 'https://xeggex.com/market/{coin}_{base}';
// initialize the rate limiter to wait 2 seconds between requests to prevent abusing external apis
const rateLimitLib = require('../ratelimit');
const rateLimit = new rateLimitLib.RateLimit(1, 2000, false);
function get_summary(coin, exchange, api_error_msg, cb) {
const req_url = base_url + '/market/getbysymbol/' + coin + '_' + exchange;
// pause for 2 seconds before continuing
rateLimit.schedule(function() {
request({uri: req_url, json: true}, function (error, response, body) {
if (error)
return cb(error, null);
else if (body == null || body == '' || typeof body !== 'object')
return cb(api_error_msg, null);
else if (body.error != null)
return cb((body.error.message != null ? body.error.message : api_error_msg), null);
else {
try {
const summary = {
'high': parseFloat(body.highPriceNumber),
'low': parseFloat(body.lowPriceNumber),
'volume': parseFloat(body.volumeNumber),
'volume_btc': parseFloat(body.volumeUsdNumber),
'bid': parseFloat(body.bestBidNumber),
'ask': parseFloat(body.bestAskNumber),
'last': parseFloat(body.lastPriceNumber),
'prev': parseFloat(body.yesterdayPriceNumber),
'change': parseFloat(body.changePercentNumber)
};
return cb(null, summary);
} catch(err) {
return cb(api_error_msg, null);
}
}
});
});
}
function get_trades(coin, exchange, api_error_msg, cb) {
const req_url = base_url + '/historical_trades?ticker_id=' + coin + '_' + exchange + '&limit=300';
// pause for 2 seconds before continuing
rateLimit.schedule(function() {
request({uri: req_url, json: true}, function (error, response, body) {
if (error)
return cb(error, null);
else if (body == null || body == '' || typeof body !== 'object')
return cb(api_error_msg, null);
else if (body.error != null)
return cb((body.error.message != null ? body.error.message : api_error_msg), null);
else {
try {
let trades = [];
for (let t = 0; t < body.length; t++) {
trades.push({
ordertype: body[t].type,
price: parseFloat(body[t].price),
quantity: parseFloat(body[t].base_volume),
timestamp: parseInt(new Date(body[t].trade_timestamp).getTime() / 1000)
});
}
return cb(null, trades);
} catch(err) {
return cb(api_error_msg, null);
}
}
});
});
}
function get_orders(coin, exchange, api_error_msg, cb) {
const req_url = base_url + '/market/getorderbookbysymbol/' + coin + '_' + exchange;
// NOTE: no need to pause here because this is the first api call
request({uri: req_url, json: true}, function (error, response, body) {
if (error)
return cb(error, null, null);
else if (body == null || body == '' || typeof body !== 'object')
return cb(api_error_msg, null, null);
else if (body.error != null)
return cb((body.error.message != null ? body.error.message : api_error_msg), null, null);
else {
try {
let buys = [];
let sells = [];
for (let b = 0; b < body.bids.length; b++) {
buys.push({
price: parseFloat(body.bids[b].numberprice),
quantity: parseFloat(body.bids[b].quantity)
});
}
for (let s = 0; s < body.asks.length; s++) {
sells.push({
price: parseFloat(body.asks[s].numberprice),
quantity: parseFloat(body.asks[s].quantity)
});
}
return cb(null, buys, sells);
} catch(err) {
return cb(api_error_msg, null, null);
}
}
});
}
module.exports = {
market_name: 'Xeggex',
market_logo: 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAsVBMVEUAAABvY0FMTUHMoUJZV0FDSkFJTUG7l0KNd0GGcUF3akF8bEH4xEL4x0PzuUPzuUPgskPhukPetEPVs0LLpUK8n0KpjUGli0G4lkKagEJSU0H+zkPzv0PktUPWq0PeuELTqEPHnULYtEPSqkLar0LRqUPIokPKokO3lEK2k0K+m0K0kkKniEFmYEFlX0H/0kT/00P/6UT/1UP/40P/4EP/2UP/60T/5UP/xUP+xEP9xEOWOrtXAAAAL3RSTlMAKwjKGQ0Fk2BQRDz89/Hw69POxbWso52OaBX69+7kysjIw7+8u66soJ6Ig3kjHAzaxeEAAACkSURBVBjThc03gsIwFADRkRyxyRl2yTl+SbZJ9z8YhQsKCl431fBlaBoPMTHEIs+GiQiahdevOh3m1b7nJgH4poPvWrp3YSlXgIrtou4ebG0FAA4Wle8Y3SJKYaLyNiprf1Y1E/Inn17ZlHWWpiGl0+1MlCfc92Un2QbmVtEtL2OZ4feClozpGB/0tPAG/5nWrjZYFM2Ao6u/xIwgNvKouyE/vQGJsQ9k11uR2AAAAABJRU5ErkJggg==',
market_url_template: market_url_template,
market_url_case: 'u',
get_data: function(settings, cb) {
get_orders(settings.coin, settings.exchange, settings.api_error_msg, function(order_error, buys, sells) {
if (order_error == null) {
get_trades(settings.coin, settings.exchange, settings.api_error_msg, function(trade_error, trades) {
if (trade_error == null) {
get_summary(settings.coin, settings.exchange, settings.api_error_msg, function(summary_error, stats) {
if (summary_error == null)
return cb(null, {buys: buys, sells: sells, trades: trades, stats: stats, chartdata: null});
else
return cb(summary_error, null);
});
} else
return cb(trade_error, null);
});
} else
return cb(order_error, null);
});
}
};