Skip to content

Commit a93dd2d

Browse files
committed
* Added handler for older version of EventSupplierSlashed
* Fixed `getEntityIdArg` when calling `indexStakeEntity` for the following events: `poktroll.service.EventRelayMiningDifficultyUpdated` and `poktroll.tokenomics.EventSupplierSlashed`
1 parent 673b0cb commit a93dd2d

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

src/mappings/indexer.manager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,7 @@ async function indexService(msgByType: MessageByType, eventByType: EventByType):
214214
], {
215215
"/poktroll.service.MsgAddService": "service.id",
216216
"poktroll.service.EventRelayMiningDifficultyUpdated": (attributes) => {
217-
for (const attribute of attributes) {
218-
if (attribute.key === "service_id") {
219-
return JSON.parse(attribute.value as string).service_id
220-
}
221-
}
222-
223-
return null
217+
return attributes.find(({key}) => key === "service_id")?.value as string
224218
}
225219
})
226220
}
@@ -319,7 +313,6 @@ async function indexGateway(msgByType: MessageByType, eventByType: EventByType):
319313
"poktroll.gateway.EventGatewayUnstaked": getIdOfUnbondingEvents,
320314
"poktroll.gateway.EventGatewayUnbondingBegin": getIdOfUnbondingEvents,
321315
"poktroll.gateway.EventGatewayUnbondingEnd": getIdOfUnbondingEvents,
322-
323316
})
324317
}
325318

@@ -395,6 +388,8 @@ async function indexStakeEntity(data: Array<CosmosEvent | CosmosMessage>, getEnt
395388
if (!getEntityId) throw new Error(`getIdFromEventAttribute not found for event type ${datum.event.type}`)
396389
let entityId = getEntityId(datum.event.attributes)
397390

391+
if (!entityId) throw new Error(`entityId not found for event type ${datum.event.type}`)
392+
398393
if (Array.isArray(entityId)) {
399394
entitiesUpdatedAtSameDatum.push(entityId)
400395
// we are only taking the first one because later we will get the data for every entity in this group
@@ -516,6 +511,11 @@ async function indexSupplier(msgByType: MessageByType, eventByType: EventByType)
516511
"poktroll.supplier.EventSupplierUnbondingEnd": eventGetId,
517512
"poktroll.tokenomics.EventSupplierSlashed": (attributes) => {
518513
for (const attribute of attributes) {
514+
// in the previous version of this event this is the key to get the supplierId
515+
if (attribute.key === "supplier_operator_addr") {
516+
return attribute.value as string
517+
}
518+
519519
if (attribute.key === "claim") {
520520
return JSON.parse(attribute.value as string).supplier_operator_address
521521
}

src/mappings/poktroll/relays.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ function getSettlementOpReasonFromSDK(item: typeof SettlementOpReasonSDKType | n
133133
}
134134

135135
function parseAttribute(attribute: unknown): string {
136-
return (attribute as string).replaceAll("\"", "");
136+
return (attribute as string).replaceAll("\"", "");
137137
}
138138

139139
function 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+
671722
async 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

Comments
 (0)