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,8 +175,14 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
return; return;
} }
progressdialog.setProgress(60);
progressdialog.setText("Calculating fees...");
apirequest(SETTINGS.apis.cryptofees, {
currency: success.currency
}, function (resp) {
if (resp.status == "OK") {
try { try {
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis); var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis, resp.feePerByte);
} catch (ex) { } catch (ex) {
console.error(ex); console.error(ex);
app.dialog.close(); app.dialog.close();
@ -169,7 +190,7 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
return; return;
} }
progressdialog.setProgress(75); progressdialog.setProgress(80);
progressdialog.setText("Sending payment..."); progressdialog.setText("Sending payment...");
apirequest(SETTINGS.apis.broadcasttransaction, { apirequest(SETTINGS.apis.broadcasttransaction, {
@ -183,6 +204,7 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
return; return;
} else { } else {
app.dialog.close(); app.dialog.close();
app.dialog.alert(resp.msg, "Error");
} }
}, function (errorData) { }, function (errorData) {
app.dialog.close(); app.dialog.close();
@ -200,6 +222,26 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText); sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
} }
}); });
} 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 get transaction fees", 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 get transaction fees", "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 get transaction fees", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
});
}, function (error) { }, function (error) {
app.dialog.close(); app.dialog.close();
app.dialog.alert(error, "Error"); app.dialog.alert(error, "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