From 3a7a0bade17e2bc523c2c352dfa4a7581522cf1d Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Thu, 26 Mar 2020 19:33:39 +0530 Subject: [PATCH] applied truncateDecimals function with prices --- cash_payments_handler.html | 86 ++++++++++++++++++++---- index.html | 130 ++++++++++++++++--------------------- 2 files changed, 131 insertions(+), 85 deletions(-) diff --git a/cash_payments_handler.html b/cash_payments_handler.html index 2d44c59..f3a55a6 100644 --- a/cash_payments_handler.html +++ b/cash_payments_handler.html @@ -11394,8 +11394,8 @@ enumerable: false }); - const ENVR = 'LIVE'; // LIVE, TEST - const WS = 'wss'; + const ENVR = 'TEST'; // LIVE, TEST + const WS = 'ws'; const DBName = "paymentsHandlerDBRemote"; if(ENVR === 'LIVE') { @@ -12004,9 +12004,29 @@ } }; - localbitcoinplusplus.actions = { + localbitcoinplusplus.actions = { parse_flo_comments: async function(callback) { + text = `masterFLOPubKey=03EA5E2CAB18DA585400D6EC569438D415FAF200528E05D0E2B9BEAA2B5C3DCA90 + #!#tradableAsset1=BTC,FLO,BTC_TEST,FLO_TEST#!#tradableAsset2=INR,USD, + #!#validTradingAmount=10,50,100,#!#btcTradeMargin=5000 + #!#MaxBackups=1 + #!#miners_fee={"btc":0.0003, "flo":0.0003} + #!#supernodesPubKeys=0315C3A20FE7096CC2E0F81A80D5F1A687B8F9EFA65242A0B0881E1BA3EE7D7D53, + 03F7493F11B8E44B9798CD434D20FBE7FA34B9779D144984889D11A17C56A18742,039B4AA00DBFC0A6631DE6DA83526611A0E6B857D3579DF840BBDEAE8B6898E3B6, + 03C8E3836C9A77E2AF03D4265D034BA85732738919708EAF6A16382195AE796EDF,0349B08AA1ABDCFFB6D78CD7C949665AD2FF065EA02B3C6C47A5E9592C9A1C6BCB, + 026FCC6CFF6EB3A39E54BEB6E13FC2F02C3A93F4767AA80E49E7E876443F95AE5F, + #!#externalFiles={"d3js":"58f54395efa8346e8e94d12609770f66b916897e7f4e05f6c98780cffa5c70a3"} + #!#cashiers={"032871A74D2DDA9D0DE7135F58B5BD2D7F679D2CCA20EA7909466D1A6912DF4022":"johnDoe@upi", + "03DB4A12EB543B293DDBB0CE314C46C36D6761294AFBB7264A6D78F710FFD97CF0":"janeDoe@upi"} + #!#ShamirsMaxShares=8#!#supernodeSeeds={"ranchimall1":{"ip":"127.0.0.1:9111","kbucketId":"oZxHcbSf1JC8t5GjutopWYXs7C6Fe9p7ps"}, + "ranchimall2":{"ip":"127.0.0.1:9112","kbucketId":"oTWjPupy3Z7uMdPcu5uXd521HBkcsLuSuM"}, + "ranchimall3":{"ip":"127.0.0.1:9113","kbucketId":"odYA6KagmbokSh9GY7yAfeTUZRtZLwecY1"}, + "ranchimall4":{"ip":"127.0.0.1:9114","kbucketId":"oJosrve9dBv2Hj2bfncxv2oEpTysg3Wejv"}, + "ranchimall5":{"ip":"127.0.0.1:9115","kbucketId":"oMhv5sAzqg77sYHxmUGZWKRrVo4P4JQduS"}, + "ranchimall6":{"ip":"127.0.0.1:9116","kbucketId":"oV1wCeWca3VawbBTfUGKA7Vd368PATnKAx"}}`; + return callback(text); + if(ENVR==='LIVE') { master_data = await helper_functions .ajaxGet(`${localbitcoinplusplus.server.flo_mainnet}/api/txs/?address=${localbitcoinplusplus.RM_FLO_SENDING_ADDR}`); @@ -13387,12 +13407,14 @@ let failed_confirms_in_deposits = await readAllDB('failed_deposit_confirms'); let failed_confirms_in_deposits_ids = failed_confirms_in_deposits.map(m=>m.deposit_id); console.log(failed_confirms_in_deposits_ids); + if(failed_confirms_in_deposits_ids.length>0) { + alert("There are some failed deposit requests in DB. Please contact the admin."); + } let t = ``; let deposits_table = document.getElementById("deposits_list"); + deposits_table.innerHTML = ''; for (const m of responseData.responseData.data) { - // This deposit, token transfer was done but supernode was not informed - if(failed_confirms_in_deposits_ids.includes(m.id)) continue; - + let su_res = await localbitcoinplusplus.kademlia.determineClosestSupernode(m.trader_flo_address); let closestSupernode = su_res[0].data.id; t += ``; @@ -13400,9 +13422,21 @@ t += ` ${m.trader_flo_address} `; t += ` ${m.depositing_amount} `; t += ` ${m.currency} `; - t += ` - + if(m.status===1) { + // This deposit, token transfer was done but supernode was not informed + if(failed_confirms_in_deposits_ids.includes(m.id)) { + t += ` + `; + } else { + t += ` + + `; + } + + } else if(m.status===2) { + t += ` `; + } t += ``; } deposits_table.insertAdjacentHTML("beforeend", t); @@ -13414,6 +13448,7 @@ (async function() { let v = ``; const withdraws_table = document.getElementById("withdraws_list"); + withdraws_table.innerHTML = ''; for (const m of responseData.responseData.data) { const user_upi = localbitcoinplusplus.encrypt.decryptMessage( m.receivinAddress.secret, @@ -13428,9 +13463,14 @@ v += ` ${m.withdraw_amount} `; v += ` ${m.currency} `; v += ` ${m.token_transfer_txid} `; - v += ` - - `; + if(m.status===1) { + v += ` + + `; + } else if(m.status===2) { + v += ` + `; + } v += ``; } withdraws_table.insertAdjacentHTML("beforeend", v); @@ -13875,12 +13915,26 @@ RM_RPC.send_rpc .call(this, "cashier_confirms_user_cash_deposit", req_body) .then(resp => doSend(websocket_conn, resp)); - } else if(n<=10) { + + this.classList.remove('cnf_deposits'); + this.classList.remove('btn-info'); + this.classList.add('btn-success'); + this.innerText = "Success"; + + return true; + + } else if(n<=20) { validateTxidInBlockchain(); n++; } else { // Failed to validate token transfer. Save in local db await addDB('failed_deposit_confirms', req_body, req_body.flo_txid); + + this.classList.remove('cnf_deposits'); + this.classList.remove('btn-info'); + this.classList.add('btn-danger'); + this.innerText = "Failed to inform Supernode. Please contact the Admin."; + throw new Error(`Failed to inform Supernode of cash deposit: ${flo_txid}`); } })() @@ -13923,6 +13977,14 @@ parent_supernode: websocket_name, }) .then(resp => doSend(websocket_conn, resp)); + + this.classList.remove('cnf_withdrawal'); + this.classList.remove('btn-info'); + this.classList.add('btn-success'); + this.innerText = "Success"; + + return true; + }); }); } diff --git a/index.html b/index.html index cbecdf9..55fb2fc 100644 --- a/index.html +++ b/index.html @@ -15579,7 +15579,7 @@ params.status = 1; params.btc_address = generate_btc_keys_for_requester.address; - params.bitcoinToBePaid = Number(params.depositing_amount); + params.bitcoinToBePaid = helper_functions.truncateDecimals(params.depositing_amount); let receivedTradeInfo = { ...params }; @@ -15902,7 +15902,7 @@ params.product ) ) { - const eqBTC = Number(parseFloat(params.withdrawing_amount).toFixed(8)); + const eqBTC = helper_functions.truncateDecimals(params.withdrawing_amount); if ( trade_margin.remaining_crypto_credit < 0 || @@ -15929,9 +15929,9 @@ "string" && btc_balance_res.crypto_balance > 0 ) { - let withdrawer_btc_balance = Number( + let withdrawer_btc_balance = helper_functions.truncateDecimals( btc_balance_res.crypto_balance - ).toFixed(8); + ); let withdrawer_new_btc_balance = withdrawer_btc_balance - eqBTC; @@ -15969,7 +15969,7 @@ for (const dl in deposit_list) { if (deposit_list.hasOwnProperty(dl)) { const deposit_dl = deposit_list[dl]; - sum_total_btc += parseFloat( + sum_total_btc += helper_functions.truncateDecimals( deposit_dl.bitcoinToBePaid ); @@ -16109,11 +16109,11 @@ typeof cash_balances_res.cash_balance == "number" && cash_balances_res.cash_balance > 0 ) { - let withdrawer_cash_balance = parseFloat( - cash_balances_res.cash_balance + let withdrawer_cash_balance = helper_functions.truncateDecimals( + cash_balances_res.cash_balance, 2 ); - let withdrawing_cash_amount = parseFloat( - params.withdrawing_amount + let withdrawing_cash_amount = helper_functions.truncateDecimals( + params.withdrawing_amount, 2 ); let bank_details = params.receivinAddress.trim(); // user UPI @@ -16238,8 +16238,8 @@ if (msg.length > 0) { // Deduct balance of withdrawer - cash_balances_res.cash_balance -= parseFloat( - withdrawing_cash_amount + cash_balances_res.cash_balance -= helper_functions.truncateDecimals( + withdrawing_cash_amount, 2 ); const withdrawer_cash_data = await updateinDB( "cash_balances", @@ -16447,13 +16447,13 @@ resp_obj.txid.result || resp_obj.txid; msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`; - } else if ( - res.signedTxHash.length > 0 - ) { - msg = `INFO: We could not broadcast your transaction. Please broadcast - this Signed Raw Tx manually yourself: ${res.signedTxHash}`; } else { console.log(res); + err_response = { + user_flo_addr: params.trader_flo_address, + msg: `Withdraw of crypto failed. Please try again later.` + } + reactor.dispatchEvent('message_for_user', err_response); throw new Errror( `ERROR: Failed to make transaction.` ); @@ -16594,11 +16594,9 @@ !isNaN(current_balance) && parseFloat(current_balance) > 0 ) { - current_balance = Number( - parseFloat( + current_balance = helper_functions.truncateDecimals( current_balance / decimal - ) - ); + ); } if ( @@ -17201,7 +17199,7 @@ params.btc_address = generate_btc_keys_for_requester.address; - params.bitcoinToBePaid = Number(params.depositing_amount); + params.bitcoinToBePaid = helper_functions.truncateDecimals(params.depositing_amount); let receivedTradeInfo = { ...params }; @@ -17519,12 +17517,11 @@ "string" && btc_balance_res.crypto_balance > 0 ) { - let withdrawer_btc_balance = Number( + let withdrawer_btc_balance = helper_functions.truncateDecimals( btc_balance_res.crypto_balance - ).toFixed(8); - const eqBTC = Number( - parseFloat(params.withdrawing_amount).toFixed(8) ); + const eqBTC = helper_functions.truncateDecimals(params.withdrawing_amount); + let withdrawer_new_btc_balance = withdrawer_btc_balance - eqBTC; if ( @@ -17561,7 +17558,7 @@ for (const dl in deposit_list) { if (deposit_list.hasOwnProperty(dl)) { const deposit_dl = deposit_list[dl]; - sum_total_btc += parseFloat( + sum_total_btc += helper_functions.truncateDecimals( deposit_dl.bitcoinToBePaid ); @@ -17742,11 +17739,11 @@ "number" && cash_balances_res.cash_balance > 0 ) { - let withdrawer_cash_balance = parseFloat( - cash_balances_res.cash_balance + let withdrawer_cash_balance = helper_functions.truncateDecimals( + cash_balances_res.cash_balance, 2 ); - let withdrawing_cash_amount = parseFloat( - params.withdrawing_amount + let withdrawing_cash_amount = helper_functions.truncateDecimals( + params.withdrawing_amount, 2 ); let bank_details = params.receivinAddress.trim(); // user UPI @@ -17872,8 +17869,8 @@ if (msg.length > 0) { // Deduct balance of withdrawer - cash_balances_res.cash_balance -= parseFloat( - withdrawing_cash_amount + cash_balances_res.cash_balance -= helper_functions.truncateDecimals( + withdrawing_cash_amount, 2 ); const withdrawer_cash_data = await backup_server_db_instance.backup_updateinDB( "cash_balances", @@ -18059,7 +18056,7 @@ withdraw_res.product, withdraw_res.currency ); - let EqCryptoWd = Number( + let EqCryptoWd = helper_functions.truncateDecimals( withdraw_res.receivingBTC ); @@ -18256,11 +18253,8 @@ current_balance ) > 0 ) { - current_balance = Number( - parseFloat( - current_balance / - decimal - ) + current_balance = helper_functions.truncateDecimals( + current_balance / decimal ); } @@ -19011,7 +19005,7 @@ typeof res.cash_balance == "number" && !isNaN(res.cash_balance) ) { - let buyer_cash_balance = parseFloat(res.cash_balance); + let buyer_cash_balance = helper_functions.truncateDecimals(res.cash_balance, 2); let buy_price_btc = parseFloat(params.buy_price); if (buyer_cash_balance < buy_price_btc) { err_msg = "WARNING: Insufficient balance of buyer."; @@ -19024,7 +19018,7 @@ params.currency, params.product ); - eqBTC = parseFloat(eqBTC); + eqBTC = helper_functions.truncateDecimals(eqBTC); if (typeof eqBTC == "number" && eqBTC > 0) { let res_btc; @@ -19134,14 +19128,14 @@ res.trader_flo_address.length > 0 && res.crypto_balance > 0 ) { - let seller_btc_balance = parseFloat(res.crypto_balance); + let seller_btc_balance = helper_functions.truncateDecimals(res.crypto_balance); let sell_price_in_inr = parseFloat(params.buy_price); let eqBTC = RM_TRADE.calculateCryptoEquivalentOfCash( sell_price_in_inr, params.currency, params.product ); - eqBTC = parseFloat(eqBTC); + eqBTC = helper_functions.truncateDecimals(eqBTC); if (typeof eqBTC == "number" && eqBTC > 0) { if (seller_btc_balance < eqBTC) { err_msg = "Insufficient BTC balance."; @@ -19353,9 +19347,7 @@ typeof current_crypto_price == "object" && current_crypto_price.rate > 0 ) { - return Number( - parseFloat(price / current_crypto_price.rate).toFixed(8) - ); + return helper_functions.truncateDecimals(price / current_crypto_price.rate); } } let err_msg = `WARNING: Failed to calculate crypto equivalent of cash.`; @@ -19687,11 +19679,8 @@ if (utxo_list.length > 0) { try { utxo_list.sort((a,b)=>b.confirmations - a.confirmations); - let btc_eq_receiving_amount = Number(receiving_amount); - btc_eq_receiving_amount = Number( - btc_eq_receiving_amount.toFixed(8) - ); - + let btc_eq_receiving_amount = helper_functions.truncateDecimals(receiving_amount); + let trx = bitjs[crypto_type].transaction(); let sum = 0; @@ -19726,15 +19715,15 @@ btc_eq_receiving_amount = btc_eq_receiving_amount - miners_fee; - btc_eq_receiving_amount = Number( - btc_eq_receiving_amount.toFixed(8) + btc_eq_receiving_amount = helper_functions.truncateDecimals( + btc_eq_receiving_amount ); trx.addoutput(receiver_address, btc_eq_receiving_amount); let change_amount = 0; if (sum - btc_eq_receiving_amount - miners_fee > 0) { change_amount = sum - btc_eq_receiving_amount - miners_fee; - change_amount = Number(change_amount.toFixed(8)); + change_amount = helper_functions.truncateDecimals(change_amount); } if (change_amount > 0) { @@ -19966,7 +19955,7 @@ eqBTCBuyer != "" && eqBTCBuyer != undefined ) { - eqBTCBuyer = parseFloat(eqBTCBuyer); + eqBTCBuyer = helper_functions.truncateDecimals(eqBTCBuyer); } // Check seller's crypto balance @@ -19978,9 +19967,7 @@ typeof sellPipeBTCRes == "object" && typeof sellPipeBTCRes.crypto_balance == "number" ) { - let seller_btc_balance = Number( - parseFloat(sellPipeBTCRes.crypto_balance).toFixed(8) - ); + let seller_btc_balance = helper_functions.truncateDecimals(sellPipeBTCRes.crypto_balance); let sell_price_in_inr = parseFloat(sellPipeObj.buy_price); let eqBTCSeller = RM_TRADE.calculateCryptoEquivalentOfCash( sell_price_in_inr, @@ -19992,7 +19979,7 @@ eqBTCSeller != "" && eqBTCSeller != undefined ) { - eqBTCSeller = parseFloat(eqBTCSeller); + eqBTCSeller = helper_functions.truncateDecimals(eqBTCSeller); if (seller_btc_balance < eqBTCSeller) { err_msg = "Insufficient BTC balance of seller."; showMessage(err_msg); @@ -20010,7 +19997,7 @@ typeof buyPipeBTCRes.crypto_balance == "number" ) { buyPipeBTCRes.crypto_balance = - parseFloat(buyPipeBTCRes.crypto_balance) + + helper_functions.truncateDecimals(buyPipeBTCRes.crypto_balance) + eqBTCBuyer; buyerBTCResponseObject = buyPipeBTCRes; } else { @@ -20064,9 +20051,8 @@ // Decrease seller BTC balance let new_seller_btc_balance = seller_btc_balance - eqBTCSeller; - new_seller_btc_balance = Number( - parseFloat(new_seller_btc_balance).toFixed(8) - ); + new_seller_btc_balance = helper_functions.truncateDecimals(new_seller_btc_balance); + let sellerBTCResponseObject = { id: `${sellPipeObj.trader_flo_address}_${sellPipeObj.product}`, trader_flo_address: sellPipeObj.trader_flo_address, @@ -20306,13 +20292,13 @@ sell_orders.currency, sell_orders.product ); - sell_order_crypto_equivalent += Number(sell_order_crypto_eq); + sell_order_crypto_equivalent += helper_functions.truncateDecimals(sell_order_crypto_eq); }); let withdraw_crypto_equivalent = 0; user_crypto_withdraw_request.map(req => { withdraw_crypto_eq = req.receivingBTC; - withdraw_crypto_equivalent += Number(withdraw_crypto_eq); + withdraw_crypto_equivalent += helper_functions.truncateDecimals(withdraw_crypto_eq); }); remaining_crypto_credit = @@ -20429,7 +20415,7 @@ console.log(url); helper_functions.ajaxGet(url).then(async balance => { if (!isNaN(balance) && parseFloat(balance) > 0) { - balance = Number(parseFloat(balance / decimal)); + balance = helper_functions.truncateDecimals(balance / decimal); console.log(balance); if (balance - amount_to_receive > crypto_diff) { @@ -29682,7 +29668,7 @@ depositAssetButton.addEventListener("click", function() { let asset_type = assetTypeInput.value; - let tradeAmount = parseFloat(tradeAmountSelect.value); + let tradeAmount = helper_functions.truncateDecimals(tradeAmountSelect.value); //let fiatCurrency = currencySelect.value; let fiatCurrency = "INR"; if ( @@ -29724,7 +29710,7 @@ throw new Error(err_msg); } - let tradeAmount = parseFloat(tradeAmountSelect.value); + let tradeAmount = helper_functions.truncateDecimals(tradeAmountSelect.value); //let fiatCurrency = currencySelect.value; let fiatCurrency = "INR"; @@ -30207,12 +30193,12 @@ case "FLO": explorer = localbitcoinplusplus.server.flo_mainnet; decimal = 1; - crypto_diff = Number(localbitcoinplusplus.master_configurations.btcTradeMargin/decimal); + crypto_diff = helper_functions.truncateDecimals(localbitcoinplusplus.master_configurations.btcTradeMargin/decimal); break; case "FLO_TEST": explorer = localbitcoinplusplus.server.flo_testnet; decimal = 1; - crypto_diff = Number(localbitcoinplusplus.master_configurations.btcTradeMargin/decimal); + crypto_diff = helper_functions.truncateDecimals(localbitcoinplusplus.master_configurations.btcTradeMargin/decimal); break; default: break; @@ -30222,7 +30208,7 @@ console.log(url); helper_functions.ajaxGet(url).then(async balance => { if (!isNaN(balance) && parseFloat(balance) > 0) { - balance = Number(parseFloat(balance / decimal)); + balance = helper_functions.truncateDecimals(balance / decimal); console.log(balance); /************************ Case of dispute *****************/ @@ -30281,11 +30267,9 @@ //&& typeof res_btc_balances.result == "object" typeof res_btc_balances.crypto_balance == "number" ) { - updatedCryptobalances.crypto_balance = Number( - parseFloat( + updatedCryptobalances.crypto_balance = helper_functions.truncateDecimals( res_btc_balances.crypto_balance + updatedCryptobalances.crypto_balance - ) ); } // Update crypto balance of user in crypto_balances