Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Unable to handle tx fees correctly. #250

Open
aasimali141290 opened this issue Jul 17, 2018 · 1 comment
Open

Unable to handle tx fees correctly. #250

aasimali141290 opened this issue Jul 17, 2018 · 1 comment

Comments

@aasimali141290
Copy link

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);
  }
});
@NishidaRyu416
Copy link

NishidaRyu416 commented Nov 8, 2018

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);
  }
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants