Skip to content

Commit a10c265

Browse files
authored
Refactored some event handlers to match their changes (#63)
## Summary Refactored some event handlers of applications, gateways and suppliers to match the changes made to those events: pokt-network/poktroll#1677
1 parent 4ec3a0d commit a10c265

File tree

3 files changed

+153
-100
lines changed

3 files changed

+153
-100
lines changed

src/mappings/pocket/applications.ts

Lines changed: 101 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
430424
async 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(),

src/mappings/pocket/gateways.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ async function _handleGatewayMsgStake(
4343
stakeDenom: stake.denom,
4444
accountId: msg.msg.decodedMsg.address,
4545
stakeStatus: StakeStatus.Staked,
46+
unstakingEndHeight: undefined,
47+
unstakingEndBlockId: undefined,
48+
unstakingBeginBlockId: undefined,
4649
});
4750

4851
const msgId = messageId(msg);
@@ -143,7 +146,7 @@ async function _handleGatewayUnstakeEvent(
143146
}
144147

145148
async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) {
146-
let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewaySdk: GatewaySDKType | null = null;
149+
let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewayAddress: string | null = null;
147150

148151
for (const attribute of event.event.attributes) {
149152
if (attribute.key === "unbonding_end_height") {
@@ -154,8 +157,13 @@ async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) {
154157
sessionEndHeight = BigInt((attribute.value as unknown as string).replaceAll("\"", ""));
155158
}
156159

160+
if (attribute.key === "gateway_address") {
161+
gatewayAddress = (attribute.value as string).replaceAll("\"", "");
162+
}
163+
164+
// older versions of this event included this attribute
157165
if (attribute.key === "gateway") {
158-
gatewaySdk = JSON.parse(attribute.value as unknown as string);
166+
gatewayAddress = (JSON.parse(attribute.value as string) as GatewaySDKType).address;
159167
}
160168
}
161169

@@ -167,14 +175,14 @@ async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) {
167175
throw new Error(`[handleEventGatewayUnbondingBegin] sessionEndHeight not found in event`);
168176
}
169177

170-
if (!gatewaySdk) {
171-
throw new Error(`[handleEventGatewayUnbondingBegin] gateway not found in event`);
178+
if (!gatewayAddress) {
179+
throw new Error(`[handleEventGatewayUnbondingBegin] gatewayAddress not found in event`);
172180
}
173181

174-
const gateway = await Gateway.get(gatewaySdk.address);
182+
const gateway = await Gateway.get(gatewayAddress);
175183

176184
if (!gateway) {
177-
throw new Error(`[handleEventGatewayUnbondingBegin] gateway not found for address ${gatewaySdk.address}`);
185+
throw new Error(`[handleEventGatewayUnbondingBegin] gateway not found for address ${gatewayAddress}`);
178186
}
179187

180188
gateway.unstakingEndHeight = unstakingEndHeight;
@@ -196,7 +204,7 @@ async function _handleEventGatewayUnbondingBegin(event: CosmosEvent) {
196204
}
197205

198206
async function _handleEventGatewayUnbondingEnd(event: CosmosEvent) {
199-
let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewaySdk: GatewaySDKType | null = null;
207+
let unstakingEndHeight: bigint | null = null, sessionEndHeight: bigint | null = null, gatewayAddress: string | null = null;
200208

201209
for (const attribute of event.event.attributes) {
202210
if (attribute.key === "unbonding_end_height") {
@@ -207,8 +215,13 @@ async function _handleEventGatewayUnbondingEnd(event: CosmosEvent) {
207215
sessionEndHeight = BigInt((attribute.value as unknown as string).replaceAll("\"", ""));
208216
}
209217

218+
if (attribute.key === "gateway_address") {
219+
gatewayAddress = (attribute.value as string).replaceAll("\"", "");
220+
}
221+
222+
// older versions of this event included this attribute
210223
if (attribute.key === "gateway") {
211-
gatewaySdk = JSON.parse(attribute.value as unknown as string);
224+
gatewayAddress = (JSON.parse(attribute.value as string) as GatewaySDKType).address;
212225
}
213226
}
214227

@@ -220,17 +233,17 @@ async function _handleEventGatewayUnbondingEnd(event: CosmosEvent) {
220233
throw new Error(`[handleEventGatewayUnbondingEnd] sessionEndHeight not found in event`);
221234
}
222235

223-
if (!gatewaySdk) {
224-
throw new Error(`[handleEventGatewayUnbondingEnd] gateway not found in event`);
236+
if (!gatewayAddress) {
237+
throw new Error(`[handleEventGatewayUnbondingEnd] gatewayAddress not found in event`);
225238
}
226239

227240
const [gateway, undelegates] = await Promise.all([
228-
Gateway.get(gatewaySdk.address),
229-
getUndelegatesForUnstakedGateway(gatewaySdk.address)
241+
Gateway.get(gatewayAddress),
242+
getUndelegatesForUnstakedGateway(gatewayAddress)
230243
]);
231244

232245
if (!gateway) {
233-
throw new Error(`[handleEventGatewayUnbondingEnd] gateway not found for address ${gatewaySdk.address}`);
246+
throw new Error(`[handleEventGatewayUnbondingEnd] gateway not found for address ${gatewayAddress}`);
234247
}
235248

236249
gateway.unstakingEndBlockId = unstakingEndHeight;

0 commit comments

Comments
 (0)