<p>You will, by default, only see the values of the outputs and not the inputs. To gather all values for inputs, you will need to loop through your inputs and use an API to gather the transactions where the inputs were once outputs.</p>

<p>I used the BitPay insight API and left some notes in the code below:</p>

<pre><code>var Bitcoin = require('bitcoinjs-lib');
var request = require('request');

function getRawBlock(blockHash) {
  return new Promise((resolve, reject) =&gt; {
    request('https://insight.bitpay.com/api/rawblock/' + blockHash, // hitting an insight API to get the full block
      (error, response, body) =&gt; {
        try {
          var block = JSON.parse(body); // result is in JSON
          resolve(block.rawblock)
        } catch(error) {
          reject(error)
        }
      })
  })
}

function getTransaction(transactionId) {
  return new Promise((resolve, reject) =&gt; {
    request('https://insight.bitpay.com/api/tx/' + transactionId, // hitting an insight API to get the full transaction
      (error, response, body) =&gt; {
        try {
          var transaction = JSON.parse(body); // result is in JSON
          resolve(transaction);
        } catch(e) {
          reject(e)
        }
      });
  })
}

getRawBlock('000000000000000000aabc9831a830ab8542137c68998368b86f9127432767d5')
  .then((rawBlock) =&gt; {

    var block = Bitcoin.Block.fromHex(rawBlock); // use bitcoinjs-lib to convert the raw block to usable data
    var transactions = block.transactions; // transactions reside here
    var transaction = transactions[1]; // using second transaction as an example since the first one is miner reward
    // you can loop the transactions the way you like

    var inputs = transaction.ins; // will need to use the API to get the outputs that each input once was
    var outputs = transaction.outs; // no need to hit API for outputs, value is in outputs[i].value

    var inputsWork = inputs.map((i) =&gt; { // map the inputs to an array of promises which we can run later
      return new Promise((resolve, reject) =&gt; {
        var transactionId = Buffer.from(i.hash).reverse().toString('hex'); // derive the transaction id this way
        var outputIndex = i.index; // output index is also needed
        getTransaction(transactionId)
          .then((transaction) =&gt; { //this transaction belongs to the looped input (i)
            var outputs = transaction.vout; // get outputs of transaction
            var output = outputs.find((i) =&gt; (i.n === outputIndex)); // match the input/output indexes
            resolve(output.value); // resolve with our output value
          })
      })
    });

    Promise.all(inputsWork)
      .then((inputValues) =&gt; { // execute array of promises for inputs

        var totalInputs = inputValues.reduce((a,b) =&gt; (a+b)); // add up the input amounts
        var totalOutputs = outputs.reduce((a,b) =&gt; (a.value+b.value)); // add up the output amounts
        totalOutputs = totalOutputs / 100000000; // outputs in satoshis

        var feesIncurred = totalInputs - totalOutputs; // fees of a transaction is input amount - output amount
        console.log(feesIncurred); // log the fees to your console
      })

  })
</code></pre>

<p>Cheers!</p>

CC BY-SA 3.0
2017-12-14T22:26:12.173Z
2017-12-14T22:39:20.480Z
2017-12-14T22:39:20.480Z
Monstrum
<blockquote>
  <p>How should I process this data so I can get total in and out amounts and calculate transaction fee?</p>
</blockquote>

<p>Each <code>in</code> contains a (hash, index) to a previous output in the blockchain. Assuming you have a full node with <code>txindex=1</code> meaning there's a full index of all previous transactions. You can use (hash, index) to find the original UTXO that carries the actual value.</p>

<p>Therefore, <code>transaction fees = sum of previous outputs - sum of current outputs</code></p>

CC BY-SA 3.0
2017-12-11T12:51:12.500Z
2017-12-11T12:51:12.500Z
rny
<p>I need to calculate fees for each transaction in block.</p>

<p>I retrieve raw block data and use <code>bitcoinjs-lib</code> library to decode it to <code>Block</code> object:</p>

<pre><code>const block = Bitcoin.Block.fromHex(data.rawblock);
</code></pre>

<p>It has an array of <code>Transaction</code> objects. However, while these transactions' <code>outs</code> have <code>value</code>s, <code>ins</code> have only <code>hash</code> and <code>script</code>.</p>

