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=<floID>&token=<token> : responses balance of a specific token for a floID /get-balance?floID=<floID> : responses all token balance for a floID /get-balance?token=<token> : reponses balance of a specific token for every floID Note (optional): in GET request - floID=<floID> can be replaced with addr=<floID> - token=<token> can be replaced with asset=<asset> Both responses the same result as above
This commit is contained in:
parent
a18ccf426e
commit
ef0599d915
@ -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)
|
||||
|
||||
13
src/app.js
13
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);
|
||||
@ -73,9 +73,10 @@ module.exports = function App(secret, DB) {
|
||||
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;
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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;
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user