completed sell order function

This commit is contained in:
Abhishek Sinha 2018-12-10 11:48:07 +05:30
parent 869a3c5e4d
commit 6f166e352b

View File

@ -7856,27 +7856,45 @@
switch (method) { switch (method) {
case "trade_buy": case "trade_buy":
localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) { localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) {
if (is_valid_request !== true) { if (is_valid_request !== true) {
return false; return false;
} }
request.response = localbitcoinplusplus.trade.prototype.trade_buy.call(this, ...request.params, request.response = localbitcoinplusplus.trade.prototype.trade_buy.call(this,
function (supernode_signed_res) { ...request.params,
if (typeof supernode_signed_res == "object") { function (supernode_signed_res) {
let buy_request_response = localbitcoinplusplus.rpc.prototype.send_rpc if (typeof supernode_signed_res == "object") {
.call(this, "trade_buy_request_response", supernode_signed_res); let buy_request_response = localbitcoinplusplus.rpc.prototype
doSend(buy_request_response); .send_rpc
return true; .call(this, "trade_buy_request_response",
} supernode_signed_res);
}); doSend(buy_request_response);
return true;
}
});
}); });
break; break;
case "trade_sell": case "trade_sell":
request.response = localbitcoinplusplus.trade.prototype.trade_sell.call(this, ... localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) {
request.params, if (is_valid_request !== true) {
function () { return false;
//localbitcoinplusplus.trade.prototype.match_trade.call(this); }
}); console.info("i am supernode");
request.response = localbitcoinplusplus.trade.prototype.trade_sell.call(
this, ...request.params,
function (supernode_signed_res) {
if (typeof supernode_signed_res == "object") {
let sell_request_response = localbitcoinplusplus.rpc.prototype
.send_rpc
.call(this, "trade_sell_request_response",
supernode_signed_res);
doSend(sell_request_response);
return true;
}
}
);
});
break; break;
case "broadcastBlockDataToAll": case "broadcastBlockDataToAll":
// TODO: Make a separate class for syncing // TODO: Make a separate class for syncing
@ -8272,7 +8290,7 @@
} }
let placeNewOrder = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, this let placeNewOrder = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, this
.rpc_job, newOrderDataObj); .rpc_job, newOrderDataObj);
return placeNewOrder; return placeNewOrder;
} else if (typeof is_valid_order == "object") { } else if (typeof is_valid_order == "object") {
@ -8306,16 +8324,20 @@
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 = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash(buy_price_btc); let eqBTC = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash(
buy_price_btc);
if (!isNaN(eqBTC) && typeof eqBTC == "number") { if (!isNaN(eqBTC) && typeof eqBTC == "number") {
eqBTC = parseFloat(eqBTC); eqBTC = parseFloat(eqBTC);
let res_btc; let res_btc;
readDB("btc_balances", params.trader_flo_address, function (btc_balances_response) { readDB("btc_balances", params.trader_flo_address, function (
if (typeof btc_balances_response == "object" && typeof btc_balances_response.btc_balance == "number" btc_balances_response) {
&& !isNaN(btc_balances_response.btc_balance)) { if (typeof btc_balances_response == "object" && typeof btc_balances_response
btc_balances_response.btc_balance = parseFloat(btc_balances_response.btc_balance) + eqBTC; .btc_balance == "number" &&
!isNaN(btc_balances_response.btc_balance)) {
btc_balances_response.btc_balance = parseFloat(
btc_balances_response.btc_balance) + eqBTC;
res_btc = btc_balances_response; res_btc = btc_balances_response;
} else { } else {
// The user bought BTC for first time // The user bought BTC for first time
@ -8327,44 +8349,48 @@
// supernode data query // supernode data query
readDB('localbitcoinUser', '00-01', function (user_data) { readDB('localbitcoinUser', '00-01', function (user_data) {
if (typeof user_data == "object" && typeof user_data.myLocalFLOPrivateKey == if (typeof user_data == "object" && typeof user_data.myLocalFLOPrivateKey ==
"string" && user_data.myLocalFLOPrivateKey.length > 0) { "string" && user_data.myLocalFLOPrivateKey.length >
try { 0) {
// Increase BTC balance of buyer with extra eqBTC amount of BTC try {
updateinDB("btc_balances", res_btc, params.trader_flo_address); // Increase BTC balance of buyer with extra eqBTC amount of BTC
// Descrease INR balance of user in cash table updateinDB("btc_balances", res_btc, params.trader_flo_address);
let new_cash_balance = buyer_cash_balance - buy_price_btc; // Descrease INR balance of user in cash table
let res_cash = { let new_cash_balance = buyer_cash_balance -
trader_flo_address: params.trader_flo_address, buy_price_btc;
cash_balance: new_cash_balance let res_cash = {
} trader_flo_address: params.trader_flo_address,
cash_balance: new_cash_balance
updateinDB("cash_balances", res_cash, params.trader_flo_address);
// Add buy oder
params['id'] = +new Date();
let hashed_data = Crypto.SHA256(JSON.stringify(params));
// Signing of the data by Supernode
let signed_data = localbitcoinplusplus.wallets.prototype.sign(hashed_data, user_data.myLocalFLOPrivateKey);
params["data_hash"] = hashed_data;
params["supernode_sign"] = signed_data;
params["supernodePubKey"] = user_data.myLocalFLOPublicKey;
params["status"] = 1;
addDB("buyOrders", params);
let response_for_client = {
"buyOrders_data": params,
"btc_balances_data": res_btc,
"cash_balances_data": res_cash
};
// Send data for further action
callback(response_for_client);
} catch (error) {
console.error(error);
callback(false);
} }
updateinDB("cash_balances", res_cash, params.trader_flo_address);
// Add buy oder
params['id'] = +new Date();
let hashed_data = Crypto.SHA256(JSON.stringify(
params));
// Signing of the data by Supernode
let signed_data = localbitcoinplusplus.wallets.prototype
.sign(hashed_data, user_data.myLocalFLOPrivateKey);
params["data_hash"] = hashed_data;
params["supernode_sign"] = signed_data;
params["supernodePubKey"] = user_data.myLocalFLOPublicKey;
params["status"] = 1;
addDB("buyOrders", params);
let response_for_client = {
"buyOrders_data": params,
"btc_balances_data": res_btc,
"cash_balances_data": res_cash
};
// Send data for further action
callback(response_for_client);
} catch (error) {
console.error(error);
callback(false);
}
} }
}); });
callback(false); callback(false);
@ -8405,28 +8431,64 @@
} }
// Increase INR balance of seller // Increase INR balance of seller
readDB("cash_balances", params.trader_flo_address, function (res) { let res_cash;
if (typeof res == "object" && typeof res.cash_balance == "number" && readDB("cash_balances", params.trader_flo_address, function (res_cash) {
!isNaN(res.cash_balance)) { if (typeof res_cash == "object" && typeof res_cash.cash_balance ==
res.cash_balance = parseFloat(res.cash_balance) + "number" &&
!isNaN(res_cash.cash_balance)) {
res_cash.cash_balance = parseFloat(res_cash.cash_balance) +
sell_price_in_inr; sell_price_in_inr;
} else { } else {
// User got cash for the first time // User got cash for the first time
let res = { res_cash = {
trader_flo_address: params.trader_flo_address, trader_flo_address: params.trader_flo_address,
cash_balance: sell_price_in_inr cash_balance: sell_price_in_inr
} }
} }
updateinDB("cash_balances", res, params.trader_flo_address);
// Decrease BTC balance of seller // supernode data query
res.btc_balance = seller_btc_balance - eqBTC; readDB('localbitcoinUser', '00-01', function (user_data) {
updateinDB("btc_balances", res, params.trader_flo_address); if (typeof user_data == "object" && typeof user_data.myLocalFLOPrivateKey ==
"string" && user_data.myLocalFLOPrivateKey.length > 0) {
//Add cash balance updateinDB("cash_balances", res_cash, params.trader_flo_address);
params['rand_id'] = +new Date();
addDB("sellOrders", params); // Decrease BTC balance of seller
callback(); let new_btc_balance = seller_btc_balance - eqBTC;
let res_btc = {
trader_flo_address: params.trader_flo_address,
btc_balance: new_btc_balance
}
updateinDB("btc_balances", res_btc, params.trader_flo_address);
//Add cash balance
params['id'] = +new Date();
let hashed_data = Crypto.SHA256(JSON.stringify(
params));
// Signing of the data by Supernode
let signed_data = localbitcoinplusplus.wallets.prototype
.sign(hashed_data, user_data.myLocalFLOPrivateKey);
params["data_hash"] = hashed_data;
params["supernode_sign"] = signed_data;
params["supernodePubKey"] = user_data.myLocalFLOPublicKey;
params["status"] = 1;
console.log(params);
addDB("sellOrders", params);
let response_for_client = {
"sellOrders_data": params,
"btc_balances_data": res_btc,
"cash_balances_data": res_cash
};
callback(response_for_client);
}
});
callback(false);
}); });
} else { } else {
throw new Error("Failed to fetch cuurent BTC price."); throw new Error("Failed to fetch cuurent BTC price.");
@ -8809,32 +8871,59 @@
JSON.stringify(res_obj)); JSON.stringify(res_obj));
break; break;
case "trade_buy_request_response": case "trade_buy_request_response":
console.log(res_obj.params); console.log(res_obj.params);
if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object" if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object" &&
&& typeof res_obj.params[0].btc_balances_data == "object" typeof res_obj.params[0].btc_balances_data == "object" &&
&& typeof res_obj.params[0].cash_balances_data == "object" typeof res_obj.params[0].cash_balances_data == "object" &&
&& typeof res_obj.params[0].buyOrders_data == "object" typeof res_obj.params[0].buyOrders_data == "object"
) { ) {
let resp = res_obj.params[0]; let resp = res_obj.params[0];
if (typeof localbitcoinplusplus.master_configurations.supernodesPubKeys == "object" if (typeof localbitcoinplusplus.master_configurations.supernodesPubKeys == "object" &&
&& localbitcoinplusplus.master_configurations.supernodesPubKeys.includes(resp.buyOrders_data.supernodePubKey)) { localbitcoinplusplus.master_configurations.supernodesPubKeys.includes(resp.buyOrders_data
.supernodePubKey)) {
let isDataSignedBySuperNode = localbitcoinplusplus.wallets.prototype let isDataSignedBySuperNode = localbitcoinplusplus.wallets.prototype
.verify(resp.buyOrders_data.data_hash, resp.buyOrders_data.supernode_sign, resp.buyOrders_data.supernodePubKey); .verify(resp.buyOrders_data.data_hash, resp.buyOrders_data.supernode_sign,
resp.buyOrders_data.supernodePubKey);
if (isDataSignedBySuperNode === true) { if (isDataSignedBySuperNode === true) {
// Increase BTC balance of buyer with extra eqBTC amount of BTC // Increase BTC balance of buyer with extra eqBTC amount of BTC
updateinDB("btc_balances", resp.btc_balances_data, resp.btc_balances_data.trader_flo_address); updateinDB("btc_balances", resp.btc_balances_data, resp.btc_balances_data.trader_flo_address);
// Descrease INR balance of user in cash table // Descrease INR balance of user in cash table
updateinDB("cash_balances", resp.cash_balances_data, resp.cash_balances_data.trader_flo_address); updateinDB("cash_balances", resp.cash_balances_data, resp.cash_balances_data
.trader_flo_address);
// Add buy order // Add buy order
addDB("buyOrders", resp.buyOrders_data); addDB("buyOrders", resp.buyOrders_data);
} }
} }
} }
break; break;
case "trade_sell": case "trade_sell":
response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this, response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this,
JSON.stringify(res_obj)); JSON.stringify(res_obj));
//doSend(response_from_sever); // send response to client break;
case "trade_sell_request_response":
if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object" &&
typeof res_obj.params[0].btc_balances_data == "object" &&
typeof res_obj.params[0].cash_balances_data == "object" &&
typeof res_obj.params[0].sellOrders_data == "object"
) {
let resp = res_obj.params[0];
if (typeof localbitcoinplusplus.master_configurations.supernodesPubKeys == "object" &&
localbitcoinplusplus.master_configurations.supernodesPubKeys.includes(resp.sellOrders_data
.supernodePubKey)) {
let isDataSignedBySuperNode = localbitcoinplusplus.wallets.prototype
.verify(resp.sellOrders_data.data_hash, resp.sellOrders_data.supernode_sign,
resp.sellOrders_data.supernodePubKey);
if (isDataSignedBySuperNode === true) {
// Increase BTC balance of buyer with extra eqBTC amount of BTC
updateinDB("btc_balances", resp.btc_balances_data, resp.btc_balances_data.trader_flo_address);
// Descrease INR balance of user in cash table
updateinDB("cash_balances", resp.cash_balances_data, resp.cash_balances_data
.trader_flo_address);
// Add buy order
addDB("sellOrders", resp.sellOrders_data);
}
}
}
break; break;
case "broadcastBlockDataToAll": case "broadcastBlockDataToAll":
response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this, response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this,
@ -9085,7 +9174,7 @@
if (request.result) { if (request.result) {
callback(request.result); callback(request.result);
} else { } else {
console.error("Data couldn't be found in your database!"); console.info("Data couldn't be found in your database!");
callback(); callback();
} }
}; };
@ -9115,7 +9204,7 @@
var objectStore = db.transaction(tablename).objectStore(tablename); var objectStore = db.transaction(tablename).objectStore(tablename);
let response = []; let response = [];
objectStore.openCursor().onerror = function (event) { objectStore.openCursor().onerror = function (event) {
console.err("Error fetching data"); console.error("Error fetching data");
}; };
objectStore.openCursor().onsuccess = function (event) { objectStore.openCursor().onsuccess = function (event) {
let cursor = event.target.result; let cursor = event.target.result;