diff --git a/src/mappings/events/balances.ts b/src/mappings/events/balances.ts index 9aad7669..92600084 100644 --- a/src/mappings/events/balances.ts +++ b/src/mappings/events/balances.ts @@ -1,4 +1,5 @@ import { Balances } from '../../types'; +import { BALANCE_FIELDS } from '../constants'; import { b64decode } from '../utils'; import { CosmosEvent } from '@subql/types-cosmos'; @@ -22,11 +23,28 @@ interface BLDTransaction { amount: string; } export const balancesEventKit = () => { - function getAttributeValue(decodedData: DecodedEvent, key: string) { - const attribute = decodedData.attributes.find((attr) => attr.key === key); + function getAttributeValue(data: any, key: string) { + const attribute = data.attributes.find( + (attr: Attribute) => attr.key === key + ); return attribute ? attribute.value : null; } + function getData(cosmosEvent: CosmosEvent) { + let dataAlreadyDecoded = true; + const value = getAttributeValue(cosmosEvent.event, BALANCE_FIELDS.amount); + + if (!value) { + dataAlreadyDecoded = false; + } + + const data = dataAlreadyDecoded + ? cosmosEvent.event + : decodeEvent(cosmosEvent); + + return data; + } + function decodeEvent(cosmosEvent: CosmosEvent): DecodedEvent { const { event } = cosmosEvent; @@ -133,6 +151,7 @@ export const balancesEventKit = () => { validateBLDTransaction, getAttributeValue, decodeEvent, + getData, addressExists, createBalancesEntry, updateBalance, diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 22d614d4..791136e3 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -177,21 +177,22 @@ export async function handleTransferEvent( } logger.info('Event:TRANSFER'); + logger.info(`Event Data:${JSON.stringify(cosmosEvent.event)}`); const balancesKit = balancesEventKit(); - const decodedData: DecodedEvent = balancesKit.decodeEvent(cosmosEvent); - logger.info(`Decoded transaction data ${JSON.stringify(decodedData)}`); + const data = balancesKit.getData(cosmosEvent); + logger.info(`Decoded Data:${JSON.stringify(data)}`); const recipientAddress = balancesKit.getAttributeValue( - decodedData, + data, BALANCE_FIELDS.transfer_recipient ); const senderAddress = balancesKit.getAttributeValue( - decodedData, + data, BALANCE_FIELDS.transfer_sender ); const transactionAmount = balancesKit.getAttributeValue( - decodedData, + data, BALANCE_FIELDS.amount ); @@ -269,18 +270,19 @@ export async function handleBalanceEvent( } logger.info(`Event:${event.type}`); + logger.info(`Event Data:${JSON.stringify(cosmosEvent.event)}`); const balancesKit = balancesEventKit(); - const decodedData: DecodedEvent = balancesKit.decodeEvent(cosmosEvent); - logger.info(`Decoded transaction data ${JSON.stringify(decodedData)}`); + const data = balancesKit.getData(cosmosEvent); + logger.info(`Decoded Data:${JSON.stringify(data)}`); const address = balancesKit.getAttributeValue( - decodedData, + data, BALANCE_FIELDS[event.type as keyof typeof BALANCE_FIELDS] ); const transactionAmount = balancesKit.getAttributeValue( - decodedData, + data, BALANCE_FIELDS.amount );