From 85c2e35b02e92de06f914b26904024281249102a Mon Sep 17 00:00:00 2001 From: Josep M Sobrepere Date: Tue, 17 Jun 2025 14:12:46 +0200 Subject: [PATCH] feat: load events from old blocks --- src/pages/Explorer/block.state.ts | 71 +++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/pages/Explorer/block.state.ts b/src/pages/Explorer/block.state.ts index 4efad75..5f0ffb7 100644 --- a/src/pages/Explorer/block.state.ts +++ b/src/pages/Explorer/block.state.ts @@ -61,6 +61,7 @@ export interface BlockInfo { status: BlockState diff: Record | null } + export const [blockInfo$, recordedBlocks$] = partitionByKey( client$.pipe(switchMap((client) => client.blocks$)), (v) => v.hash, @@ -117,31 +118,56 @@ export const [blockInfo$, recordedBlocks$] = partitionByKey( ), ) -const getUnpinnedBlockInfo$ = (hash: string): Observable => { - const throughRpc$ = chainClient$.pipe( +const getUnpinnedBlockInfo$ = (hash: string): Observable => + client$.pipe( switchMap((client) => - defer(() => - client.client._request< - { - block: { - extrinsics: HexString[] - header: { - digest: { logs: Array } - extrinsicsRoot: string - number: HexString - parentHash: HexString - stateRoot: HexString - } - } - } | null, - [string] - >("chain_getBlock", [hash]), - ).pipe( - repeat({ - delay: 1000, - }), + from(client.getBlockHeader(hash)).pipe( + mergeMap((header) => + combineLatest({ + hash: of(hash), + parent: of(header.parentHash), + number: of(header.number), + body: client.watchBlockBody(hash), + events: from( + client.getUnsafeApi().query.System.Events.getValue({ + at: hash, + }), + ), + header: from(client.getBlockHeader(hash)), + status: of(BlockState.Finalized), + diff: of(null), + }), + ), + catchError(() => getUnpinnedBlockInfoFallback$(hash, client)), + tap((v) => disconnectedBlocks$.next(v)), ), ), + ) + +const getUnpinnedBlockInfoFallback$ = ( + hash: string, + client: PolkadotClient, +): Observable => { + const throughRpc$ = defer(() => + client._request< + { + block: { + extrinsics: HexString[] + header: { + digest: { logs: Array } + extrinsicsRoot: string + number: HexString + parentHash: HexString + stateRoot: HexString + } + } + } | null, + [string] + >("chain_getBlock", [hash]), + ).pipe( + repeat({ + delay: 1000, + }), filter((v) => !!v), take(1), catchError(() => EMPTY), @@ -166,7 +192,6 @@ const getUnpinnedBlockInfo$ = (hash: string): Observable => { diff: null, }), ), - tap((v) => disconnectedBlocks$.next(v)), ) }