fixed bitcoin sending

This commit is contained in:
Abhishek Sinha 2018-12-23 20:42:09 +05:30
parent b778efdaab
commit 7dcffcd9dc
2 changed files with 79 additions and 54 deletions

View File

@ -3460,6 +3460,9 @@
trx.addoutput(address, value); trx.addoutput(address, value);
var sendFloData = document.getElementById("flotextdata").value; //flochange adding place for flodata -- need a validation of 1024 chars var sendFloData = document.getElementById("flotextdata").value; //flochange adding place for flodata -- need a validation of 1024 chars
trx.addflodata(sendFloData); // flochange .. create this function trx.addflodata(sendFloData); // flochange .. create this function
console.log(trx);
var wif = document.getElementById("wif").value; var wif = document.getElementById("wif").value;
var textArea = document.getElementById("rawTrx"); var textArea = document.getElementById("rawTrx");
textArea.value = trx.sign(wif, 1); //SIGHASH_ALL DEFAULT 1 textArea.value = trx.sign(wif, 1); //SIGHASH_ALL DEFAULT 1
@ -3526,6 +3529,9 @@
alert("Empty Signature"); alert("Empty Signature");
return false; return false;
} }
console.log(signedTxHash);
var params = `{"rawtx":"${signedTxHash}"}`; var params = `{"rawtx":"${signedTxHash}"}`;

View File

