Getting a block by height

Get the block information given a height.

Prerequisites

Method #01: Using the SDK

What happened in the genesis block? Run the following code to get the first block header recorded on the blockchain.

// replace with node endpoint
const nodeUrl = 'NODE_URL';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const blockHttp = repositoryFactory.createBlockRepository();

const height = 1;
blockHttp.getBlockByHeight(UInt64.fromUint(height)).subscribe(
  (block) => console.log(block),
  (err) => console.error(err),
);
// replace with node endpoint
const nodeUrl = 'NODE_URL';
const repositoryFactory = new bitxor_sdk_1.RepositoryFactoryHttp(nodeUrl);
const blockHttp = repositoryFactory.createBlockRepository();
const height = 1;
blockHttp.getBlockByHeight(bitxor_sdk_1.UInt64.fromUint(height)).subscribe(
  (block) => console.log(block),
  (err) => console.error(err),
);
        // replace with node endpoint
        try (final RepositoryFactory repositoryFactory = new RepositoryFactoryVertxImpl(
            "NODE_URL")) {
            final BlockRepository blockRepository = repositoryFactory.createBlockRepository();

            // Replace with block height
            final BigInteger blockHeight = BigInteger.valueOf(1);
            final BlockInfo blockInfo = blockRepository.getBlockByHeight(blockHeight).toFuture()
                .get();
            final JsonHelper helper = new JsonHelperJackson2();
            System.out.println(helper.prettyPrint(blockInfo));
        }

The following snippet returns the height of the latest block.

// replace with node endpoint
const nodeUrl = 'NODE_URL';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();

chainHttp.getChainInfo().subscribe(
  (info: ChainInfo) => console.log(info.height.compact()),
  (err) => console.error(err),
);
// replace with node endpoint
const nodeUrl = 'NODE_URL';
const repositoryFactory = new bitxor_sdk_1.RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();
chainHttp.getChainInfo().subscribe(
  (info) => console.log(info.height.compact()),
  (err) => console.error(err),
);

Method #02: Using the CLI

Get the block header:

bitxor-cli block header --height 1

Get the height of the blockchain:

bitxor-cli chain height