@@ -133,10 +133,11 @@ function getSettlementOpReasonFromSDK(item: typeof SettlementOpReasonSDKType | n
133133}
134134
135135function parseAttribute ( attribute : unknown ) : string {
136- return ( attribute as string ) . replaceAll ( "\"" , "" ) ;
136+ return ( attribute as string ) . replaceAll ( "\"" , "" ) ;
137137}
138138
139139function getAttributes ( attributes : CosmosEvent [ "event" ] [ "attributes" ] ) {
140+
140141 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
141142 // @ts -ignore
142143 let proof : ProofSDKType = { } ,
@@ -578,11 +579,11 @@ function _handleEventApplicationOverserviced(event: CosmosEvent): EventApplicati
578579
579580 for ( const attribute of event . event . attributes ) {
580581 if ( attribute . key === "application_addr" ) {
581- applicationAddress = ( attribute . value as string ) . replaceAll ( '"' , '' ) ;
582+ applicationAddress = parseAttribute ( attribute . value )
582583 }
583584
584585 if ( attribute . key === "supplier_operator_addr" ) {
585- supplierAddress = ( attribute . value as string ) . replaceAll ( '"' , '' ) ;
586+ supplierAddress = parseAttribute ( attribute . value as string )
586587 }
587588
588589 if ( attribute . key === "expected_burn" ) {
@@ -668,18 +669,72 @@ function _handleEventApplicationReimbursementRequest(event: CosmosEvent): EventA
668669 }
669670}
670671
672+ async function _handleOldEventSupplierSlashed ( event : CosmosEvent ) {
673+ let slashingCoin : CoinSDKType | null = null , operatorAddress = '' ;
674+
675+ for ( const attribute of event . event . attributes ) {
676+ if ( attribute . key === "slashing_amount" ) {
677+ slashingCoin = JSON . parse ( attribute . value as string ) ;
678+ }
679+
680+ if ( attribute . key === "supplier_operator_addr" ) {
681+ operatorAddress = parseAttribute ( attribute . value ) ;
682+ }
683+ }
684+
685+ if ( ! slashingCoin ) {
686+ throw new Error ( `[handleEventSupplierSlashed] slashingCoin not found in event` ) ;
687+ }
688+
689+ if ( ! operatorAddress ) {
690+ throw new Error ( `[handleEventSupplierSlashed] operatorAddress not found in event` ) ;
691+ }
692+
693+ const supplier = await Supplier . get ( operatorAddress )
694+
695+ if ( ! supplier ) {
696+ throw new Error ( `[handleEventSupplierSlashed] supplier not found for operator address ${ operatorAddress } ` )
697+ }
698+
699+ supplier . stakeAmount -= BigInt ( slashingCoin . amount )
700+
701+ await Promise . all ( [
702+ supplier . save ( ) ,
703+ EventSupplierSlashed . create ( {
704+ id : getEventId ( event ) ,
705+ supplierId : operatorAddress ,
706+ blockId : getBlockId ( event . block ) ,
707+ eventId : getEventId ( event ) ,
708+ proofMissingPenalty : BigInt ( slashingCoin . amount ) ,
709+ proofMissingPenaltyDenom : slashingCoin . denom ,
710+ previousStakeAmount : supplier . stakeAmount ,
711+ afterStakeAmount : supplier . stakeAmount ,
712+ // in alpha this event does not have the values below, so we are setting them to empty values for now
713+ applicationId : '' ,
714+ serviceId : '' ,
715+ sessionId : '' ,
716+ sessionStartHeight : BigInt ( 0 ) ,
717+ sessionEndHeight : BigInt ( 0 ) ,
718+ } ) . save ( ) ,
719+ ] )
720+ }
721+
671722async function _handleEventSupplierSlashed ( event : CosmosEvent ) {
672723 const {
673724 claim,
674725 proofMissingPenalty
675726 } = getAttributes ( event . event . attributes ) ;
676727
677728 if ( ! claim ) {
678- throw new Error ( `[handleEventSupplierSlashed] claim not found in event` ) ;
729+ logger . warn ( `[handleEventSupplierSlashed] claim not found in event, trying to handle with previous version` ) ;
730+ await _handleOldEventSupplierSlashed ( event )
731+ return
679732 }
680733
681734 if ( ! claim . session_header ) {
682- throw new Error ( `[handleEventSupplierSlashed] session_header not found in event` ) ;
735+ logger . warn ( `[handleEventSupplierSlashed] session_header not found in event, trying to handle with previous version` ) ;
736+ await _handleOldEventSupplierSlashed ( event )
737+ return
683738 }
684739
685740 const supplier = await Supplier . get ( claim . supplier_operator_address )
0 commit comments