From 6615e3067cca25e86bc7bc31d4351db3ac1807cf Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Tue, 20 Nov 2018 21:40:45 +0530 Subject: [PATCH] added function to deposit and withdraw assets --- supernode/index.html | 207 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 199 insertions(+), 8 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index 90cd57b..5b8d23c 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -45,6 +45,11 @@
+
Asset Box
+
+
+
+
Sync
@@ -7885,6 +7890,7 @@ this.valid_assets = ["BTC", "INR"]; this.currency = null; this.valid_currencies = ["BTC", "INR"]; + this.valid_trading_amount = [10000, 50000, 100000]; this.buy_price = null; this.buyer_public_key = null; this.buyer_key_signature = null; @@ -7894,6 +7900,8 @@ this.super_nodes_array = null; this.buy_list = null; this.sell_list = null; + this.user_flo_address = null; + this.receiving_address = null; } Trade.prototype = { @@ -7905,7 +7913,7 @@ this.level = level; } }, - validate_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature, + validate_order(order_type, user_flo_address, receiving_address, product, currency, buy_price, buyer_public_key, buyer_key_signature, order_validator_public_key) { if (this.valid_order_type.indexOf(order_type) >= 0) { @@ -7913,6 +7921,16 @@ } else { this.errors.push("Inavlid trade type value."); } + if (user_flo_address.trim()!=="") { + this.user_flo_address= user_flo_address; + } else { + this.errors.push("No receiving BTC or Bank address provided."); + } + if (receiving_address.trim()!=="") { + this.receiving_address= receiving_address; + } else { + this.errors.push("No receiving BTC or Bank address provided."); + } if (this.valid_assets.indexOf(product) >= 0) { this.product = product; } else { @@ -7949,9 +7967,9 @@ } return true; }, - place_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature, + place_order(order_type, user_flo_address, receiving_address, product, currency, buy_price, buyer_public_key, buyer_key_signature, order_validator_public_key) { - var is_valid_order = this.validate_order(order_type, product, currency, buy_price, buyer_public_key, + var is_valid_order = this.validate_order(order_type, user_flo_address, receiving_address, product, currency, buy_price, buyer_public_key, buyer_key_signature, order_validator_public_key); if (is_valid_order === true) { @@ -7959,12 +7977,15 @@ let placeNewOrder = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, this.rpc_job, { "order_type": this.order_type, + "user_flo_address": this.user_flo_address, "product": this.product, "currency": this.currency, "buy_price": this.buy_price, "buyer_public_key": this.buyer_public_key, "buyer_key_signature": this.buyer_key_signature, - "order_validator_public_key": this.order_validator_public_key} + "order_validator_public_key": this.order_validator_public_key, + "receiving_address": this.receiving_address + } ); return placeNewOrder; } else if (typeof is_valid_order == "object") { @@ -7985,22 +8006,78 @@ if (buyOrdersList.length>0) { let list10ksell = sellOrdersList.filter(function(sellOrder) { return sellOrder.buy_price == 10000; + }).sort(function(trustLevelA, trustLevelB) { + if (trustLevelA.rand_id > trustLevelB.rand_id) { return 1; + } else { return -1; } }); let list50ksell = sellOrdersList.filter(function(sellOrder) { return sellOrder.buy_price == 50000; + }).sort(function(trustLevelA, trustLevelB) { + if (trustLevelA.rand_id > trustLevelB.rand_id) { return 1; + } else { return -1; } }); let list100ksell = sellOrdersList.filter(function(sellOrder) { return sellOrder.buy_price == 100000; + }).sort(function(trustLevelA, trustLevelB) { + if (trustLevelA.rand_id > trustLevelB.rand_id) { return 1; + } else { return -1; } }); let list10kbuy = buyOrdersList.filter(function(buyOrder) { return buyOrder.buy_price == 10000; + }).sort(function(trustLevelA, trustLevelB) { + if (trustLevelA.rand_id > trustLevelB.rand_id) { return 1; + } else { return -1; } }); let list50kbuy = buyOrdersList.filter(function(buyOrder) { return buyOrder.buy_price == 50000; + }).sort(function(trustLevelA, trustLevelB) { + if (trustLevelA.rand_id > trustLevelB.rand_id) { return 1; + } else { return -1; } }); let list100kbuy = buyOrdersList.filter(function(buyOrder) { return buyOrder.buy_price == 100000; - }); + }).sort(function(trustLevelA, trustLevelB) { + if (trustLevelA.rand_id > trustLevelB.rand_id) { return 1; + } else { return -1; } + }); + + let list10k = [list10kbuy, list10ksell]; + let list50k = [list50kbuy, list50ksell]; + let list100k = [list100kbuy, list100ksell]; + + let all_trade_list = [list10k, list50k, list100k]; + + all_trade_list.forEach(function(listXk) { + let iter = listXk[0].length < listXk[1].length ? listXk[0].length:listXk[1].length; + console.log(iter); + + for (let index = 0; index < iter; index++) { + let buy_element = listXk[0][index]; + let sell_element = listXk[1][index]; + + let buyers_job = `${buy_element.user_flo_address} to pay ${buy_element.currency} ${buy_element.buy_price} to bank address "${sell_element.receiving_address}"`; + let sellers_job = `${sell_element.user_flo_address} to pay X Bitcoins to address ${buy_element.receiving_address}`; + + doSend(buyers_job); + doSend(sellers_job); + } + }); + + // for list10k + // let iter = list10k[0].length < list10k[1].length ? list10k[0].length:list10k[1].length; + // console.log(iter); + + // for (let index = 0; index < iter; index++) { + // const buy_element = list10k[0][index]; + // const sell_element = list10k[1][index]; + + // let buyers_job = `${buy_element.user_flo_address} to pay ${buy_element.currency} ${buy_element.buy_price} to bank address "${sell_element.receiving_address}"`; + // let sellers_job = `${sell_element.user_flo_address} to pay X Bitcoins to address ${buy_element.receiving_address}`; + + // doSend(buyers_job); + // doSend(sellers_job); + // } + } }); } @@ -8043,6 +8120,35 @@ this.sell_list = params; addDB("sellOrders", params); callback(); + }, + findTrader(traderAssetType, traderAssetAmount) { + if (this.valid_assets.includes(traderAssetType) && this.valid_trading_amount.includes(traderAssetAmount)) { + if(traderAssetType=="BTC") { + return "1TRADERBITCOINADDRESS"; + } else if(traderAssetType=="INR") { + return "TRADERBANKACCOUNT"; + } + } + return false; + }, + depositAsset(assetType, amount, userFLOaddress, callback) { + if(!this.valid_assets.includes(assetType)) { + throw new Error("Invalid asset error"); + } else if(parseFloat(amount)<=0) { + throw new Error("Invalid amount error."); + } else if(userFLOaddress.length < 0) { + throw new Error("User address required."); + } + + console.log(userFLOaddress); + + // Determine the level of the user and check if he/she can trade for 'amount' + // in params above + + // Find a person selling BTC in lieu of Cash or Find a person buying BTC in lieu of Cash + let counterTraderAsset = assetType=="BTC" ? "INR":"BTC"; + let getCounterTraderAccountAddress = this.findTrader(counterTraderAsset, amount); + callback(getCounterTraderAccountAddress); } } @@ -8288,7 +8394,7 @@ break; case "trade_sell": var response_from_sever = orderRPC.receive_rpc_response(JSON.stringify(res_obj)); - doSend(response_from_sever); // send response to client + //doSend(response_from_sever); // send response to client break; case "broadcastBlockDataToAll": var response_from_sever = orderRPC.receive_rpc_response(JSON.stringify(res_obj)); @@ -8592,6 +8698,10 @@ buyul.onclick = function(event) { let target = getEventTarget(event); let intAmount = parseFloat(target.innerHTML.match(/\d+/)[0]); // Amount of INR/BTC/whatever in integer + let buyer_btc_address = prompt("Please enter your Bitcoin address."); + if (typeof buyer_btc_address == null || buyer_btc_address.trim().length<1) { + throw new Error("Bitcoin address cannot be empty."); + } let signing_object = { flo_address: idbData.myLocalFLOAddress, order_type: "buy", @@ -8601,7 +8711,11 @@ }; // Get private key here //let trader_signature = RM_WALLET.sign(JSON.stringify(signing_object), privateKeyWIF); - let buytrade = RM_TRADE.place_order("buy", "BTC", "INR", intAmount, idbData.myLocalFLOPublicKey, + + if (typeof idbData.myLocalFLOAddress == undefined || idbData.myLocalFLOAddress.trim()=="") { + throw new Error("You must have a BTC address to receive Bitcoin. No Bitcoin address found in database."); + } + let buytrade = RM_TRADE.place_order("buy", idbData.myLocalFLOAddress, buyer_btc_address, "BTC", "INR", intAmount, idbData.myLocalFLOPublicKey, "trader_signature", "order_validator_public_key"); console.log(buytrade); doSend(buytrade); @@ -8609,12 +8723,24 @@ sellul.onclick = function(event) { let target = getEventTarget(event); + let seller_bank_details = prompt("Please provide your full bank details."); + + if (typeof idbData.myLocalFLOAddress == undefined || idbData.myLocalFLOAddress.trim()=="") { + throw new Error("You must have a FLO address to trade. No such address found in database."); + } + if (typeof seller_bank_details == null || seller_bank_details.trim().length <1) { + throw new Error("Bank detail information cannot be empty."); + } + let intAmount = parseFloat(target.innerHTML.match(/\d+/)[0]); // Amount of INR/BTC/whatever in integer - var buytrade = RM_TRADE.place_order("sell", "BTC", "INR", intAmount, idbData.myLocalFLOPublicKey, + let buytrade = RM_TRADE.place_order("sell", idbData.myLocalFLOAddress, seller_bank_details.trim(), "BTC", "INR", intAmount, idbData.myLocalFLOPublicKey, "trader_signature", "order_validator_public_key"); doSend(buytrade); } + // Deposit / Withdraw asset + deopositWithdrawAsset(idbData.myLocalFLOAddress); + }); } catch (e) { @@ -8707,6 +8833,71 @@ } + + \ No newline at end of file