How to size in bytes for a transaction using BitcoinJ

0

The calculation of a transaction fee uses fee/kb. So how do I get the size in bytes for a transaction using BitcoinJ?

SendRequest req = SendRequest.to(address, value);

req.feePerKb = Coin.parseCoin("0.0000100");

Wallet.SendResult result = kit.wallet().sendCoins(req);

Aman Vyas

Posted 2018-02-06T11:34:08.750

Reputation: 73

Answers

1

You cannot know the size in bytes of a transaction without knowing the inputs to the transaction. Just knowing the amount you want to transfer and just the output does not tell you anything.

Using the sendCoins() method and setting feePerKb in your SendRequest should create a transaction that pays the specified fee rate. If you want to get the transaction details after you send, the SendResult that you get has a field named tx which is a Transaction object. You can then get the necessary information from there.

If you want to know the size before you send, then you need to craft the transaction yourself as sendCoins() will automatically create and send the transaction for you.

Andrew Chow

Posted 2018-02-06T11:34:08.750

Reputation: 40 910

Thank you for the answer, So my next question is how should I know the fee per kb for a transaction.For example if transaction is 2.0 BTC and fatest fee is 240/kb so how much in total fee I should include. @Andrew ChowAman Vyas 2018-02-08T05:03:38.297

As I said in my answer, you need to know the size of the transaction, which can only be known after inputs are selected.Andrew Chow 2018-02-08T06:29:49.323

AFAIK, the fee is .001 BTC/kb and more the miner fee, the faster it gets confirmed.bitsabhi 2018-02-09T20:20:19.913

Median transactions are of sizes 226b, which is approx 24000 Satoshibitsabhi 2018-02-09T21:06:16.793