Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/mappings/indexer.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ async function indexService(msgByType: MessageByType, eventByType: EventByType):
], {
"/poktroll.service.MsgAddService": "service.id",
"poktroll.service.EventRelayMiningDifficultyUpdated": (attributes) => {
for (const attribute of attributes) {
if (attribute.key === "service_id") {
return JSON.parse(attribute.value as string).service_id
}
}

return null
return attributes.find(({key}) => key === "service_id")?.value as string
}
})
}
Expand Down Expand Up @@ -319,7 +313,6 @@ async function indexGateway(msgByType: MessageByType, eventByType: EventByType):
"poktroll.gateway.EventGatewayUnstaked": getIdOfUnbondingEvents,
"poktroll.gateway.EventGatewayUnbondingBegin": getIdOfUnbondingEvents,
"poktroll.gateway.EventGatewayUnbondingEnd": getIdOfUnbondingEvents,

})
}

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

if (!entityId) throw new Error(`entityId not found for event type ${datum.event.type}`)

if (Array.isArray(entityId)) {
entitiesUpdatedAtSameDatum.push(entityId)
// we are only taking the first one because later we will get the data for every entity in this group
Expand Down Expand Up @@ -516,6 +511,11 @@ async function indexSupplier(msgByType: MessageByType, eventByType: EventByType)
"poktroll.supplier.EventSupplierUnbondingEnd": eventGetId,
"poktroll.tokenomics.EventSupplierSlashed": (attributes) => {
for (const attribute of attributes) {
// in the previous version of this event this is the key to get the supplierId
if (attribute.key === "supplier_operator_addr") {
return attribute.value as string
}

if (attribute.key === "claim") {
return JSON.parse(attribute.value as string).supplier_operator_address
}
Expand Down
65 changes: 60 additions & 5 deletions src/mappings/poktroll/relays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ function getSettlementOpReasonFromSDK(item: typeof SettlementOpReasonSDKType | n
}

function parseAttribute(attribute: unknown): string {
return (attribute as string).replaceAll("\"", "");
return (attribute as string).replaceAll("\"", "");
}

function getAttributes(attributes: CosmosEvent["event"]["attributes"]) {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let proof: ProofSDKType = {},
Expand Down Expand Up @@ -578,11 +579,11 @@ function _handleEventApplicationOverserviced(event: CosmosEvent): EventApplicati

for (const attribute of event.event.attributes) {
if (attribute.key === "application_addr") {
applicationAddress = (attribute.value as string).replaceAll('"', '');
applicationAddress = parseAttribute(attribute.value)
}

if (attribute.key === "supplier_operator_addr") {
supplierAddress = (attribute.value as string).replaceAll('"', '');
supplierAddress = parseAttribute(attribute.value as string)
}

if (attribute.key === "expected_burn") {
Expand Down Expand Up @@ -668,18 +669,72 @@ function _handleEventApplicationReimbursementRequest(event: CosmosEvent): EventA
}
}

async function _handleOldEventSupplierSlashed(event: CosmosEvent) {
let slashingCoin: CoinSDKType | null = null, operatorAddress = '';

for (const attribute of event.event.attributes) {
if (attribute.key === "slashing_amount") {
slashingCoin = JSON.parse(attribute.value as string);
}

if (attribute.key === "supplier_operator_addr") {
operatorAddress = parseAttribute(attribute.value);
}
}

if (!slashingCoin) {
throw new Error(`[handleEventSupplierSlashed] slashingCoin not found in event`);
}

if (!operatorAddress) {
throw new Error(`[handleEventSupplierSlashed] operatorAddress not found in event`);
}

const supplier = await Supplier.get(operatorAddress)

if (!supplier) {
throw new Error(`[handleEventSupplierSlashed] supplier not found for operator address ${operatorAddress}`)
}

supplier.stakeAmount -= BigInt(slashingCoin.amount)

await Promise.all([
supplier.save(),
EventSupplierSlashed.create({
id: getEventId(event),
supplierId: operatorAddress,
blockId: getBlockId(event.block),
eventId: getEventId(event),
proofMissingPenalty: BigInt(slashingCoin.amount),
proofMissingPenaltyDenom: slashingCoin.denom,
previousStakeAmount: supplier.stakeAmount,
afterStakeAmount: supplier.stakeAmount,
// in alpha this event does not have the values below, so we are setting them to empty values for now
applicationId: '',
serviceId: '',
sessionId: '',
sessionStartHeight: BigInt(0),
sessionEndHeight: BigInt(0),
}).save(),
])
}

async function _handleEventSupplierSlashed(event: CosmosEvent) {
const {
claim,
proofMissingPenalty
} = getAttributes(event.event.attributes);

if (!claim) {
throw new Error(`[handleEventSupplierSlashed] claim not found in event`);
logger.warn(`[handleEventSupplierSlashed] claim not found in event, trying to handle with previous version`);
await _handleOldEventSupplierSlashed(event)
return
}

if (!claim.session_header) {
throw new Error(`[handleEventSupplierSlashed] session_header not found in event`);
logger.warn(`[handleEventSupplierSlashed] session_header not found in event, trying to handle with previous version`);
await _handleOldEventSupplierSlashed(event)
return
}

const supplier = await Supplier.get(claim.supplier_operator_address)
Expand Down
Loading