added code to send signedrawtx if broadcast fails in sendTransaction function

This commit is contained in:
Abhishek Sinha 2019-07-06 15:28:57 +05:30
parent ffbc6f03f7
commit 804abe0a6f

View File

@ -12498,17 +12498,35 @@
withdraw_res.change_adress, withdraw_res.change_adress,
async function (res) { async function (res) {
console.log(res); console.log(res);
if (typeof res == "string" if (typeof res == "object") {
&& res.length > 0) {
try { try {
let resp_obj = JSON.parse(res); let msg = '';
let resp_txid = resp_obj.txid.result || resp_obj.txid; if (typeof res.txid=="string" && res.txid.length>0) {
let msg = resp_obj = JSON.parse(res.txid);
`Transaction Id for your withdrawn crypto asset: ${resp_txid}`; resp_txid = resp_obj.txid.result || resp_obj.txid;
msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`;
} else if (res.signedTxHash.length>0) {
msg = `INFO: We could not broadcast your transaction. Please broadcast
this Signed Raw Tx manually yourself: ${res.signedTxHash}`;
} else {
console.log(res);
throw new Errror(`ERROR: Failed to make transaction.`);
return false;
}
if (msg.length>0) {
const RM_RPC = new localbitcoinplusplus.rpc;
RM_RPC
.send_rpc
.call(this, "supernode_message", {
"trader_flo_address": withdraw_res.trader_flo_address,
"receiver_flo_address": withdraw_res.trader_flo_address,
"server_msg": msg
}).then(server_response=>doSend(server_response));
} else return;
readDB('crypto_balances', withdraw_res.id) readDB('crypto_balances', withdraw_res.id)
.then( .then(res_bal => {
res_bal => {
// btc_eq_receiving_amount // btc_eq_receiving_amount
// = // =
// Number(parseFloat(EqCryptoWd).toFixed(8)); // Number(parseFloat(EqCryptoWd).toFixed(8));
@ -12524,8 +12542,7 @@
.MY_SUPERNODE_PRIVATE_KEY .MY_SUPERNODE_PRIVATE_KEY
); );
const const updateUserCryptoBalanceObject = {
updateUserCryptoBalanceObject = {
updatedBTCBalanceObject: res_bal, updatedBTCBalanceObject: res_bal,
updatedBTCBalanceObjectSign: res_obj_sign, updatedBTCBalanceObjectSign: res_obj_sign,
trader_flo_address: withdraw_res.trader_flo_address, trader_flo_address: withdraw_res.trader_flo_address,
@ -12539,14 +12556,10 @@
).then(updateUserCryptoBalanceRequestObject=> ).then(updateUserCryptoBalanceRequestObject=>
doSend(updateUserCryptoBalanceRequestObject)); doSend(updateUserCryptoBalanceRequestObject));
} });
)
} }
); );
} catch ( } catch (error) {
error
) {
console.warn(error); console.warn(error);
showMessage(error); showMessage(error);
} }
@ -12558,52 +12571,21 @@
*******************CHECK ACTUAL BTC BALANCE HERE THROUGH AN API AND UPDATE DEPOSIT TABLE**************************************************** *******************CHECK ACTUAL BTC BALANCE HERE THROUGH AN API AND UPDATE DEPOSIT TABLE****************************************************
************************************************************************************************************************************/ ************************************************************************************************************************************/
readDBbyIndex readDBbyIndex('deposit', 'btc_address', withdraw_res.utxo_addr)
( .then(function (deposit_arr_resp) {
'deposit', if (typeof deposit_arr_resp == "object") {
'btc_address', deposit_arr_resp.map(deposit_arr => {
withdraw_res deposit_arr.bitcoinToBePaid -= EqCryptoWd;
.utxo_addr
).then(
function (
deposit_arr_resp
) {
if (
typeof deposit_arr_resp ==
"object"
) {
deposit_arr_resp
.map(
deposit_arr => {
deposit_arr
.bitcoinToBePaid -=
EqCryptoWd;
if ( if (deposit_arr.bitcoinToBePaid > 0) {
deposit_arr
.bitcoinToBePaid >
0
) {
// update deposits in db // update deposits in db
deposit_arr deposit_arr.status = 2; // UTXO ready to be used again
.status = updateinDB("deposit", deposit_arr,
2; // UTXO ready to be used again deposit_arr.trader_flo_address);
updateinDB
(
"deposit",
deposit_arr,
deposit_arr
.trader_flo_address
);
} else { } else {
// delete entry in deposits in db // delete entry in deposits in db
removeinDB removeinDB("deposit", deposit_arr.trader_flo_address);
(
"deposit",
deposit_arr
.trader_flo_address
);
} }
} }
); );
@ -13799,11 +13781,32 @@
withdraw_res.change_adress, withdraw_res.change_adress,
async function (res) { async function (res) {
console.log(res); console.log(res);
if (typeof res == "string" && res.length > 0) { if (typeof res == "object") {
try { try {
let resp_obj = JSON.parse(res); let msg = '';
let resp_txid = resp_obj.txid.result || resp_obj.txid; if (typeof res.txid=="string" && res.txid.length>0) {
let msg=`Transaction Id for your withdrawn crypto asset: ${resp_txid}`; resp_obj = JSON.parse(res.txid);
resp_txid = resp_obj.txid.result || resp_obj.txid;
msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`;
} else if (res.signedTxHash.length>0) {
msg = `INFO: We could not broadcast your transaction. Please broadcast
this Signed Raw Tx manually yourself: ${res.signedTxHash}`;
} else {
console.log(res);
throw new Errror(`ERROR: Failed to make transaction.`);
return false;
}
if (msg.length>0) {
const RM_RPC = new localbitcoinplusplus.rpc;
RM_RPC
.send_rpc
.call(this, "supernode_message", {
"trader_flo_address": withdraw_res.trader_flo_address,
"receiver_flo_address": withdraw_res.trader_flo_address,
"server_msg": msg
}).then(server_response=>doSend(server_response));
} else return;
let withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`; let withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`;
backup_server_db_instance.backup_readDB backup_server_db_instance.backup_readDB
@ -14649,10 +14652,30 @@
http.open('POST', tx_send_url, true); http.open('POST', tx_send_url, true);
http.setRequestHeader('Content-type', 'application/json'); http.setRequestHeader('Content-type', 'application/json');
http.onreadystatechange = function () { //Call a function when the state changes. http.onreadystatechange = function () { //Call a function when the state changes.
if (http.readyState == 4 && http.status == 200) { if (http.readyState == 4) {
showMessage(http.responseText); if (http.status == 200) {
callback(http.responseText); console.log(http.responseText);
let response_obj = {
signedTxHash: signedTxHash,
txid: http.responseText
} }
//callback(http.responseText);
callback(response_obj);
} else {
let response_obj = {
signedTxHash: signedTxHash,
txid: ""
}
callback(response_obj);
}
}
}
http.onerror = function() {
let response_obj = {
signedTxHash: signedTxHash,
txid: ""
}
callback(response_obj);
} }
http.send(params); http.send(params);
@ -21411,16 +21434,21 @@
receiving_crypto_amount_input.value, null, change_adress_input.value, receiving_crypto_amount_input.value, null, change_adress_input.value,
async function (res) { async function (res) {
console.log(res); console.log(res);
if (typeof res == "string" && res.length > 0) { if (typeof res == "object") {
try { try {
let resp_obj = JSON.parse(res); let resp_obj = JSON.parse(res.txid);
let resp_txid = resp_obj.txid.result || resp_obj.txid; let resp_txid = resp_obj.txid.result || resp_obj.txid;
let msg = `Transaction Id for your deposited crypto asset: ${resp_txid}`; let msg = `Transaction Id for your deposited crypto asset: ${resp_txid}`;
showMessage(msg); showMessage(msg);
msg = `Signed Raw Tx for your deposited crypto asset: ${res.signedTxHash}`;
showMessage(msg);
return true; return true;
} catch (error) { } catch (error) {
console.warn(error); console.warn(error);
showMessage(error); showMessage(error);
showMessage(`INFO: We could not broadcast your transaction. Please broadcast
this Signed Raw Tx manually yourself: ${res.signedTxHash}`);
} }
} }
}); });