import { ApiPromise } from '@polkadot/api' ; import { BlockNumber , EventRecord , FullIdentification , Hash , IdentificationTuple , ValidatorId , } from '@polkadot/types/interfaces' ; import { logger } from '@polkadot/util' ; import { prisma } from '../generated/prisma-client' ; import { filterEvents } from '../util/filterEvents' ; import { Cached , NomidotOfflineValidator , Task } from './types' ; const l = logger ( 'Task: OfflineValidator' ) ; const createOfflineValidator = { name : 'createOfflineValidator' , read : ( _blockHash , cached , _api ) : Promise < NomidotOfflineValidator [ ] > => { const { events , sessionIndex } = cached ; const someOfflineEvents = filterEvents ( events , 'imOnline' , 'SomeOffline' ) ; const result = [ ] ; if ( someOfflineEvents && someOfflineEvents . length ) { someOfflineEvents . map ( ( { event : { data } } ) => { data . map ( idTuples => { idTuples . map ( ( idTuple ) => { const validatorId = idTuple [ 0 ] ; const fullId = idTuple [ 1 ] ; const { total , own , others } = fullId ; result . push ( { sessionIndex , validatorId , total , own , others , } as NomidotOfflineValidator ) ; } ) ; } ) ; } ) ; } l . log ( `Offline Validators : ${ JSON . stringify ( result ) }` ) ; return Promise . resolve ( result ) ; } , write : async ( blockNumber , offlineValidators ) => { await Promise . all ( offlineValidators . map ( async ( offlineValidator ) => { const { sessionIndex , validatorId , total , own , others , } = offlineValidator ; await prisma . createOfflineValidator ( { sessionIndex : { connect : { index : sessionIndex . toNumber ( ) , } , } , validatorId : validatorId . toString ( ) , total : total . toHex ( ) , own : own . toHex ( ) , others : { set : others . map ( other => { return { who : other . who . toHex ( ) , value : other . value . toHex ( ) , } ; } ) , } , } ) ; } ) ) ; } , } ; export default createOfflineValidator ;