Skip to content

Commit

Permalink
chore: verify BLD transactions and handle multiple tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed May 2, 2024
1 parent d92e50e commit 1bcfb52
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
30 changes: 25 additions & 5 deletions src/mappings/events/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export enum Operation {
Increment = 'increment',
Decrement = 'decrement',
}

interface BLDTransaction {
isBLDTransaction: boolean;
amount: string;
}
export const balancesEventKit = () => {
function getAttributeValue(decodedData: DecodedEvent, key: string) {
const attribute = decodedData.attributes.find((attr) => attr.key === key);
Expand Down Expand Up @@ -63,11 +68,26 @@ export const balancesEventKit = () => {
logger.info(`Created new entry for address: ${address}`);
}

function isBLDTransaction(amount: string) {
if (amount.slice(-4) === 'ubld') {
return true;
function validateBLDTransaction(amount: string | null): BLDTransaction {
const result: BLDTransaction = {
isBLDTransaction: false,
amount: '',
};

if (!amount) {
return result;
}
return false;
const coins = amount.split(',');

for (let coin of coins) {
if (coin.endsWith('ubld')) {
result.isBLDTransaction = true;
result.amount = coin;
return result;
}
}

return result;
}

async function updateBalance(
Expand Down Expand Up @@ -110,7 +130,7 @@ export const balancesEventKit = () => {
}

return {
isBLDTransaction,
validateBLDTransaction,
getAttributeValue,
decodeEvent,
addressExists,
Expand Down
39 changes: 25 additions & 14 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ export async function handleTransferEvent(
return;
}

if (!transactionAmount || !balancesKit.isBLDTransaction(transactionAmount)) {
logger.error(`Amount ${transactionAmount?.slice(0, -4)} invalid.`);
const { isBLDTransaction, amount } =
balancesKit.validateBLDTransaction(transactionAmount);

if (!transactionAmount || !isBLDTransaction) {
logger.error(`Amount ${transactionAmount} invalid.`);
return;
}

Expand All @@ -223,19 +226,27 @@ export async function handleTransferEvent(
await balancesKit.createBalancesEntry(senderAddress);
}

const adjustedAmount = BigInt(Math.round(Number(transactionAmount.slice(0, -4))));
const adjustedAmount = BigInt(Math.round(Number(amount.slice(0, -4))));

await Promise.all([
balancesKit.updateBalance(senderAddress, adjustedAmount, Operation.Decrement),
balancesKit.updateBalance(recipientAddress, adjustedAmount, Operation.Increment),
balancesKit.updateBalance(
senderAddress,
adjustedAmount,
Operation.Decrement
),
balancesKit.updateBalance(
recipientAddress,
adjustedAmount,
Operation.Increment
),
]);
}

export async function handleBalanceEvent(
cosmosEvent: CosmosEvent
): Promise<void> {
const { event } = cosmosEvent;

const incrementEventTypes = [
EVENT_TYPES.COMMISSION,
EVENT_TYPES.REWARDS,
Expand All @@ -244,10 +255,7 @@ export async function handleBalanceEvent(
EVENT_TYPES.COINBASE,
];

const decrementEventTypes = [
EVENT_TYPES.COIN_SPENT,
EVENT_TYPES.BURN,
];
const decrementEventTypes = [EVENT_TYPES.COIN_SPENT, EVENT_TYPES.BURN];

let operation: Operation | null = null;

Expand All @@ -261,7 +269,7 @@ export async function handleBalanceEvent(
}

logger.info(`Event:${event.type}`);

const balancesKit = balancesEventKit();
const decodedData: DecodedEvent = balancesKit.decodeEvent(cosmosEvent);
logger.info(`Decoded transaction data ${JSON.stringify(decodedData)}`);
Expand All @@ -281,7 +289,10 @@ export async function handleBalanceEvent(
return;
}

if (!transactionAmount || !balancesKit.isBLDTransaction(transactionAmount)) {
const { isBLDTransaction, amount } =
balancesKit.validateBLDTransaction(transactionAmount);

if (!transactionAmount || !isBLDTransaction) {
logger.error(`Amount ${transactionAmount} invalid.`);
return;
}
Expand All @@ -292,6 +303,6 @@ export async function handleBalanceEvent(
await balancesKit.createBalancesEntry(address);
}

const amount = BigInt(Math.round(Number(transactionAmount.slice(0, -4))));
await balancesKit.updateBalance(address, amount, operation);
const formattedAmount = BigInt(Math.round(Number(amount.slice(0, -4))));
await balancesKit.updateBalance(address, formattedAmount, operation);
}

0 comments on commit 1bcfb52

Please sign in to comment.