From 9443ac5d51a70639959d045b837fd5e1cb118618 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Thu, 2 Sep 2021 21:24:11 +0530 Subject: [PATCH] Adding Order cancellation request --- public/fn.js | 36 +++++-- public/home.html | 269 +++++++++++++++++++++++++++++++++++------------ src/app.js | 3 + src/market.js | 25 +++++ src/request.js | 15 +++ 5 files changed, 270 insertions(+), 78 deletions(-) diff --git a/public/fn.js b/public/fn.js index c90da56..07447c2 100644 --- a/public/fn.js +++ b/public/fn.js @@ -84,7 +84,7 @@ function signUp(privKey, sid) { }).then(result => responseParse(result, false) .then(result => resolve(result)) .catch(error => reject(error))) - .catch(error => console.error(error)); + .catch(error => reject(error)); }) } @@ -109,7 +109,7 @@ function login(privKey, sid, rememberMe = false) { }).then(result => responseParse(result, false) .then(result => resolve(result)) .catch(error => reject(error))) - .catch(error => console.error(error)); + .catch(error => reject(error)); }) } @@ -126,9 +126,9 @@ function logout() { function buy(quantity, max_price) { return new Promise((resolve, reject) => { if (typeof quantity !== "number" || quantity <= 0) - return reject(INVALID(`Invalid quantity (${quantity})`)); + return reject(`Invalid quantity (${quantity})`); else if (typeof max_price !== "number" || max_price <= 0) - return reject(INVALID(`Invalid max_price (${max_price})`)); + return reject(`Invalid max_price (${max_price})`); fetch('/buy', { method: "POST", headers: { @@ -141,7 +141,7 @@ function buy(quantity, max_price) { }).then(result => responseParse(result, false) .then(result => resolve(result)) .catch(error => reject(error))) - .catch(error => console.error(error)) + .catch(error => reject(error)) }) } @@ -149,9 +149,9 @@ function buy(quantity, max_price) { function sell(quantity, min_price) { return new Promise((resolve, reject) => { if (typeof quantity !== "number" || quantity <= 0) - return reject(INVALID(`Invalid quantity (${quantity})`)); + return reject(`Invalid quantity (${quantity})`); else if (typeof min_price !== "number" || min_price <= 0) - return reject(INVALID(`Invalid min_price (${min_price})`)); + return reject(`Invalid min_price (${min_price})`); fetch('/sell', { method: "POST", headers: { @@ -164,7 +164,27 @@ function sell(quantity, min_price) { }).then(result => responseParse(result, false) .then(result => resolve(result)) .catch(error => reject(error))) - .catch(error => console.error(error)) + .catch(error => reject(error)) }) +} + +function cancelOrder(type, id) { + return new Promise((resolve, reject) => { + if (type !== "buy" && type !== "sell") + return reject(`Invalid type (${type}): type should be sell (or) buy`); + fetch('/cancel', { + method: "POST", + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + orderType: type, + orderID: id + }) + }).then(result => responseParse(result, false) + .then(result => resolve(result)) + .catch(error => reject(error))) + .catch(error => reject(error)) + }) } \ No newline at end of file diff --git a/public/home.html b/public/home.html index 1a17961..66670d7 100644 --- a/public/home.html +++ b/public/home.html @@ -23,82 +23,147 @@
- User + Profile
FLO:
Rupee:
-
-
-
- Buy - - - -
-
-
-
- Sell - - - -
-
-
-
-
- BuyOrders - - - - - - - - - - -
BuyerQuantityMax PriceOrder Placed
-
-
-
-
- SellOrders - - - - - - - - - - -
SellerQuantityMin PriceOrder Placed
-
-
-
-
- transactions - - - - - - - - - - - -
SellerBuyerQuantityUnit ValueTime
-
-
+
+
+ Buy + + + +
+
+
+
+ Sell + + + +
+
+ +
+
+
+ My Orders +
+ Buying + + + + + + + + + + +
SelectQuantityMax PriceOrder Placed
+
+
+ Selling + + + + + + + + + + +
SelectQuantityMin PriceOrder Placed
+
+ +
- +
+
+
+ My Transactions + + + + + + + + + + + +
Sold/BroughtTo/FromQuantityUnit ValueTime
+
+
+
+ + + + +
+
+
+ BuyOrders + + + + + + + + + + +
BuyerQuantityMax PriceOrder Placed
+
+
+
+
+ SellOrders + + + + + + + + + + +
SellerQuantityMin PriceOrder Placed
+
+
+
+
+ Transactions + + + + + + + + + + + +
SellerBuyerQuantityUnit ValueTime
+
+
+
+ \ No newline at end of file diff --git a/src/app.js b/src/app.js index 7bd245e..fb52995 100644 --- a/src/app.js +++ b/src/app.js @@ -41,6 +41,9 @@ module.exports = function App(secret, DB) { app.post('/buy', Request.PlaceBuyOrder); app.post('/sell', Request.PlaceSellOrder); + //cancel sell or buy order + app.post('/cancel', Request.CancelOrder); + //list sell or buy order app.get('/list-sellorders', Request.ListSellOrders); app.get('/list-buyorders', Request.ListBuyOrders); diff --git a/src/market.js b/src/market.js index 2a9c222..f78f108 100644 --- a/src/market.js +++ b/src/market.js @@ -120,6 +120,30 @@ function addBuyOrder(floID, quantity, max_price) { }); } +function cancelOrder(type, id, floID) { + return new Promise((resolve, reject) => { + if (!floID || !floCrypto.validateAddr(floID)) + return reject(INVALID("Invalid FLO ID")); + let tableName; + if (type === "buy") + tableName = "BuyOrder"; + else if (type === "sell") + tableName = "SellOrder"; + else + return reject(INVALID("Invalid Order type! Order type must be buy (or) sell")); + DB.query(`SELECT floID FROM ${tableName} WHERE id=?`, [id]).then(result => { + if (result.length < 1) + return reject(INVALID("Order not found!")); + else if (result[0].floID !== floID) + return reject(INVALID("Order doesnt belong to the current user")); + //Delete the order + DB.query(`DELETE FROM ${tableName} WHERE id=?`, [id]) + .then(result => resolve(tableName + "#" + id + " cancelled successfully")) + .catch(error => reject(error)); + }).catch(error => reject(error)); + }); +} + function matchBuyAndSell() { let cur_price = net_FLO_price; //get the best buyer @@ -306,6 +330,7 @@ let refresher = setInterval(intervalFunction, REFRESH_INTERVAL); module.exports = { addBuyOrder, addSellOrder, + cancelOrder, getAccountDetails, set DB(db) { DB = db; diff --git a/src/request.js b/src/request.js index bfff026..86f9734 100644 --- a/src/request.js +++ b/src/request.js @@ -109,6 +109,20 @@ function PlaceBuyOrder(req, res) { }); } +function CancelOrder(req, res) { + let data = req.body, + session = req.session; + market.cancelOrder(data.orderType, data.orderID, session.user_id) + .then(result => res.send(result)) + .catch(error => { + console.error(error); + if (error instanceof INVALID) + res.status(INVALID.e_code).send(error.message); + else + res.status(INTERNAL.e_code).send("Order cancellation failed! Try again later!"); + }); +} + function ListSellOrders(req, res) { //TODO: Limit size (best) DB.query("SELECT * FROM SellOrder ORDER BY time_placed") @@ -168,6 +182,7 @@ module.exports = { Logout, PlaceBuyOrder, PlaceSellOrder, + CancelOrder, ListSellOrders, ListBuyOrders, ListTransactions,