Get tx fees from API instead of using bitcore's too-expensive fees

master
Skylar Ittner 2 years ago
parent 334fb85c51
commit aba3c871ba

@ -45,19 +45,34 @@ function scanPrivateKeyQrCode(callback) {
* @param {type} outputSatoshis Amount to send to recipient's wallet * @param {type} outputSatoshis Amount to send to recipient's wallet
* @returns {string} Hex of serialized transaction, suitable for broadcast via Bitcoin Core or an API. * @returns {string} Hex of serialized transaction, suitable for broadcast via Bitcoin Core or an API.
*/ */
function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, destinationAddress, utxos, outputSatoshis) { function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, destinationAddress, utxos, outputSatoshis, feePerByte) {
if (typeof feePerByte == "undefined") {
feePerByte = -1;
}
try { try {
var privateKey = new bitcoreLib.PrivateKey(privateKeyString); var privateKey = new bitcoreLib.PrivateKey(privateKeyString);
var transaction = new bitcoreLib.Transaction() var transaction = new bitcoreLib.Transaction()
.from(utxos) .from(utxos)
.to(destinationAddress, outputSatoshis) .to(destinationAddress, outputSatoshis)
.change(sourceAddress) .change(sourceAddress);
.sign(privateKey);
var size = transaction._estimateSize();
var fee = size * feePerByte;
if (feePerByte > -1) {
// use our fee
transaction = transaction.fee(fee);
} else {
// use lib's fee
fee = transaction.getFee();
}
transaction = transaction.sign(privateKey);
var inputTotal = transaction._getInputAmount(); var inputTotal = transaction._getInputAmount();
var outputTotal = transaction.getFee() + outputSatoshis; var outputTotal = fee + outputSatoshis;
} catch (ex) { } catch (ex) {
throw new Error("There was an internal error while creating the transaction. Details: " + ex.message); throw new Error("There was an internal error while creating the transaction. Details: " + ex.message);
} }
@ -132,9 +147,9 @@ function getUTXOData(walletaddress, successCallback, errorCallback) {
} }
function sendCoins(privatekey, fromaddress, toaddress, amount) { function sendCoins(privatekey, fromaddress, toaddress, amount) {
var progressdialog = app.dialog.progress("Querying blockchain...", 25); var progressdialog = app.dialog.progress("Querying blockchain...", 20);
getUTXOData(fromaddress, function (success) { getUTXOData(fromaddress, function (success) {
progressdialog.setProgress(50); progressdialog.setProgress(40);
progressdialog.setText("Creating transaction..."); progressdialog.setText("Creating transaction...");
if (success.utxos.length == 0) { if (success.utxos.length == 0) {
app.dialog.close(); app.dialog.close();
@ -160,29 +175,56 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
return; return;
} }
try { progressdialog.setProgress(60);
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis); progressdialog.setText("Calculating fees...");
} catch (ex) { apirequest(SETTINGS.apis.cryptofees, {
console.error(ex);
app.dialog.close();
app.dialog.alert(ex.message, "Error");
return;
}
progressdialog.setProgress(75);
progressdialog.setText("Sending payment...");
apirequest(SETTINGS.apis.broadcasttransaction, {
transactiondata: txdata,
currency: success.currency currency: success.currency
}, function (resp) { }, function (resp) {
if (resp.status == "OK") { if (resp.status == "OK") {
app.dialog.close(); try {
app.dialog.alert("Sent " + amount + " " + success.currency + " to " + toaddress.substring(0, 5) + "..." + toaddress.substring(toaddress.length - 5, 999), "Success!"); var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis, resp.feePerByte);
$('#walletPrivateKey').val(""); // clear private key input box } catch (ex) {
return; console.error(ex);
app.dialog.close();
app.dialog.alert(ex.message, "Error");
return;
}
progressdialog.setProgress(80);
progressdialog.setText("Sending payment...");
apirequest(SETTINGS.apis.broadcasttransaction, {
transactiondata: txdata,
currency: success.currency
}, function (resp) {
if (resp.status == "OK") {
app.dialog.close();
app.dialog.alert("Sent " + amount + " " + success.currency + " to " + toaddress.substring(0, 5) + "..." + toaddress.substring(toaddress.length - 5, 999), "Success!");
$('#walletPrivateKey').val(""); // clear private key input box
return;
} else {
app.dialog.close();
app.dialog.alert(resp.msg, "Error");
}
}, function (errorData) {
app.dialog.close();
try {
var error = $.parseJSON(errorData.responseText);
if (error && typeof error.msg != 'undefined') {
app.dialog.alert(error.msg, "Error");
sendErrorReport("Crypto", "Couldn't broadcast transaction", error.msg);
} else {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error");
sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
} catch (ex) {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error");
sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
});
} else { } else {
app.dialog.close(); app.dialog.close();
app.dialog.alert(resp.msg, "Error");
} }
}, function (errorData) { }, function (errorData) {
app.dialog.close(); app.dialog.close();
@ -190,14 +232,14 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
var error = $.parseJSON(errorData.responseText); var error = $.parseJSON(errorData.responseText);
if (error && typeof error.msg != 'undefined') { if (error && typeof error.msg != 'undefined') {
app.dialog.alert(error.msg, "Error"); app.dialog.alert(error.msg, "Error");
sendErrorReport("Crypto", "Couldn't broadcast transaction", error.msg); sendErrorReport("Crypto", "Couldn't get transaction fees", error.msg);
} else { } else {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error"); app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error");
sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText); sendErrorReport("Crypto", "Couldn't get transaction fees", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
} }
} catch (ex) { } catch (ex) {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error"); app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error");
sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText); sendErrorReport("Crypto", "Couldn't get transaction fees", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
} }
}); });
}, function (error) { }, function (error) {

@ -41,7 +41,8 @@ var SETTINGS = {
// Crypto: check balance and send transactions // Crypto: check balance and send transactions
walletbalance: "http://localhost/helena.express/apis/crypto/walletbalance", walletbalance: "http://localhost/helena.express/apis/crypto/walletbalance",
getutxo: "http://localhost/helena.express/apis/crypto/getutxo", getutxo: "http://localhost/helena.express/apis/crypto/getutxo",
broadcasttransaction: "http://localhost/helena.express/apis/crypto/broadcasttransaction" broadcasttransaction: "http://localhost/helena.express/apis/crypto/broadcasttransaction",
cryptofees: "http://localhost/helena.express/apis/crypto/fees"
}, },
stripe_pubkey: "pk_test_51J6qFXCa1Fboir5UzPO3LCiMsVNiFP2lq4wR0dEcjJJVzAaJ3uRggDekZPB3qeYpMD3ayIYHKyD5sSn0IFLlEXMW001LqrvGSH", stripe_pubkey: "pk_test_51J6qFXCa1Fboir5UzPO3LCiMsVNiFP2lq4wR0dEcjJJVzAaJ3uRggDekZPB3qeYpMD3ayIYHKyD5sSn0IFLlEXMW001LqrvGSH",
branding: { branding: {

Loading…
Cancel
Save