@@ -109,6 +109,10 @@ async function _stakeApplication<T extends StakeApplicationProps['serviceMsgIdKe
109109 stakeStatus : StakeStatus . Staked ,
110110 transferringToId : prevApp ?. transferringToId ,
111111 transferEndHeight : prevApp ?. transferEndHeight ,
112+ unstakingBeginBlockId : undefined ,
113+ unstakingEndBlockId : undefined ,
114+ unstakingReason : undefined ,
115+ unstakingEndHeight : undefined ,
112116 } ;
113117
114118 // used to create the services that came in the stake message
@@ -188,7 +192,7 @@ async function _handleMsgClaimMorseApplication(
188192 const msgId = messageId ( msg ) ;
189193 const { shannonDestAddress, } = msg . msg . decodedMsg ;
190194
191- let stakeCoin : Coin | null = null , balanceCoin : Coin | null = null , app : ApplicationSDKType | null = null ;
195+ let stakeCoin : Coin | null = null , balanceCoin : Coin | null = null ;
192196
193197 for ( const event of msg . tx . tx . events ) {
194198 if ( event . type === 'pocket.migration.EventMorseApplicationClaimed' ) {
@@ -210,10 +214,6 @@ async function _handleMsgClaimMorseApplication(
210214 amount : coin . amount ,
211215 }
212216 }
213-
214- if ( attribute . key === 'application' ) {
215- app = JSON . parse ( attribute . value as string ) ;
216- }
217217 }
218218 }
219219 }
@@ -226,13 +226,7 @@ async function _handleMsgClaimMorseApplication(
226226 throw new Error ( `[handleMsgClaimMorseApplication] balance coin not found in event` ) ;
227227 }
228228
229- if ( ! app ) {
230- throw new Error ( `[handleMsgClaimMorseApplication] app not found in event` ) ;
231- }
232-
233- if ( ! app . stake ) {
234- throw new Error ( `[handleMsgClaimMorseApplication] app stake not found in event` ) ;
235- }
229+ const app = await Application . get ( msg . msg . decodedMsg . shannonDestAddress ) ;
236230
237231 const stakeAmount = BigInt ( stakeCoin . amount ) ;
238232 const stakeDenom = stakeCoin . denom ;
@@ -267,8 +261,8 @@ async function _handleMsgClaimMorseApplication(
267261 address : shannonDestAddress ,
268262 msgId,
269263 services : [ msg . msg . decodedMsg . serviceConfig ! ] ,
270- stakeAmount : BigInt ( app . stake . amount ) ,
271- stakeDenom : app . stake . denom ,
264+ stakeAmount : stakeAmount + BigInt ( app ?. stakeAmount ?. toString ( ) || "0" ) ,
265+ stakeDenom : stakeDenom ,
272266 serviceMsgIdKey : 'claimMsgId'
273267 } ) ,
274268 ] ;
@@ -414,7 +408,22 @@ async function _handleTransferApplicationBeginEvent(
414408async function _handleTransferApplicationEndEvent (
415409 event : CosmosEvent ,
416410) {
417- let sourceAddress = event . event . attributes . find ( attribute => attribute . key === "source_address" ) ?. value as unknown as string ;
411+ let sourceAddress : string | undefined , destinationAppAddress : string | undefined
412+
413+ for ( const attribute of event . event . attributes ) {
414+ if ( attribute . key === "source_address" ) {
415+ sourceAddress = ( attribute . value as string ) . replaceAll ( "\"" , "" ) ;
416+ }
417+
418+ if ( attribute . key === "destination_address" ) {
419+ destinationAppAddress = ( attribute . value as string ) . replaceAll ( "\"" , "" ) ;
420+ }
421+
422+ // Older versions of this event included the whole destination application
423+ if ( attribute . key === "destination_application" ) {
424+ destinationAppAddress = ( JSON . parse ( attribute . value as string ) as ApplicationSDKType ) . address ;
425+ }
426+ }
418427
419428 if ( ! sourceAddress ) {
420429 throw new Error ( `[handleTransferApplicationEndEvent] event.event.attributes not found` ) ;
@@ -423,7 +432,27 @@ async function _handleTransferApplicationEndEvent(
423432 // the source address is surrounded by quotes
424433 sourceAddress = sourceAddress . replaceAll ( "\"" , "" ) ;
425434
426- const sourceApplication = await Application . get ( sourceAddress ) ;
435+ if ( ! destinationAppAddress ) {
436+ throw new Error ( `[handleTransferApplicationMsg] destination application not in event` ) ;
437+ }
438+
439+ destinationAppAddress = destinationAppAddress . replaceAll ( "\"" , "" ) ;
440+
441+ const results = await Promise . all ( [
442+ Application . get ( sourceAddress ) ,
443+ Application . get ( destinationAppAddress ) ,
444+ fetchAllApplicationServiceByApplicationId ( sourceAddress ) ,
445+ fetchAllApplicationGatewayByApplicationId ( sourceAddress ) ,
446+ ] )
447+
448+ const [
449+ sourceApplication ,
450+ ,
451+ sourceApplicationServices ,
452+ sourceApplicationGateways ,
453+ ] = results ;
454+
455+ let destinationApplication = results [ 1 ]
427456
428457 if ( ! sourceApplication ) {
429458 throw new Error ( `[handleTransferApplicationMsg] source application not found for address ${ sourceAddress } ` ) ;
@@ -436,44 +465,42 @@ async function _handleTransferApplicationEndEvent(
436465 sourceApplication . stakeStatus = StakeStatus . Unstaked ;
437466 sourceApplication . unstakingReason = ApplicationUnbondingReason . TRANSFER ;
438467 sourceApplication . unstakingEndBlockId = getBlockId ( event . block ) ;
439-
440- const destinationAppStringified = event . event . attributes . find ( attribute => attribute . key === "destination_application" ) ?. value as string ;
441-
442- if ( ! destinationAppStringified ) {
443- throw new Error ( `[handleTransferApplicationMsg] destination application not in event` ) ;
444- }
445-
446- const destinationApp : Required < ApplicationSDKType > = JSON . parse ( destinationAppStringified ) ;
447-
448- sourceApplication . destinationApplicationId = destinationApp . address ;
449-
450- const { delegatee_gateway_addresses, service_configs } = destinationApp ;
451- const stake = destinationApp . stake as Required < typeof destinationApp . stake > ;
452-
453- const destinationApplication = Application . create ( {
454- id : destinationApp . address ,
455- accountId : destinationApp . address ,
456- stakeAmount : BigInt ( stake . amount ) ,
457- stakeDenom : stake . denom ,
458- stakeStatus : StakeStatus . Staked ,
459- sourceApplicationId : sourceAddress ,
460- transferredFromAtId : getBlockId ( event . block ) ,
461- unstakingEndBlockId : prevUnstakingEndBlockId ,
462- unstakingBeginBlockId : sourceApplication . unstakingBeginBlockId ,
463- } ) ;
464-
465- const appDelegatedToGateways : Array < ApplicationGatewayProps > = delegatee_gateway_addresses . map ( gateway => ( {
466- id : getAppDelegatedToGatewayId ( destinationApplication . id , gateway ) ,
468+ sourceApplication . destinationApplicationId = destinationAppAddress ;
469+
470+ if ( destinationApplication && destinationApplication . stakeStatus === StakeStatus . Staked ) {
471+ destinationApplication . stakeAmount = sourceApplication . stakeAmount . valueOf ( ) + destinationApplication . stakeAmount . valueOf ( ) ;
472+ } else {
473+ destinationApplication = Application . create ( {
474+ id : destinationAppAddress ,
475+ accountId : destinationAppAddress ,
476+ stakeAmount : sourceApplication . stakeAmount . valueOf ( ) ,
477+ stakeDenom : sourceApplication . stakeDenom ,
478+ stakeStatus : StakeStatus . Staked ,
479+ unstakingEndHeight : undefined ,
480+ unstakingReason : undefined ,
481+ unstakingEndBlockId : undefined ,
482+ unstakingBeginBlockId : undefined ,
483+ } )
484+ }
485+
486+ destinationApplication . sourceApplicationId = sourceAddress
487+ destinationApplication . transferredFromAtId = getBlockId ( event . block )
488+ destinationApplication . unstakingBeginBlockId = sourceApplication . unstakingBeginBlockId
489+ destinationApplication . unstakingEndBlockId = destinationApplication . unstakingEndBlockId ?
490+ destinationApplication . unstakingEndBlockId :
491+ prevUnstakingEndBlockId
492+
493+ const appDelegatedToGateways : Array < ApplicationGatewayProps > = sourceApplicationGateways . map ( gateway => ( {
494+ id : getAppDelegatedToGatewayId ( destinationApplication . id , gateway . gatewayId ) ,
467495 applicationId : destinationApplication . id ,
468- gatewayId : gateway ,
496+ gatewayId : gateway . gatewayId ,
469497 } ) ) ;
470498
471- const sourceApplicationServices = await fetchAllApplicationServiceByApplicationId ( sourceAddress ) ;
472- const sourceApplicationGateways = await fetchAllApplicationGatewayByApplicationId ( sourceAddress ) ;
473- const newApplicationServices : Array < ApplicationServiceProps > = service_configs ?. map ( service => ( {
474- id : getStakeServiceId ( destinationApp . address , service . service_id ) ,
475- serviceId : service . service_id ,
476- applicationId : destinationApp . address ,
499+
500+ const newApplicationServices : Array < ApplicationServiceProps > = sourceApplicationServices . map ( service => ( {
501+ id : getStakeServiceId ( destinationApplication . id , service . serviceId ) ,
502+ serviceId : service . serviceId ,
503+ applicationId : destinationApplication . id ,
477504 } ) ) || [ ] ;
478505
479506 const eventId = getEventId ( event ) ;
@@ -484,7 +511,7 @@ async function _handleTransferApplicationEndEvent(
484511 EventTransferEndEntity . create ( {
485512 id : eventId ,
486513 sourceId : sourceAddress ,
487- destinationId : destinationApp . address ,
514+ destinationId : destinationApplication . id ,
488515 blockId : getBlockId ( event . block ) ,
489516 eventId,
490517 } ) . save ( ) ,
@@ -509,6 +536,11 @@ async function _handleTransferApplicationErrorEvent(
509536 destinationAddress = ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ;
510537 }
511538
539+ // Older versions of this event included the whole destination application
540+ if ( attribute . key === "destination_application" ) {
541+ destinationAddress = ( JSON . parse ( attribute . value as string ) as ApplicationSDKType ) . address ;
542+ }
543+
512544 if ( attribute . key === "error" ) {
513545 error = ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ;
514546 }
@@ -613,7 +645,7 @@ async function _handleApplicationUnbondingEndEvent(
613645 event : CosmosEvent ,
614646) {
615647 let unstakingEndHeight = BigInt ( 0 ) , sessionEndHeight = BigInt ( 0 ) , reason : number | null = null ,
616- applicationSdk : ApplicationSDKType | undefined ;
648+ applicationAddress : string | undefined ;
617649
618650 for ( const attribute of event . event . attributes ) {
619651 if ( attribute . key === "unbonding_end_height" ) {
@@ -628,8 +660,13 @@ async function _handleApplicationUnbondingEndEvent(
628660 reason = applicationUnbondingReasonFromJSON ( ( attribute . value as unknown as string ) . replaceAll ( "\"" , "" ) ) ;
629661 }
630662
663+ if ( attribute . key === "application_address" ) {
664+ applicationAddress = ( attribute . value as string ) . replaceAll ( "\"" , "" )
665+ }
666+
667+ // Older versions of this event included the whole application
631668 if ( attribute . key === "application" ) {
632- applicationSdk = JSON . parse ( attribute . value as unknown as string ) ;
669+ applicationAddress = ( JSON . parse ( attribute . value as unknown as string ) as ApplicationSDKType ) . address ;
633670 }
634671 }
635672
@@ -645,21 +682,21 @@ async function _handleApplicationUnbondingEndEvent(
645682 throw new Error ( `[handleApplicationUnbondingEndEvent] reason not found in event` ) ;
646683 }
647684
648- if ( ! applicationSdk ) {
649- throw new Error ( `[handleApplicationUnbondingEndEvent] application not found in event` ) ;
685+ if ( ! applicationAddress ) {
686+ throw new Error ( `[handleApplicationUnbondingEndEvent] applicationAddress not found in event` ) ;
650687 }
651688
652- const application = await Application . get ( applicationSdk . address ) ;
689+ const application = await Application . get ( applicationAddress ) ;
653690
654691 if ( ! application ) {
655- throw new Error ( `[handleApplicationUnbondingEndEvent] application not found for address ${ applicationSdk . address } ` ) ;
692+ throw new Error ( `[handleApplicationUnbondingEndEvent] application not found for address ${ applicationAddress } ` ) ;
656693 }
657694
658695 application . unstakingEndBlockId = getBlockId ( event . block ) ;
659696 application . stakeStatus = StakeStatus . Unstaked ;
660697 application . unstakingReason = getAppUnbondingReasonFromSDK ( reason ) ;
661698
662- const applicationServices = ( await fetchAllApplicationServiceByApplicationId ( applicationSdk . address ) ) . map ( item => item . id ) ;
699+ const applicationServices = ( await fetchAllApplicationServiceByApplicationId ( applicationAddress ) ) . map ( item => item . id ) ;
663700
664701 const eventId = getEventId ( event ) ;
665702
@@ -670,7 +707,7 @@ async function _handleApplicationUnbondingEndEvent(
670707 sessionEndHeight,
671708 unstakingEndHeight,
672709 reason,
673- applicationId : applicationSdk . address ,
710+ applicationId : applicationAddress ,
674711 eventId,
675712 } ) . save ( ) ,
676713 application . save ( ) ,
0 commit comments