Skip to content

Commit

Permalink
chore: handle encoded and decoded data
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed May 2, 2024
1 parent 1bcfb52 commit 1a6f8d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
23 changes: 21 additions & 2 deletions src/mappings/events/balances.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Balances } from '../../types';
import { BALANCE_FIELDS } from '../constants';
import { b64decode } from '../utils';
import { CosmosEvent } from '@subql/types-cosmos';

Expand All @@ -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;

Expand Down Expand Up @@ -133,6 +151,7 @@ export const balancesEventKit = () => {
validateBLDTransaction,
getAttributeValue,
decodeEvent,
getData,
addressExists,
createBalancesEntry,
updateBalance,
Expand Down
20 changes: 11 additions & 9 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down Expand Up @@ -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
);

Expand Down

0 comments on commit 1a6f8d9

Please sign in to comment.