( api: ApiPromise, height: number, )
| 297 | */ |
| 298 | |
| 299 | export async function getBlockByHeight( |
| 300 | api: ApiPromise, |
| 301 | height: number, |
| 302 | ): Promise<SignedBlock> { |
| 303 | const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => { |
| 304 | logger.error(`failed to fetch BlockHash ${height}`); |
| 305 | throw ApiPromiseConnection.handleError(e); |
| 306 | }); |
| 307 | |
| 308 | const block = await api.rpc.chain.getBlock(blockHash).catch((e) => { |
| 309 | logger.error( |
| 310 | `failed to fetch Block hash="${blockHash}" height="${height}"${getApiDecodeErrMsg(e.message)}`, |
| 311 | ); |
| 312 | throw ApiPromiseConnection.handleError(e); |
| 313 | }); |
| 314 | |
| 315 | // validate block is valid |
| 316 | if (block.block.header.hash.toHex() !== blockHash.toHex()) { |
| 317 | throw new Error( |
| 318 | `fetched block header hash ${block.block.header.hash.toHex()} is not match with blockHash ${blockHash.toHex()} at block ${height}. This is likely a problem with the rpc provider.`, |
| 319 | ); |
| 320 | } |
| 321 | return block; |
| 322 | } |
| 323 | |
| 324 | export async function getHeaderByHeight( |
| 325 | api: ApiPromise, |
no test coverage detected