From ef0599d915add8aff7c718164a40ebcac615c397 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 18 Mar 2022 00:50:39 +0530 Subject: [PATCH] get-balance API Added: get-balance API to fetch total balance - accepts 2 parameters: floID and token. Requires atleast one of them for the API to work /get-balance?floID=&token= : responses balance of a specific token for a floID /get-balance?floID= : responses all token balance for a floID /get-balance?token= : reponses balance of a specific token for every floID Note (optional): in GET request - floID= can be replaced with addr= - token= can be replaced with asset= Both responses the same result as above --- docs/scripts/exchangeAPI.js | 15 +++++++++ src/app.js | 15 +++++---- src/market.js | 64 +++++++++++++++++++++++++++++++++++++ src/request.js | 58 ++++++++++++++++++++++----------- 4 files changed, 127 insertions(+), 25 deletions(-) diff --git a/docs/scripts/exchangeAPI.js b/docs/scripts/exchangeAPI.js index fa32a8b..b1ccbb0 100644 --- a/docs/scripts/exchangeAPI.js +++ b/docs/scripts/exchangeAPI.js @@ -114,6 +114,21 @@ function getRates(asset = null) { }); } +function getBalance(floID = null, token = null) { + return new Promise((resolve, reject) => { + if (!floID && !token) + return reject("Need atleast one argument") + let queryStr = (floID ? "floID=" + floID : "") + + (floID && token ? "&" : "") + + (token ? "token=" + token : ""); + exchangeAPI('/get-balance?' + queryStr) + .then(result => responseParse(result) + .then(result => resolve(result)) + .catch(error => reject(error))) + .catch(error => reject(error)); + }) +} + function getTx(txid) { return new Promise((resolve, reject) => { if (!txid) diff --git a/src/app.js b/src/app.js index cda9589..ac5ec04 100644 --- a/src/app.js +++ b/src/app.js @@ -50,7 +50,7 @@ module.exports = function App(secret, DB) { }) //get code for login - app.get('/get-login-code', Request.getLoginCode); + app.get('/get-login-code', Request.GetLoginCode); //login request app.post('/login', Request.Login); @@ -72,10 +72,11 @@ module.exports = function App(secret, DB) { app.get('/list-sellorders', Request.ListSellOrders); app.get('/list-buyorders', Request.ListBuyOrders); app.get('/list-trades', Request.ListTradeTransactions); - - //get rates and tx - app.get('/get-rates', Request.getRates); - app.get('/get-transaction', Request.getTransaction); + + //get rates, balance and tx + app.get('/get-rates', Request.GetRates); + app.get('/get-balance', Request.GetBalance); + app.get('/get-transaction', Request.GetTransaction); //get account details app.post('/account', Request.Account); @@ -87,8 +88,8 @@ module.exports = function App(secret, DB) { app.post('/withdraw-token', Request.WithdrawToken); //Manage user tags (Access to trusted IDs only) - app.post('/add-tag', Request.addUserTag); - app.post('/remove-tag', Request.removeUserTag); + app.post('/add-tag', Request.AddUserTag); + app.post('/remove-tag', Request.RemoveUserTag); Request.DB = DB; Request.secret = secret; diff --git a/src/market.js b/src/market.js index e98ebc1..8733829 100644 --- a/src/market.js +++ b/src/market.js @@ -30,6 +30,69 @@ function logout(floID) { }) } +function getBalance(floID, token) { + return new Promise((resolve, reject) => { + if (floID && !floCrypto.validateAddr(floID)) + reject(INVALID(`Invalid floID(${floID})`)); + else if (token && token !== floGlobals.currency && !assetList.includes(token)) + reject(INVALID(`Invalid token(${token})`)); + else if (!floID && !token) + reject(INVALID('Missing parameters: requires atleast one (floID, token)')); + else { + var promise; + if (floID && token) + promise = getBalance.floID_token(floID, token); + else if (floID) + promise = getBalance.floID(floID); + else if (token) + promise = getBalance.token(token); + promise.then(result => resolve(result)).catch(error => reject(error)) + } + }) +} + +getBalance.floID_token = (floID, token) => new Promise((resolve, reject) => { + (token === floGlobals.currency ? + DB.query("SELECT SUM(balance) AS balance FROM Cash WHERE floID=?", [floID]) : + DB.query("SELECT SUM(quantity) AS balance FROM Vault WHERE floID=? AND asset=?", [floID, token]) + ).then(result => resolve({ + floID, + token, + balance: result[0].balance.toFixed(4) + })).catch(error => reject(error)) +}); + +getBalance.floID = (floID) => new Promise((resolve, reject) => { + Promise.all([ + DB.query("SELECT SUM(balance) AS balance FROM Cash WHERE floID=?", [floID]), + DB.query("SELECT asset, SUM(quantity) AS balance FROM Vault WHERE floID=? GROUP BY asset", [floID]) + ]).then(result => { + let response = { + floID, + balance: {} + }; + response.balance[floGlobals.currency] = result[0][0].balance.toFixed(4); + for (let row of result[1]) + response.balance[row.asset] = row.balance.toFixed(4); + resolve(response); + }).catch(error => reject(error)) +}); + +getBalance.token = (token) => new Promise((resolve, reject) => { + (token === floGlobals.currency ? + DB.query("SELECT floID, balance FROM Cash") : + DB.query("SELECT floID, SUM(quantity) AS balance FROM Vault WHERE asset = ? GROUP BY floID", [token]) + ).then(result => { + let response = { + token: token, + balance: {} + }; + for (let row of result) + response.balance[row.floID] = row.balance.toFixed(4); + resolve(response); + }).catch(error => reject(error)) +}); + const getAssetBalance = (floID, asset) => new Promise((resolve, reject) => { let promises = (asset === floGlobals.currency) ? [ DB.query("SELECT SUM(balance) AS balance FROM Cash WHERE floID=?", [floID]), @@ -558,6 +621,7 @@ module.exports = { addBuyOrder, addSellOrder, cancelOrder, + getBalance, getAccountDetails, getTransactionDetails, transferToken, diff --git a/src/request.js b/src/request.js index 78a0901..bdce801 100644 --- a/src/request.js +++ b/src/request.js @@ -253,7 +253,7 @@ function WithdrawToken(req, res) { ); } -function addUserTag(req, res) { +function AddUserTag(req, res) { let data = req.body; if (!trustedIDs.includes(data.floID)) res.status(INVALID.e_code).send("Access Denied"); @@ -267,7 +267,7 @@ function addUserTag(req, res) { ); } -function removeUserTag(req, res) { +function RemoveUserTag(req, res) { let data = req.body; if (!trustedIDs.includes(data.floID)) res.status(INVALID.e_code).send("Access Denied"); @@ -283,15 +283,17 @@ function removeUserTag(req, res) { /* Public Requests */ -function getLoginCode(req, res) { +function GetLoginCode(req, res) { if (!serving) - return res.status(INVALID.e_code).send(INVALID_SERVER_MSG); - let randID = floCrypto.randString(8, true) + Math.round(Date.now() / 1000); - let hash = Crypto.SHA1(randID + secret); - res.send({ - code: randID, - hash: hash - }); + res.status(INVALID.e_code).send(INVALID_SERVER_MSG); + else { + let randID = floCrypto.randString(8, true) + Math.round(Date.now() / 1000); + let hash = Crypto.SHA1(randID + secret); + res.send({ + code: randID, + hash: hash + }); + } } function ListSellOrders(req, res) { @@ -315,7 +317,7 @@ function ListTradeTransactions(req, res) { .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); } -function getRates(req, res) { +function GetRates(req, res) { if (!serving) res.status(INVALID.e_code).send(INVALID_SERVER_MSG); else { @@ -332,14 +334,33 @@ function getRates(req, res) { } -function getTransaction(req, res) { +function GetTransaction(req, res) { if (!serving) res.status(INVALID.e_code).send(INVALID_SERVER_MSG); else { let txid = req.query.txid; if (!txid) res.status(INVALID.e_code).send("txid (transactionID) parameter missing"); - market.getTransactionDetails(txid) + else market.getTransactionDetails(txid) + .then(result => res.send(result)) + .catch(error => { + if (error instanceof INVALID) + res.status(INVALID.e_code).send(error.message); + else { + console.error(error); + res.status(INTERNAL.e_code).send("Unable to process! Try again later!"); + } + }); + } +} + +function GetBalance(req, res) { + if (!serving) + res.status(INVALID.e_code).send(INVALID_SERVER_MSG); + else { + let floID = req.query.floID || req.query.addr, + token = req.query.token || req.query.asset; + market.getBalance(floID, token) .then(result => res.send(result)) .catch(error => { if (error instanceof INVALID) @@ -353,7 +374,7 @@ function getTransaction(req, res) { } module.exports = { - getLoginCode, + GetLoginCode, Login, Logout, PlaceBuyOrder, @@ -363,16 +384,17 @@ module.exports = { ListSellOrders, ListBuyOrders, ListTradeTransactions, - getRates, - getTransaction, + GetRates, + GetTransaction, + GetBalance, Account, DepositFLO, WithdrawFLO, DepositToken, WithdrawToken, periodicProcess: market.periodicProcess, - addUserTag, - removeUserTag, + AddUserTag, + RemoveUserTag, set trustedIDs(ids) { trustedIDs = ids; },