import { Injectable } from '@nestjs/common' ; import { OnEvent } from '@nestjs/event-emitter' ; import { Interval } from '@nestjs/schedule' ; import { BestBlockPayload , EventPayload , IndexerEvent , NetworkMetadataPayload , ProcessBlockPayload , TargetBlockPayload , } from '../indexer/events' ; import { StoreService } from '../indexer/store.service' ; const UPDATE_HEIGHT_INTERVAL = 60000 ; const { version : polkadotSdkVersion } = require ( '@polkadot/api/package.json' ) ; const { version : packageVersion } = require ( '../../package.json' ) ; @ Injectable ( ) export class MetaService { private currentProcessingHeight : number ; private currentProcessingTimestamp : number ; private bestHeight : number ; private targetHeight : number ; private networkMeta : NetworkMetadataPayload ; private apiConnected : boolean ; private usingDictionary : boolean ; private injectedApiConnected : boolean ; private lastProcessedHeight : number ; private lastProcessedTimestamp : number ; constructor ( private storeService ) { } getMeta ( ) { return { currentProcessingHeight : this . currentProcessingHeight , currentProcessingTimestamp : this . currentProcessingTimestamp , targetHeight : this . targetHeight , bestHeight : this . bestHeight , indexerNodeVersion : packageVersion , lastProcessedHeight : this . lastProcessedHeight , lastProcessedTimestamp : this . lastProcessedTimestamp , uptime : process . uptime ( ) , polkadotSdkVersion , apiConnected : this . apiConnected , injectedApiConnected : this . injectedApiConnected , usingDictionary : this . usingDictionary , ... this . networkMeta , } ; } @ Interval ( UPDATE_HEIGHT_INTERVAL ) async getTargetHeight ( ) { await this . storeService . setMetadata ( 'targetHeight' , this . targetHeight ) ; } @ OnEvent ( IndexerEvent . BlockProcessing ) handleProcessingBlock ( blockPayload ) { this . currentProcessingHeight = blockPayload . height ; this . currentProcessingTimestamp = blockPayload . timestamp ; } @ OnEvent ( IndexerEvent . BlockTarget ) handleTargetBlock ( blockPayload ) { this . targetHeight = blockPayload . height ; } @ OnEvent ( IndexerEvent . BlockBest ) handleBestBlock ( blockPayload ) { this . bestHeight = blockPayload . height ; } @ OnEvent ( IndexerEvent . NetworkMetadata ) handleNetworkMetadata ( networkMeta ) { this . networkMeta = networkMeta ; } @ OnEvent ( IndexerEvent . ApiConnected ) handleApiConnected ( { value } ) { this . apiConnected = ! ! value ; } @ OnEvent ( IndexerEvent . InjectedApiConnected ) handleInjectedApiConnected ( { value } ) { this . injectedApiConnected = ! ! value ; } @ OnEvent ( IndexerEvent . UsingDictionary ) handleUsingDictionary ( { value } ) { this . usingDictionary = ! ! value ; } }