Skip to content

Examples: Get BlockChain info

Chun Lam edited this page Feb 21, 2024 · 4 revisions
import { 
    ChainHttp, BlockHttp, DiagnosticHttp, TransactionQueryParams,
    LimitType
} from "tsjs-xpx-chain-sdk";

const API_URL = 'http://localhost:3000';

const chainHttp = new ChainHttp(API_URL);
const blockHttp = new BlockHttp(API_URL);
const diagnosticHttp = new DiagnosticHttp(API_URL);

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

const getTransactionsFromAblock = () => {

    let txnQP = new TransactionQueryParams();
    txnQP.pageSize = 100;

    blockHttp.getBlockTransactions(1, txnQP).subscribe({
        next: transacions => {
            transacions.forEach(tx => {
                console.log(tx);
            });
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log("done.");
        }
    });
}

const getCurrentHeightOfTheChain = () => {
    chainHttp.getBlockchainHeight().subscribe({
        next: height => {
            console.log(height.toBigInt());
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log("done.");
        }
    });
}

const getCurrentScoreOfTheChain = () => {
    chainHttp.getBlockchainScore().subscribe({
        next: score => {
            console.log(score);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log("done.");
        }
    });
}

const getTheStorageInformation = () => {
    diagnosticHttp.getDiagnosticStorage().subscribe({
        next: blockchainStorageInfo => {
            console.log(blockchainStorageInfo);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log("done.");
        }
    });
}

const getAnArrayOfBlockInfo = () => {
    blockHttp.getBlocksByHeightWithLimit(1, LimitType.N_100).subscribe({
        next: blockInfos => {
            blockInfos.forEach(blockInfo => {
                console.log(blockInfo);
            });
            console.log(blockInfos.length);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log("done.");
        }
    });
}

getBlockInfoForGivenBlockHeight();
getTransactionsFromAblock();
getCurrentHeightOfTheChain();
getCurrentScoreOfTheChain();
getTheStorageInformation();
getAnArrayOfBlockInfo();