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
This commit is contained in:
Joe Uhren
2023-10-27 18:33:52 -06:00
parent 83d7f4b182
commit 74ca66d44e
13 changed files with 1279 additions and 929 deletions
+109 -51
View File
@@ -131,7 +131,7 @@ function get_market_data(market, coin_symbol, pair_symbol, cb) {
if (fs.existsSync('./lib/markets/' + market + '.js')) {
exMarket = require('./markets/' + market);
exMarket.get_data({coin: coin_symbol, exchange: pair_symbol}, function(err, obj) {
exMarket.get_data({coin: coin_symbol, exchange: pair_symbol, api_error_msg: locale.mkt_unexpected_api_data}, function(err, obj) {
return cb(err, obj);
});
} else
@@ -214,9 +214,11 @@ function hex_to_ascii(hex) {
}
function init_markets(cb) {
let installed_markets = [];
// check if markets/exchanges feature is enabled
if (settings.markets_page.enabled == true) {
var marketCounter = 0;
let marketCounter = 0;
// loop through and test all exchanges defined in the settings.json file
Object.keys(settings.markets_page.exchanges).forEach(function (key, index, map) {
@@ -224,20 +226,29 @@ function init_markets(cb) {
if (settings.markets_page.exchanges[key].enabled == true) {
// check if exchange is installed/supported
if (module.exports.fs.existsSync('./lib/markets/' + key + '.js')) {
var pairCounter = 0;
let pairCounter = 0;
// loop through all trading pairs
settings.markets_page.exchanges[key].trading_pairs.forEach(function (pair_key, pair_index, pair_map) {
// split the pair data
var split_pair = pair_key.split('/');
let split_pair = pair_key.toUpperCase().split('/');
// check if this is a valid trading pair
if (split_pair.length == 2) {
// add this pair to the list of installed markets
installed_markets.push({
market: key,
coin_symbol: split_pair[0],
pair_symbol: split_pair[1]
});
// lookup the exchange in the market collection
module.exports.check_market(key, split_pair[0], split_pair[1], function(market, exists) {
// check if exchange trading pair exists in the market collection
if (!exists) {
// exchange doesn't exist in the market collection so add a default definition now
console.log('No %s: %s entry found. Creating new entry now..', market, pair_key);
console.log('No %s[%s] entry found. Creating new entry now..', market, split_pair[0] + '/' + split_pair[1]);
module.exports.create_market(split_pair[0], split_pair[1], market, function() {
pairCounter++;
@@ -248,7 +259,7 @@ function init_markets(cb) {
// check if all exchanges have been tested
if (marketCounter == Object.keys(settings.markets_page.exchanges).length) {
// finished initializing markets
return cb();
return cb(installed_markets);
}
});
} else {
@@ -262,7 +273,7 @@ function init_markets(cb) {
// check if all exchanges have been tested
if (marketCounter == Object.keys(settings.markets_page.exchanges).length) {
// finished initializing markets
return cb();
return cb(installed_markets);
}
});
} else {
@@ -282,10 +293,47 @@ function init_markets(cb) {
// check if all exchanges have been tested
if (marketCounter == Object.keys(settings.markets_page.exchanges).length) {
// finished initializing markets
return cb();
return cb(installed_markets);
}
} else
return cb(installed_markets);
}
function remove_inactive_markets(installed_markets, cb) {
// lookup the list of markets in the database collection
Markets.find({}).then((db_markets) => {
// check if the database has any markets installed
if (db_markets != null && db_markets.length > 0) {
// loop through the list of markets in the database
lib.syncLoop(db_markets.length, function(market_loop) {
let m = market_loop.iteration();
// check if this market is installed
if (installed_markets.findIndex(x => x.market.toUpperCase() == db_markets[m].market.toUpperCase() && x.coin_symbol.toUpperCase() == db_markets[m].coin_symbol.toUpperCase() && x.pair_symbol.toUpperCase() == db_markets[m].pair_symbol.toUpperCase()) == -1) {
// remove this market from the database because it is not installed or active
Markets.deleteOne({market: db_markets[m].market, coin_symbol: db_markets[m].coin_symbol, pair_symbol: db_markets[m].pair_symbol}).then(() => {
// move to the next market record
market_loop.next();
}).catch((err) => {
console.log(err);
// move to the next market record
market_loop.next();
});
} else {
// move to the next market record
market_loop.next();
}
}, function() {
// finished removing inactive markets
return cb();
});
} else
return cb();
}).catch((err) => {
console.log(err);
return cb();
});
}
function init_heavy(cb) {
@@ -945,7 +993,7 @@ module.exports = {
});
newMarkets.save().then(() => {
console.log("Initial market entry created for %s: %s", market, coin_symbol +'/' + pair_symbol);
console.log("Initial market entry created for %s[%s]", market, coin_symbol + '/' + pair_symbol);
return cb();
}).catch((err) => {
console.log(err);
@@ -1308,30 +1356,37 @@ module.exports = {
// check if the default market pair is found in the coin list
if (index > -1) {
// get the usd value of the default market pair from coingecko
coingecko.get_data(coin_list[index].id, function (err, last_usd) {
// check for errors
if (err == null) {
// get current stats
Stats.findOne({coin: settings.coin.name}).then((stats) => {
// update the last usd price
Stats.updateOne({coin: settings.coin.name}, {
last_usd_price: (last_usd * stats.last_price)
}).then(() => {
// last usd price updated successfully
return cb(null);
// initialize the rate limiter to wait 2 seconds between requests to prevent abusing external apis
var rateLimitLib = require('./ratelimit');
var rateLimit = new rateLimitLib.RateLimit(1, 2000, false);
// automatically pause for 2 seconds in between requests
rateLimit.schedule(function() {
// get the usd value of the default market pair from coingecko
coingecko.get_data(coin_list[index].id, function (err, last_usd) {
// check for errors
if (err == null) {
// get current stats
Stats.findOne({coin: settings.coin.name}).then((stats) => {
// update the last usd price
Stats.updateOne({coin: settings.coin.name}, {
last_usd_price: (last_usd * stats.last_price)
}).then(() => {
// last usd price updated successfully
return cb(null);
}).catch((err) => {
// return error msg
return cb(err);
});
}).catch((err) => {
// return error msg
return cb(err);
});
}).catch((err) => {
} else {
// return error msg
return cb(err);
});
} else {
// return error msg
return cb(err);
}
}
});
});
} else {
// return error msg
@@ -1778,32 +1833,35 @@ module.exports = {
// initialize the stats collection
module.exports.create_stats(settings.coin.name, skip, function() {
// check and initialize the markets collection
init_markets(function() {
// add new field(s) to tx collection if missing
module.exports.check_txes(function(txes_exists) {
// add new field(s) to masternode collection if missing
module.exports.check_masternodes(function(masternodes_exists) {
// add new field(s) and/or rename old field(s) in networkhistory collection if applicable
module.exports.check_networkhistory(function(networkhistory_exists) {
// check if richlist collection is initialized
module.exports.check_richlist(settings.coin.name, function(richlist_exists) {
skip = true;
init_markets(function(installed_markets) {
// remove inactive markets from the database collection
remove_inactive_markets(installed_markets, function() {
// add new field(s) to tx collection if missing
module.exports.check_txes(function(txes_exists) {
// add new field(s) to masternode collection if missing
module.exports.check_masternodes(function(masternodes_exists) {
// add new field(s) and/or rename old field(s) in networkhistory collection if applicable
module.exports.check_networkhistory(function(networkhistory_exists) {
// check if richlist collection is initialized
module.exports.check_richlist(settings.coin.name, function(richlist_exists) {
skip = true;
// determine if richlist collection already exists
if (richlist_exists == false) {
console.log('No richlist entry found. Creating new entry now..');
skip = false;
}
// determine if richlist collection already exists
if (richlist_exists == false) {
console.log('No richlist entry found. Creating new entry now..');
skip = false;
}
// initialize the richlist collection
module.exports.create_richlist(settings.coin.name, skip, function() {
// check and initialize the heavycoin collection
init_heavy(function() {
// check and initialize the claimaddress collection
init_claimaddress(settings.coin.name, function() {
// finished initializing startup data
console.log('Database initialization complete');
return cb();
// initialize the richlist collection
module.exports.create_richlist(settings.coin.name, skip, function() {
// check and initialize the heavycoin collection
init_heavy(function() {
// check and initialize the claimaddress collection
init_claimaddress(settings.coin.name, function() {
// finished initializing startup data
console.log('Database initialization complete');
return cb();
});
});
});
});