modified withdraw btc to withdraw any amount upto balance

This commit is contained in:
Abhishek Sinha 2019-07-26 13:46:47 +05:30
parent ca0834e293
commit 489c784bb3

View File

@ -12108,21 +12108,17 @@
if (is_valid_request !== true) return false; if (is_valid_request !== true) return false;
if (typeof params.product !== "undefined" && if (typeof params.product !== "undefined"
((localbitcoinplusplus.master_configurations.tradableAsset1.includes( && ((localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product)
params.product) || || localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.product))
localbitcoinplusplus.master_configurations.tradableAsset2.includes( && localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.currency))
params.product)) && && typeof params.withdrawing_amount !== "undefined"
localbitcoinplusplus.master_configurations.tradableAsset2.includes( && typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined'
params.currency)) && && typeof params.trader_flo_address == "string"
typeof params.withdrawing_amount !== "undefined" && && params.trader_flo_address.length > 0
typeof localbitcoinplusplus.master_configurations.validTradingAmount !== && typeof params.receivinAddress == "string"
'undefined' && && params.receivinAddress.length > 0
typeof params.trader_flo_address == "string" && params.trader_flo_address && typeof params.currency == "string"
.length > 0 &&
typeof params.receivinAddress == "string" && params.receivinAddress
.length >
0 && typeof params.currency == "string"
) { ) {
const get_requester_primary_supernode = await localbitcoinplusplus.kademlia const get_requester_primary_supernode = await localbitcoinplusplus.kademlia
.determineClosestSupernode(params.trader_flo_address); .determineClosestSupernode(params.trader_flo_address);
@ -13385,45 +13381,41 @@
if (is_valid_request !== true) return false; if (is_valid_request !== true) return false;
if (typeof params.product !== "undefined" && if (typeof params.product !== "undefined"
(localbitcoinplusplus.master_configurations.tradableAsset1.includes( && ((localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product)
params.product) || || localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.product))
localbitcoinplusplus.master_configurations.tradableAsset2.includes( && localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.currency))
params.currency)) && && typeof params.withdrawing_amount !== "undefined"
typeof params.withdrawing_amount !== "undefined" && && typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined'
typeof localbitcoinplusplus.master_configurations.validTradingAmount !== && typeof params.trader_flo_address == "string"
'undefined' && && params.trader_flo_address.length > 0
localbitcoinplusplus.master_configurations.validTradingAmount.includes( && typeof params.receivinAddress == "string"
parseFloat(params.withdrawing_amount)) && && params.receivinAddress.length > 0
typeof params.trader_flo_address == "string" && params.trader_flo_address && typeof params.currency == "string"
.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 const get_requester_primary_supernode = await localbitcoinplusplus.kademlia
.determineClosestSupernode(params.trader_flo_address); .determineClosestSupernode(params.trader_flo_address);
const primarySupernodeForThisUser = get_requester_primary_supernode[0].data.id; 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( let trade_margin = await RM_TRADE.getAssetTradeAndWithdrawLimit(
params.trader_flo_address, params.product, params.currency, params.trader_flo_address, params.product, params.currency
primarySupernodeForThisUser
); );
if (localbitcoinplusplus.master_configurations.tradableAsset1.includes( if (localbitcoinplusplus.master_configurations.tradableAsset1.includes(
params.product)) { params.product)) {
let eqCrypto = RM_TRADE.calculateCryptoEquivalentOfCash( if (trade_margin.remaining_crypto_credit < 0
params.withdrawing_amount, params.currency, params.product); || params.withdrawing_amount <= 0
if (trade_margin.remaining_crypto_credit < 0 || || trade_margin.remaining_crypto_credit < params.withdrawing_amount) {
trade_margin.remaining_crypto_credit < eqCrypto) {
err_msg = `Insufficient crypto balance to withdraw. You can withdraw upto: ${params.product} ${trade_margin.remaining_crypto_credit}` err_msg = `Insufficient crypto balance to withdraw. You can withdraw upto: ${params.product} ${trade_margin.remaining_crypto_credit}`
showMessage(err_msg); showMessage(err_msg);
throw new Error(err_msg); throw new Error(err_msg);
} }
} else { } else {
if (trade_margin.remaining_fiat_credit < 0 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}`; err_msg = `Insufficient fiat balance to withdraw. You can withdraw upto: ${params.currency} ${trade_margin.remaining_fiat_credit}`;
showMessage(err_msg); showMessage(err_msg);
throw new Error(err_msg); throw new Error(err_msg);
@ -13432,39 +13424,23 @@
params.id = helper_functions.unique_id(); params.id = helper_functions.unique_id();
params.status = 1; params.status = 1;
if (localbitcoinplusplus.master_configurations.tradableAsset1.includes( if (localbitcoinplusplus.master_configurations.tradableAsset1
params.product)) { .includes(params.product)) {
// Check how much cryptos the user can withdraw // Check how much cryptos the user can withdraw
let withdrawer_btc_id = const withdrawer_btc_id = `${params.trader_flo_address}_${params.product}`;
`${params.trader_flo_address}_${params.product}`; backup_server_db_instance.backup_readDB("crypto_balances", withdrawer_btc_id)
backup_server_db_instance.backup_readDB("crypto_balances", withdrawer_btc_id).then(function ( .then(function (btc_balance_res) {
btc_balance_res) { if (typeof btc_balance_res == "object"
if (typeof btc_balance_res == "object" && && typeof btc_balance_res.trader_flo_address == "string"
typeof btc_balance_res && btc_balance_res.crypto_balance > 0) {
.trader_flo_address == "string" && let withdrawer_btc_balance = Number(btc_balance_res.crypto_balance).toFixed(8);
btc_balance_res.crypto_balance > 0) { const eqBTC = Number(parseFloat(params.withdrawing_amount).toFixed(8));
let withdrawer_btc_balance = parseFloat( let withdrawer_new_btc_balance = withdrawer_btc_balance - eqBTC;
btc_balance_res.crypto_balance); if (withdrawer_new_btc_balance >= 0
let withdrawing_btc_amount_in_cash = && withdrawer_btc_balance > 0
parseFloat(params.withdrawing_amount); && eqBTC > 0
if (!localbitcoinplusplus.master_configurations && eqBTC <= withdrawer_btc_balance
.tradableAsset2.includes(params.currency)
) { ) {
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 // Now details of Bitcoins can be sent to withdrawer
@ -13474,55 +13450,33 @@
****************************************************************************/ ****************************************************************************/
let sum_total_btc = 0; let sum_total_btc = 0;
let valid_utxo_list = []; let valid_utxo_list = [];
let receiverBTCAddress = params.receivinAddress let receiverBTCAddress = params.receivinAddress.trim();
.trim();
backup_server_db_instance.backup_readAllDB("deposit").then(function ( backup_server_db_instance.backup_readAllDB("deposit").then(function (
deposit_list) { deposit_list) {
if (typeof deposit_list == if (typeof deposit_list == "object"
"object" && && deposit_list.length > 0
deposit_list.length > 0
) { ) {
deposit_list = deposit_list = deposit_list.filter(deposits =>
deposit_list.filter( deposits.status == 2
deposits => &&
deposits.status == localbitcoinplusplus
2 && .master_configurations
localbitcoinplusplus .tradableAsset1
.master_configurations .includes(deposits.product)
.tradableAsset1 &&
.includes( params.product == deposits.product
deposits.product );
) && for (const dl in deposit_list) {
params.product == if (deposit_list.hasOwnProperty(dl)) {
deposits.product const deposit_dl = deposit_list[dl];
); sum_total_btc += parseFloat(deposit_dl.bitcoinToBePaid);
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 <= if (eqBTC <= sum_total_btc ) {
sum_total_btc valid_utxo_list.push(deposit_dl);
) {
valid_utxo_list
.push(
deposit_dl
);
break; break;
} else { } else {
valid_utxo_list valid_utxo_list.push(deposit_dl);
.push(
deposit_dl
);
} }
} }
} }
@ -13534,52 +13488,37 @@
backup_server_db_instance.backup_updateinDB( backup_server_db_instance.backup_updateinDB(
"deposit", "deposit",
deposit_arr, deposit_arr,
deposit_arr deposit_arr.trader_flo_address
.trader_flo_address
); );
// save the address and id in a table // save the address and id in a table
let const withdraw_id = helper_functions.unique_id();
withdraw_id = const withdraw_btc_order_object = {
helper_functions id: withdraw_id,
.unique_id(); trader_flo_address: params.trader_flo_address,
const utxo_addr: deposit_arr.btc_address,
withdraw_btc_order_object = { receiverBTCAddress: params.receivinAddress,
id: withdraw_id, receivingBTC: eqBTC,
trader_flo_address: params currency: params.currency,
.trader_flo_address, product: params.product,
utxo_addr: deposit_arr change_adress: deposit_arr.btc_address,
.btc_address, timestamp: + new Date()
receiverBTCAddress: params }
.receivinAddress, backup_server_db_instance
receivingBTC: eqBTC, .backup_addDB('withdraw_btc', withdraw_btc_order_object);
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 { return {
withdraw_id: withdraw_id, withdraw_id: withdraw_id,
deposited_btc_address: deposit_arr deposited_btc_address: deposit_arr.btc_address
.btc_address
}; };
}); });
// doSend btc_private_key_shamirs_id from system_btc_reserves_private_keys // doSend btc_private_key_shamirs_id from system_btc_reserves_private_keys
valid_btc_list.map(vbl => { valid_btc_list.map(vbl => {
backup_server_db_instance.backup_readDBbyIndex readDBbyIndex
( (
'system_btc_reserves_private_keys', 'system_btc_reserves_private_keys',
'btc_address', 'btc_address',
vbl vbl.deposited_btc_address
.deposited_btc_address
).then( ).then(
function (res) { function (res) {
let retrieve_pvtkey_req_id = res[0].id; let retrieve_pvtkey_req_id = res[0].id;
@ -13587,7 +13526,7 @@
.btc_private_key_shamirs_id .btc_private_key_shamirs_id
.map( .map(
bpks => { bpks => {
RM_RPC RM_RPC
.send_rpc .send_rpc
.call( .call(
this, this,
@ -13597,8 +13536,7 @@
withdraw_id: vbl.withdraw_id, withdraw_id: vbl.withdraw_id,
db_inst: primarySupernodeForThisUser db_inst: primarySupernodeForThisUser
} }
).then(retrieve_pvtkey_req=> ).then(retrieve_pvtkey_req=>doSend(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)) { .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 // Check if there's no already a withdraw cash order of this user
/*ONLY DELETE A WITHDRAW ORDER WHEN A DEPOSITOR HAS CONFIRMED DEPOSIT /*ONLY DELETE A WITHDRAW ORDER WHEN A DEPOSITOR HAS CONFIRMED DEPOSIT
AND RECEIVER HAS CONFIRMED WITHDRAW*/ AND RECEIVER HAS CONFIRMED WITHDRAW*/
// Check how much Cash user can withdraw // Check how much Cash user can withdraw
const trader_cash_id = const trader_cash_id = `${params.trader_flo_address}_${params.currency}`;
`${params.trader_flo_address}_${params.currency}`; backup_server_db_instance.backup_readDB("cash_balances", trader_cash_id)
backup_server_db_instance.backup_readDB("cash_balances", trader_cash_id).then(function ( .then(function (cash_balances_res) {
cash_balances_res) { if (typeof cash_balances_res == "object"
if (typeof cash_balances_res == "object" && && typeof cash_balances_res.trader_flo_address == "string"
typeof cash_balances_res && typeof cash_balances_res.cash_balance == "number"
.trader_flo_address == "string" && && cash_balances_res.cash_balance > 0
typeof cash_balances_res.cash_balance == ) {
"number" && let withdrawer_cash_balance = parseFloat(cash_balances_res.cash_balance);
cash_balances_res.cash_balance > 0) { let withdrawing_cash_amount = parseFloat(params.withdrawing_amount);
let withdrawer_cash_balance = parseFloat(
cash_balances_res.cash_balance);
let withdrawing_cash_amount = parseFloat(
params.withdrawing_amount);
let bank_details = params.receivinAddress.trim(); let bank_details = params.receivinAddress.trim();
if (withdrawer_cash_balance > 0 && if (withdrawer_cash_balance > 0 &&
withdrawing_cash_amount > 0 && withdrawing_cash_amount > 0 &&
withdrawer_cash_balance >= withdrawer_cash_balance >= withdrawing_cash_amount
withdrawing_cash_amount) { ) {
// Add it to cash withdrawal table // Add it to cash withdrawal table
let withdraw_request_db_object = { let withdraw_request_db_object = {
id: helper_functions.unique_id(), id: helper_functions.unique_id(),
@ -13672,68 +13613,57 @@
status: 1, // withdraw request called status: 1, // withdraw request called
} }
backup_server_db_instance.backup_readDB("localbitcoinUser", "00-01").then( const getPubKeyOfSupernodeOfThisUser = RM_WALLET
function ( .getSupernodePublicKeyFromFloId(primarySupernodeForThisUser);
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
)) {
let if (typeof getPubKeyOfSupernodeOfThisUser == "string"
withdraw_request_db_object_hash = && getPubKeyOfSupernodeOfThisUser.length > 0
Crypto.SHA256(JSON.stringify( && localbitcoinplusplus.master_configurations
withdraw_request_db_object .supernodesPubKeys.includes(
)); getPubKeyOfSupernodeOfThisUser
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;
try { let withdraw_request_db_object_hash =
// add the request to supernode db Crypto.SHA256(JSON.stringify(
backup_server_db_instance.backup_addDB( withdraw_request_db_object
"withdraw_cash", ));
withdraw_request_db_object withdraw_request_db_object
); ["withdrawDataHash"] =
// return back the response to client withdraw_request_db_object_hash;
withdraw_request_db_object.receiver_flo_address = withdraw_request_db_object
params.trader_flo_address; ["order_validator_sign"] =
withdraw_request_db_object.trader_flo_address = RM_WALLET
params.trader_flo_address; .sign(
withdraw_request_db_object_hash,
localbitcoinplusplus
.wallets.MY_SUPERNODE_PRIVATE_KEY
);
withdraw_request_db_object
["order_validator_public_key"] =
getPubKeyOfSupernodeOfThisUser;
RM_RPC.send_rpc try {
.call(this, // add the request to supernode db
"withdrawal_request_response", backup_server_db_instance.backup_addDB(
withdraw_request_db_object "withdraw_cash",
).then(withdrawal_request_response=> withdraw_request_db_object
doSend(withdrawal_request_response)); );
return true; // return back the response to client
} catch (error) { withdraw_request_db_object.receiver_flo_address =
console.log(error); 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 { } else {
// Return error to the requester // Return error to the requester
@ -13750,7 +13680,7 @@
} }
} }
}); });
break; break;
case "retrieve_shamirs_secret_btc_pvtkey": case "retrieve_shamirs_secret_btc_pvtkey":
RM_RPC.filter_legit_backup_requests(params.trader_flo_address, function (is_valid_request) { RM_RPC.filter_legit_backup_requests(params.trader_flo_address, function (is_valid_request) {