Skip to content

Examples: Get Transactions info

Chun Lam edited this page Feb 21, 2024 · 5 revisions
import { 
    TransactionHttp, TransactionGroupType, TransactionQueryParams 
} from "tsjs-xpx-chain-sdk";

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

const transactionHttp = new TransactionHttp(API_URL);

const getTransactionInformationOfTxIdOrHash = () => {
    const txHash = '6C274716520ABCDCE0BB799CF6F82B64EBC9B954DBE3B14FD5767CD1A89970BE';
    const txId = '5DA2474DDD19C600014C9084';
    transactionHttp.getTransaction(txHash).subscribe({
        next: transaction => {
            console.log(transaction);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });

    transactionHttp.getTransaction(txId).subscribe({
        next: transaction => {
            console.log(transaction);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });
}

const getTransactionInformationOfSetOfTxIdsOrHashes = () => {
    const txHash1 = '6C274716520ABCDCE0BB799CF6F82B64EBC9B954DBE3B14FD5767CD1A89970BE';
    const txHash2 = 'BE34D62D7410F2DE7F70F423647F1D983FD315FAE44576A75714CB902355FC72';
    const txId1 = '5DA2474DDD19C600014C9084';
    const txId2 = '5DA2474DDD19C600014C9085';
    transactionHttp.getTransactions([txHash1, txHash2]).subscribe({
        next: transaction => {
            console.log(transaction);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });

    transactionHttp.getTransactions([txId1, txId2]).subscribe({
        next: transaction => {
            console.log(transaction);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });
}

const getTransactionStatusOfTxHash = () => {
    const txHash = '6C274716520ABCDCE0BB799CF6F82B64EBC9B954DBE3B14FD5767CD1A89970BE';
    transactionHttp.getTransactionStatus(txHash).subscribe({
        next: status => {
            console.log(status);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });
}

const getTransactionStatusesOfSetOfTxHashes = () => {
    const txHash1 = '6C274716520ABCDCE0BB799CF6F82B64EBC9B954DBE3B14FD5767CD1A89970BE';
    const txHash2 = 'BE34D62D7410F2DE7F70F423647F1D983FD315FAE44576A75714CB902355FC72';
    transactionHttp.getTransactionsStatuses([txHash1, txHash2]).subscribe({
        next: status => {
            console.log(status);
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });
}

const searchTransactions = (groupType) => {
    transactionHttp.searchTransactions(groupType).subscribe({
        next: transactionsSearchResult => {
            console.log(transactionsSearchResult.transactions); // transactions list
            console.log(transactionsSearchResult.pagination); // pagination info
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });
}

const searchTransactionsWithQueryParams = (groupType) => {

    // you can see the full structure of the transactionQueryParams, all are optional
    /*
        pageSize?: number;
        pageNumber?: number;
        type?: TransactionType[] | number[];
        embedded?: boolean;
        order?: Order;
        sortField?: TransactionSortingField;
        toHeight?: number;
        fromHeight?: number;
        height?: number;
        signerPublicKey?: string;
        recipientAddress?: string;
        address?: string;
    */
    let txnQueryParams = new TransactionQueryParams();
    txnQueryParams.embedded = true; // set embedded to true, it will return the embedded txn in the aggregate txn

    transactionHttp.searchTransactions(groupType, txnQueryParams).subscribe({
        next: transactionsSearchResult => {
            console.log(transactionsSearchResult.transactions); // transactions list
            console.log(transactionsSearchResult.pagination); // pagination info
        }, 
        error: error => {
            console.error(error);
        }, 
        complete: () => {
            console.log('done.');
        }
    });
}

getTransactionInformationOfTxIdOrHash();
getTransactionInformationOfSetOfTxIdsOrHashes();
getTransactionStatusOfTxHash();
getTransactionStatusesOfSetOfTxHashes();
searchTransactions(TransactionGroupType.CONFIRMED);
searchTransactionsWithQueryParams(TransactionGroupType.PARTIAL);