@@ -6,7 +6,12 @@ import { CosmWasmClient, IndexedTx } from '@cosmjs/cosmwasm-stargate';
66import { toHex } from '@cosmjs/encoding' ;
77import { GeneratedType , Registry } from '@cosmjs/proto-signing' ;
88import { defaultRegistryTypes , SearchTxQuery } from '@cosmjs/stargate' ;
9- import { CometClient } from '@cosmjs/tendermint-rpc' ;
9+ import {
10+ Comet38Client ,
11+ CometClient ,
12+ Tendermint34Client ,
13+ Tendermint37Client ,
14+ } from '@cosmjs/tendermint-rpc' ;
1015import { Injectable , OnApplicationShutdown } from '@nestjs/common' ;
1116import { EventEmitter2 } from '@nestjs/event-emitter' ;
1217import { CosmosProjectNetConfig } from '@subql/common-cosmos' ;
@@ -203,6 +208,25 @@ export class CosmosClient extends CosmWasmClient {
203208 super ( _cometClient as any ) ;
204209 }
205210
211+ async getBlockInterval ( ) : Promise < number > {
212+ const { blockMetas } = await this . _cometClient . blockchain ( ) ;
213+
214+ if ( ! blockMetas || blockMetas . length < 2 ) {
215+ throw new Error (
216+ `Insufficient block headers to calculate interval: ${
217+ blockMetas ?. length ?? 0
218+ } headers available`,
219+ ) ;
220+ }
221+
222+ const timestamps = blockMetas . map ( ( h ) => h . header . time . getTime ( ) ) ;
223+
224+ return Math . abs (
225+ timestamps . slice ( 1 ) . reduce ( ( sum , v , i ) => sum + ( v - timestamps [ i ] ) , 0 ) /
226+ ( timestamps . length - 1 ) ,
227+ ) ;
228+ }
229+
206230 // eslint-disable-next-line @typescript-eslint/require-await
207231 async blockInfo ( height ?: number ) : Promise < BlockResponse > {
208232 return this . _cometClient . block ( height ) ;
0 commit comments