From a175e03bfa1b484eac340c0c86ca05e6cbf4efb1 Mon Sep 17 00:00:00 2001 From: joeuhren Date: Fri, 7 Jun 2019 20:05:28 -0600 Subject: [PATCH] v1.0.1 --- UPGRADE | 8 ++- app.js | 13 ++++ lib/database.js | 32 +++++---- lib/locale.js | 1 + lib/markets/database.js | 32 +++++---- lib/markets/stex.js | 140 ++++++++++++++++++++++++++++++++++++++++ lib/settings.js | 1 + locale/en.json | 1 + package.json | 6 +- scripts/sync.js | 19 ++++-- settings.json.template | 3 +- views/info.pug | 6 ++ views/markets/stex.pug | 106 ++++++++++++++++++++++++++++++ 13 files changed, 332 insertions(+), 36 deletions(-) create mode 100644 lib/markets/stex.js create mode 100644 views/markets/stex.pug diff --git a/UPGRADE b/UPGRADE index 2a4e47d4..d86b6a41 100644 --- a/UPGRADE +++ b/UPGRADE @@ -1,4 +1,10 @@ NOTE: All updates require the explorer to be restarted -1.0.0 +1.0.1 [June 7, 2019] +-Added stex.com market support +-Added a new public api /ext/getbasicstats +-Fixed all depreciation warnings regarding mongo db.update statements +-Updated outdated mongodb and mongoose references + +1.0.0 [May 27, 2019] -Initial release \ No newline at end of file diff --git a/app.js b/app.js index 0bf62c54..bc7ca4c5 100644 --- a/app.js +++ b/app.js @@ -117,6 +117,19 @@ app.use('/ext/getcurrentprice', function(req,res){ }); }); +app.use('/ext/getbasicstats', function(req,res){ + lib.get_blockcount(function(blockcount){ + lib.get_supply(function(supply){ + db.get_stats(settings.coin, function (stats){ + lib.get_masternodecount(function(masternodestotal){ + eval('var p_ext = { "block_count": blockcount, "money_supply": supply, "last_price_'+settings.markets.exchange.toLowerCase()+'": stats.last_price, "last_price_usd": stats.last_usd_price, "masternode_count": masternodestotal.total }'); + res.send(p_ext); + }); + }); + }); + }); +}); + app.use('/ext/connections', function(req,res){ db.get_peers(function(peers){ res.send({data: peers}); diff --git a/lib/database.js b/lib/database.js index cfd7afaa..b9bd65a2 100644 --- a/lib/database.js +++ b/lib/database.js @@ -17,6 +17,7 @@ var mongoose = require('mongoose') , empoex = require('./markets/empoex') , ccex = require('./markets/ccex') , coinexchange = require('./markets/coinexchange') + , stex = require('./markets/stex') , coindesk = require('./apis/coindesk'); function find_address(hash, cb) { @@ -45,7 +46,7 @@ function update_address(hash, txid, amount, type, cb) { if (address) { // if coinbase (new coins PoW), update sent only and return cb. if ( hash == 'coinbase' ) { - Address.update({a_id:hash}, { + Address.updateOne({a_id:hash}, { sent: address.sent + amount, balance: 0, }, function() { @@ -67,7 +68,7 @@ function update_address(hash, txid, amount, type, cb) { if ( tx_array.length > settings.txcount ) { tx_array.shift(); } - Address.update({a_id:hash}, { + Address.updateOne({a_id:hash}, { txs: tx_array, received: received, sent: sent, @@ -79,7 +80,7 @@ function update_address(hash, txid, amount, type, cb) { if (type == tx_array[index].type) { return cb(); //duplicate } else { - Address.update({a_id:hash}, { + Address.updateOne({a_id:hash}, { txs: tx_array, received: received, sent: sent, @@ -236,6 +237,11 @@ function get_market_data(market, cb) { return cb(err, obj); }); break; + case 'stex': + stex.get_data(settings.markets.coin, settings.markets.exchange, settings.markets.stex_id, function(err, obj){ + return cb(err, obj); + }); + break; default: return cb(null); } @@ -263,7 +269,7 @@ module.exports = { Stats.findOne({last_usd_price: {$exists: false}}, function(err, stats) { if (stats) { // the last_usd_price needs to be added to the collection - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { last_usd_price: 0, }, function() { return cb(null); }); } @@ -317,7 +323,7 @@ module.exports = { update_richlist: function(list, cb){ if(list == 'received') { Address.find({}).sort({received: 'desc'}).limit(100).exec(function(err, addresses){ - Richlist.update({coin: settings.coin}, { + Richlist.updateOne({coin: settings.coin}, { received: addresses, }, function() { return cb(); @@ -325,7 +331,7 @@ module.exports = { }); } else { //balance Address.find({}).sort({balance: 'desc'}).limit(100).exec(function(err, addresses){ - Richlist.update({coin: settings.coin}, { + Richlist.updateOne({coin: settings.coin}, { balance: addresses, }, function() { return cb(); @@ -563,7 +569,7 @@ module.exports = { }); }, function(){ console.log(newVotes); - Heavy.update({coin: coin}, { + Heavy.updateOne({coin: coin}, { lvote: vote, reward: reward, supply: supply, @@ -592,7 +598,7 @@ module.exports = { update_markets_db: function(market, cb) { get_market_data(market, function (err, obj) { if (err == null) { - Markets.update({market:market}, { + Markets.updateOne({market:market}, { chartdata: JSON.stringify(obj.chartdata), buys: obj.buys, sells: obj.sells, @@ -600,7 +606,7 @@ module.exports = { summary: obj.stats, }, function() { if ( market == settings.markets.default ) { - Stats.update({coin:settings.coin}, { + Stats.updateOne({coin:settings.coin}, { last_price: obj.stats.last, }, function(){ return cb(null); @@ -623,7 +629,7 @@ module.exports = { // Get current stats Stats.findOne({coin:settings.coin}, function(err, stats) { // Update the last usd price - Stats.update({coin:settings.coin}, { + Stats.updateOne({coin:settings.coin}, { last_usd_price: (last_usd * stats.last_price), }, function(){ return cb(null); @@ -644,7 +650,7 @@ module.exports = { } lib.get_supply( function (supply){ lib.get_connectioncount(function (connections) { - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { coin: coin, count : count, supply: supply, @@ -664,7 +670,7 @@ module.exports = { var x = loop.iteration(); if (x % 5000 === 0) { Tx.find({}).where('blockindex').lt(start + x).sort({timestamp: 'desc'}).limit(settings.index.last_txs).exec(function(err, txs){ - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { last: start + x - 1, last_txs: '' //not used anymore left to clear out existing objects }, function() {}); @@ -710,7 +716,7 @@ module.exports = { }); }, function(){ Tx.find({}).sort({timestamp: 'desc'}).limit(settings.index.last_txs).exec(function(err, txs){ - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { last: end, last_txs: '' //not used anymore left to clear out existing objects }, function() { diff --git a/lib/locale.js b/lib/locale.js index 8acb65d2..f1ea3f52 100644 --- a/lib/locale.js +++ b/lib/locale.js @@ -148,6 +148,7 @@ exports.cryptopia = "Cryptopia", exports.empoex = "Empoex", exports.ccex = "C-Cex", exports.coinexchange = "CoinExchange", +exports.stex = "Stex", exports.reloadLocale = function reloadLocale(locale) { // Discover where the locale file lives diff --git a/lib/markets/database.js b/lib/markets/database.js index cfd7afaa..b9bd65a2 100644 --- a/lib/markets/database.js +++ b/lib/markets/database.js @@ -17,6 +17,7 @@ var mongoose = require('mongoose') , empoex = require('./markets/empoex') , ccex = require('./markets/ccex') , coinexchange = require('./markets/coinexchange') + , stex = require('./markets/stex') , coindesk = require('./apis/coindesk'); function find_address(hash, cb) { @@ -45,7 +46,7 @@ function update_address(hash, txid, amount, type, cb) { if (address) { // if coinbase (new coins PoW), update sent only and return cb. if ( hash == 'coinbase' ) { - Address.update({a_id:hash}, { + Address.updateOne({a_id:hash}, { sent: address.sent + amount, balance: 0, }, function() { @@ -67,7 +68,7 @@ function update_address(hash, txid, amount, type, cb) { if ( tx_array.length > settings.txcount ) { tx_array.shift(); } - Address.update({a_id:hash}, { + Address.updateOne({a_id:hash}, { txs: tx_array, received: received, sent: sent, @@ -79,7 +80,7 @@ function update_address(hash, txid, amount, type, cb) { if (type == tx_array[index].type) { return cb(); //duplicate } else { - Address.update({a_id:hash}, { + Address.updateOne({a_id:hash}, { txs: tx_array, received: received, sent: sent, @@ -236,6 +237,11 @@ function get_market_data(market, cb) { return cb(err, obj); }); break; + case 'stex': + stex.get_data(settings.markets.coin, settings.markets.exchange, settings.markets.stex_id, function(err, obj){ + return cb(err, obj); + }); + break; default: return cb(null); } @@ -263,7 +269,7 @@ module.exports = { Stats.findOne({last_usd_price: {$exists: false}}, function(err, stats) { if (stats) { // the last_usd_price needs to be added to the collection - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { last_usd_price: 0, }, function() { return cb(null); }); } @@ -317,7 +323,7 @@ module.exports = { update_richlist: function(list, cb){ if(list == 'received') { Address.find({}).sort({received: 'desc'}).limit(100).exec(function(err, addresses){ - Richlist.update({coin: settings.coin}, { + Richlist.updateOne({coin: settings.coin}, { received: addresses, }, function() { return cb(); @@ -325,7 +331,7 @@ module.exports = { }); } else { //balance Address.find({}).sort({balance: 'desc'}).limit(100).exec(function(err, addresses){ - Richlist.update({coin: settings.coin}, { + Richlist.updateOne({coin: settings.coin}, { balance: addresses, }, function() { return cb(); @@ -563,7 +569,7 @@ module.exports = { }); }, function(){ console.log(newVotes); - Heavy.update({coin: coin}, { + Heavy.updateOne({coin: coin}, { lvote: vote, reward: reward, supply: supply, @@ -592,7 +598,7 @@ module.exports = { update_markets_db: function(market, cb) { get_market_data(market, function (err, obj) { if (err == null) { - Markets.update({market:market}, { + Markets.updateOne({market:market}, { chartdata: JSON.stringify(obj.chartdata), buys: obj.buys, sells: obj.sells, @@ -600,7 +606,7 @@ module.exports = { summary: obj.stats, }, function() { if ( market == settings.markets.default ) { - Stats.update({coin:settings.coin}, { + Stats.updateOne({coin:settings.coin}, { last_price: obj.stats.last, }, function(){ return cb(null); @@ -623,7 +629,7 @@ module.exports = { // Get current stats Stats.findOne({coin:settings.coin}, function(err, stats) { // Update the last usd price - Stats.update({coin:settings.coin}, { + Stats.updateOne({coin:settings.coin}, { last_usd_price: (last_usd * stats.last_price), }, function(){ return cb(null); @@ -644,7 +650,7 @@ module.exports = { } lib.get_supply( function (supply){ lib.get_connectioncount(function (connections) { - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { coin: coin, count : count, supply: supply, @@ -664,7 +670,7 @@ module.exports = { var x = loop.iteration(); if (x % 5000 === 0) { Tx.find({}).where('blockindex').lt(start + x).sort({timestamp: 'desc'}).limit(settings.index.last_txs).exec(function(err, txs){ - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { last: start + x - 1, last_txs: '' //not used anymore left to clear out existing objects }, function() {}); @@ -710,7 +716,7 @@ module.exports = { }); }, function(){ Tx.find({}).sort({timestamp: 'desc'}).limit(settings.index.last_txs).exec(function(err, txs){ - Stats.update({coin: coin}, { + Stats.updateOne({coin: coin}, { last: end, last_txs: '' //not used anymore left to clear out existing objects }, function() { diff --git a/lib/markets/stex.js b/lib/markets/stex.js new file mode 100644 index 00000000..3c8cc152 --- /dev/null +++ b/lib/markets/stex.js @@ -0,0 +1,140 @@ +var request = require('request'); +var base_url = 'https://api3.stex.com/public'; + +function get_summary(coin, exchange, stex_id, cb) { + var summary = {}; + + request({ uri: base_url + '/ticker/' + stex_id, json: true, headers: {'User-Agent': 'eiquidus'} }, function (error, response, body) { + if (error) + return cb(error, null); + else if (body.success === true) { + summary['bid'] = parseFloat(body.data['bid']).toFixed(8); + summary['ask'] = parseFloat(body.data['ask']).toFixed(8); + summary['volume'] = body.data['volume']; + summary['high'] = parseFloat(body.data['high']).toFixed(8); + summary['low'] = parseFloat(body.data['low']).toFixed(8); + summary['last'] = parseFloat(body.data['last']).toFixed(8); + return cb(null, summary); + } else + return cb(error, null); + }).on('error', function(err) { + return cb(error, null); + }); +} + +function get_trades(coin, exchange, stex_id, cb) { + var req_url = base_url + '/trades/' + stex_id + '?sort=DESC&limit=100'; + + request({ uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'} }, function (error, response, body) { + if (body.success == true) { + var tTrades = body.data; + var trades = []; + for (var i = 0; i < tTrades.length; i++) { + var Trade = { + orderpair: coin, + ordertype: tTrades[i].type, + amount: parseFloat(tTrades[i].amount).toFixed(8), + price: parseFloat(tTrades[i].price).toFixed(8), + // total: parseFloat(tTrades[i].Total).toFixed(8) + // Necessary because API will return 0.00 for small volume transactions + total: (parseFloat(tTrades[i].amount).toFixed(8) * parseFloat(tTrades[i].price)).toFixed(8), + timestamp: tTrades[i].timestamp + } + trades.push(Trade); + } + return cb(null, trades); + } + else + return cb(body.message, null); + }).on('error', function(err) { + return cb(error, null); + }); +} + +function get_orders(coin, exchange, stex_id, cb) { + var req_url = base_url + '/orderbook/' + stex_id + '?limit_bids=100&limit_asks=100'; + + request({ uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'} }, function (error, response, body) { + if (body.success == true) { + var orders = body.data; + var buys = []; + var sells = []; + if (orders['bid'].length > 0){ + for (var i = 0; i < orders['bid'].length; i++) { + var order = { + amount: parseFloat(orders.bid[i].amount).toFixed(8), + price: parseFloat(orders.bid[i].price).toFixed(8), + // total: parseFloat(orders.bid[i].Total).toFixed(8) + // Necessary because API will return 0.00 for small volume transactions + total: (parseFloat(orders.bid[i].amount).toFixed(8) * parseFloat(orders.bid[i].price)).toFixed(8) + } + buys.push(order); + } + } + if (orders['ask'].length > 0) { + for (var x = 0; x < orders['ask'].length; x++) { + var order = { + amount: parseFloat(orders.ask[x].amount).toFixed(8), + price: parseFloat(orders.ask[x].price).toFixed(8), + // total: parseFloat(orders.ask[x].Total).toFixed(8) + // Necessary because API will return 0.00 for small volume transactions + total: (parseFloat(orders.ask[x].amount).toFixed(8) * parseFloat(orders.ask[x].price)).toFixed(8) + } + sells.push(order); + } + } + return cb(null, buys, sells); + } + else + return cb(body.message, [], []) + }).on('error', function(err) { + return cb(error, null, null); + }); +} + +function get_chartdata(coin, exchange, stex_id, cb) { + // do not collect chart data for now + return cb(null, []); +/* + var req_url = base_url + '/chart/' + stex_id + '/30?timeStart=' + start + '&timeEnd=' + end + '&limit=100'; + var end = Date.now(); + end = end / 1000; + start = end - 86400; + + request({ uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'} }, function (error, response, chartdata) { + if (error) { + return cb(error, []); + } else { + if (chartdata.success == true) { + var processed = []; + for (var i = 0; i < chartdata.data.length; i++) { + processed.push([chartdata.data[i].time, parseFloat(chartdata.data[i].open), parseFloat(chartdata.data[i].high), parseFloat(chartdata.data[i].low), parseFloat(chartdata.data[i].close)]); + if (i == chartdata.data.length - 1) + return cb(null, processed); + } + } + else + return cb(chartdata.message, []); + } + }); +*/ +} + +module.exports = { + get_data: function (coin, exchange, stex_id, cb) { + var error = null; + get_chartdata(coin, exchange, stex_id, function (err, chartdata) { + if (err) { chartdata = []; error = err; } + get_orders(coin, exchange, stex_id, function (err, buys, sells) { + if (err) { error = err; } + get_trades(coin, exchange, stex_id, function (err, trades) { + if (err) { error = err; } + get_summary(coin, exchange, stex_id, function (err, stats) { + if (err) { error = err; } + return cb(error, { buys: buys, sells: sells, chartdata: chartdata, trades: trades, stats: stats }); + }); + }); + }); + }); + } +}; \ No newline at end of file diff --git a/lib/settings.js b/lib/settings.js index 77558570..746ac031 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -98,6 +98,7 @@ exports.markets = { "cryptopia_id": "", "ccex_key" : "Get-Your-Own-Key", "coinexchange_id": "", + "stex_id": "", "default": "" }; diff --git a/locale/en.json b/locale/en.json index 87fc0b0f..c6933e0b 100644 --- a/locale/en.json +++ b/locale/en.json @@ -138,6 +138,7 @@ "cryptopia": "Cryptopia", "ccex": "C-Cex", "coinexchange": "CoinExchange", + "stex": "Stex", // Heavy rewards view "heavy_title": "Reward/voting information", diff --git a/package.json b/package.json index a75b1c64..ad37baaf 100644 --- a/package.json +++ b/package.json @@ -17,11 +17,11 @@ "pug": "~2.0.3", "request": "2.88.0", "jsonminify": "0.4.1", - "mongodb": "3.2.6", - "mongoose": "5.5.11", + "mongodb": "3.2.7", + "mongoose": "5.5.13", "qr-image": "~3.2.0" }, "devDependencies": { "jasmine": "~3.4.0" } -} +} \ No newline at end of file diff --git a/scripts/sync.js b/scripts/sync.js index 374b0f41..6b558f0e 100644 --- a/scripts/sync.js +++ b/scripts/sync.js @@ -146,11 +146,11 @@ is_locked(function (exists) { if (mode == 'reindex') { Tx.remove({}, function(err) { Address.remove({}, function(err2) { - Richlist.update({coin: settings.coin}, { + Richlist.updateOne({coin: settings.coin}, { received: [], balance: [], }, function(err3) { - Stats.update({coin: settings.coin}, { + Stats.updateOne({coin: settings.coin}, { last: 0, }, function() { console.log('index cleared (reindex)'); @@ -203,21 +203,30 @@ is_locked(function (exists) { if (!err) { console.log('%s market data updated successfully.', mkt); complete++; + if (complete == markets.length) + get_last_usd_price(); } else { console.log('%s: %s', mkt, err); complete++; + if (complete == markets.length) + get_last_usd_price(); } }); } else { console.log('error: entry for %s does not exists in markets db.', mkt); complete++; + if (complete == markets.length) + get_last_usd_price(); } }); } - // Get the last usd price for coinstats - db.get_last_usd_price(function(retVal) { exit(); }); } }); }); } -}); \ No newline at end of file +}); + +function get_last_usd_price() { + // Get the last usd price for coinstats + db.get_last_usd_price(function(retVal) { exit(); }); +} \ No newline at end of file diff --git a/settings.json.template b/settings.json.template index 801411e8..0745faac 100644 --- a/settings.json.template +++ b/settings.json.template @@ -107,7 +107,7 @@ }, // market settings - //supported markets: bittrex, poloniex, yobit, empoex, bleutrade, cryptopia, ccex, coinexchange + //supported markets: bittrex, poloniex, yobit, empoex, bleutrade, cryptopia, ccex, coinexchange, stex //default market is loaded by default and determines last price in header "markets": { "coin": "EXOR", @@ -116,6 +116,7 @@ "cryptopia_id": "", "ccex_key" : "Get-Your-Own-Key", "coinexchange_id": "", + "stex_id": "", "default": "" }, diff --git a/views/info.pug b/views/info.pug index 84c50db9..7dc31137 100644 --- a/views/info.pug +++ b/views/info.pug @@ -158,6 +158,12 @@ block content div em Returns last known exchange price a(href='/ext/getcurrentprice') #{address}/ext/getcurrentprice + li + p + div(style='font-weight:bold;') getbasicstats + div + em Returns basic statistics about the coin including: block count, circulating supply, USD price, BTC price and # of masternodes + a(href='/ext/getbasicstats') #{address}/ext/getbasicstats hr h3 Linking (GET) p diff --git a/views/markets/stex.pug b/views/markets/stex.pug new file mode 100644 index 00000000..f98aacd8 --- /dev/null +++ b/views/markets/stex.pug @@ -0,0 +1,106 @@ +extends menu + +block market_view + .row + .col-md-12 + .panel.panel-default + .panel-heading + strong #{settings.locale.stex} - #{marketdata.coin}/#{marketdata.exchange} - #{settings.locale.mkt_hours} + a(href='#') + span.fas.fa-chart-line.pull-right.view-chart-disabled.iquidus.market-toggle(data-toggle='tooltip', data-placement='bottom', title=settings.locale.mkt_no_chart) + table.table.table-bordered.summary-table + thead + tr + th #{settings.locale.mkt_high} + th #{settings.locale.mkt_low} + th #{settings.locale.mkt_volume} + th.hidden-xs #{settings.locale.mkt_top_bid} + th.hidden-xs #{settings.locale.mkt_top_ask} + th.hidden-xs #{settings.locale.mkt_last} + tbody + tr + td #{marketdata.data.summary.high} + td #{marketdata.data.summary.low} + td #{marketdata.data.summary.volume} + td.hidden-xs #{marketdata.data.summary.bid} + td.hidden-xs #{marketdata.data.summary.ask} + td.hidden-xs #{marketdata.data.summary.last} + .row + .col-md-6.col-xs-12 + .panel.panel-default + .panel-heading + h3.panel-title #{settings.locale.mkt_buy_orders} + table.table.table-striped.table-bordered.order-table + thead + tr + th #{settings.locale.mkt_price} (#{marketdata.exchange}) + th #{settings.locale.mkt_amount} (#{marketdata.coin}) + th.hidden-xs #{settings.locale.mkt_total} (#{marketdata.exchange}) + tbody + each buy in marketdata.data.buys + tr + td + =buy.price + td + =buy.amount + td.hidden-xs + =buy.total + .col-md-6.col-xs-12 + .panel.panel-default + .panel-heading + h3.panel-title #{settings.locale.mkt_sell_orders} + table.table.table-striped.table-bordered.order-table + thead + tr + th #{settings.locale.mkt_price} (#{marketdata.exchange}) + th #{settings.locale.mkt_amount} (#{marketdata.coin}) + th.hidden-xs #{settings.locale.mkt_total} (#{marketdata.exchange}) + tbody + each sell in marketdata.data.sells + tr + td + =sell.price + td + =sell.amount + td.hidden-xs + =sell.total + .row + .col-md-12 + .panel.panel-default + .panel-heading + h3.panel-title #{settings.locale.mkt_trade_history} + table.table.table-hover.history-table.table-bordered(cellspacing="0") + thead + tr + th.hidden-xs #{settings.locale.mkt_type} + th #{settings.locale.mkt_amount} (#{marketdata.coin}) + th #{settings.locale.mkt_price} (#{marketdata.exchange}) + th #{settings.locale.mkt_total} (#{marketdata.exchange}) + th.hidden-xs #{settings.locale.mkt_time_stamp} + tbody + each order in marketdata.data.history + if order.ordertype.toUpperCase() == 'BUY' + tr.success + td.hidden-xs + =order.ordertype + td + =order.amount + td + =order.price + td + =order.total + td.hidden-xs + =(new Date(order.timestamp * 1000).toUTCString()) + else + tr.danger + td.hidden-xs + =order.ordertype + td + =order.amount + td + =order.price + td + =order.total + td.hidden-xs + =(new Date(order.timestamp * 1000).toUTCString()) + .footer-padding \ No newline at end of file