How to decode transaction inputs from raw data using bitcoinjs-lib? 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 values, 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?
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) =&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
      })

  })


Cheers!