import { ApiPromise } from '@polkadot/api' ; import { BlockNumber , EventRecord , Hash } from '@polkadot/types/interfaces' ; import { logger } from '@polkadot/util' ; import { prisma } from '../generated/prisma-client' ; import { filterEvents } from '../util/filterEvents' ; import { Cached , NomidotHeartBeat , Task } from './types' ; const l = logger ( 'Task: HeartBeat' ) ; const createHeartBeat = { name : 'createHeartBeat' , read : ( _blockHash , cached , _api ) : Promise < NomidotHeartBeat [ ] > => { const { events , sessionIndex } = cached ; const heartbeatEvents = filterEvents ( events , 'imOnline' , 'HeartbeatReceived' ) ; const result = [ ] ; if ( heartbeatEvents ) { heartbeatEvents . map ( ( { event : { data } } ) => { data . map ( authorityId => { result . push ( { authorityId , sessionIndex , } as NomidotHeartBeat ) ; } ) ; } ) ; } l . log ( `Heartbeat: ${ JSON . stringify ( result ) }` ) ; return Promise . resolve ( result ) ; } , write : async ( blockNumber , heartbeats ) => { await Promise . all ( heartbeats . map ( async ( { authorityId , sessionIndex } ) => { await prisma . createHeartBeat ( { sessionIndex : { connect : { index : sessionIndex . toNumber ( ) , } , } , authorityId : authorityId . toString ( ) , } ) ; } ) ) ; } , } ; export default createHeartBeat ;