import { ApiPromise } from '@polkadot/api' ; import { BlockNumber , Hash } from '@polkadot/types/interfaces' ; import { logger } from '@polkadot/util' ; import { prisma } from '../generated/prisma-client' ; import { referendumStatus } from '../util/statuses' ; import { Cached , NomidotReferendumRawEvent , NomidotReferendumStatusUpdate , Task , } from './types' ; const l = logger ( 'Task: Referendum Status Update' ) ; const createReferendumStatus = { name : 'createReferendumStatusUpdate' , read : async ( _blockHash , cached , _api ) : Promise < NomidotReferendumStatusUpdate [ ] > => { const { events } = cached ; const referendumEvents = events . filter ( ( { event : { method , section } } ) => section === 'democracy' && Object . values ( referendumStatus ) . filter ( status => status !== referendumStatus . STARTED ) . includes ( method ) ) ; const results = [ ] ; if ( ! referendumEvents ) { return results ; } await Promise . all ( referendumEvents . map ( async ( { event : { data , typeDef , method } } ) => { const referendumRawEvent = data . reduce ( ( prev , curr , index ) => { const type = typeDef [ index ] . type ; return { ... prev , [ type ] : curr . toJSON ( ) , } ; } , { } ) ; if ( ! referendumRawEvent . ReferendumIndex && referendumRawEvent . ReferendumIndex !== 0 ) { l . error ( `Expected ReferendumIndex not found on the event: ${ referendumRawEvent . ReferendumIndex }` ) ; return null ; } const relatedReferendum = await prisma . referendum ( { referendumId : referendumRawEvent . ReferendumIndex , } ) ; if ( ! relatedReferendum ) { l . error ( `No existing referendum found for referendum id: ${ referendumRawEvent . ReferendumIndex }` ) ; return [ ] ; } const result = { referendumId : referendumRawEvent . ReferendumIndex , status : method , } ; l . log ( `Nomidot Referendum Status Update: ${ JSON . stringify ( result ) }` ) ; results . push ( result ) ; } ) ) ; return results ; } , write : async ( blockNumber , value ) => { if ( value && value . length ) { await Promise . all ( value . map ( async ( ref ) => { const { referendumId , status } = ref ; await prisma . createReferendumStatus ( { blockNumber : { connect : { number : blockNumber . toNumber ( ) , } , } , referendum : { connect : { referendumId , } , } , status , uniqueStatus : `${ referendumId }_${ status }` , } ) ; } ) ) ; } } , } ; export default createReferendumStatus ;