Skip to content

Examples: Get BlockChain info

yaaccount edited this page May 22, 2019 · 4 revisions
import { BlockchainHttp, NetworkType, Order, BlockchainStorageInfo } from "tsjs-xpx-catapult-sdk";

const API_URL = 'http://localhost:3000';
const NETWORK_TYPE = NetworkType.MIJIN_TEST;

const blockachainHttp = new BlockchainHttp(API_URL);

const getBlockInfoForGivenBlockHeight = () => {
    blockachainHttp.getBlockByHeight(1).subscribe(blockInfo => {
        console.log(blockInfo);
    }, error => {
        console.error(error);
    }, () => {
        console.log("done.");
    })
};

const getTransactionsFromAblock = () => {
    blockachainHttp.getBlockTransactions(1, { pageSize: 100, order: Order.ASC }).subscribe(transacions => {
        transacions.forEach(tx => {
            console.log(tx);
        });
    }, error => {
        console.error(error);
    }, () => {
        console.log("done.");
    })
}

const getCurrentHeightOfTheChain = () => {
    blockachainHttp.getBlockchainHeight().subscribe(height => {
        console.log(height.compact());
    }, error => {
        console.error(error);
    }, () => {
        console.log("done.");
    })
}

const getCurrentScoreOfTheChain = () => {
    blockachainHttp.getBlockchainScore().subscribe(score => {
        console.log(score);
    }, error => {
        console.error(error);
    }, () => {
        console.log("done.");
    })
}

const getTheStorageInformation = () => {
    blockachainHttp.getDiagnosticStorage().subscribe(blockchainStorageInfo => {
        console.log(blockchainStorageInfo);
    }, error => {
        console.error(error);
    }, () => {
        console.log("done.");
    })
}

const getAnArrayOfBlockInfo = () => {
    blockachainHttp.getBlocksByHeightWithLimit(1, 20).subscribe(blockInfos => {
        blockInfos.forEach(blockInfo => {
            console.log(blockInfo);
        });
        console.log(blockInfos.length);
    }, error => {
        console.error(error);
    }, () => {
        console.log("done.");
    });
}

export {
    getBlockInfoForGivenBlockHeight,
    getTransactionsFromAblock,
    getCurrentHeightOfTheChain,
    getCurrentScoreOfTheChain,
    getTheStorageInformation,
    getAnArrayOfBlockInfo
}
Clone this wiki locally