@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
81 lines (80 loc) • 4.12 kB
TypeScript
import { ApiPromise } from '@polkadot/api';
import { BlockHash } from '@polkadot/types/interfaces';
import BN from 'bn.js';
/**
* Result of searching for a parachain block's inclusion on the relay chain.
*/
export interface IInclusionSearchResult {
/** The relay chain block number where the parachain block was included */
inclusionBlockNumber: number | null;
/** The relay parent number from setValidationData */
relayParentNumber: number;
/** Whether the inclusion was found */
found: boolean;
}
/**
* Extract the relay chain parent block number from a parachain block.
*
* Parachain blocks contain a `parachainSystem.setValidationData` extrinsic that
* includes the relay chain block number that was the parent when the parachain
* block was created.
*
* Note: This is the relay PARENT, not the inclusion block. For the actual
* inclusion block, use `getInclusionBlockNumber` or `searchForInclusionBlock`.
*
* @param api - The ApiPromise instance connected to the parachain
* @param blockHash - The hash of the parachain block to extract relay parent from
* @returns The relay chain parent block number as a BN
* @throws Error if the block doesn't contain setValidationData extrinsic
*/
export declare const getRelayParentNumber: (api: ApiPromise, blockHash: BlockHash) => Promise<BN>;
/**
* Extract relay parent number as a plain number (for use with search functions).
*
* @param api - The ApiPromise instance connected to the parachain
* @param blockHash - The hash of the parachain block
* @returns The relay chain parent block number as a number
*/
export declare const getRelayParentNumberRaw: (api: ApiPromise, blockHash: BlockHash) => Promise<number>;
/**
* Check if a block contains the setValidationData extrinsic.
*
* This can be used to determine if a block is from a parachain (has validation data)
* or a relay chain (does not have validation data).
*
* @param api - The ApiPromise instance
* @param blockHash - The hash of the block to check
* @returns true if the block contains setValidationData, false otherwise
*/
export declare const hasRelayParentData: (api: ApiPromise, blockHash: BlockHash) => Promise<boolean>;
/**
* Search relay chain blocks for the inclusion of a specific parachain block.
*
* This searches relay chain blocks starting from `relayParentNumber + 1` looking
* for a `paraInclusion.CandidateIncluded` event that matches the given parachain
* block number and paraId.
*
* @param rcApi - The ApiPromise instance connected to the relay chain
* @param paraId - The parachain ID (e.g., 1000 for Asset Hub)
* @param parachainBlockNumber - The parachain block number to search for
* @param relayParentNumber - The relay parent number from setValidationData
* @param maxDepth - Maximum number of relay blocks to search (default: 10)
* @returns The relay chain block number where inclusion was found, or null if not found
*/
export declare const searchForInclusionBlock: (rcApi: ApiPromise, paraId: number, parachainBlockNumber: string, relayParentNumber: number, maxDepth?: number) => Promise<number | null>;
/**
* Get the relay chain block number where a parachain block was included.
*
* This is the main function for finding the accurate relay chain block number
* corresponding to a parachain block. It:
* 1. Extracts the relayParentNumber from the parachain block's setValidationData
* 2. Searches relay chain blocks for the actual inclusion event
*
* @param parachainApi - The ApiPromise instance connected to the parachain
* @param rcApi - The ApiPromise instance connected to the relay chain
* @param parachainBlockHash - The hash of the parachain block
* @param paraId - The parachain ID (e.g., 1000 for Asset Hub)
* @param maxDepth - Maximum number of relay blocks to search (default: 10)
* @returns The inclusion search result with inclusionBlockNumber, relayParentNumber, and found status
*/
export declare const getInclusionBlockNumber: (parachainApi: ApiPromise, rcApi: ApiPromise, parachainBlockHash: BlockHash, paraId: number, maxDepth?: number) => Promise<IInclusionSearchResult>;