bitcore-lib icon indicating copy to clipboard operation
bitcore-lib copied to clipboard

Unable to handle tx fees correctly.

Open aasimali141290 opened this issue 7 years ago • 1 comments

My tx https://live.blockcypher.com/btc-testnet/tx/7e3aad4b6cde87b95a6352d899c49db46ab61048445345883010d23594a43f54/

I am giving .01 as fees but I am getting back .099 in return address and only .0001 is using in a transaction.

For example, if I have Alice have 1 btc she wants to send .5 to Bob and give .001 as fees and rest in return address how do I do that.

var publicKeys = privateKeys.map(bitcore.PublicKey);
var address = new bitcore.Address(publicKeys, 2, bitcore.Networks.testnet); // 2 of 2

var utxos = {
  "txId" : "bb2b942ce7e506e25cdd9c5bc48a4cd4c758a10a83afedbd41c98e780dfa4d5e",
  "outputIndex" : 1,
  "address" : address.toString(),
  "script" : new bitcore.Script(address).toHex(),
  "satoshis" : 130000000 
};
var target = "2N22PZib79pwGjfuYJRweBd1DjxxLeFb3Df"
var tx = new bitcore.Transaction();
tx.from(utxos, publicKeys, 2);
tx.to(target, 120000000);
tx.change(address);
tx.sign(privateKeys[0]);
var serializedTx = tx.toObject();
tx = new bitcore.Transaction(serializedTx)
tx.sign(privateKeys[1]);

var Insight = require("bitcore-explorers").Insight.Insight;
var insight = new Insight("testnet");
var Insight = require("bitcore-explorers").Insight.Insight;
var insight = new Insight("testnet");
insight.broadcast(tx, function(error, transactionId) {
  if (error) {
    console.log("error",error)
  } else {
    console.log(transactionId);
  }
});

aasimali141290 avatar Jul 17 '18 21:07 aasimali141290

You can just do what you exactly want to do giving an option by calling transaction’s method .fee(fee).

In detail,

var publicKeys = privateKeys.map(bitcore.PublicKey);
var address = new bitcore.Address(publicKeys, 2, bitcore.Networks.testnet); // 2 of 2
fee= 100000; // in Satoshi, the amount of a fee whatever you want to specify
var utxos = {
  "txId" : "bb2b942ce7e506e25cdd9c5bc48a4cd4c758a10a83afedbd41c98e780dfa4d5e",
  "outputIndex" : 1,
  "address" : address.toString(),
  "script" : new bitcore.Script(address).toHex(),
  "satoshis" : 130000000 
};
var target = "2N22PZib79pwGjfuYJRweBd1DjxxLeFb3Df"
var tx = new bitcore.Transaction();
tx.from(utxos, publicKeys, 2);
tx.to(target, 120000000);
tx.change(address);
tx.fee(fee);
tx.sign(privateKeys[0]);
var serializedTx = tx.toObject();
tx = new bitcore.Transaction(serializedTx)
tx.sign(privateKeys[1]);

var Insight = require("bitcore-explorers")
var insight = new Insight("testnet");
insight.broadcast(tx, function(error, transactionId) {
  if (error) {
    console.log("error",error)
  } else {
    console.log(transactionId);
  }
});

NishidaRyu416 avatar Nov 08 '18 23:11 NishidaRyu416