added placing buy and sell orders functionality
This commit is contained in:
parent
a13bcc3338
commit
7331d50c9d
@ -6,6 +6,28 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
<title>Document</title>
|
<title>Document</title>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.tradebox {
|
||||||
|
display: flex;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
ul {
|
||||||
|
list-style-type: none;
|
||||||
|
}
|
||||||
|
ul.panel-box > li {
|
||||||
|
width: auto;
|
||||||
|
height: auto;
|
||||||
|
padding: 10px 10px;
|
||||||
|
margin: 10px 10px;
|
||||||
|
}
|
||||||
|
.buybox > ul.panel-box > li {
|
||||||
|
background-color: chocolate;
|
||||||
|
}
|
||||||
|
.sellbox > ul.panel-box > li {
|
||||||
|
background-color: cornflowerblue;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
@ -29,6 +51,26 @@
|
|||||||
<div id="datablocksdiv"></div>
|
<div id="datablocksdiv"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<h5>Buy--Sell</h5>
|
||||||
|
<div class="box tradebox">
|
||||||
|
<div class="buybox">
|
||||||
|
<strong>Buy BTC</strong>
|
||||||
|
<ul class="panel-box trade_amount_box" id="buyul">
|
||||||
|
<li>INR 10000</li>
|
||||||
|
<li>INR 50000</li>
|
||||||
|
<li>INR 100000</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="sellbox">
|
||||||
|
<strong>Sell BTC</strong>
|
||||||
|
<ul class="panel-box trade_amount_box" id="sellul">
|
||||||
|
<li>INR 10000</li>
|
||||||
|
<li>INR 50000</li>
|
||||||
|
<li>INR 100000</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- SHA1 -->
|
<!-- SHA1 -->
|
||||||
@ -7912,18 +7954,20 @@
|
|||||||
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, product, currency, buy_price, buyer_public_key,
|
||||||
buyer_key_signature, order_validator_public_key);
|
buyer_key_signature, order_validator_public_key);
|
||||||
if (is_valid_order === true) {
|
if (is_valid_order === true) {
|
||||||
var orderRPC = new localbitcoinplusplus.rpc();
|
|
||||||
this.rpc_job = 'trade_' + this.order_type;
|
this.rpc_job = 'trade_' + this.order_type;
|
||||||
return orderRPC.send_rpc(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,
|
||||||
"product": this.product,
|
"product": this.product,
|
||||||
"currency": this.currency,
|
"currency": this.currency,
|
||||||
"buy_price": this.buy_price,
|
"buy_price": this.buy_price,
|
||||||
"buyer_public_key": this.buyer_public_key,
|
"buyer_public_key": this.buyer_public_key,
|
||||||
"buyer_key_signature": this.buyer_key_signature,
|
"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}
|
||||||
});
|
);
|
||||||
} else if (is_valid_order == "object") {
|
return placeNewOrder;
|
||||||
|
} else if (typeof is_valid_order == "object") {
|
||||||
var err;
|
var err;
|
||||||
for (err = 0; err < is_valid_order.length; err++) {
|
for (err = 0; err < is_valid_order.length; err++) {
|
||||||
alert(is_valid_order[err]);
|
alert(is_valid_order[err]);
|
||||||
@ -7932,8 +7976,13 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
match_trade() {
|
match_trade() {
|
||||||
console.log(this.buy_list);
|
try {
|
||||||
console.log(this.sell_list);
|
readAllDB("sellOrders", function(ss) {
|
||||||
|
console.log(ss);
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
trade_buy(params, callback) {
|
trade_buy(params, callback) {
|
||||||
for (var key in params) {
|
for (var key in params) {
|
||||||
@ -7967,6 +8016,7 @@
|
|||||||
|
|
||||||
params['rand_id'] = Math.floor(Math.random(1,1000)*1000);
|
params['rand_id'] = Math.floor(Math.random(1,1000)*1000);
|
||||||
this.sell_list = params;
|
this.sell_list = params;
|
||||||
|
addDB("sellOrders", params);
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8189,7 +8239,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(evt) {
|
function onMessage(evt) {
|
||||||
console.log(evt);
|
//console.log(evt);
|
||||||
|
|
||||||
var response = evt.data;
|
var response = evt.data;
|
||||||
var res_pos = response.indexOf('{');
|
var res_pos = response.indexOf('{');
|
||||||
@ -8385,7 +8435,7 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function readAllDB(tablename) {
|
function readAllDB(tablename, callback) {
|
||||||
var objectStore = db.transaction(tablename).objectStore(tablename);
|
var objectStore = db.transaction(tablename).objectStore(tablename);
|
||||||
|
|
||||||
objectStore.openCursor().onsuccess = function (event) {
|
objectStore.openCursor().onsuccess = function (event) {
|
||||||
@ -8393,6 +8443,7 @@
|
|||||||
|
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
console.log(cursor);
|
console.log(cursor);
|
||||||
|
callback(cursor);
|
||||||
} else {
|
} else {
|
||||||
alert("No more entries!");
|
alert("No more entries!");
|
||||||
}
|
}
|
||||||
@ -8454,16 +8505,6 @@
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Failed to fetch configurations: ${error}`);
|
throw new Error(`Failed to fetch configurations: ${error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Test: Trade functionality
|
|
||||||
var trade_btn = document.createElement("button");
|
|
||||||
trade_btn.innerText = "Trade";
|
|
||||||
trade_btn.onclick = function () {
|
|
||||||
var buytrade = RM_TRADE.place_order("buy", "BTC", "INR", 10000.00, "buyer_public_key",
|
|
||||||
"buyer_key_signature", "order_validator_public_key");
|
|
||||||
doSend(buytrade);
|
|
||||||
}
|
|
||||||
document.getElementById("output_div").appendChild(trade_btn);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Database operations -->
|
<!-- Database operations -->
|
||||||
@ -8512,7 +8553,43 @@
|
|||||||
<p>Declared BTC Balance: ${idbData.mySelfdeclaredBalanceBitcoin}<p>
|
<p>Declared BTC Balance: ${idbData.mySelfdeclaredBalanceBitcoin}<p>
|
||||||
<p>Declared INR Balance: ${idbData.mySelfDeclaredBalanceINR}<p>`;
|
<p>Declared INR Balance: ${idbData.mySelfDeclaredBalanceINR}<p>`;
|
||||||
|
|
||||||
|
/* Give user the facillity to trade */
|
||||||
|
var buyul = document.getElementById('buyul');
|
||||||
|
var sellul = document.getElementById('sellul');
|
||||||
|
|
||||||
|
function getEventTarget(e) {
|
||||||
|
e = e || window.event; // for browsers compatibility
|
||||||
|
return e.target || e.srcElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
buyul.onclick = function(event) {
|
||||||
|
let target = getEventTarget(event);
|
||||||
|
let intAmount = parseFloat(target.innerHTML.match(/\d+/)[0]); // Amount of INR/BTC/whatever in integer
|
||||||
|
let signing_object = {
|
||||||
|
flo_address: idbData.myLocalFLOAddress,
|
||||||
|
order_type: "buy",
|
||||||
|
trading_product: "BTC",
|
||||||
|
currency: "INR",
|
||||||
|
tradingAmount: intAmount
|
||||||
|
};
|
||||||
|
// 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,
|
||||||
|
"trader_signature", "order_validator_public_key");
|
||||||
|
console.log(buytrade);
|
||||||
|
doSend(buytrade);
|
||||||
|
}
|
||||||
|
|
||||||
|
sellul.onclick = function(event) {
|
||||||
|
let target = getEventTarget(event);
|
||||||
|
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,
|
||||||
|
"trader_signature", "order_validator_public_key");
|
||||||
|
doSend(buytrade);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -8529,6 +8606,7 @@
|
|||||||
<p>Block Signature: ${blockData.blockSignature}</p>
|
<p>Block Signature: ${blockData.blockSignature}</p>
|
||||||
<p>Blockhash: ${blockData.blockhash}</p>`
|
<p>Blockhash: ${blockData.blockhash}</p>`
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error("ERROR: Failed to initialise the datablocks database. You are unable to trade at the moment.");
|
throw new Error("ERROR: Failed to initialise the datablocks database. You are unable to trade at the moment.");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user