From df69ee0f7b289f1aad578c0e4dfe769af2122019 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 4 Feb 2022 02:54:05 +0530 Subject: [PATCH] Multi-asset: User API --- public/fn.js | 44 +++++++++++++++++++++++++------------------- public/home.html | 17 +++++++++-------- src/app.js | 2 +- src/request.js | 4 ++-- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/public/fn.js b/public/fn.js index 7501148..d2f939f 100644 --- a/public/fn.js +++ b/public/fn.js @@ -151,9 +151,9 @@ function getTransactionList() { }); } -function getRate() { +function getRates() { return new Promise((resolve, reject) => { - exchangeAPI('/get-rate') + exchangeAPI('/get-rates') .then(result => responseParse(result, false) .then(result => resolve(result)) .catch(error => reject(error))) @@ -268,7 +268,7 @@ function logout(floID, proxySecret) { }) } -function buy(quantity, max_price, floID, proxySecret) { +function buy(asset, quantity, max_price, floID, proxySecret) { return new Promise((resolve, reject) => { if (typeof quantity !== "number" || quantity <= 0) return reject(`Invalid quantity (${quantity})`); @@ -276,12 +276,14 @@ function buy(quantity, max_price, floID, proxySecret) { return reject(`Invalid max_price (${max_price})`); let request = { floID: floID, + asset: asset, quantity: quantity, max_price: max_price, timestamp: Date.now() }; request.sign = signRequest({ type: "buy_order", + asset: asset, quantity: quantity, max_price: max_price, timestamp: request.timestamp @@ -302,7 +304,7 @@ function buy(quantity, max_price, floID, proxySecret) { } -function sell(quantity, min_price, floID, proxySecret) { +function sell(asset, quantity, min_price, floID, proxySecret) { return new Promise((resolve, reject) => { if (typeof quantity !== "number" || quantity <= 0) return reject(`Invalid quantity (${quantity})`); @@ -310,6 +312,7 @@ function sell(quantity, min_price, floID, proxySecret) { return reject(`Invalid min_price (${min_price})`); let request = { floID: floID, + asset: asset, quantity: quantity, min_price: min_price, timestamp: Date.now() @@ -317,6 +320,7 @@ function sell(quantity, min_price, floID, proxySecret) { request.sign = signRequest({ type: "sell_order", quantity: quantity, + asset: asset, min_price: min_price, timestamp: request.timestamp }, proxySecret); @@ -379,7 +383,7 @@ function depositFLO(quantity, floID, sinkID, privKey, proxySecret) { }; request.sign = signRequest({ type: "deposit_FLO", - txid: request.txid, + txid: txid, timestamp: request.timestamp }, proxySecret); console.debug(request); @@ -407,7 +411,7 @@ function withdrawFLO(quantity, floID, proxySecret) { }; request.sign = signRequest({ type: "withdraw_FLO", - amount: request.amount, + amount: quantity, timestamp: request.timestamp }, proxySecret); console.debug(request); @@ -425,24 +429,24 @@ function withdrawFLO(quantity, floID, proxySecret) { }) } -function depositRupee(quantity, floID, sinkID, privKey, proxySecret) { +function depositToken(token, quantity, floID, sinkID, privKey, proxySecret) { return new Promise((resolve, reject) => { if (!floCrypto.verifyPrivKey(privKey, floID)) return reject("Invalid Private Key"); - tokenAPI.sendToken(privKey, quantity, sinkID, 'Deposit Rupee in market').then(txid => { + tokenAPI.sendToken(privKey, quantity, sinkID, 'Deposit Rupee in market', token).then(txid => { let request = { floID: floID, txid: txid, timestamp: Date.now() }; request.sign = signRequest({ - type: "deposit_Rupee", - txid: request.txid, + type: "deposit_Token", + txid: txid, timestamp: request.timestamp }, proxySecret); console.debug(request); - exchangeAPI('/deposit-rupee', { + exchangeAPI('/deposit-token', { method: "POST", headers: { 'Content-Type': 'application/json' @@ -456,21 +460,23 @@ function depositRupee(quantity, floID, sinkID, privKey, proxySecret) { }) } -function withdrawRupee(quantity, floID, proxySecret) { +function withdrawToken(token, quantity, floID, proxySecret) { return new Promise((resolve, reject) => { let request = { floID: floID, + token: token, amount: quantity, timestamp: Date.now() }; request.sign = signRequest({ - type: "withdraw_Rupee", - amount: request.amount, + type: "withdraw_Token", + token: token, + amount: quantity, timestamp: request.timestamp }, proxySecret); console.debug(request); - exchangeAPI('/withdraw-rupee', { + exchangeAPI('/withdraw-token', { method: "POST", headers: { 'Content-Type': 'application/json' @@ -493,8 +499,8 @@ function addUserTag(tag_user, tag, floID, proxySecret) { }; request.sign = signRequest({ command: "add_Tag", - user: request.user, - tag: request.tag, + user: tag_user, + tag: tag, timestamp: request.timestamp }, proxySecret); console.debug(request); @@ -522,8 +528,8 @@ function removeUserTag(tag_user, tag, floID, proxySecret) { }; request.sign = signRequest({ command: "remove_Tag", - user: request.user, - tag: request.tag, + user: tag_user, + tag: tag, timestamp: request.timestamp }, proxySecret); console.debug(request); diff --git a/public/home.html b/public/home.html index c02e190..771ec0f 100644 --- a/public/home.html +++ b/public/home.html @@ -1052,9 +1052,9 @@ showProcess('trade_button_wrapper') try { if (tradeType === 'buy') { - await buy(quantity, price, proxy.userID, await proxy.secret) + await buy(quantity, price, proxy.userID, await proxy.secret) //TODO: asset_name } else { - await sell(quantity, price, proxy.userID, await proxy.secret) + await sell(quantity, price, proxy.userID, await proxy.secret) //TODO: asset_name } getRef('trade_button_wrapper').append(getRef('success_template').content.cloneNode(true)) notify(`Placed ${tradeType} order`, 'success') @@ -1197,14 +1197,14 @@ if (asset === 'FLO') { await depositFLO(quantity, proxy.userID, proxy.sinkID, privKey, proxySecret) } else { - await depositRupee(quantity, proxy.userID, proxy.sinkID, privKey, proxySecret) + await depositToken(quantity, proxy.userID, proxy.sinkID, privKey, proxySecret) //TODO: token_name } showWalletResult('success', `Sent ${asset} deposit request`, 'This may take upto 30 mins to reflect in your wallet.') } else { if (asset === 'FLO') { await withdrawFLO(quantity, proxy.userID, proxySecret) } else { - await withdrawRupee(quantity, proxy.userID, proxySecret) + await withdrawToken(quantity, proxy.userID, proxySecret) //TODO: token_name } showWalletResult('success', `Sent ${asset} withdraw request`, 'This may take upto 30 mins to reflect in your wallet.') } @@ -1630,10 +1630,11 @@ let floExchangeRate = 0 function updateRate() { - getRate().then(rate => { - floExchangeRate = parseFloat(rate) - getRef('flo_rate').textContent = formatAmount(parseFloat(rate)) - getRef('get_price').value = parseFloat(parseFloat(rate).toFixed(2)) + getRates().then(rates => { + let flo_rate = rates["FLO"]; + floExchangeRate = parseFloat(flo_rate) + getRef('flo_rate').textContent = formatAmount(parseFloat(flo_rate)) + getRef('get_price').value = parseFloat(parseFloat(flo_rate).toFixed(2)) }).catch(error => console.error(error)) } diff --git a/src/app.js b/src/app.js index 385580e..7281b5e 100644 --- a/src/app.js +++ b/src/app.js @@ -72,7 +72,7 @@ module.exports = function App(secret, DB) { //list all process transactions and rate app.get('/list-transactions', Request.ListTransactions); - app.get('/get-rate', Request.getRate) + app.get('/get-rates', Request.getRates) //get account details app.post('/account', Request.Account); diff --git a/src/request.js b/src/request.js index b869796..964c38a 100644 --- a/src/request.js +++ b/src/request.js @@ -268,7 +268,7 @@ function ListTransactions(req, res) { .catch(error => res.status(INTERNAL.e_code).send("Try again later!")); } -function getRate(req, res) { +function getRates(req, res) { if (!serving) res.status(INVALID.e_code).send(INVALID_SERVER_MSG); else @@ -484,7 +484,7 @@ module.exports = { ListSellOrders, ListBuyOrders, ListTransactions, - getRate, + getRates, Account, DepositFLO, WithdrawFLO,