added withdraw inr function

This commit is contained in:
Abhishek Sinha 2018-12-05 22:46:52 +05:30
parent 8e39cf6a6c
commit bdc14379ff

View File

@ -7814,6 +7814,7 @@
var request = JSON.parse(request); var request = JSON.parse(request);
var params = request.params[0]; var params = request.params[0];
var method = request.method; var method = request.method;
console.log(request);
if (typeof params == "object" && typeof method == "string") { if (typeof params == "object" && typeof method == "string") {
if (typeof params.trader_flo_address != "string" && params.trader_flo_address.length < 1) { if (typeof params.trader_flo_address != "string" && params.trader_flo_address.length < 1) {
@ -7824,18 +7825,18 @@
/** CHECK HERE IF USER IS INDULGED IN ANY MORE TRADE. /** CHECK HERE IF USER IS INDULGED IN ANY MORE TRADE.
IF TRUE RETURN ERROR */ IF TRUE RETURN ERROR */
readAllDB("deposit", function(res) { // readAllDB("deposit", function(res) {
if (typeof res == "object" && res.length>0) { // if (typeof res == "object" && res.length>0) {
let canUserTrade = res.map(function(user) { // let canUserTrade = res.map(function(user) {
return respective_trader_id == user.trader_flo_address; // return respective_trader_id == user.trader_flo_address;
}); // });
if (canUserTrade.includes(true)) { // if (canUserTrade.includes(true)) {
request.response = `Trader id ${respective_trader_id} is not clear for trade currently. // request.response = `Trader id ${respective_trader_id} is not clear for trade currently.
You must finish your previos pending orders to qualify again to trade.`; // You must finish your previos pending orders to qualify again to trade.`;
return false; // return false;
} // }
} // }
}); // });
switch (method) { switch (method) {
case "trade_buy": case "trade_buy":
@ -7893,11 +7894,16 @@
/*************************************************** /***************************************************
GET EQUIVALENT BTC HERE IN TERMS OF ORDERED INR I.E 10K, 50K... GET EQUIVALENT BTC HERE IN TERMS OF ORDERED INR I.E 10K, 50K...
******************************************************/ ******************************************************/
params.bitcoinToBePaid = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash.call(params.depositing_amount); console.log(params.depositing_amount);
console.log(localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash(params.depositing_amount));
params.bitcoinToBePaid = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash(params.depositing_amount);
let receivedTradeInfo = {...params}; let receivedTradeInfo = {...params};
try { try {
console.log(receivedTradeInfo);
addDB("deposit", receivedTradeInfo); addDB("deposit", receivedTradeInfo);
} catch (error) { } catch (error) {
throw new Error(error); throw new Error(error);
@ -7925,29 +7931,139 @@
} }
// YOU NEED TO DETERMINE A BANK ACCOUNT HERE // YOU NEED TO DETERMINE A BANK ACCOUNT HERE
let bank_account = try {
'Name: John Doe, State Bank of India, Branch: Ashok Nagar Ranchi'; readDBbyIndex("withdraw_cash", "status", 1, function(withdrawers_list) {
let requester_data ={ withdrawers_list.map(function(withdrawer) {
msg: `Plese send the money to following bank address: "${bank_account}"`, if(withdrawer.withdraw_amount==params.depositing_amount) {
deposit_db_object: receivedTradeInfo let withdrawer_bank_account = withdrawer.receivinAddress;
}; let requester_data ={
return { msg: `Plese send the money to following bank address: "${withdrawer_bank_account}"`,
error: false, deposit_db_object: receivedTradeInfo
method: "deposit_asset_request_response", };
data: requester_data return {
}; error: false,
method: "deposit_asset_request_response",
data: requester_data
};
} else {
// Return error to the requester
return {
error: true,
method: "deposit_asset_request_response",
data: "Deposit request failed: We could not find a withdrawer."
};
}
});
});
} catch (error) {
return {
error: true,
method: "deposit_asset_request_response",
data: "Deposit request failed: We could not find a withdrawer. Come again later."
};
}
} }
// Return error to the requester
return {
error: true,
method: "deposit_asset_request_response",
data: "Deposit request failed."
};
} else { } else {
console.log("oops"); console.log("deposit asset request error");
} }
break; break;
case "withdraw_request_method":
if (typeof params.product !== "undefined"
&& localbitcoinplusplus.master_configurations.validAssets.includes(params.product)
&& 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
) {
params.id = + new Date();
params.status = 1;
if (params.product == "BTC") {
// Check how much Bitcoins the user can withdraw
readDB("btc_balances", params.trader_flo_address, function(btc_balance_res) {
if (typeof btc_balance_res == "object" && typeof btc_balance_res.trader_flo_address == "string"
&& typeof btc_balance_res.btc_balance == "number" && btc_balance_res > 0) {
let withdrawer_btc_balance = parseFloat(btc_balance_res.btc_balance);
let withdrawing_btc_amount = parseFloat(params.withdrawing_amount);
let eqBTC = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash(withdrawing_btc_amount);
if (withdrawer_btc_balance>0 && withdrawing_btc_amount>0 && eqBTC>0 && eqBTC<=withdrawer_btc_balance) {
// Now details of Bitcoins can be sent to withdrawer
// Your code here ...
// send him Bitcoins from system's reserve
} else {
// Return error to the requester
return {
error: true,
method: "withdrawal_request_response",
data: "Withdrawal request failed: You are trying to withdraw more Bitcoins than you have."
};
}
} else {
// Return error to the requester
return {
error: true,
method: "withdrawal_request_response",
data: `Withdrawal request failed: You don't seem to have any Bitcoin balance in the system yet.
Please buy some Bitcoins to withdraw.`
};
}
});
} else if(params.product == "INR") {
// 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
readDB("cash_balances", params.trader_flo_address, 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>=withdrawer_cash_balance) {
// Add it to cash withdrawal table
let withdraw_request_db_object = {
id: + new Date(),
trader_flo_address: params.trader_flo_address,
withdraw_amount: params.withdrawing_cash_amount,
receivinAddress: bank_details,
status: 1 // withdraw request called
}
try {
addDB("withdraw_cash", withdraw_request_db_object);
return {
error: false,
method: "withdrawal_request_response",
data: withdraw_request_db_object
};
} catch (error) {
console.log(error);
return {
error: true,
method: "withdrawal_request_response",
data: "Withdrawal request failed: "+error
};
}
} else {
// Return error to the requester
return {
error: true,
method: "withdrawal_request_response",
data: "Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."
};
}
}
});
} else {
console.log("withdraw request error");
}
}
break;
default: default:
alert("Unknown method called for execution."); alert("Unknown method called for execution.");
@ -8158,7 +8274,7 @@
throw new Error("Insufficient balance."); throw new Error("Insufficient balance.");
} }
// calculate equivalent BTC for x amount of Cash // calculate equivalent BTC for x amount of Cash
let eqBTC = this.calculateBTCEquivalentOfCash("INR", buy_price_btc); let eqBTC = this.calculateBTCEquivalentOfCash(buy_price_btc);
if (!isNaN(eqBTC)) { if (!isNaN(eqBTC)) {
let eqBTC = parseFloat(eqBTC); let eqBTC = parseFloat(eqBTC);
@ -8210,7 +8326,7 @@
&& typeof res.btc_balance == "number" && res.btc_balance>0) { && typeof res.btc_balance == "number" && res.btc_balance>0) {
let seller_btc_balance = parseFloat(res.btc_balance); let seller_btc_balance = parseFloat(res.btc_balance);
let sell_price_in_inr = parseFloat(params.buy_price); let sell_price_in_inr = parseFloat(params.buy_price);
let eqBTC = this.calculateBTCEquivalentOfCash("INR", sell_price_in_inr); let eqBTC = this.calculateBTCEquivalentOfCash(sell_price_in_inr);
console.log(eqBTC); console.log(eqBTC);
if (!isNaN(eqBTC)) { if (!isNaN(eqBTC)) {
@ -8285,7 +8401,7 @@
doSend(deposit_request); doSend(deposit_request);
callback(deposit_request); callback(deposit_request);
}, },
withdrawAsset(assetType, amount, userFLOaddress, callback){ withdrawAsset(assetType, amount, receivinAddress, userFLOaddress, callback) {
if (typeof localbitcoinplusplus.master_configurations.validAssets !== 'undefined' && ! if (typeof localbitcoinplusplus.master_configurations.validAssets !== 'undefined' && !
localbitcoinplusplus.master_configurations.validAssets.includes(assetType)) { localbitcoinplusplus.master_configurations.validAssets.includes(assetType)) {
throw new Error("Invalid asset error"); throw new Error("Invalid asset error");
@ -8293,28 +8409,31 @@
throw new Error("Invalid amount error."); throw new Error("Invalid amount error.");
} else if (userFLOaddress.length < 0) { } else if (userFLOaddress.length < 0) {
throw new Error("User address required."); throw new Error("User address required.");
} else if (receivinAddress.trim() == "") {
throw new Error("You must provide a vaid Bitcoin address to receive funds.");
} }
let withdraw_request_object = { let withdraw_request_object = {
trader_flo_address: userFLOaddress+'_'+Math.floor(Math.random(1, 100) * 100), trader_flo_address: userFLOaddress,
depositing_amount: amount, receivinAddress: receivinAddress,
depositor_key_signature: null, withdrawing_amount: amount,
depositor_public_key: null, wihdrawer_key_signature: null,
wihdrawer_public_key: null,
operation_type: "withdraw", operation_type: "withdraw",
order_validator_public_key: null, order_validator_public_key: null,
product: assetType product: assetType
} }
let withdraw_request = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, let withdraw_request = localbitcoinplusplus.rpc.prototype.send_rpc.call(this,
"withdraw_request_object", withdraw_request_object); "withdraw_request_method", withdraw_request_object);
console.log(deposit_request); //doSend(withdraw_request);
console.log(withdraw_request);
doSend(withdraw_request); callback(withdraw_request);
}, },
calculateBTCEquivalentOfCash(btc_buy_price) { calculateBTCEquivalentOfCash(btc_buy_price) {
console.log(this);
if (localbitcoinplusplus.master_configurations.validTradingAmount.includes(btc_buy_price)) { if (localbitcoinplusplus.master_configurations.validTradingAmount.includes(btc_buy_price)) {
let current_btc_price = this.get_current_btc_price_in_fiat(); let current_btc_price = localbitcoinplusplus.trade.prototype.get_current_btc_price_in_fiat.call();
if (current_btc_price > 0) { if (current_btc_price > 0) {
return parseFloat(btc_buy_price/current_btc_price); return parseFloat(btc_buy_price/current_btc_price);
} }
@ -8647,16 +8766,23 @@
doSend(JSON.stringify(response_from_sever)); // send response to client doSend(JSON.stringify(response_from_sever)); // send response to client
case "deposit_asset_request_response": case "deposit_asset_request_response":
console.log(res_obj); console.log(res_obj);
if (!res_obj.error && typeof res_obj.data !== "undefined" && typeof res_obj.data.msg !== "undefined" && res_obj.data.msg.length if (typeof res_obj.error !== "undefined" && !res_obj.error && typeof res_obj.data !== "undefined" && typeof res_obj.data.msg !== "undefined" && res_obj.data.msg.length
&& typeof res_obj.data.deposit_db_object.trader_flo_address !== 'undefined' && typeof res_obj.data.deposit_db_object.trader_flo_address !== 'undefined'
&& res_obj.data.deposit_db_object.trader_flo_address.length>0 ) { && res_obj.data.deposit_db_object.trader_flo_address.length>0 ) {
addDB('deposit', res_obj.data.deposit_db_object, res_obj.data.deposit_db_object.trader_flo_address); addDB('deposit', res_obj.data.deposit_db_object);
alert(res_obj.data.msg);
} }
else if (typeof res_obj.params[0] == "object") { break;
addDB('deposit', res_obj.params[0], res_obj.params[0].trader_flo_address); case "withdraw_request_method":
} response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this, JSON.stringify(res_obj));
//console.log(typeof res_obj.params[0] == "object"); console.log(response_from_sever);
doSend(JSON.stringify(response_from_sever)); // send response to client
break;
case "withdrawal_request_response":
console.log(res_obj);
// if (!res_obj.error && typeof res_obj.data == "object") {
// addDB('withdraw_cash', res_obj.data);
// }
break; break;
default: default:
break; break;
@ -8774,6 +8900,15 @@
status: 0 status: 0
} }
const withdraw_cash = {
id: "",
trader_flo_address: null,
withdraw_amount: null,
receivinAddress: null,
status: null,
depositor_found_at: null
}
const btc_balances = { const btc_balances = {
trader_flo_address: null, trader_flo_address: null,
btc_balance: null btc_balance: null
@ -8793,7 +8928,7 @@
var db; var db;
const DBName = "localbitcoinDB"; const DBName = "localbitcoinDB";
var request = window.indexedDB.open(DBName, 1); var request = window.indexedDB.open(DBName, 2);
request.onerror = function (event) { request.onerror = function (event) {
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox //https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
@ -8830,6 +8965,10 @@
var objectStore = db.createObjectStore("deposit", {keyPath: 'id'}); var objectStore = db.createObjectStore("deposit", {keyPath: 'id'});
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false }); objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false });
} }
if (!db.objectStoreNames.contains('withdraw_cash')) {
var objectStore = db.createObjectStore("withdraw_cash", {keyPath: 'id'});
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: true });
}
if (!db.objectStoreNames.contains('btc_balances')) { if (!db.objectStoreNames.contains('btc_balances')) {
var objectStore = db.createObjectStore("btc_balances", {keyPath: 'trader_flo_address'}); var objectStore = db.createObjectStore("btc_balances", {keyPath: 'trader_flo_address'});
} }
@ -9226,6 +9365,45 @@
throw new Error("Error while depositing your address."); throw new Error("Error while depositing your address.");
} }
}); });
withdrawAssetButton.addEventListener('click', function(params) {
let receivinAddress = prompt("Please enter a Bitcoin address or full bank details.");
if (receivinAddress.trim == "") {
alert("You must specify either a Bitcoin address to withdraw Bitcoin or your bank detail to withdraw cash.");
return false;
}
let asset_type = assetTypeInput.value;
let tradeAmount = parseFloat(tradeAmountSelect.value);
if (typeof userFLOaddress == undefined || userFLOaddress.trim().length < 1) {
throw new Error("Invalid or empty user FLO address.");
}
if (typeof localbitcoinplusplus.master_configurations.validTradingAmount !== 'undefined' &&
localbitcoinplusplus.master_configurations.validTradingAmount.includes(tradeAmount) &&
typeof localbitcoinplusplus.master_configurations.validAssets !== 'undefined' &&
localbitcoinplusplus.master_configurations.validAssets.includes(asset_type)) {
RM_TRADE.withdrawAsset(asset_type, tradeAmount, receivinAddress, userFLOaddress, function (server_response) {
try {
let withdrawAssetResponseObj = JSON.parse(server_response);
if (typeof withdrawAssetResponseObj == "object" && typeof withdrawAssetResponseObj.params == "object"
&& withdrawAssetResponseObj.params.length>0 && withdrawAssetResponseObj.method == "withdraw_request_method"
) {
console.log(withdrawAssetResponseObj.params[0]);
//updateinDB('withdraw_cash', withdrawAssetResponseObj.params[0], userFLOaddress);
if (withdrawAssetResponseObj.params[0].trader_flo_address == userFLOaddress) {
// inform user
}
}
} catch (error) {
throw new Error(error);
}
});
} else {
throw new Error("Error while depositing your address.");
}
});
} }
</script> </script>