improved db structure
This commit is contained in:
parent
114aadc4c4
commit
69271aba7b
@ -7885,6 +7885,7 @@
|
|||||||
// Need to do: Super Node saves the private keys and sends the BTC address to the requester
|
// Need to do: Super Node saves the private keys and sends the BTC address to the requester
|
||||||
// THIS IS VERY DANGEROUS STEP BCOZ BTC PRIVATE KEY DATA CAN BE LEAKED HERE IF ANYTHING GOES WRONG
|
// THIS IS VERY DANGEROUS STEP BCOZ BTC PRIVATE KEY DATA CAN BE LEAKED HERE IF ANYTHING GOES WRONG
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
params.id = + new Date();
|
||||||
params.status = 1;
|
params.status = 1;
|
||||||
params.btc_private_key = generate_btc_keys_for_requester.privateKeyWIF;
|
params.btc_private_key = generate_btc_keys_for_requester.privateKeyWIF;
|
||||||
params.btc_address = generate_btc_keys_for_requester.address;
|
params.btc_address = generate_btc_keys_for_requester.address;
|
||||||
@ -7893,7 +7894,7 @@
|
|||||||
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);
|
params.bitcoinToBePaid = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash.call(params.depositing_amount);
|
||||||
|
|
||||||
let receivedTradeInfo = {...params};
|
let receivedTradeInfo = {...params};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -7913,6 +7914,7 @@
|
|||||||
data: requester_data
|
data: requester_data
|
||||||
};
|
};
|
||||||
} else if (params.product == "INR") {
|
} else if (params.product == "INR") {
|
||||||
|
params.id = + new Date();
|
||||||
params.status = 1;
|
params.status = 1;
|
||||||
let receivedTradeInfo = {...params};
|
let receivedTradeInfo = {...params};
|
||||||
|
|
||||||
@ -8065,7 +8067,7 @@
|
|||||||
if (is_valid_order === true) {
|
if (is_valid_order === true) {
|
||||||
|
|
||||||
this.rpc_job = 'trade_' + this.order_type;
|
this.rpc_job = 'trade_' + this.order_type;
|
||||||
|
|
||||||
let placeNewOrder = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, this.rpc_job, {
|
let placeNewOrder = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, this.rpc_job, {
|
||||||
"order_type": this.order_type,
|
"order_type": this.order_type,
|
||||||
"trader_flo_address": this.user_flo_address,
|
"trader_flo_address": this.user_flo_address,
|
||||||
@ -8184,7 +8186,7 @@
|
|||||||
// "receiving_address": this.receiving_address
|
// "receiving_address": this.receiving_address
|
||||||
|
|
||||||
//Check buyer's INR balance
|
//Check buyer's INR balance
|
||||||
readDBbyIndex("cash_balances", "trader_flo_address", params.trader_flo_address, function(res) {
|
readDB("cash_balances", "trader_flo_address", params.trader_flo_address, function(res) {
|
||||||
if(!isNaN(res.cash_balance)) {
|
if(!isNaN(res.cash_balance)) {
|
||||||
let buyer_cash_balance = parseFloat(res.cash_balance);
|
let buyer_cash_balance = parseFloat(res.cash_balance);
|
||||||
let buy_price_btc = parseFloat(params.buy_price);
|
let buy_price_btc = parseFloat(params.buy_price);
|
||||||
@ -8202,7 +8204,7 @@
|
|||||||
updateinDB("cash_balances", res, params.trader_flo_address);
|
updateinDB("cash_balances", res, params.trader_flo_address);
|
||||||
|
|
||||||
// Increase BTC balance of buyer with extra eqBTC amount of BTC
|
// Increase BTC balance of buyer with extra eqBTC amount of BTC
|
||||||
readDBbyIndex("btc_balances", "trader_flo_address", params.trader_flo_address, function(res) {
|
readDB("btc_balances", "trader_flo_address", params.trader_flo_address, function(res) {
|
||||||
if (typeof res.btc_balance == "undefined" && !NaN(res.btc_balance)) {
|
if (typeof res.btc_balance == "undefined" && !NaN(res.btc_balance)) {
|
||||||
res.btc_balance = parseFloat(res.btc_balance)+eqBTC;
|
res.btc_balance = parseFloat(res.btc_balance)+eqBTC;
|
||||||
updateinDB("btc_balances", res, params.trader_flo_address);
|
updateinDB("btc_balances", res, params.trader_flo_address);
|
||||||
@ -8211,12 +8213,12 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Failed to fetch cuurent BTC price.");
|
throw new Error("Failed to fetch current BTC price.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
params['rand_id'] = Math.floor(Math.random(1, 1000) * 1000);
|
params['id'] = + new Date();
|
||||||
addDB("buyOrders", params);
|
addDB("buyOrders", params);
|
||||||
callback();
|
callback();
|
||||||
},
|
},
|
||||||
@ -8240,7 +8242,7 @@
|
|||||||
// Check BTC balance of the seller
|
// Check BTC balance of the seller
|
||||||
console.log(params.trader_flo_address);
|
console.log(params.trader_flo_address);
|
||||||
|
|
||||||
readDBbyIndex("btc_balances", "trader_flo_address", params.trader_flo_address, function(res) {
|
readDB("btc_balances", "trader_flo_address", params.trader_flo_address, function(res) {
|
||||||
if (typeof res.trader_flo_address == "string" && res.trader_flo_address.length>0
|
if (typeof res.trader_flo_address == "string" && res.trader_flo_address.length>0
|
||||||
&& 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);
|
||||||
@ -8271,7 +8273,7 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
params['rand_id'] = Math.floor(Math.random(1, 1000) * 1000);
|
params['rand_id'] = + new Date();
|
||||||
addDB("sellOrders", params);
|
addDB("sellOrders", params);
|
||||||
callback();
|
callback();
|
||||||
},
|
},
|
||||||
@ -8798,7 +8800,6 @@
|
|||||||
depositing_amount: 0,
|
depositing_amount: 0,
|
||||||
depositor_key_signature: null,
|
depositor_key_signature: null,
|
||||||
depositor_public_key: null,
|
depositor_public_key: null,
|
||||||
//currency: null,
|
|
||||||
operation_type: null,
|
operation_type: null,
|
||||||
order_validator_public_key: null,
|
order_validator_public_key: null,
|
||||||
product: null,
|
product: null,
|
||||||
@ -8807,7 +8808,6 @@
|
|||||||
|
|
||||||
const btc_balances = {
|
const btc_balances = {
|
||||||
trader_flo_address: null,
|
trader_flo_address: null,
|
||||||
btc_address: null,
|
|
||||||
btc_balance: null
|
btc_balance: null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8815,10 +8815,16 @@
|
|||||||
trader_flo_address: null,
|
trader_flo_address: null,
|
||||||
cash_balance: null
|
cash_balance: null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const system_btc_reserves = {
|
||||||
|
id: '',
|
||||||
|
btc_address: null,
|
||||||
|
balance: null
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
@ -8834,47 +8840,32 @@
|
|||||||
request.onupgradeneeded = function (event) {
|
request.onupgradeneeded = function (event) {
|
||||||
var db = event.target.result;
|
var db = event.target.result;
|
||||||
if (!db.objectStoreNames.contains('localbitcoinUser')) {
|
if (!db.objectStoreNames.contains('localbitcoinUser')) {
|
||||||
var objectStore = db.createObjectStore("localbitcoinUser", {
|
var objectStore = db.createObjectStore("localbitcoinUser", {keyPath: "id"});
|
||||||
keyPath: "id"
|
objectStore.createIndex('myLocalFLOAddress', 'myLocalFLOAddress', { unique: false });
|
||||||
});
|
|
||||||
objectStore.put(localbitcoinplusplusObj);
|
objectStore.put(localbitcoinplusplusObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!db.objectStoreNames.contains('datablocks')) {
|
if (!db.objectStoreNames.contains('datablocks')) {
|
||||||
var objectStore = db.createObjectStore("datablocks", {
|
var objectStore = db.createObjectStore("datablocks", {keyPath: "id"});
|
||||||
keyPath: "id"
|
|
||||||
});
|
|
||||||
objectStore.put(dataBlock);
|
objectStore.put(dataBlock);
|
||||||
}
|
}
|
||||||
if (!db.objectStoreNames.contains('buyOrders')) {
|
if (!db.objectStoreNames.contains('buyOrders')) {
|
||||||
var objectStore = db.createObjectStore("buyOrders", {
|
var objectStore = db.createObjectStore("buyOrders", {keyPath: 'id'});
|
||||||
keyPath: 'trader_flo_address'
|
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false });
|
||||||
});
|
|
||||||
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false, multiEntry:true });
|
|
||||||
}
|
}
|
||||||
if (!db.objectStoreNames.contains('sellOrders')) {
|
if (!db.objectStoreNames.contains('sellOrders')) {
|
||||||
var objectStore = db.createObjectStore("sellOrders", {
|
var objectStore = db.createObjectStore("sellOrders", {keyPath: 'id'});
|
||||||
keyPath: 'trader_flo_address'
|
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false });
|
||||||
});
|
|
||||||
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false, multiEntry:true });
|
|
||||||
}
|
}
|
||||||
if (!db.objectStoreNames.contains('deposit')) {
|
if (!db.objectStoreNames.contains('deposit')) {
|
||||||
var objectStore = db.createObjectStore("deposit", {
|
var objectStore = db.createObjectStore("deposit", {keyPath: 'id'});
|
||||||
keyPath: 'trader_flo_address'
|
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false });
|
||||||
});
|
|
||||||
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false, multiEntry:true });
|
|
||||||
}
|
}
|
||||||
if (!db.objectStoreNames.contains('btc_balances')) {
|
if (!db.objectStoreNames.contains('btc_balances')) {
|
||||||
var objectStore = db.createObjectStore("btc_balances", {
|
var objectStore = db.createObjectStore("btc_balances", {keyPath: 'trader_flo_address'});
|
||||||
keyPath: 'trader_flo_address'
|
|
||||||
});
|
|
||||||
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false, multiEntry:true });
|
|
||||||
}
|
}
|
||||||
if (!db.objectStoreNames.contains('cash_balances')) {
|
if (!db.objectStoreNames.contains('cash_balances')) {
|
||||||
var objectStore = db.createObjectStore("cash_balances", {
|
var objectStore = db.createObjectStore("cash_balances", {keyPath: 'trader_flo_address'});
|
||||||
keyPath: 'trader_flo_address'
|
|
||||||
});
|
|
||||||
objectStore.createIndex('trader_flo_address', 'trader_flo_address', { unique: false, multiEntry:true });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8964,33 +8955,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// function updateDeposit(trader_flo_address, updatedObject) {
|
|
||||||
// const transaction = db.transaction(['deposit'], 'readwrite');
|
|
||||||
// const objectStore = transaction.objectStore('deposit');
|
|
||||||
// objectStore.openCursor().onsuccess = function(event) {
|
|
||||||
// const cursor = event.target.result;
|
|
||||||
// if (cursor) {
|
|
||||||
// if (cursor.value.trader_flo_address === trader_flo_address) {
|
|
||||||
// const updateData = cursor.value;
|
|
||||||
|
|
||||||
// for (const key in updatedObject) {
|
|
||||||
// if (updatedObject.hasOwnProperty(key)) {
|
|
||||||
// updateData.key = updatedObject[key];
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// const request = cursor.update(updateData);
|
|
||||||
// request.onsuccess = function() {
|
|
||||||
// console.log();
|
|
||||||
// };
|
|
||||||
// };
|
|
||||||
// cursor.continue();
|
|
||||||
// } else {
|
|
||||||
// console.log('Entries displayed.');
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
function removeinDB(tablename, id) {
|
function removeinDB(tablename, id) {
|
||||||
var request = db.transaction([tablename], "readwrite")
|
var request = db.transaction([tablename], "readwrite")
|
||||||
.objectStore(tablename)
|
.objectStore(tablename)
|
||||||
@ -9355,13 +9319,13 @@
|
|||||||
removeinDB("deposit", trader_flo_address);
|
removeinDB("deposit", trader_flo_address);
|
||||||
updateinDB("deposit", res, trader_flo_address);
|
updateinDB("deposit", res, trader_flo_address);
|
||||||
|
|
||||||
let updateBalanceTableOfUser = {
|
let updateSystemBTCreserve = {
|
||||||
trader_flo_address: trader_flo_address,
|
id: + new Date(),
|
||||||
btc_address: BTCAddress,
|
btc_address: BTCAddress,
|
||||||
btc_balance: balance
|
btc_balance: balance
|
||||||
}
|
}
|
||||||
// Periodly update the BTC balance of this trader in balances table
|
// Periodly update the BTC balance of this trader in system_btc_reserves table
|
||||||
updateinDB("btc_balances", updateBalanceTableOfUser, trader_flo_address);
|
updateinDB("system_btc_reserves", updateSystemBTCreserve, BTCAddress);
|
||||||
|
|
||||||
// update balance of user
|
// update balance of user
|
||||||
readDB("localbitcoinUser", "00-01", function(user_data) {
|
readDB("localbitcoinUser", "00-01", function(user_data) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user