From 489c784bb3863dcb3b7972e73130e76bb0cf4f46 Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Fri, 26 Jul 2019 13:46:47 +0530 Subject: [PATCH] modified withdraw btc to withdraw any amount upto balance --- supernode/index.html | 388 ++++++++++++++++++------------------------- 1 file changed, 159 insertions(+), 229 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index cfb6717..b6223c5 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -12108,21 +12108,17 @@ if (is_valid_request !== true) return false; - if (typeof params.product !== "undefined" && - ((localbitcoinplusplus.master_configurations.tradableAsset1.includes( - params.product) || - localbitcoinplusplus.master_configurations.tradableAsset2.includes( - params.product)) && - localbitcoinplusplus.master_configurations.tradableAsset2.includes( - params.currency)) && - typeof params.withdrawing_amount !== "undefined" && - typeof localbitcoinplusplus.master_configurations.validTradingAmount !== - 'undefined' && - typeof params.trader_flo_address == "string" && params.trader_flo_address - .length > 0 && - typeof params.receivinAddress == "string" && params.receivinAddress - .length > - 0 && typeof params.currency == "string" + if (typeof params.product !== "undefined" + && ((localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product) + || localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.product)) + && localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.currency)) + && typeof params.withdrawing_amount !== "undefined" + && typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined' + && typeof params.trader_flo_address == "string" + && params.trader_flo_address.length > 0 + && typeof params.receivinAddress == "string" + && params.receivinAddress.length > 0 + && typeof params.currency == "string" ) { const get_requester_primary_supernode = await localbitcoinplusplus.kademlia .determineClosestSupernode(params.trader_flo_address); @@ -13385,45 +13381,41 @@ if (is_valid_request !== true) return false; - if (typeof params.product !== "undefined" && - (localbitcoinplusplus.master_configurations.tradableAsset1.includes( - params.product) || - localbitcoinplusplus.master_configurations.tradableAsset2.includes( - params.currency)) && - typeof params.withdrawing_amount !== "undefined" && - typeof localbitcoinplusplus.master_configurations.validTradingAmount !== - 'undefined' && - localbitcoinplusplus.master_configurations.validTradingAmount.includes( - parseFloat(params.withdrawing_amount)) && - typeof params.trader_flo_address == "string" && params.trader_flo_address - .length > 0 && - typeof params.receivinAddress == "string" && params.receivinAddress - .length > - 0 && typeof params.currency == "string" + if (typeof params.product !== "undefined" + && ((localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product) + || localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.product)) + && localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.currency)) + && typeof params.withdrawing_amount !== "undefined" + && typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined' + && typeof params.trader_flo_address == "string" + && params.trader_flo_address.length > 0 + && typeof params.receivinAddress == "string" + && params.receivinAddress.length > 0 + && typeof params.currency == "string" ) { - await RM_TRADE.resolve_current_crypto_price_in_fiat(params.product, - params.currency); const get_requester_primary_supernode = await localbitcoinplusplus.kademlia .determineClosestSupernode(params.trader_flo_address); const primarySupernodeForThisUser = get_requester_primary_supernode[0].data.id; + + await RM_TRADE.resolve_current_crypto_price_in_fiat(params.product, + params.currency); let trade_margin = await RM_TRADE.getAssetTradeAndWithdrawLimit( - params.trader_flo_address, params.product, params.currency, - primarySupernodeForThisUser + params.trader_flo_address, params.product, params.currency ); if (localbitcoinplusplus.master_configurations.tradableAsset1.includes( params.product)) { - let eqCrypto = RM_TRADE.calculateCryptoEquivalentOfCash( - params.withdrawing_amount, params.currency, params.product); - if (trade_margin.remaining_crypto_credit < 0 || - trade_margin.remaining_crypto_credit < eqCrypto) { + if (trade_margin.remaining_crypto_credit < 0 + || params.withdrawing_amount <= 0 + || trade_margin.remaining_crypto_credit < params.withdrawing_amount) { err_msg = `Insufficient crypto balance to withdraw. You can withdraw upto: ${params.product} ${trade_margin.remaining_crypto_credit}` showMessage(err_msg); throw new Error(err_msg); } } else { if (trade_margin.remaining_fiat_credit < 0 - || trade_margin.remaining_fiat_credit < params.withdrawing_amount) { + || params.withdrawing_amount <= 0 + || trade_margin.remaining_fiat_credit < params.withdrawing_amount) { err_msg = `Insufficient fiat balance to withdraw. You can withdraw upto: ${params.currency} ${trade_margin.remaining_fiat_credit}`; showMessage(err_msg); throw new Error(err_msg); @@ -13432,39 +13424,23 @@ params.id = helper_functions.unique_id(); params.status = 1; - if (localbitcoinplusplus.master_configurations.tradableAsset1.includes( - params.product)) { + if (localbitcoinplusplus.master_configurations.tradableAsset1 + .includes(params.product)) { // Check how much cryptos the user can withdraw - let withdrawer_btc_id = - `${params.trader_flo_address}_${params.product}`; - backup_server_db_instance.backup_readDB("crypto_balances", withdrawer_btc_id).then(function ( - btc_balance_res) { - if (typeof btc_balance_res == "object" && - typeof btc_balance_res - .trader_flo_address == "string" && - btc_balance_res.crypto_balance > 0) { - let withdrawer_btc_balance = parseFloat( - btc_balance_res.crypto_balance); - let withdrawing_btc_amount_in_cash = - parseFloat(params.withdrawing_amount); - if (!localbitcoinplusplus.master_configurations - .tradableAsset2.includes(params.currency) + const withdrawer_btc_id = `${params.trader_flo_address}_${params.product}`; + backup_server_db_instance.backup_readDB("crypto_balances", withdrawer_btc_id) + .then(function (btc_balance_res) { + if (typeof btc_balance_res == "object" + && typeof btc_balance_res.trader_flo_address == "string" + && btc_balance_res.crypto_balance > 0) { + let withdrawer_btc_balance = Number(btc_balance_res.crypto_balance).toFixed(8); + const eqBTC = Number(parseFloat(params.withdrawing_amount).toFixed(8)); + let withdrawer_new_btc_balance = withdrawer_btc_balance - eqBTC; + if (withdrawer_new_btc_balance >= 0 + && withdrawer_btc_balance > 0 + && eqBTC > 0 + && eqBTC <= withdrawer_btc_balance ) { - err_msg = "Invalid or unsupported currency."; - showMessage(err_msg); - throw new Error(err_msg); - } - let eqBTC = RM_TRADE.calculateCryptoEquivalentOfCash( - withdrawing_btc_amount_in_cash, - params.currency, params.product); - eqBTC = Number(parseFloat(eqBTC).toFixed(8)); - let withdrawer_new_btc_balance = - withdrawer_btc_balance - eqBTC; - if (withdrawer_new_btc_balance >= 0 && - withdrawer_btc_balance > 0 && - withdrawing_btc_amount_in_cash > 0 && - eqBTC > 0 && eqBTC <= - withdrawer_btc_balance) { // Now details of Bitcoins can be sent to withdrawer @@ -13474,112 +13450,75 @@ ****************************************************************************/ let sum_total_btc = 0; let valid_utxo_list = []; - let receiverBTCAddress = params.receivinAddress - .trim(); + let receiverBTCAddress = params.receivinAddress.trim(); backup_server_db_instance.backup_readAllDB("deposit").then(function ( deposit_list) { - if (typeof deposit_list == - "object" && - deposit_list.length > 0 + if (typeof deposit_list == "object" + && deposit_list.length > 0 ) { - deposit_list = - deposit_list.filter( - deposits => - deposits.status == - 2 && - localbitcoinplusplus - .master_configurations - .tradableAsset1 - .includes( - deposits.product - ) && - params.product == - deposits.product - ); - for (const dl in - deposit_list) { - if (deposit_list.hasOwnProperty( - dl)) { - const deposit_dl = - deposit_list[ - dl]; - sum_total_btc += - parseFloat( - deposit_dl - .bitcoinToBePaid - ); + deposit_list = deposit_list.filter(deposits => + deposits.status == 2 + && + localbitcoinplusplus + .master_configurations + .tradableAsset1 + .includes(deposits.product) + && + params.product == deposits.product + ); + for (const dl in deposit_list) { + if (deposit_list.hasOwnProperty(dl)) { + const deposit_dl = deposit_list[dl]; + sum_total_btc += parseFloat(deposit_dl.bitcoinToBePaid); - if (eqBTC <= - sum_total_btc - ) { - valid_utxo_list - .push( - deposit_dl - ); + if (eqBTC <= sum_total_btc ) { + valid_utxo_list.push(deposit_dl); break; } else { - valid_utxo_list - .push( - deposit_dl - ); + valid_utxo_list.push(deposit_dl); } } } let valid_btc_list = valid_utxo_list.map( deposit_arr => { - // Deposited Bitcoin is under process - deposit_arr.status = 3; + // Deposited Bitcoin is under process + deposit_arr.status = 3; backup_server_db_instance.backup_updateinDB( "deposit", deposit_arr, - deposit_arr - .trader_flo_address + deposit_arr.trader_flo_address ); // save the address and id in a table - let - withdraw_id = - helper_functions - .unique_id(); - const - withdraw_btc_order_object = { - id: withdraw_id, - trader_flo_address: params - .trader_flo_address, - utxo_addr: deposit_arr - .btc_address, - receiverBTCAddress: params - .receivinAddress, - receivingBTC: eqBTC, - currency: params - .currency, - product: params - .product, - change_adress: deposit_arr - .btc_address, - timestamp: + new Date() - } - backup_server_db_instance.backup_addDB( - 'withdraw_btc', - withdraw_btc_order_object - ); + const withdraw_id = helper_functions.unique_id(); + const withdraw_btc_order_object = { + id: withdraw_id, + trader_flo_address: params.trader_flo_address, + utxo_addr: deposit_arr.btc_address, + receiverBTCAddress: params.receivinAddress, + receivingBTC: eqBTC, + currency: params.currency, + product: params.product, + change_adress: deposit_arr.btc_address, + timestamp: + new Date() + } + backup_server_db_instance + .backup_addDB('withdraw_btc', withdraw_btc_order_object); return { withdraw_id: withdraw_id, - deposited_btc_address: deposit_arr - .btc_address + deposited_btc_address: deposit_arr.btc_address }; }); // doSend btc_private_key_shamirs_id from system_btc_reserves_private_keys valid_btc_list.map(vbl => { - backup_server_db_instance.backup_readDBbyIndex + readDBbyIndex ( 'system_btc_reserves_private_keys', 'btc_address', - vbl - .deposited_btc_address + vbl.deposited_btc_address ).then( function (res) { let retrieve_pvtkey_req_id = res[0].id; @@ -13587,7 +13526,7 @@ .btc_private_key_shamirs_id .map( bpks => { - RM_RPC + RM_RPC .send_rpc .call( this, @@ -13595,10 +13534,9 @@ retrieve_pvtkey_req_id: retrieve_pvtkey_req_id, chunk_val: bpks, withdraw_id: vbl.withdraw_id, - db_inst: primarySupernodeForThisUser + db_inst: primarySupernodeForThisUser } - ).then(retrieve_pvtkey_req=> - doSend(retrieve_pvtkey_req)); + ).then(retrieve_pvtkey_req=>doSend(retrieve_pvtkey_req)); } ); @@ -13635,33 +13573,36 @@ }; } }); - } else if (!localbitcoinplusplus.master_configurations.tradableAsset1 + } else if (localbitcoinplusplus.master_configurations.tradableAsset2 .includes(params.product)) { + + if (!localbitcoinplusplus.master_configurations.validTradingAmount + .includes(parseFloat(params.withdrawing_amount))) { + err_msg = `Withdrawal request failed: Please enter valid fiat amount.`; + showMessage(err_msg); + throw new Error(err_msg); + } // Check if there's no already a withdraw cash order of this user /*ONLY DELETE A WITHDRAW ORDER WHEN A DEPOSITOR HAS CONFIRMED DEPOSIT AND RECEIVER HAS CONFIRMED WITHDRAW*/ // Check how much Cash user can withdraw - const trader_cash_id = - `${params.trader_flo_address}_${params.currency}`; - backup_server_db_instance.backup_readDB("cash_balances", trader_cash_id).then(function ( - cash_balances_res) { - if (typeof cash_balances_res == "object" && - typeof cash_balances_res - .trader_flo_address == "string" && - typeof cash_balances_res.cash_balance == - "number" && - cash_balances_res.cash_balance > 0) { - let withdrawer_cash_balance = parseFloat( - cash_balances_res.cash_balance); - let withdrawing_cash_amount = parseFloat( - params.withdrawing_amount); + const trader_cash_id = `${params.trader_flo_address}_${params.currency}`; + backup_server_db_instance.backup_readDB("cash_balances", trader_cash_id) + .then(function (cash_balances_res) { + if (typeof cash_balances_res == "object" + && typeof cash_balances_res.trader_flo_address == "string" + && typeof cash_balances_res.cash_balance == "number" + && cash_balances_res.cash_balance > 0 + ) { + let withdrawer_cash_balance = parseFloat(cash_balances_res.cash_balance); + let withdrawing_cash_amount = parseFloat(params.withdrawing_amount); let bank_details = params.receivinAddress.trim(); if (withdrawer_cash_balance > 0 && withdrawing_cash_amount > 0 && - withdrawer_cash_balance >= - withdrawing_cash_amount) { + withdrawer_cash_balance >= withdrawing_cash_amount + ) { // Add it to cash withdrawal table let withdraw_request_db_object = { id: helper_functions.unique_id(), @@ -13672,68 +13613,57 @@ status: 1, // withdraw request called } - backup_server_db_instance.backup_readDB("localbitcoinUser", "00-01").then( - function ( - su_data) { - if (typeof su_data == - "object" && - typeof su_data.myLocalFLOPublicKey == - "string" && - su_data.myLocalFLOPublicKey - .length > - 0 && - localbitcoinplusplus.master_configurations - .supernodesPubKeys.includes( - su_data.myLocalFLOPublicKey - )) { + const getPubKeyOfSupernodeOfThisUser = RM_WALLET + .getSupernodePublicKeyFromFloId(primarySupernodeForThisUser); - let - withdraw_request_db_object_hash = - Crypto.SHA256(JSON.stringify( - withdraw_request_db_object - )); - withdraw_request_db_object - ["withdrawDataHash"] = - withdraw_request_db_object_hash; - withdraw_request_db_object - [ - "order_validator_sign" - ] = - RM_WALLET - .sign( - withdraw_request_db_object_hash, - localbitcoinplusplus - .wallets.MY_SUPERNODE_PRIVATE_KEY - ); - withdraw_request_db_object - [ - "order_validator_public_key" - ] = su_data.myLocalFLOPublicKey; + if (typeof getPubKeyOfSupernodeOfThisUser == "string" + && getPubKeyOfSupernodeOfThisUser.length > 0 + && localbitcoinplusplus.master_configurations + .supernodesPubKeys.includes( + getPubKeyOfSupernodeOfThisUser + )) { - try { - // add the request to supernode db - backup_server_db_instance.backup_addDB( - "withdraw_cash", - withdraw_request_db_object - ); - // return back the response to client - withdraw_request_db_object.receiver_flo_address = - params.trader_flo_address; - withdraw_request_db_object.trader_flo_address = - params.trader_flo_address; - - RM_RPC.send_rpc - .call(this, - "withdrawal_request_response", - withdraw_request_db_object - ).then(withdrawal_request_response=> - doSend(withdrawal_request_response)); - return true; - } catch (error) { - console.log(error); - } - } - }); + let withdraw_request_db_object_hash = + Crypto.SHA256(JSON.stringify( + withdraw_request_db_object + )); + withdraw_request_db_object + ["withdrawDataHash"] = + withdraw_request_db_object_hash; + withdraw_request_db_object + ["order_validator_sign"] = + RM_WALLET + .sign( + withdraw_request_db_object_hash, + localbitcoinplusplus + .wallets.MY_SUPERNODE_PRIVATE_KEY + ); + withdraw_request_db_object + ["order_validator_public_key"] = + getPubKeyOfSupernodeOfThisUser; + + try { + // add the request to supernode db + backup_server_db_instance.backup_addDB( + "withdraw_cash", + withdraw_request_db_object + ); + // return back the response to client + withdraw_request_db_object.receiver_flo_address = + params.trader_flo_address; + withdraw_request_db_object.trader_flo_address = + params.trader_flo_address; + RM_RPC.send_rpc + .call(this, + "withdrawal_request_response", + withdraw_request_db_object + ).then(withdrawal_request_response + =>doSend(withdrawal_request_response)); + return true; + } catch (error) { + console.log(error); + } + } } else { // Return error to the requester @@ -13750,7 +13680,7 @@ } } }); - break; + break; case "retrieve_shamirs_secret_btc_pvtkey": RM_RPC.filter_legit_backup_requests(params.trader_flo_address, function (is_valid_request) {