From 55bd121021a84e3f8b5f0a7897510f46a78ac39d Mon Sep 17 00:00:00 2001 From: sairajzero Date: Mon, 2 May 2022 22:53:57 +0530 Subject: [PATCH] API for ordered list for sell/buy - The API for Listing sell/buy orders now returns ordered list of the queue (approx). - list-buyorders, list-sellorders and list-trades API now accepts optional GET parameter 'asset'. . If passed, then orders/trades of only given asset will be returned. . If asset is not passed, then returns all asset by default as before. - list buy/sell order API return max of 100 best orders - list trade API return max of 1000 recent trades --- docs/scripts/floExchangeAPI.js | 12 +++---- src/market.js | 3 ++ src/request.js | 65 +++++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/docs/scripts/floExchangeAPI.js b/docs/scripts/floExchangeAPI.js index 7ffeaf8..71c3011 100644 --- a/docs/scripts/floExchangeAPI.js +++ b/docs/scripts/floExchangeAPI.js @@ -541,9 +541,9 @@ }); } - exchangeAPI.getBuyList = function() { + exchangeAPI.getBuyList = function(asset = null) { return new Promise((resolve, reject) => { - fetch_api('/list-buyorders') + fetch_api('/list-buyorders' + (asset ? "?asset=" + asset : "")) .then(result => responseParse(result) .then(result => resolve(result)) .catch(error => reject(error))) @@ -551,9 +551,9 @@ }); } - exchangeAPI.getSellList = function() { + exchangeAPI.getSellList = function(asset = null) { return new Promise((resolve, reject) => { - fetch_api('/list-sellorders') + fetch_api('/list-sellorders' + (asset ? "?asset=" + asset : "")) .then(result => responseParse(result) .then(result => resolve(result)) .catch(error => reject(error))) @@ -561,9 +561,9 @@ }); } - exchangeAPI.getTradeList = function() { + exchangeAPI.getTradeList = function(asset = null) { return new Promise((resolve, reject) => { - fetch_api('/list-trades') + fetch_api('/list-trades' + (asset ? "?asset=" + asset : "")) .then(result => responseParse(result) .then(result => resolve(result)) .catch(error => reject(error))) diff --git a/src/market.js b/src/market.js index b59b1bb..d734711 100644 --- a/src/market.js +++ b/src/market.js @@ -705,5 +705,8 @@ module.exports = { }, set assetList(assets) { assetList = assets; + }, + get assetList() { + return assetList } }; \ No newline at end of file diff --git a/src/request.js b/src/request.js index dbb3bc3..6ca0806 100644 --- a/src/request.js +++ b/src/request.js @@ -325,24 +325,65 @@ function GetLoginCode(req, res) { } function ListSellOrders(req, res) { - //TODO: Limit size (best) - DB.query("SELECT * FROM SellOrder ORDER BY time_placed") - .then(result => res.send(result)) - .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); + if (!serving) + res.status(INVALID.e_code).send(INVALID_SERVER_MSG); + else { + let asset = req.query.asset; + if (asset && !market.assetList.includes(asset)) + res.status(INVALID.e_code).send("Invalid asset parameter"); + else + DB.query("SELECT SellOrder.floID, SellOrder.asset, GREATEST(SellOrder.minPrice, SellChips.base) AS minPrice, SellOrder.quantity, SellOrder.time_placed FROM SellOrder" + + " INNER JOIN UserBalance ON UserBalance.floID = SellOrder.floID AND UserBalance.token = SellOrder.asset" + + " INNER JOIN SellChips ON SellChips.floID = SellOrder.floID AND SellChips.asset = SellOrder.asset" + + " LEFT JOIN UserTag ON UserTag.floID = SellOrder.floID" + + " LEFT JOIN TagList ON TagList.tag = UserTag.tag" + + " WHERE UserBalance.quantity >= SellOrder.quantity" + + (asset ? " AND SellOrder.asset = ?" : "") + + " GROUP BY SellOrder.id" + + " ORDER BY TagList.sellPriority DESC, SellChips.locktime ASC, SellOrder.time_placed ASC" + + " LIMIT 100", [asset || null]) + .then(result => res.send(result)) + .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); + } + } function ListBuyOrders(req, res) { - //TODO: Limit size (best) - DB.query("SELECT * FROM BuyOrder ORDER BY time_placed") - .then(result => res.send(result)) - .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); + if (!serving) + res.status(INVALID.e_code).send(INVALID_SERVER_MSG); + else { + let asset = req.query.asset; + if (asset && !market.assetList.includes(asset)) + res.status(INVALID.e_code).send("Invalid asset parameter"); + else + DB.query("SELECT BuyOrder.floID, BuyOrder.asset, BuyOrder.maxPrice, BuyOrder.quantity, BuyOrder.time_placed FROM BuyOrder" + + " INNER JOIN UserBalance ON UserBalance.floID = BuyOrder.floID AND UserBalance.token = ?" + + " LEFT JOIN UserTag ON UserTag.floID = BuyOrder.floID" + + " LEFT JOIN TagList ON TagList.tag = UserTag.tag" + + " WHERE UserBalance.quantity >= BuyOrder.maxPrice * BuyOrder.quantity" + + (asset ? " AND BuyOrder.asset = ?" : "") + + " GROUP BY BuyOrder.id" + + " ORDER BY TagList.buyPriority DESC, BuyOrder.time_placed ASC" + + " LIMIT 100", [floGlobals.currency, asset || null]) + .then(result => res.send(result)) + .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); + } } function ListTradeTransactions(req, res) { - //TODO: Limit size (recent) - DB.query("SELECT * FROM TradeTransactions ORDER BY tx_time DESC") - .then(result => res.send(result)) - .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); + if (!serving) + res.status(INVALID.e_code).send(INVALID_SERVER_MSG); + else { + let asset = req.query.asset; + if (asset && !market.assetList.includes(asset)) + res.status(INVALID.e_code).send("Invalid asset parameter"); + else + DB.query("SELECT * FROM TradeTransactions" + + (asset ? " WHERE asset = ?" : "") + + " ORDER BY tx_time DESC LIMIT 1000", [asset || null]) + .then(result => res.send(result)) + .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); + } } function GetRates(req, res) {