modified withdraw btc to withdraw any amount upto balance

This commit is contained in:
Abhishek Sinha 2019-07-25 16:33:57 +05:30
parent 8ac62c34f1
commit ca0834e293

View File

@ -12109,15 +12109,15 @@
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 !== typeof localbitcoinplusplus.master_configurations.validTradingAmount !==
'undefined' && 'undefined' &&
localbitcoinplusplus.master_configurations.validTradingAmount.includes(
parseFloat(params.withdrawing_amount)) &&
typeof params.trader_flo_address == "string" && params.trader_flo_address typeof params.trader_flo_address == "string" && params.trader_flo_address
.length > 0 && .length > 0 &&
typeof params.receivinAddress == "string" && params.receivinAddress typeof params.receivinAddress == "string" && params.receivinAddress
@ -12136,17 +12136,17 @@
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 || trade_margin.remaining_fiat_credit < if (trade_margin.remaining_fiat_credit < 0
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);
@ -12166,26 +12166,13 @@
typeof btc_balance_res typeof btc_balance_res
.trader_flo_address == "string" && .trader_flo_address == "string" &&
btc_balance_res.crypto_balance > 0) { btc_balance_res.crypto_balance > 0) {
let withdrawer_btc_balance = parseFloat( let withdrawer_btc_balance = Number(
btc_balance_res.crypto_balance); btc_balance_res.crypto_balance).toFixed(8);
let withdrawing_btc_amount_in_cash = const eqBTC = Number(parseFloat(params.withdrawing_amount).toFixed(8));
parseFloat(params.withdrawing_amount);
if (!localbitcoinplusplus.master_configurations
.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 = let withdrawer_new_btc_balance =
withdrawer_btc_balance - eqBTC; withdrawer_btc_balance - eqBTC;
if (withdrawer_new_btc_balance >= 0 && if (withdrawer_new_btc_balance >= 0 &&
withdrawer_btc_balance > 0 && withdrawer_btc_balance > 0 &&
withdrawing_btc_amount_in_cash > 0 &&
eqBTC > 0 && eqBTC <= eqBTC > 0 && eqBTC <=
withdrawer_btc_balance) { withdrawer_btc_balance) {
@ -12197,8 +12184,7 @@
****************************************************************************/ ****************************************************************************/
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();
readAllDB("deposit").then(function ( readAllDB("deposit").then(function (
deposit_list) { deposit_list) {
@ -12275,13 +12261,10 @@
.btc_address, .btc_address,
receiverBTCAddress: params receiverBTCAddress: params
.receivinAddress, .receivinAddress,
receiverBTCEquivalentInCash: withdrawing_btc_amount_in_cash, receivingBTC: eqBTC,
currency: params currency: params.currency,
.currency, product: params.product,
product: params change_adress: deposit_arr.btc_address,
.product,
change_adress: deposit_arr
.btc_address,
timestamp: + new Date() timestamp: + new Date()
} }
addDB( addDB(
@ -12355,8 +12338,15 @@
}; };
} }
}); });
} 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*/
@ -12512,10 +12502,7 @@
await RM_TRADE.resolve_current_crypto_price_in_fiat( await RM_TRADE.resolve_current_crypto_price_in_fiat(
withdraw_res.product, withdraw_res.product,
withdraw_res.currency); withdraw_res.currency);
const EqCryptoWd = RM_TRADE.calculateCryptoEquivalentOfCash( const EqCryptoWd = withdraw_res.receivingBTC;
withdraw_res.receiverBTCEquivalentInCash,
withdraw_res.currency,
withdraw_res.product);
let transaction_key = let transaction_key =
btc_reserves.supernode_transaction_key; btc_reserves.supernode_transaction_key;
@ -12531,8 +12518,7 @@
withdraw_res.utxo_addr, withdraw_res.utxo_addr,
btc_private_key, btc_private_key,
withdraw_res.receiverBTCAddress, withdraw_res.receiverBTCAddress,
withdraw_res.receiverBTCEquivalentInCash, withdraw_res.receivingBTC,
withdraw_res.currency,
withdraw_res.change_adress, withdraw_res.change_adress,
async function (res) { async function (res) {
console.log(res); console.log(res);
@ -13566,7 +13552,7 @@
.btc_address, .btc_address,
receiverBTCAddress: params receiverBTCAddress: params
.receivinAddress, .receivinAddress,
receiverBTCEquivalentInCash: withdrawing_btc_amount_in_cash, receivingBTC: eqBTC,
currency: params currency: params
.currency, .currency,
product: params product: params
@ -13812,11 +13798,7 @@
await RM_TRADE.resolve_current_crypto_price_in_fiat( await RM_TRADE.resolve_current_crypto_price_in_fiat(
withdraw_res.product, withdraw_res.product,
withdraw_res.currency); withdraw_res.currency);
let EqCryptoWd = RM_TRADE.calculateCryptoEquivalentOfCash( let EqCryptoWd = Number(withdraw_res.receivingBTC);
withdraw_res.receiverBTCEquivalentInCash,
withdraw_res.currency,
withdraw_res.product);
EqCryptoWd = parseFloat(EqCryptoWd);
let transaction_key = let transaction_key =
btc_reserves.supernode_transaction_key; btc_reserves.supernode_transaction_key;
@ -13831,8 +13813,7 @@
withdraw_res.utxo_addr, withdraw_res.utxo_addr,
btc_private_key, btc_private_key,
withdraw_res.receiverBTCAddress, withdraw_res.receiverBTCAddress,
withdraw_res.receiverBTCEquivalentInCash, withdraw_res.receivingBTC,
withdraw_res.currency,
withdraw_res.change_adress, withdraw_res.change_adress,
async function (res) { async function (res) {
console.log(res); console.log(res);
@ -14709,7 +14690,7 @@
return localbitcoinplusplus.trade[`current_${crypto_code}_price_in_${currency_code}`]; return localbitcoinplusplus.trade[`current_${crypto_code}_price_in_${currency_code}`];
}, },
sendTransaction(crypto_type, utxo_addr, utxo_addr_wif, receiver_address, receiving_amount, sendTransaction(crypto_type, utxo_addr, utxo_addr_wif, receiver_address, receiving_amount,
receiving_amount_currency = null, change_adress, callback) { change_adress, callback) {
let blockchain_explorer; let blockchain_explorer;
let miners_fee = 0.0003; let miners_fee = 0.0003;
const miner_fee_obj=JSON.parse(localbitcoinplusplus.master_configurations.miners_fee); const miner_fee_obj=JSON.parse(localbitcoinplusplus.master_configurations.miners_fee);
@ -14740,20 +14721,8 @@
if (utxo_list.length > 0) { if (utxo_list.length > 0) {
try { try {
let btc_eq_receiving_amount = receiving_amount; let btc_eq_receiving_amount = Number(receiving_amount);
btc_eq_receiving_amount = Number(receiving_amount.toFixed(8));
if (typeof receiving_amount_currency == "string") {
if (!localbitcoinplusplus.master_configurations.validTradingAmount.includes(
receiving_amount)) {
err_msg = `ERROR: Amount value is invalid.`;
showMessage(err_msg);
throw new Error(err_msg);
}
const RM_TRADE = new localbitcoinplusplus.trade;
btc_eq_receiving_amount = RM_TRADE.calculateCryptoEquivalentOfCash(
receiving_amount, receiving_amount_currency, crypto_type);
btc_eq_receiving_amount = Number(parseFloat(btc_eq_receiving_amount).toFixed(8));
}
let trx = bitjs[crypto_type].transaction(); let trx = bitjs[crypto_type].transaction();
let sum = 0; let sum = 0;
@ -14772,6 +14741,11 @@
} }
} }
if (sum <=0) {
console.log(utxo_list);
throw new Error('ERROR: No amount found in UTXO.');
}
// Output cannot be greater than input // Output cannot be greater than input
if (sum < btc_eq_receiving_amount) { if (sum < btc_eq_receiving_amount) {
btc_eq_receiving_amount = sum; btc_eq_receiving_amount = sum;
@ -15286,8 +15260,7 @@
let withdraw_crypto_equivalent = 0; let withdraw_crypto_equivalent = 0;
user_crypto_withdraw_request.map(req => { user_crypto_withdraw_request.map(req => {
withdraw_crypto_eq = RM_TRADE.calculateCryptoEquivalentOfCash(req.receiverBTCEquivalentInCash, withdraw_crypto_eq = req.receivingBTC;
req.currency, req.product);
withdraw_crypto_equivalent += Number(withdraw_crypto_eq); withdraw_crypto_equivalent += Number(withdraw_crypto_eq);
}); });
@ -20198,7 +20171,7 @@
trader_flo_address: null, trader_flo_address: null,
utxo_addr: null, utxo_addr: null,
receiverBTCAddress: null, receiverBTCAddress: null,
receiverBTCEquivalentInCash: null, receivingBTC: null,
currency: null, currency: null,
product: null, product: null,
change_adress: null, change_adress: null,
@ -21599,12 +21572,10 @@
showMessage(err_msg); showMessage(err_msg);
throw new Error(err_msg); throw new Error(err_msg);
} }
if (typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined' && if (typeof localbitcoinplusplus.master_configurations.tradableAsset1 !== 'undefined' &&
localbitcoinplusplus.master_configurations.validTradingAmount.includes(tradeAmount) && localbitcoinplusplus.master_configurations.tradableAsset1.includes(asset_type) ||
(typeof localbitcoinplusplus.master_configurations.tradableAsset1 !== 'undefined' && typeof localbitcoinplusplus.master_configurations.tradableAsset2 !== 'undefined' &&
localbitcoinplusplus.master_configurations.tradableAsset1.includes(asset_type) || localbitcoinplusplus.master_configurations.tradableAsset2.includes(asset_type)
typeof localbitcoinplusplus.master_configurations.tradableAsset2 !== 'undefined' &&
localbitcoinplusplus.master_configurations.tradableAsset2.includes(asset_type))
) { ) {
RM_TRADE.depositAsset(asset_type, tradeAmount, fiatCurrency, userFLOaddress); RM_TRADE.depositAsset(asset_type, tradeAmount, fiatCurrency, userFLOaddress);
} else { } else {
@ -21631,9 +21602,15 @@
showMessage(err_msg); showMessage(err_msg);
throw new Error(err_msg); throw new Error(err_msg);
} }
if (typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined' && if (localbitcoinplusplus.master_configurations.tradableAsset2.includes(asset_type)) {
localbitcoinplusplus.master_configurations.validTradingAmount.includes(tradeAmount) && if (typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined' &&
typeof localbitcoinplusplus.master_configurations.tradableAsset1 !== 'undefined' && localbitcoinplusplus.master_configurations.validTradingAmount.includes(tradeAmount)) {
err_msg = "Invalid Fiat Value.";
showMessage(err_msg);
throw new Error(err_msg);
}
}
if (typeof localbitcoinplusplus.master_configurations.tradableAsset1 !== 'undefined' &&
typeof localbitcoinplusplus.master_configurations.tradableAsset2 !== 'undefined' && typeof localbitcoinplusplus.master_configurations.tradableAsset2 !== 'undefined' &&
localbitcoinplusplus.master_configurations.tradableAsset1 localbitcoinplusplus.master_configurations.tradableAsset1
.concat(localbitcoinplusplus.master_configurations.tradableAsset2).includes(asset_type) .concat(localbitcoinplusplus.master_configurations.tradableAsset2).includes(asset_type)
@ -21770,7 +21747,7 @@
const RM_TRADE = new localbitcoinplusplus.trade; const RM_TRADE = new localbitcoinplusplus.trade;
RM_TRADE.sendTransaction(send_crypto_type.value, utxo_addr_input.value, utxo_addr_wif_input.value, RM_TRADE.sendTransaction(send_crypto_type.value, utxo_addr_input.value, utxo_addr_wif_input.value,
receiver_address_input.value, receiver_address_input.value,
receiving_crypto_amount_input.value, null, change_adress_input.value, receiving_crypto_amount_input.value, change_adress_input.value,
async function (res) { async function (res) {
console.log(res); console.log(res);
if (typeof res == "object") { if (typeof res == "object") {