( api: ApiPromise, height: number, )
| 322 | } |
| 323 | |
| 324 | export async function getHeaderByHeight( |
| 325 | api: ApiPromise, |
| 326 | height: number, |
| 327 | ): Promise<SubstrateHeader> { |
| 328 | const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => { |
| 329 | logger.error(`failed to fetch BlockHash ${height}`); |
| 330 | throw ApiPromiseConnection.handleError(e); |
| 331 | }); |
| 332 | |
| 333 | const header = await api.rpc.chain.getHeader(blockHash).catch((e) => { |
| 334 | logger.error( |
| 335 | `failed to fetch Block Header hash="${blockHash}" height="${height}"`, |
| 336 | ); |
| 337 | throw ApiPromiseConnection.handleError(e); |
| 338 | }); |
| 339 | // validate block is valid |
| 340 | if (header.hash.toHex() !== blockHash.toHex()) { |
| 341 | throw new Error( |
| 342 | `fetched block header hash ${header.hash.toHex()} is not match with blockHash ${blockHash.toHex()} at block ${height}. This is likely a problem with the rpc provider.`, |
| 343 | ); |
| 344 | } |
| 345 | return header; |
| 346 | } |
| 347 | |
| 348 | export async function fetchBlocksArray( |
| 349 | api: ApiPromise, |
no test coverage detected