diff --git a/src/app.js b/src/app.js index 8f2be44..b97a756 100644 --- a/src/app.js +++ b/src/app.js @@ -1,9 +1,10 @@ +'use strict'; const express = require('express'); const cookieParser = require("cookie-parser"); const sessions = require('express-session'); const Request = require('./request'); -const REFRESH_INTERVAL = 60 * 1000; //1 min +const REFRESH_INTERVAL = 5 * 1000; //10 * 60 * 1000; module.exports = function App(secret, trustedIDs, DB) { diff --git a/src/backup/storage.js b/src/backup/storage.js index 582e0b6..b17bfcd 100644 --- a/src/backup/storage.js +++ b/src/backup/storage.js @@ -1,3 +1,4 @@ +'use strict'; const config = require('../../args/backup-config.json'); const floGlobals = require('../../public/floGlobals'); require('../set_globals'); diff --git a/src/backup/transmit.js b/src/backup/transmit.js index 4c87e12..c91b987 100644 --- a/src/backup/transmit.js +++ b/src/backup/transmit.js @@ -1,3 +1,4 @@ +'use strict'; const WebSocket = require('ws'); var DB; //Container for database diff --git a/src/group.js b/src/group.js index 33e1015..84c3dbb 100644 --- a/src/group.js +++ b/src/group.js @@ -1,3 +1,5 @@ +'use strict'; + var DB; //container for database function addTag(floID, tag) { @@ -127,7 +129,7 @@ const bestPair = function(cur_rate, tags_buy, tags_sell) { .then(result => resolve(result)) .catch(error => reject(error)) }).catch(error => reject(error)); - } else if (!mode_null) { //Lowest priority Coins (FLO Brought from other sources) + } else if (!cache.mode_null) { //Lowest priority Coins (FLO Brought from other sources) cache.orders = cache.null_queue.reverse(); //Reverse it so that we can pop the highest priority cache.mode_null = true; cache.null_queue = null; diff --git a/src/market.js b/src/market.js index 10a6d5a..f05ba1e 100644 --- a/src/market.js +++ b/src/market.js @@ -1,3 +1,5 @@ +'use strict'; + const group = require("./group"); const price = require("./price"); const MINIMUM_BUY_REQUIREMENT = 0.1; @@ -155,7 +157,7 @@ function initiateCoupling() { group.getBestPairs(cur_rate) .then(bestPairQueue => processCoupling(bestPairQueue)) .catch(error => console.error("initiateCoupling", error)) - }).catch(error => reject(error)); + }).catch(error => console.error(error)); } function processCoupling(bestPairQueue) { @@ -172,7 +174,7 @@ function processCoupling(bestPairQueue) { updateBalance(seller_best, buyer_best, txQueries, bestPairQueue.cur_rate, tx_quantity); //process txn query in SQL DB.transaction(txQueries).then(_ => { - bestPairQueue.next(quantity, spend_result.incomplete, spend_result.flag_baseNull); + bestPairQueue.next(tx_quantity, spend_result.incomplete, spend_result.flag_baseNull); console.log(`Transaction was successful! BuyOrder:${buyer_best.id}| SellOrder:${seller_best.id}`); price.updateLastTime(); //Since a tx was successful, match again @@ -192,7 +194,7 @@ function processCoupling(bestPairQueue) { } if (error.sell === undefined) noSell = false; - if (error.sell !== false) { + else if (error.sell !== false) { console.error(error.sell); noSell = null; } else { @@ -207,8 +209,8 @@ function spendFLO(buyOrder, sellOrder, null_base) { return new Promise((resolve, reject) => { DB.query("SELECT id, quantity, base FROM Vault WHERE floID=? ORDER BY base", [sellOrder.floID]).then(result => { let rem = Math.min(buyOrder.quantity, sellOrder.quantity), - txQueries = [] - flag_baseNull = false; + txQueries = [], + flag_baseNull = false; for (let i = 0; i < result.length && rem > 0; i++) if (result[i].base || null_base) { if (rem < result[i].quantity) { diff --git a/src/price.js b/src/price.js index 5665c42..e2cfa66 100644 --- a/src/price.js +++ b/src/price.js @@ -1,22 +1,28 @@ -const MIN_TIME = 1 * 60 * 60 * 1000, +'use strict'; + +const MIN_TIME = 10 * 1000, // 1 * 60 * 60 * 1000, DOWN_RATE = 0.2 / 100, UP_RATE = 0.5 / 100, MAX_DOWN_PER_DAY = 4.8 / 100, MAX_UP_PER_DAY = 12 / 100, - TOP_RANGE = 10 / 100; + TOP_RANGE = 10 / 100, + REC_HISTORY_INTERVAL = 5 * 60 * 1000; // 1 * 60 * 60 * 1000; var DB; //container for database var cur_rate, //container for FLO price (from API or by model) - lastTxTime = Date.now(), //container for timestamp of the last tx + lastTime = Date.now(), //container for timestamp of the last tx noBuyOrder, - noSellOrder + noSellOrder; + +const updateLastTime = () => lastTime = Date.now(); //store FLO price in DB every 1 hr -setInterval(function storeRate() { - DB.query("INSERT INTO priceHistory (rate) VALUE (?)", cur_rate) +function storeRate(rate = cur_rate) { + DB.query("INSERT INTO priceHistory (rate) VALUE (?)", rate) .then(_ => null).catch(error => console.error(error)) -}) +} +setInterval(storeRate, REC_HISTORY_INTERVAL) function getPastRate(hrs = 24) { return new Promise((resolve, reject) => { @@ -43,8 +49,9 @@ function fetchRates() { return new Promise((resolve, reject) => { fetchRates.FLO_USD().then(FLO_rate => { fetchRates.USD_INR().then(INR_rate => { - FLO_INR_rate = FLO_rate * INR_rate; + let FLO_INR_rate = FLO_rate * INR_rate; console.debug('Rates:', FLO_rate, INR_rate, FLO_INR_rate); + storeRate(FLO_INR_rate); resolve(FLO_INR_rate); }).catch(error => reject(error)) }).catch(error => reject(error)) @@ -81,7 +88,7 @@ function getRates() { return new Promise((resolve, reject) => { loadRate().then(_ => { let cur_time = Date.now(); - if (cur_time - lastTxTime < MIN_TIME) //Minimum time to update not crossed: No update required + if (cur_time - lastTime < MIN_TIME) //Minimum time to update not crossed: No update required resolve(cur_rate); else if (noBuyOrder && noSellOrder) //Both are not available: No update required resolve(cur_rate); @@ -92,16 +99,20 @@ function getRates() { if (noBuyOrder) { //No Buy, But Sell available: Decrease the price let tmp_val = cur_rate * (1 - DOWN_RATE); - if (tmp_val >= ratePast24hr * (1 - MAX_DOWN_PER_DAY)) - cur_rate *= tmp_val; + if (tmp_val >= ratePast24hr * (1 - MAX_DOWN_PER_DAY)) { + cur_rate = tmp_val; + updateLastTime(); + } resolve(cur_rate); } else if (noSellOrder) { //No Sell, But Buy available: Increase the price checkForRatedSellers().then(result => { if (result) { - let tmp_val = cur_rate * (1 + UP_RATE) - if (tmp_val >= ratePast24hr * (1 + MAX_UP_PER_DAY)) - cur_rate *= tmp_val; + let tmp_val = cur_rate * (1 + UP_RATE); + if (tmp_val <= ratePast24hr * (1 + MAX_UP_PER_DAY)) { + cur_rate = tmp_val; + updateLastTime(); + } } }).catch(error => console.error(error)).finally(_ => resolve(cur_rate)); } @@ -116,8 +127,8 @@ function getRates() { function checkForRatedSellers() { //Check if there are best rated sellers? return new Promise((resolve, reject) => { - DB.query("SELECT MAX(sellPriority) as maxValue FROM TagList").then(result => { - let ratedMin = result[0].maxValue * (1 - TOP_RANGE); + DB.query("SELECT MAX(sellPriority) as max_p FROM TagList").then(result => { + let ratedMin = result[0].max_p * (1 - TOP_RANGE); DB.query("SELECT COUNT(*) as value FROM SellOrder WHERE floID IN (" + " SELECT Tags.floID FROM Tags INNER JOIN TagList ON Tags.tag = TagList.tag" + " WHERE TagList.sellPriority > ?)", [ratedMin]).then(result => { @@ -129,8 +140,8 @@ function checkForRatedSellers() { module.exports = { getRates, - updateLastTime: () => lastTxTime = Date.now(), - set noOrder(buy, sell) { + updateLastTime, + noOrder(buy, sell) { noBuyOrder = buy; noSellOrder = sell; }, diff --git a/src/request.js b/src/request.js index 7507038..64d35f4 100644 --- a/src/request.js +++ b/src/request.js @@ -1,3 +1,5 @@ +'use strict'; + const market = require("./market"); var DB, trustedIDs; //container for database