diff --git a/public/fn.js b/public/fn.js index dea020e..c90da56 100644 --- a/public/fn.js +++ b/public/fn.js @@ -93,7 +93,7 @@ function login(privKey, sid, rememberMe = false) { return new Promise((resolve, reject) => { let pubKey = floCrypto.getPubKeyHex(privKey); let floID = floCrypto.getFloID(pubKey); - if(!floID || !floCrypto.verifyPrivKey(privKey, floID)) + if (!floID || !floCrypto.verifyPrivKey(privKey, floID)) return reject("Invalid Private key"); let sign = floCrypto.signData(sid, privKey); fetch("/login", { @@ -125,6 +125,10 @@ function logout() { function buy(quantity, max_price) { return new Promise((resolve, reject) => { + if (typeof quantity !== "number" || quantity <= 0) + return reject(INVALID(`Invalid quantity (${quantity})`)); + else if (typeof max_price !== "number" || max_price <= 0) + return reject(INVALID(`Invalid max_price (${max_price})`)); fetch('/buy', { method: "POST", headers: { @@ -144,6 +148,10 @@ 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})`)); + else if (typeof min_price !== "number" || min_price <= 0) + return reject(INVALID(`Invalid min_price (${min_price})`)); fetch('/sell', { method: "POST", headers: { diff --git a/public/home.html b/public/home.html index d4ae954..1a17961 100644 --- a/public/home.html +++ b/public/home.html @@ -16,33 +16,33 @@
Login - -
- RememberMe + +
+ RememberMe
User -
- FLO:
- Rupee:
- +
+ FLO:
+ Rupee:
+
-
+
Buy - +
-
+
Sell - +
@@ -153,38 +153,43 @@ function account() { getAccount().then(acc => { + console.debug(acc); document.getElementById("login-form").style.display = "none"; document.getElementById('user-container').style.display = "block"; document.getElementById("user_id").textContent = acc.floID; - let flo_total = acc.coins.reduce((a, x) => x.quantity, 0); - let flo_locked = acc.sellOrders.reduce((a, x) => x.quantity); + let flo_total = acc.coins.reduce((a, x) => a + x.quantity, 0); + let flo_locked = acc.sellOrders.reduce((a, x) => a + x.quantity, 0); let flo_net = flo_total - flo_locked; + console.debug("FLO", flo_total, flo_locked, flo_net); document.getElementById("flo_bal").textContent = flo_net + "(+" + flo_locked + ")"; let rupee_total = acc.rupee_total; - let rupee_locked = acc.buyOrders.reduce((a, x) => x.quantity * x.maxPrice, 0); + let rupee_locked = acc.buyOrders.reduce((a, x) => a + x.quantity * x.maxPrice, 0); let rupee_net = rupee_total - rupee_locked; + console.debug("RUPEE", rupee_total, rupee_locked, rupee_net); document.getElementById("rupee_bal").textContent = rupee_net + "(+" + rupee_locked + ")"; }).catch(error => { - if(error instanceof ResponseError){ + if (error instanceof ResponseError) { let response = JSON.parse(error.data) console.log(error); console.log(response); document.getElementById('user-container').style.display = "none"; document.getElementById("login-form").style.display = "block"; document.forms['login-form']["sid"].value = response.sid; - } else + } else console.error(error); }) } - document.getElementById("logout").onclick = (evt) => { + const UI_evt = {}; + + UI_evt.logout = function() { logout().then(result => { console.warn(result); location.reload(); - }).catch(error => console.error(error)); + }).catch(error => console.error(error)); }; - document.forms['login-form']['login'].onclick = evt => { + UI_evt.login = function() { let formInputs = document.forms['login-form']; let privKey = formInputs['priv-key'].value; let sid = formInputs['sid'].value; @@ -193,7 +198,21 @@ console.log(result); account(); }).catch(error => console.error(error)); - } + }; + + UI_evt.sell = function() { + let formInputs = document.forms['sell-form']; + sell(parseFloat(formInputs["quantity"].value), parseFloat(formInputs["min-price"].value)) + .then(result => console.log(result)) + .catch(error => console.error(error)); + }; + + UI_evt.buy = function() { + let formInputs = document.forms['buy-form']; + buy(parseFloat(formInputs["quantity"].value), parseFloat(formInputs["max-price"].value)) + .then(result => console.log(result)) + .catch(error => console.error(error)); + }; (function init() { account();