From 53b47057ff3088f31cc3eaa88dfca23f595a68ac Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 18 Feb 2022 20:11:34 +0530 Subject: [PATCH] Minor fixes - Sell requirement checks for respective asset for minimum buy - Minimum buy is set to 0 (for exchange startup) - Fixed: buy/sell orders sorted incorrectly by time_placed (should be FCFS. ie, ASC) --- src/_constants.js | 2 +- src/group.js | 13 ++++++------- src/market.js | 8 ++++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/_constants.js b/src/_constants.js index 29cdad8..47f78ae 100644 --- a/src/_constants.js +++ b/src/_constants.js @@ -8,7 +8,7 @@ module.exports = { INVALID_SERVER_MSG: "INCORRECT_SERVER_ERROR" //Should be reflected in public backend script }, market: { - MINIMUM_BUY_REQUIREMENT: 0.1 + MINIMUM_BUY_REQUIREMENT: 0 }, price: { MIN_TIME: 1 * 60 * 60 * 1000, // 1 hr diff --git a/src/group.js b/src/group.js index 9888866..18d1087 100644 --- a/src/group.js +++ b/src/group.js @@ -208,7 +208,7 @@ function getUntaggedSellOrders(asset, cur_price) { DB.query("SELECT SellOrder.id, SellOrder.floID, SellOrder.quantity FROM SellOrder" + " LEFT JOIN UserTag ON UserTag.floID = SellOrder.floID" + " WHERE UserTag.floID IS NULL AND SellOrder.asset = ? AND SellOrder.minPrice <=?" + - " ORDER BY SellOrder.time_placed DESC", [asset, cur_price]) + " ORDER BY SellOrder.time_placed", [asset, cur_price]) .then(orders => resolve(orders)) .catch(error => reject(error)) }) @@ -219,7 +219,7 @@ function getUntaggedBuyOrders(asset, cur_price) { DB.query("SELECT BuyOrder.id, BuyOrder.floID, BuyOrder.quantity FROM BuyOrder" + " LEFT JOIN UserTag ON UserTag.floID = BuyOrder.floID" + " WHERE UserTag.floID IS NULL AND BuyOrder.asset = ? AND BuyOrder.maxPrice >=? " + - " ORDER BY BuyOrder.time_placed DESC", [asset, cur_price]) + " ORDER BY BuyOrder.time_placed", [asset, cur_price]) .then(orders => resolve(orders)) .catch(error => reject(error)) }) @@ -230,7 +230,7 @@ function getSellOrdersInTag(tag, asset, cur_price) { DB.query("SELECT SellOrder.id, SellOrder.floID, SellOrder.quantity FROM SellOrder" + " INNER JOIN UserTag ON UserTag.floID = SellOrder.floID" + " WHERE UserTag.tag = ? AND SellOrder.asset = ? AND SellOrder.minPrice <=?" + - " ORDER BY SellOrder.time_placed DESC", [tag, asset, cur_price]).then(orders => { + " ORDER BY SellOrder.time_placed", [tag, asset, cur_price]).then(orders => { if (orders.length <= 1) // No (or) Only-one order, hence priority sort not required. resolve(orders); else @@ -249,7 +249,7 @@ function getBuyOrdersInTag(tag, asset, cur_price) { DB.query("SELECT BuyOrder.id, BuyOrder.floID, BuyOrder.quantity FROM BuyOrder" + " INNER JOIN UserTag ON UserTag.floID = BuyOrder.floID" + " WHERE UserTag.tag = ? AND BuyOrder.asset = ? AND BuyOrder.maxPrice >=?" + - " ORDER BY BuyOrder.time_placed DESC", [tag, asset, cur_price]).then(orders => { + " ORDER BY BuyOrder.time_placed", [tag, asset, cur_price]).then(orders => { if (orders.length <= 1) // No (or) Only-one order, hence priority sort not required. resolve(orders); else @@ -271,10 +271,9 @@ function getPointsFromAPI(tag, floIDs) { Promise.allSettled(floIDs.map(id => fetch_api(api, id))).then(result => { let points = {}; for (let i in result) - if (result[i].status === "fulfilled") - points[floIDs[i]] = result[i].value; + points[floIDs[i]] = result[i].status === "fulfilled" ? result[i].value : 0; resolve(points); - }).catch(error => reject(error)) + }) }).catch(error => reject(error)) }); } diff --git a/src/market.js b/src/market.js index 838a898..a1575a4 100644 --- a/src/market.js +++ b/src/market.js @@ -69,7 +69,7 @@ function addSellOrder(floID, asset, quantity, min_price) { else if (!assetList.includes(asset)) return reject(INVALID(`Invalid asset (${asset})`)); getAssetBalance.check(floID, asset, quantity).then(_ => { - checkSellRequirement(floID).then(_ => { + checkSellRequirement(floID, asset).then(_ => { DB.query("INSERT INTO SellOrder(floID, asset, quantity, minPrice) VALUES (?, ?, ?, ?)", [floID, asset, quantity, min_price]) .then(result => resolve("Added SellOrder to DB")) .catch(error => reject(error)); @@ -78,16 +78,16 @@ function addSellOrder(floID, asset, quantity, min_price) { }); } -const checkSellRequirement = floID => new Promise((resolve, reject) => { +const checkSellRequirement = (floID, asset) => new Promise((resolve, reject) => { DB.query("SELECT * FROM UserTag WHERE floID=? AND tag=?", [floID, "MINER"]).then(result => { if (result.length) return resolve(true); //TODO: Should seller need to buy same type of asset before selling? - DB.query("SELECT SUM(quantity) AS brought FROM TransactionHistory WHERE buyer=?", [floID]).then(result => { + DB.query("SELECT SUM(quantity) AS brought FROM TransactionHistory WHERE buyer=? AND asset=?", [floID, asset]).then(result => { if (result[0].brought >= MINIMUM_BUY_REQUIREMENT) resolve(true); else - reject(INVALID(`Sellers required to buy atleast ${MINIMUM_BUY_REQUIREMENT} FLO before placing a sell order unless they are a Miner`)); + reject(INVALID(`Sellers required to buy atleast ${MINIMUM_BUY_REQUIREMENT} ${asset} before placing a sell order unless they are a Miner`)); }).catch(error => reject(error)) }).catch(error => reject(error)) });