@ -8921,7 +8921,8 @@
RMAssets = RMAssets =
`validAssets=BTC,INR#!#supernodes=127.0.0.1,212.88.88.2#!#MASTER_NODE=023B9F60692A17FAC805D012C5C8ADA3DD19A980A3C5F0D8A5B3500CC54D6E8B75 `validAssets=BTC,INR#!#supernodes=127.0.0.1,212.88.88.2#!#MASTER_NODE=023B9F60692A17FAC805D012C5C8ADA3DD19A980A3C5F0D8A5B3500CC54D6E8B75
#!#MASTER_RECEIVING_ADDRESS=oVRq2nka1GtALQT8pbuLHAGjqAQ7PAo6uy#!#validTradingAmount=10000,50000,100000#!#btcTradeMargin=5000 #!#MASTER_RECEIVING_ADDRESS=oVRq2nka1GtALQT8pbuLHAGjqAQ7PAo6uy#!#validTradingAmount=10000,50000,100000#!#btcTradeMargin=5000
#!#supernodesPubKeys=0314413409B26B7FA6A7BA4A5AC73D13F206638F88AFDF7DAB82F69B384FD3A1CB,`; #!#supernodesPubKeys=039F39FA71B15AC5689E0424432A3506DAB89E7FC3C510503CA2DBB1BCAF9DD081,`;
//#!#supernodesPubKeys=0314413409B26B7FA6A7BA4A5AC73D13F206638F88AFDF7DAB82F69B384FD3A1CB,`;
let floAssetsArray = RMAssets.split('#!#'); let floAssetsArray = RMAssets.split('#!#');
if (floAssetsArray.length > 0 && typeof floAssetsArray[0] !== undefined && if (floAssetsArray.length > 0 && typeof floAssetsArray[0] !== undefined &&
@ -9161,26 +9162,26 @@
// }); // });
// Check if user id is in deposit or withdraw. If true prevent him from trading // Check if user id is in deposit or withdraw. If true prevent him from trading
try { // try {
readAllDB('withdraw_cash', function(res) { // readAllDB('withdraw_cash', function(res) {
let check_deposit_withdraw_id_array = res.filter(f=>f.status===2) // let check_deposit_withdraw_id_array = res.filter(f=>f.status===2)
.map(m=>{ // .map(m=>{
if (m.trader_flo_address==respective_trader_id||m.deposit_withdraw_id_array==respective_trader_id) { // if (m.trader_flo_address==respective_trader_id||m.deposit_withdraw_id_array==respective_trader_id) {
let server_msg = `Trader id ${respective_trader_id} is not clear for trade currently. // let server_msg = `Trader id ${respective_trader_id} is not clear for trade currently.
You must finish your previous pending deposit/withdraw action to qualify again to trade.`; // You must finish your previous pending deposit/withdraw action to qualify again to trade.`;
let server_response = localbitcoinplusplus.rpc.prototype // let server_response = localbitcoinplusplus.rpc.prototype
.send_rpc // .send_rpc
.call(this, "supernode_message", // .call(this, "supernode_message",
{"trader_flo_id":respective_trader_id, "server_msg":server_msg}); // {"trader_flo_id":respective_trader_id, "server_msg":server_msg});
doSend(server_response); // doSend(server_response);
throw new Error("User has not finished previous pending actions."); // throw new Error("User has not finished previous pending actions.");
} // }
}); // });
}); // });
} catch (error) { // } catch (error) {
throw new Error(error); // throw new Error(error);
} // }
switch (method) { switch (method) {
case "trade_buy": case "trade_buy":
@ -9536,20 +9537,22 @@
receiverBTCAddress, receiverBTCAddress,
withdrawing_btc_amount, withdrawing_btc_amount,
deposit_arr.btc_address, deposit_arr.btc_address,
function (txid) { function (txres) {
console.log(txid); console.log(txres);
if (typeof txres !== "object" || typeof txres.result!=="string") return;
const txid = txres.result;
deposit_arr.bitcoinToBePaid -= eqBTC; deposit_arr.bitcoinToBePaid -= eqBTC;
if (deposit_arr.bitcoinToBePaid >0) { if (deposit_arr.bitcoinToBePaid >0) {
// update deposits in db // update deposits in db
deposit_arr.status = 3; deposit_arr.status = 3;
updateinDB( updateinDB(
"deposit", "deposit",
deposit_arr, deposit_arr,
deposit_arr deposit_arr
.trader_flo_address .trader_flo_address
); );
return true; return true;
} else { } else {
// delete entry in deposits in db // delete entry in deposits in db
@ -10028,48 +10031,60 @@
if (res.length > 0) { if (res.length > 0) {
try { try {
receiving_amount = parseInt(receiving_amount * 100000000); if (!localbitcoinplusplus.master_configurations.validTradingAmount.includes(receiving_amount)) {
throw new Error('Invalid amount');
}
let btc_eq_receiving_amount = localbitcoinplusplus.trade.prototype.calculateBTCEquivalentOfCash(receiving_amount);
btc_eq_receiving_amount = parseFloat(btc_eq_receiving_amount).toFixed(8);
let utxo_list = JSON.parse(res); let utxo_list = JSON.parse(res);
var trx = bitjs.transaction(); var trx = bitjs.transaction();
let sum = 0; let sum = 0;
for (var key in utxo_list) { for (var key in utxo_list) {
if (utxo_list[key].confirmations > 6) { if (utxo_list[key].confirmations > 0) {
var obj = utxo_list[key]; var obj = utxo_list[key];
sum += obj.satoshis; sum += obj.satoshis;
if (receiving_amount <= sum) { if (btc_eq_receiving_amount <= sum) {
console.log(obj);
trx.addinput(obj.txid, obj.vout, obj.scriptPubKey); trx.addinput(obj.txid, obj.vout, obj.scriptPubKey);
break; break;
} else { } else {
console.log(obj);
trx.addinput(obj.txid, obj.vout, obj.scriptPubKey); trx.addinput(obj.txid, obj.vout, obj.scriptPubKey);
} }
} }
} }
let change_amount = sum - receiving_amount - 0.00006060; sum = parseFloat(sum / 100000000).toFixed(8);
trx.addoutput(receiver_address, receiving_amount); let change_amount = sum - btc_eq_receiving_amount - 0.00006060;
trx.addoutput(receiver_address, btc_eq_receiving_amount);
trx.addoutput(change_adress, change_amount); trx.addoutput(change_adress, change_amount);
var sendFloData = var sendFloData =
`localbitcoinpluslus tx: Send BTC ${receiving_amount} to ${receiver_address}.`; //flochange adding place for flodata -- need a validation of 1024 chars `localbitcoinpluslus tx: Send ${btc_eq_receiving_amount} satoshis to ${receiver_address}.`; //flochange adding place for flodata -- need a validation of 1024 chars
trx.addflodata(sendFloData); // flochange .. create this function trx.addflodata(sendFloData); // flochange .. create this function
try { try {
console.log(trx); console.log(trx);
let signedTxHash = trx.sign(utxo_addr_wif, 1); //SIGHASH_ALL DEFAULT 1 let signedTxHash = trx.sign(utxo_addr_wif, 1); //SIGHASH_ALL DEFAULT 1
var params = `{"rawtx":"${signedTxHash}"}`;
var tx_send_url = `${localbitcoinplusplus.flocha}/api/tx/send`;
console.log(signedTxHash); console.log(signedTxHash);
// helper_functions.ajaxPost(tx_send_url, params, function (txid) { var http = new XMLHttpRequest();
// console.log(txid); var tx_send_url = `${localbitcoinplusplus.flocha}/api/tx/send`;
// callback(txid); var params = `{"rawtx":"${signedTxHash}"}`;
// }); http.open('POST', tx_send_url, true);
http.setRequestHeader('Content-type', 'application/json');
http.onreadystatechange = function () { //Call a function when the state changes.
if (http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
callback(http.responseText);
} else {
// console.log(http.responseText);
// callback(null);
}
}
http.send(params);
} catch (error) { } catch (error) {
throw new Error(error); throw new Error(error);
@ -10399,17 +10414,21 @@
//AJAX Post //AJAX Post
ajaxPost: function (url, data, callback) { ajaxPost: function (url, data, callback) {
xhr = new XMLHttpRequest(); xhttp = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhttp.onreadystatechange = function() {
xhr.onload = function () { if (this.readyState == 4 && this.status == 200) {
if (xhr.status == 200) { console.log(this.responseText);
callback(); callback(this.responseText);
} else { } else {
throw new Error(`Request to ${url} failed: ${xhr.status}`); callback(false);
throw new Error(`Request to ${url} failed: ${xhttp.status}`);
} }
}; };
xhr.send(encodeURI(data));
xhttp.open('POST', url, true);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send(JSON.stringify(data));
}, },
// Create unique id // Create unique id
@ -10602,8 +10621,8 @@
/* Websocket Code Starts here */ /* Websocket Code Starts here */
//var wsUri = "ws://localhost:9000/"; var wsUri = "ws://localhost:9000/";
var wsUri = "ws://ranchimall.duckdns.org:9000/"; //var wsUri = "ws://ranchimall.duckdns.org:9000/";
var output; var output;
function init() { function init() {