@@ -7,7 +7,7 @@ import { Asset, NonFungible, NonFungibleCall, NewNonFungible, Unique } from '../
77import * as a from './assets'
88import * as n from './nfts'
99import * as u from './uniques'
10- import { BatchContext , Context , SelectedEvent } from './utils/types'
10+ import { BatchContext , Block , Context , SelectedEvent } from './utils/types'
1111import { ParachainSystemCall } from '../processable'
1212import { updateSwapsCache } from './utils/cache'
1313
@@ -200,6 +200,18 @@ const globalHandler: Record<string, HandlerFunction> = {
200200 Assets : assets ,
201201}
202202
203+ function getRelayParentNumber ( block : Block ) : number | undefined {
204+ let relayParentNumber : number | undefined
205+
206+ const validationCall = block . calls ?. find ( ( c ) => c . name === ParachainSystemCall . setValidationData )
207+
208+ if ( validationCall ) {
209+ relayParentNumber = validationCall . args ?. data ?. validationData ?. relayParentNumber as number | undefined
210+ }
211+
212+ return relayParentNumber
213+ }
214+
203215/**
204216 * mainFrame is the main entry point for processing a batch of blocks
205217**/
@@ -216,13 +228,8 @@ export async function mainFrame(ctx: BatchContext<Store>): Promise<void> {
216228 )
217229
218230 for ( const block of ctx . blocks ) {
219- let relayParentNumber : number | undefined
231+ const relayParentNumber = getRelayParentNumber ( block )
220232
221- const validationCall = block . calls ?. find ( ( c ) => c . name === ParachainSystemCall . setValidationData )
222-
223- if ( validationCall ) {
224- relayParentNumber = validationCall . args ?. data ?. validationData ?. relayParentNumber as number | undefined
225- }
226233
227234 for ( let event of block . events ) {
228235 logger . debug ( `Processing ${ event . name } ` )
@@ -244,10 +251,11 @@ export async function mainFrame(ctx: BatchContext<Store>): Promise<void> {
244251 }
245252
246253 if ( ctx . isHead ) {
247- const lastBlock = ctx . blocks [ ctx . blocks . length - 1 ] . header
248- const lastDate = new Date ( lastBlock . timestamp || Date . now ( ) )
254+ const lastBlock = ctx . blocks [ ctx . blocks . length - 1 ]
255+ const relayParentNumber = getRelayParentNumber ( lastBlock )
256+ const lastDate = new Date ( lastBlock . header . timestamp || Date . now ( ) )
249257 logger . info ( `Found head block, updating cache` )
250- await updateSwapsCache ( lastDate , lastBlock . height , ctx . store )
258+ await updateSwapsCache ( lastDate , ( relayParentNumber || lastBlock . header . height ) , ctx . store )
251259 }
252260}
253261
0 commit comments