# All files / src/api/methods getBlockByHash.js

100% Statements 16/16
100% Branches 7/7
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    383x
    3x
    3x

    3x
    3x

    3x
    4x
    3x
    3x

    2x
    2x
    2x

    2x

    2x

     const { Block } = require('leap-core');
    const { EMPTY_ADDRESS } = require('../../utils/constants');
    const txResponse = require('./txResponse');

    const NULL_HASH =
      '0x0000000000000000000000000000000000000000000000000000000000000000';
    const NA = 0;

    module.exports = async (db, hash, showFullTxs = false) => {
      if (!hash) return null;
      const blockDoc = await db.getBlock(hash);
      if (!blockDoc) return null;

      const { blockData } = blockDoc;
      const block = Block.fromJSON(blockData);
      const txs = !showFullTxs
        ? block.txHashList
        : await Promise.all(
            block.txList.map((tx, pos) =>
              txResponse(db, tx, block.hash(), block.height, pos)
            )
          );
      return {
        number: `0x${block.height.toString(16)}`,
        hash: block.hash(),
        parentHash: NULL_HASH,
        difficulty: NA,
        gasLimit: NA,
        gasUsed: NA,
        miner: EMPTY_ADDRESS,
        extraData: NA,
        size: `0x${block.hex().length.toString(16)}`,
        timestamp: `0x${block.timestamp.toString(16)}`,
        transactions: txs,
        uncles: [],
      };
    };