diff --git a/src/mappings/pocket/applications.ts b/src/mappings/pocket/applications.ts index e366d95..69abaed 100644 --- a/src/mappings/pocket/applications.ts +++ b/src/mappings/pocket/applications.ts @@ -125,6 +125,10 @@ async function _stakeApplication attribute.key === "source_address")?.value as unknown as string; + let sourceAddress: string | undefined, destinationAppAddress: string | undefined + + for (const attribute of event.event.attributes) { + if (attribute.key === "source_address") { + sourceAddress = (attribute.value as string).replaceAll("\"", ""); + } + + if (attribute.key === "destination_address") { + destinationAppAddress = (attribute.value as string).replaceAll("\"", ""); + } + + // Older versions of this event included the whole destination application + if (attribute.key === "destination_application") { + destinationAppAddress = (JSON.parse(attribute.value as string) as ApplicationSDKType).address; + } + } if (!sourceAddress) { throw new Error(`[handleTransferApplicationEndEvent] event.event.attributes not found`); @@ -439,7 +448,27 @@ async function _handleTransferApplicationEndEvent( // the source address is surrounded by quotes sourceAddress = sourceAddress.replaceAll("\"", ""); - const sourceApplication = await Application.get(sourceAddress); + if (!destinationAppAddress) { + throw new Error(`[handleTransferApplicationMsg] destination application not in event`); + } + + destinationAppAddress = destinationAppAddress.replaceAll("\"", ""); + + const results = await Promise.all([ + Application.get(sourceAddress), + Application.get(destinationAppAddress), + fetchAllApplicationServiceByApplicationId(sourceAddress), + fetchAllApplicationGatewayByApplicationId(sourceAddress), + ]) + + const [ + sourceApplication, + , + sourceApplicationServices, + sourceApplicationGateways, + ] = results; + + let destinationApplication = results[1] if (!sourceApplication) { throw new Error(`[handleTransferApplicationMsg] source application not found for address ${sourceAddress}`); @@ -452,44 +481,42 @@ async function _handleTransferApplicationEndEvent( sourceApplication.stakeStatus = StakeStatus.Unstaked; sourceApplication.unstakingReason = ApplicationUnbondingReason.TRANSFER; sourceApplication.unstakingEndBlockId = getBlockId(event.block); - - const destinationAppStringified = event.event.attributes.find(attribute => attribute.key === "destination_application")?.value as string; - - if (!destinationAppStringified) { - throw new Error(`[handleTransferApplicationMsg] destination application not in event`); - } - - const destinationApp: Required = JSON.parse(destinationAppStringified); - - sourceApplication.destinationApplicationId = destinationApp.address; - - const { delegatee_gateway_addresses, service_configs } = destinationApp; - const stake = destinationApp.stake as Required; - - const destinationApplication = Application.create({ - id: destinationApp.address, - accountId: destinationApp.address, - stakeAmount: BigInt(stake.amount), - stakeDenom: stake.denom, - stakeStatus: StakeStatus.Staked, - sourceApplicationId: sourceAddress, - transferredFromAtId: getBlockId(event.block), - unstakingEndBlockId: prevUnstakingEndBlockId, - unstakingBeginBlockId: sourceApplication.unstakingBeginBlockId, - }); - - const appDelegatedToGateways: Array = delegatee_gateway_addresses.map(gateway => ({ - id: getAppDelegatedToGatewayId(destinationApplication.id, gateway), + sourceApplication.destinationApplicationId = destinationAppAddress; + + if (destinationApplication && destinationApplication.stakeStatus === StakeStatus.Staked) { + destinationApplication.stakeAmount = sourceApplication.stakeAmount.valueOf() + destinationApplication.stakeAmount.valueOf(); + } else { + destinationApplication = Application.create({ + id: destinationAppAddress, + accountId: destinationAppAddress, + stakeAmount: sourceApplication.stakeAmount.valueOf(), + stakeDenom: sourceApplication.stakeDenom, + stakeStatus: StakeStatus.Staked, + unstakingEndHeight: undefined, + unstakingReason: undefined, + unstakingEndBlockId: undefined, + unstakingBeginBlockId: undefined, + }) + } + + destinationApplication.sourceApplicationId = sourceAddress + destinationApplication.transferredFromAtId = getBlockId(event.block) + destinationApplication.unstakingBeginBlockId = sourceApplication.unstakingBeginBlockId + destinationApplication.unstakingEndBlockId = destinationApplication.unstakingEndBlockId ? + destinationApplication.unstakingEndBlockId : + prevUnstakingEndBlockId + + const appDelegatedToGateways: Array = sourceApplicationGateways.map(gateway => ({ + id: getAppDelegatedToGatewayId(destinationApplication.id, gateway.gatewayId), applicationId: destinationApplication.id, - gatewayId: gateway, + gatewayId: gateway.gatewayId, })); - const sourceApplicationServices = await fetchAllApplicationServiceByApplicationId(sourceAddress); - const sourceApplicationGateways = await fetchAllApplicationGatewayByApplicationId(sourceAddress); - const newApplicationServices: Array = service_configs?.map(service => ({ - id: getStakeServiceId(destinationApp.address, service.service_id), - serviceId: service.service_id, - applicationId: destinationApp.address, + + const newApplicationServices: Array = sourceApplicationServices.map(service => ({ + id: getStakeServiceId(destinationApplication.id, service.serviceId), + serviceId: service.serviceId, + applicationId: destinationApplication.id, })) || []; const eventId = getEventId(event); @@ -500,7 +527,7 @@ async function _handleTransferApplicationEndEvent( EventTransferEndEntity.create({ id: eventId, sourceId: sourceAddress, - destinationId: destinationApp.address, + destinationId: destinationApplication.id, blockId: getBlockId(event.block), eventId, }).save(), @@ -525,6 +552,11 @@ async function _handleTransferApplicationErrorEvent( destinationAddress = (attribute.value as unknown as string).replaceAll("\"", ""); } + // Older versions of this event included the whole destination application + if (attribute.key === "destination_application") { + destinationAddress = (JSON.parse(attribute.value as string) as ApplicationSDKType).address; + } + if (attribute.key === "error") { error = (attribute.value as unknown as string).replaceAll("\"", ""); } @@ -620,6 +652,10 @@ async function _handleApplicationUnbondingBeginEvent( reason = applicationUnbondingReasonFromJSON((attribute.value as unknown as string).replaceAll("\"", "")); } + if (!msg && attribute.key === "application_address") { + address = (attribute.value as string).replaceAll("\"", "") + } + if (!msg && attribute.key === "application") { // now this is a block event? const application: ApplicationSDKType = parseJson(attribute.value as unknown as string); @@ -673,7 +709,7 @@ async function _handleApplicationUnbondingEndEvent( event: CosmosEvent, ) { let unstakingEndHeight = BigInt(0), sessionEndHeight = BigInt(0), reason: number | null = null, - applicationSdk: ApplicationSDKType | undefined; + applicationAddress: string | undefined; for (const attribute of event.event.attributes) { if (attribute.key === "unbonding_end_height") { @@ -688,8 +724,13 @@ async function _handleApplicationUnbondingEndEvent( reason = applicationUnbondingReasonFromJSON((attribute.value as unknown as string).replaceAll("\"", "")); } + if (attribute.key === "application_address") { + applicationAddress = (attribute.value as string).replaceAll("\"", "") + } + + // Older versions of this event included the whole application if (attribute.key === "application") { - applicationSdk = JSON.parse(attribute.value as unknown as string); + applicationAddress = (JSON.parse(attribute.value as unknown as string) as ApplicationSDKType).address; } } @@ -705,21 +746,21 @@ async function _handleApplicationUnbondingEndEvent( throw new Error(`[handleApplicationUnbondingEndEvent] reason not found in event`); } - if (!applicationSdk) { - throw new Error(`[handleApplicationUnbondingEndEvent] application not found in event`); + if (!applicationAddress) { + throw new Error(`[handleApplicationUnbondingEndEvent] applicationAddress not found in event`); } - const application = await Application.get(applicationSdk.address); + const application = await Application.get(applicationAddress); if (!application) { - throw new Error(`[handleApplicationUnbondingEndEvent] application not found for address ${applicationSdk.address}`); + throw new Error(`[handleApplicationUnbondingEndEvent] application not found for address ${applicationAddress}`); } application.unstakingEndBlockId = getBlockId(event.block); application.stakeStatus = StakeStatus.Unstaked; application.unstakingReason = getAppUnbondingReasonFromSDK(reason); - const applicationServices = (await fetchAllApplicationServiceByApplicationId(applicationSdk.address)).map(item => item.id); + const applicationServices = (await fetchAllApplicationServiceByApplicationId(applicationAddress)).map(item => item.id); const eventId = getEventId(event); @@ -730,7 +771,7 @@ async function _handleApplicationUnbondingEndEvent( sessionEndHeight, unstakingEndHeight, reason, - applicationId: applicationSdk.address, + applicationId: applicationAddress, eventId, }).save(), application.save(), diff --git a/src/mappings/pocket/gateways.ts b/src/mappings/pocket/gateways.ts index 65a1bdc..8855553 100644 --- a/src/mappings/pocket/gateways.ts +++ b/src/mappings/pocket/gateways.ts @@ -43,6 +43,9 @@ async function _handleGatewayMsgStake( stakeDenom: stake.denom, accountId: msg.msg.decodedMsg.address, stakeStatus: StakeStatus.Staked, + unstakingEndHeight: undefined, + unstakingEndBlockId: undefined, + unstakingBeginBlockId: undefined, }); const msgId = messageId(msg); @@ -143,7 +146,7 @@ async function _handleGatewayUnstakeEvent( } async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) { - let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewaySdk: GatewaySDKType | null = null; + let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewayAddress: string | null = null; for (const attribute of event.event.attributes) { if (attribute.key === "unbonding_end_height") { @@ -154,8 +157,13 @@ async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) { sessionEndHeight = BigInt((attribute.value as unknown as string).replaceAll("\"", "")); } + if (attribute.key === "gateway_address") { + gatewayAddress = (attribute.value as string).replaceAll("\"", ""); + } + + // older versions of this event included this attribute if (attribute.key === "gateway") { - gatewaySdk = JSON.parse(attribute.value as unknown as string); + gatewayAddress = (JSON.parse(attribute.value as string) as GatewaySDKType).address; } } @@ -167,14 +175,14 @@ async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) { throw new Error(`[handleEventGatewayUnbondingBegin] sessionEndHeight not found in event`); } - if (!gatewaySdk) { - throw new Error(`[handleEventGatewayUnbondingBegin] gateway not found in event`); + if (!gatewayAddress) { + throw new Error(`[handleEventGatewayUnbondingBegin] gatewayAddress not found in event`); } - const gateway = await Gateway.get(gatewaySdk.address); + const gateway = await Gateway.get(gatewayAddress); if (!gateway) { - throw new Error(`[handleEventGatewayUnbondingBegin] gateway not found for address ${gatewaySdk.address}`); + throw new Error(`[handleEventGatewayUnbondingBegin] gateway not found for address ${gatewayAddress}`); } gateway.unstakingEndHeight = unstakingEndHeight; @@ -196,7 +204,7 @@ async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) { } async function _handleEventGatewayUnbondingEnd(event: CosmosEvent) { - let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewaySdk: GatewaySDKType | null = null; + let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewayAddress: string | null = null; for (const attribute of event.event.attributes) { if (attribute.key === "unbonding_end_height") { @@ -207,8 +215,13 @@ async function _handleEventGatewayUnbondingEnd(event: CosmosEvent) { sessionEndHeight = BigInt((attribute.value as unknown as string).replaceAll("\"", "")); } + if (attribute.key === "gateway_address") { + gatewayAddress = (attribute.value as string).replaceAll("\"", ""); + } + + // older versions of this event included this attribute if (attribute.key === "gateway") { - gatewaySdk = JSON.parse(attribute.value as unknown as string); + gatewayAddress = (JSON.parse(attribute.value as string) as GatewaySDKType).address; } } @@ -220,17 +233,17 @@ async function _handleEventGatewayUnbondingEnd(event: CosmosEvent) { throw new Error(`[handleEventGatewayUnbondingEnd] sessionEndHeight not found in event`); } - if (!gatewaySdk) { - throw new Error(`[handleEventGatewayUnbondingEnd] gateway not found in event`); + if (!gatewayAddress) { + throw new Error(`[handleEventGatewayUnbondingEnd] gatewayAddress not found in event`); } const [gateway, undelegates] = await Promise.all([ - Gateway.get(gatewaySdk.address), - getUndelegatesForUnstakedGateway(gatewaySdk.address) + Gateway.get(gatewayAddress), + getUndelegatesForUnstakedGateway(gatewayAddress) ]); if (!gateway) { - throw new Error(`[handleEventGatewayUnbondingEnd] gateway not found for address ${gatewaySdk.address}`); + throw new Error(`[handleEventGatewayUnbondingEnd] gateway not found for address ${gatewayAddress}`); } gateway.unstakingEndBlockId = unstakingEndHeight; diff --git a/src/mappings/pocket/suppliers.ts b/src/mappings/pocket/suppliers.ts index 0a48bc0..5674b16 100644 --- a/src/mappings/pocket/suppliers.ts +++ b/src/mappings/pocket/suppliers.ts @@ -226,7 +226,7 @@ async function _handleSupplierStakeMsg(msg: CosmosMessage) { async function _handleMsgClaimMorseSupplier(msg: CosmosMessage) { const msgId = messageId(msg); - let stakeCoin: Coin | null = null, balanceCoin: Coin | null = null, supplier: SupplierSDKType | null = null, claimSignerType: string | null = null; + let stakeCoin: Coin | null = null, balanceCoin: Coin | null = null, claimSignerType: string | null = null; for (const event of msg.tx.tx.events) { if (event.type === 'pocket.migration.EventMorseSupplierClaimed') { @@ -244,10 +244,6 @@ async function _handleMsgClaimMorseSupplier(msg: CosmosMessage item.id); + const supplierServices = (await fetchAllSupplierServiceConfigBySupplier(operatorAddress) || []).map(item => item.id); const eventId = getEventId(event); @@ -578,7 +577,7 @@ async function _handleSupplierUnbondingEndEvent( sessionEndHeight: sessionEndHeight || BigInt(0), reason: reason !== null ? getSupplierUnbondingReasonFromSDK(reason) : SupplierUnbondingReason.UNSPECIFIED, blockId: getBlockId(event.block), - supplierId: supplierSdk.operator_address, + supplierId: operatorAddress, eventId, }).save(), supplier.save(),