( api: ApiPromise, height: number, )
| 435 | |
| 436 | // TODO why is fetchBlocksBatches a breadth first function rather than depth? |
| 437 | export async function fetchLightBlock( |
| 438 | api: ApiPromise, |
| 439 | height: number, |
| 440 | ): Promise<IBlock<LightBlockContent>> { |
| 441 | const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => { |
| 442 | logger.error(`failed to fetch BlockHash ${height}`); |
| 443 | throw ApiPromiseConnection.handleError(e); |
| 444 | }); |
| 445 | |
| 446 | const [header, events, timestamp] = await Promise.all([ |
| 447 | api.rpc.chain.getHeader(blockHash).catch((e) => { |
| 448 | logger.error( |
| 449 | `failed to fetch Block Header hash="${blockHash}" height="${height}"`, |
| 450 | ); |
| 451 | throw ApiPromiseConnection.handleError(e); |
| 452 | }), |
| 453 | api.query.system.events.at(blockHash).catch((e) => { |
| 454 | logger.error(`failed to fetch events at block ${blockHash}`); |
| 455 | throw ApiPromiseConnection.handleError(e); |
| 456 | }), |
| 457 | // TODO: Maybe api.query.timestamp.now.at(blockHash) is the only option. If we do use it we need sufficient tests and errors if a chain doesn't support getting the timestamp. |
| 458 | (await api.at(blockHash)).query.timestamp.now(), |
| 459 | ]); |
| 460 | |
| 461 | const blockHeader: BlockHeader = { |
| 462 | block: { header }, |
| 463 | events: events.toArray(), |
| 464 | }; |
| 465 | return { |
| 466 | block: { |
| 467 | block: blockHeader, |
| 468 | events: events.map((evt, idx) => merge(evt, { idx, block: blockHeader })), |
| 469 | }, |
| 470 | getHeader: () => { |
| 471 | return { |
| 472 | ...substrateHeaderToHeader(blockHeader.block.header), |
| 473 | timestamp: new Date(timestamp.toNumber()), |
| 474 | }; |
| 475 | }, |
| 476 | }; |
| 477 | } |
| 478 | |
| 479 | export async function fetchBlocksBatchesLight( |
| 480 | api: ApiPromise, |
no test coverage detected