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 , NomidotReward , Task } from './types' ; const l = logger ( 'Task: Reward' ) ; const createReward = { name : 'createReward' , read : ( blockHash , cached , api ) : Promise < NomidotReward [ ] > => { const { events , sessionIndex } = cached ; const rewardEvents = filterEvents ( events , 'staking' , 'Reward' ) ; const result = [ ] ; if ( rewardEvents ) { rewardEvents . map ( ( { event : { data } } ) => { const treasuryReward = api . createType ( 'Balance' , data [ 1 ] ) ; const validatorReward = api . createType ( 'Balance' , data [ 0 ] ) ; result . push ( { authoredBlock : blockHash , sessionIndex , treasuryReward , validatorReward , } as NomidotReward ) ; } ) ; } l . log ( `Reward: ${ JSON . stringify ( result ) }` ) ; return Promise . resolve ( result ) ; } , write : async ( _blockNumber , rewards ) => { await Promise . all ( rewards . map ( async ( { authoredBlock , sessionIndex , treasuryReward , validatorReward , } ) => { await prisma . createReward ( { authoredBlock : { connect : { hash : authoredBlock . toHex ( ) , } , } , sessionIndex : { connect : { index : sessionIndex . toNumber ( ) , } , } , treasuryReward : treasuryReward . toHex ( ) , validatorReward : validatorReward . toHex ( ) , } ) ; } ) ) ; } , } ; export default createReward ;