@@ -125,6 +125,10 @@ async function _stakeApplication<T extends StakeApplicationProps['serviceMsgIdKe
125125 stakeStatus : StakeStatus . Staked ,
126126 transferringToId : prevApp ?. transferringToId ,
127127 transferEndHeight : prevApp ?. transferEndHeight ,
128+ unstakingBeginBlockId : undefined ,
129+ unstakingEndBlockId : undefined ,
130+ unstakingReason : undefined ,
131+ unstakingEndHeight : undefined ,
128132 } ;
129133
130134 // used to create the services that came in the stake message
@@ -204,7 +208,7 @@ async function _handleMsgClaimMorseApplication(
204208 const msgId = messageId ( msg ) ;
205209 const { shannonDestAddress, } = msg . msg . decodedMsg ;
206210
207- let stakeCoin : Coin | null = null , balanceCoin : Coin | null = null , app : ApplicationSDKType | null = null ;
211+ let stakeCoin : Coin | null = null , balanceCoin : Coin | null = null ;
208212
209213 for ( const event of msg . tx . tx . events ) {
210214 if ( event . type === 'pocket.migration.EventMorseApplicationClaimed' ) {
@@ -226,10 +230,6 @@ async function _handleMsgClaimMorseApplication(
226230 amount : coin . amount ,
227231 }
228232 }
229-
230- if ( attribute . key === 'application' ) {
231- app = JSON . parse ( attribute . value as string ) ;
232- }
233233 }
234234 }
235235 }
@@ -242,13 +242,7 @@ async function _handleMsgClaimMorseApplication(
242242 throw new Error ( `[handleMsgClaimMorseApplication] balance coin not found in event` ) ;
243243 }
244244
245- if ( ! app ) {
246- throw new Error ( `[handleMsgClaimMorseApplication] app not found in event` ) ;
247- }
248-
249- if ( ! app . stake ) {
250- throw new Error ( `[handleMsgClaimMorseApplication] app stake not found in event` ) ;
251- }
245+ const app = await Application . get ( msg . msg . decodedMsg . shannonDestAddress ) ;
252246
253247 const stakeAmount = BigInt ( stakeCoin . amount ) ;
254248 const stakeDenom = stakeCoin . denom ;
@@ -283,8 +277,8 @@ async function _handleMsgClaimMorseApplication(
283277 address : shannonDestAddress ,
284278 msgId,
285279 services : [ msg . msg . decodedMsg . serviceConfig ! ] ,
286- stakeAmount : BigInt ( app . stake . amount ) ,
287- stakeDenom : app . stake . denom ,
280+ stakeAmount : stakeAmount + BigInt ( app ?. stakeAmount ?. toString ( ) || "0" ) ,
281+ stakeDenom : stakeDenom ,
288282 serviceMsgIdKey : 'claimMsgId'
289283 } ) ,
290284 ] ;
@@ -430,7 +424,22 @@ async function _handleTransferApplicationBeginEvent(
430424async function _handleTransferApplicationEndEvent (
431425 event : CosmosEvent ,
432426) {
433- let sourceAddress = event . event . attributes . find ( attribute => attribute . key === "source_address" ) ?. value as unknown as string ;
427+ let sourceAddress : string | undefined , destinationAppAddress : string | undefined
428+
429+ for ( const attribute of event . event . attributes ) {
430+ if ( attribute . key === "source_address" ) {
431+ sourceAddress = ( attribute . value as string ) . replaceAll ( "\"" , "" ) ;
432+ }
433+
434+ if ( attribute . key === "destination_address" ) {
435+ destinationAppAddress = ( attribute . value as string ) . replaceAll ( "\"" , "" ) ;
436+ }
437+
438+ // Older versions of this event included the whole destination application
439+ if ( attribute . key === "destination_application" ) {
440+ destinationAppAddress = ( JSON . parse ( attribute . value as string ) as ApplicationSDKType ) . address ;
441+ }
442+ }
434443
435444 if ( ! sourceAddress ) {
436445 throw new Error ( `[handleTransferApplicationEndEvent] event.event.attributes not found` ) ;
@@ -439,7 +448,27 @@ async function _handleTransferApplicationEndEvent(
439448 // the source address is surrounded by quotes
440449 sourceAddress = sourceAddress . replaceAll ( "\"" , "" ) ;
441450
442- const sourceApplication = await Application . get ( sourceAddress ) ;
451+ if ( ! destinationAppAddress ) {
452+ throw new Error ( `[handleTransferApplicationMsg] destination application not in event` ) ;
453+ }
454+
455+ destinationAppAddress = destinationAppAddress . replaceAll ( "\"" , "" ) ;
456+
457+ const results = await Promise . all ( [
458+ Application . get ( sourceAddress ) ,
459+ Application . get ( destinationAppAddress ) ,
460+ fetchAllApplicationServiceByApplicationId ( sourceAddress ) ,
461+ fetchAllApplicationGatewayByApplicationId ( sourceAddress ) ,
462+ ] )
463+
464+ const [
465+ sourceApplication ,
466+ ,
467+ sourceApplicationServices ,
468+ sourceApplicationGateways ,
469+ ] = results ;
470+
471+ let destinationApplication = results [ 1 ]
443472
444473 if ( ! sourceApplication ) {
445474 throw new Error ( `[handleTransferApplicationMsg] source application not found for address ${ sourceAddress } ` ) ;
@@ -452,44 +481,42 @@ async function _handleTransferApplicationEndEvent(
452481 sourceApplication . stakeStatus = StakeStatus . Unstaked ;
453482 sourceApplication . unstakingReason = ApplicationUnbondingReason . TRANSFER ;
454483 sourceApplication . unstakingEndBlockId = getBlockId ( event . block ) ;
455-
456- const destinationAppStringified = event . event . attributes . find ( attribute => attribute . key === "destination_application" ) ?. value as string ;
457-
458- if ( ! destinationAppStringified ) {
459- throw new Error ( `[handleTransferApplicationMsg] destination application not in event` ) ;
460- }
461-
462- const destinationApp : Required < ApplicationSDKType > = JSON . parse ( destinationAppStringified ) ;
463-
464- sourceApplication . destinationApplicationId = destinationApp . address ;
465-
466- const { delegatee_gateway_addresses, service_configs } = destinationApp ;
467- const stake = destinationApp . stake as Required < typeof destinationApp . stake > ;
468-
469- const destinationApplication = Application . create ( {
470- id : destinationApp . address ,
471- accountId : destinationApp . address ,
472- stakeAmount : BigInt ( stake . amount ) ,
473- stakeDenom : stake . denom ,
474- stakeStatus : StakeStatus . Staked ,
475- sourceApplicationId : sourceAddress ,
476- transferredFromAtId : getBlockId ( event . block ) ,
477- unstakingEndBlockId : prevUnstakingEndBlockId ,
478- unstakingBeginBlockId : sourceApplication . unstakingBeginBlockId ,
479- } ) ;
480-
481- const appDelegatedToGateways : Array < ApplicationGatewayProps > = delegatee_gateway_addresses . map ( gateway => ( {
482- id : getAppDelegatedToGatewayId ( destinationApplication . id , gateway ) ,
484+ sourceApplication . destinationApplicationId = destinationAppAddress ;
485+
486+ if ( destinationApplication && destinationApplication . stakeStatus === StakeStatus . Staked ) {
487+ destinationApplication . stakeAmount = sourceApplication . stakeAmount . valueOf ( ) + destinationApplication . stakeAmount . valueOf ( ) ;
488+ } else {
489+ destinationApplication = Application . create ( {
490+ id : destinationAppAddress ,
491+ accountId : destinationAppAddress ,
492+ stakeAmount : sourceApplication . stakeAmount . valueOf ( ) ,
493+ stakeDenom : sourceApplication . stakeDenom ,
494+ stakeStatus : StakeStatus . Staked ,
495+ unstakingEndHeight : undefined ,
496+ unstakingReason : undefined ,
497+ unstakingEndBlockId : undefined ,
498+ unstakingBeginBlockId : undefined ,
499+ } )
500+ }
501+
502+ destinationApplication . sourceApplicationId = sourceAddress
503+ destinationApplication . transferredFromAtId = getBlockId ( event . block )
504+ destinationApplication . unstakingBeginBlockId = sourceApplication . unstakingBeginBlockId
505+ destinationApplication . unstakingEndBlockId = destinationApplication . unstakingEndBlockId ?
506+ destinationApplication . unstakingEndBlockId :
507+ prevUnstakingEndBlockId
508+
509+ const appDelegatedToGateways : Array < ApplicationGatewayProps > = sourceApplicationGateways . map ( gateway => ( {
510+ id : getAppDelegatedToGatewayId ( destinationApplication . id , gateway . gatewayId ) ,
483511 applicationId : destinationApplication . id ,
484- gatewayId : gateway ,
512+ gatewayId : gateway . gatewayId ,
485513 } ) ) ;
486514
487- const sourceApplicationServices = await fetchAllApplicationServiceByApplicationId ( sourceAddress ) ;
488- const sourceApplicationGateways = await fetchAllApplicationGatewayByApplicationId ( sourceAddress ) ;
489- const newApplicationServices : Array < ApplicationServiceProps > = service_configs ?. map ( service => ( {
490- id : getStakeServiceId ( destinationApp . address , service . service_id ) ,
491- serviceId : service . service_id ,
492- applicationId : destinationApp . address ,
515+
516+ const newApplicationServices : Array < ApplicationServiceProps > = sourceApplicationServices . map ( service => ( {
517+ id : getStakeServiceId ( destinationApplication . id , service . serviceId ) ,
518+ serviceId : service . serviceId ,
519+ applicationId : destinationApplication . id ,
493520 } ) ) || [ ] ;
494521
495522 const eventId = getEventId ( event ) ;
@@ -500,7 +527,7 @@ async function _handleTransferApplicationEndEvent(
500527 EventTransferEndEntity . create ( {
501528 id : eventId ,
502529 sourceId : sourceAddress ,
503- destinationId : destinationApp . address ,
530+ destinationId : destinationApplication . id ,
504531 blockId : getBlockId ( event . block ) ,
505532 eventId,
506533 } ) . save ( ) ,
@@ -525,6 +552,11 @@ async function _handleTransferApplicationErrorEvent(
525552 destinationAddress = ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ;
526553 }
527554
555+ // Older versions of this event included the whole destination application
556+ if ( attribute . key === "destination_application" ) {
557+ destinationAddress = ( JSON . parse ( attribute . value as string ) as ApplicationSDKType ) . address ;
558+ }
559+
528560 if ( attribute . key === "error" ) {
529561 error = ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ;
530562 }
@@ -620,6 +652,10 @@ async function _handleApplicationUnbondingBeginEvent(
620652 reason = applicationUnbondingReasonFromJSON ( ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ) ;
621653 }
622654
655+ if ( ! msg && attribute . key === "application_address" ) {
656+ address = ( attribute . value as string ) . replaceAll ( "\"" , "" )
657+ }
658+
623659 if ( ! msg && attribute . key === "application" ) {
624660 // now this is a block event?
625661 const application : ApplicationSDKType = parseJson ( attribute . value as unknown as string ) ;
@@ -673,7 +709,7 @@ async function _handleApplicationUnbondingEndEvent(
673709 event : CosmosEvent ,
674710) {
675711 let unstakingEndHeight = BigInt ( 0 ) , sessionEndHeight = BigInt ( 0 ) , reason : number | null = null ,
676- applicationSdk : ApplicationSDKType | undefined ;
712+ applicationAddress : string | undefined ;
677713
678714 for ( const attribute of event . event . attributes ) {
679715 if ( attribute . key === "unbonding_end_height" ) {
@@ -688,8 +724,13 @@ async function _handleApplicationUnbondingEndEvent(
688724 reason = applicationUnbondingReasonFromJSON ( ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ) ;
689725 }
690726
727+ if ( attribute . key === "application_address" ) {
728+ applicationAddress = ( attribute . value as string ) . replaceAll ( "\"" , "" )
729+ }
730+
731+ // Older versions of this event included the whole application
691732 if ( attribute . key === "application" ) {
692- applicationSdk = JSON . parse ( attribute . value as unknown as string ) ;
733+ applicationAddress = ( JSON . parse ( attribute . value as unknown as string ) as ApplicationSDKType ) . address ;
693734 }
694735 }
695736
@@ -705,21 +746,21 @@ async function _handleApplicationUnbondingEndEvent(
705746 throw new Error ( `[handleApplicationUnbondingEndEvent] reason not found in event` ) ;
706747 }
707748
708- if ( ! applicationSdk ) {
709- throw new Error ( `[handleApplicationUnbondingEndEvent] application not found in event` ) ;
749+ if ( ! applicationAddress ) {
750+ throw new Error ( `[handleApplicationUnbondingEndEvent] applicationAddress not found in event` ) ;
710751 }
711752
712- const application = await Application . get ( applicationSdk . address ) ;
753+ const application = await Application . get ( applicationAddress ) ;
713754
714755 if ( ! application ) {
715- throw new Error ( `[handleApplicationUnbondingEndEvent] application not found for address ${ applicationSdk . address } ` ) ;
756+ throw new Error ( `[handleApplicationUnbondingEndEvent] application not found for address ${ applicationAddress } ` ) ;
716757 }
717758
718759 application . unstakingEndBlockId = getBlockId ( event . block ) ;
719760 application . stakeStatus = StakeStatus . Unstaked ;
720761 application . unstakingReason = getAppUnbondingReasonFromSDK ( reason ) ;
721762
722- const applicationServices = ( await fetchAllApplicationServiceByApplicationId ( applicationSdk . address ) ) . map ( item => item . id ) ;
763+ const applicationServices = ( await fetchAllApplicationServiceByApplicationId ( applicationAddress ) ) . map ( item => item . id ) ;
723764
724765 const eventId = getEventId ( event ) ;
725766
@@ -730,7 +771,7 @@ async function _handleApplicationUnbondingEndEvent(
730771 sessionEndHeight,
731772 unstakingEndHeight,
732773 reason,
733- applicationId : applicationSdk . address ,
774+ applicationId : applicationAddress ,
734775 eventId,
735776 } ) . save ( ) ,
736777 application . save ( ) ,
0 commit comments