<p>How should I process this data so I can get total in and out amounts and calculate transaction fee?</p>

<transactions><raw-data><bitcoinjs>
How to decode transaction inputs from raw data using bitcoinjs-lib?
keaukraine
# How to decode transaction inputs from raw data using bitcoinjs-lib?

(Asked by: username_0 on 2017-12-08)

I need to calculate fees for each transaction in block.

I retrieve raw block data and use `bitcoinjs-lib` library to decode it to `Block` object:

```
const block = Bitcoin.Block.fromHex(data.rawblock);

```

It has an array of `Transaction` objects. However, while these transactions' `outs` have `value`s, `ins` have only `hash` and `script`.

How should I process this data so I can get total in and out amounts and calculate transaction fee?

**Tags:** transactions, raw-data, bitcoinjs


---

## 2 answers

### Answer by username_1 on 2017-12-14. Score: 2 | Accepted answer

You will, by default, only see the values of the outputs and not the inputs. To gather all values for inputs, you will need to loop through your inputs and use an API to gather the transactions where the inputs were once outputs.

I used the BitPay insight API and left some notes in the code below:

```
var Bitcoin = require('bitcoinjs-lib');
var request = require('request');

function getRawBlock(blockHash) {
  return new Promise((resolve, reject) => {
    request('https://insight.bitpay.com/api/rawblock/' + blockHash, // hitting an insight API to get the full block
      (error, response, body) => {
        try {
          var block = JSON.parse(body); // result is in JSON
          resolve(block.rawblock)
        } catch(error) {
          reject(error)
        }
      })
  })
}

function getTransaction(transactionId) {
  return new Promise((resolve, reject) => {
    request('https://insight.bitpay.com/api/tx/' + transactionId, // hitting an insight API to get the full transaction
      (error, response, body) => {
        try {
          var transaction = JSON.parse(body); // result is in JSON
          resolve(transaction);
        } catch(e) {
          reject(e)
        }
      });
  })
}

getRawBlock('000000000000000000aabc9831a830ab8542137c68998368b86f9127432767d5')
  .then((rawBlock) => {

    var block = Bitcoin.Block.fromHex(rawBlock); // use bitcoinjs-lib to convert the raw block to usable data
    var transactions = block.transactions; // transactions reside here
    var transaction = transactions[1]; // using second transaction as an example since the first one is miner reward
    // you can loop the transactions the way you like

    var inputs = transaction.ins; // will need to use the API to get the outputs that each input once was
    var outputs = transaction.outs; // no need to hit API for outputs, value is in outputs[i].value

    var inputsWork = inputs.map((i) => { // map the inputs to an array of promises which we can run later
      return new Promise((resolve, reject) => {
        var transactionId = Buffer.from(i.hash).reverse().toString('hex'); // derive the transaction id this way
        var outputIndex = i.index; // output index is also needed
        getTransaction(transactionId)
          .then((transaction) => { //this transaction belongs to the looped input (i)
            var outputs = transaction.vout; // get outputs of transaction
            var output = outputs.find((i) => (i.n === outputIndex)); // match the input/output indexes
            resolve(output.value); // resolve with our output value
          })
      })
    });

    Promise.all(inputsWork)
      .then((inputValues) => { // execute array of promises for inputs

        var totalInputs = inputValues.reduce((a,b) => (a+b)); // add up the input amounts
        var totalOutputs = outputs.reduce((a,b) => (a.value+b.value)); // add up the output amounts
        totalOutputs = totalOutputs / 100000000; // outputs in satoshis

        var feesIncurred = totalInputs - totalOutputs; // fees of a transaction is input amount - output amount
        console.log(feesIncurred); // log the fees to your console
      })

  })

```

Cheers!


---

### Answer by username_2 on 2017-12-11. Score: 1

> How should I process this data so I can get total in and out amounts and calculate transaction fee?

Each `in` contains a (hash, index) to a previous output in the blockchain. Assuming you have a full node with `txindex=1` meaning there's a full index of all previous transactions. You can use (hash, index) to find the original UTXO that carries the actual value.

Therefore, `transaction fees = sum of previous outputs - sum of current outputs`


---

