diff --git a/src/mappings/events/balances.ts b/src/mappings/events/balances.ts index 92600084..356d24f6 100644 --- a/src/mappings/events/balances.ts +++ b/src/mappings/events/balances.ts @@ -18,9 +18,9 @@ export enum Operation { Decrement = 'decrement', } -interface BLDTransaction { - isBLDTransaction: boolean; - amount: string; +interface TransactionData { + isValidTransaction: boolean; + coins: { amount: string; denom: string }[]; } export const balancesEventKit = () => { function getAttributeValue(data: any, key: string) { @@ -66,8 +66,14 @@ export const balancesEventKit = () => { return decodedData; } - async function addressExists(address: string): Promise { - const balance = await Balances.getByAddress(address); + async function addressExists( + address: string, + denom: string + ): Promise { + const balance = await Balances.getByFields([ + ['address', '=', address], + ['denom', '=', denom], + ]); if (!balance || balance.length === 0) { return false; @@ -75,21 +81,25 @@ export const balancesEventKit = () => { return true; } - async function createBalancesEntry(address: string) { - const newBalance = new Balances(address); + async function createBalancesEntry( + address: string, + denom: string, + primaryKey: string + ) { + const newBalance = new Balances(primaryKey); newBalance.address = address; newBalance.balance = BigInt(0); - newBalance.denom = 'ubld'; + newBalance.denom = denom; await newBalance.save(); logger.info(`Created new entry for address: ${address}`); } - function validateBLDTransaction(amount: string | null): BLDTransaction { - const result: BLDTransaction = { - isBLDTransaction: false, - amount: '', + function validateTransaction(amount: string | null): TransactionData { + const result: TransactionData = { + isValidTransaction: false, + coins: [], }; if (!amount) { @@ -98,10 +108,9 @@ export const balancesEventKit = () => { const coins = amount.split(','); for (let coin of coins) { - if (coin.endsWith('ubld')) { - result.isBLDTransaction = true; - result.amount = coin; - return result; + if (coin.endsWith('ubld') || coin.endsWith('uist')) { + result.isValidTransaction = true; + result.coins.push({ amount: coin.slice(0, -4), denom: coin.slice(-4) }); } } @@ -110,10 +119,14 @@ export const balancesEventKit = () => { async function updateBalance( address: string, + denom: string, amount: bigint, operation: Operation ): Promise { - const balances = await Balances.getByAddress(address); + const balances = await Balances.getByFields([ + ['address', '=', address], + ['denom', '=', denom], + ]); if (!balances || balances.length === 0) { logger.error(`Balance not found for address: ${address}`); @@ -148,7 +161,7 @@ export const balancesEventKit = () => { } return { - validateBLDTransaction, + validateTransaction, getAttributeValue, decodeEvent, getData, diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 411d3bda..9cad1482 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -170,7 +170,7 @@ export async function handleStateChangeEvent(cosmosEvent: CosmosEvent): Promise< export async function handleBalanceEvent( cosmosEvent: CosmosEvent ): Promise { - const { event } = cosmosEvent; + const { event, block } = cosmosEvent; const incrementEventTypes = [ EVENT_TYPES.COMMISSION, @@ -215,22 +215,26 @@ export async function handleBalanceEvent( return; } - const { isBLDTransaction, amount } = - balancesKit.validateBLDTransaction(transactionAmount); + const { isValidTransaction, coins } = + balancesKit.validateTransaction(transactionAmount); - if (!transactionAmount || !isBLDTransaction) { + if (!transactionAmount || !isValidTransaction) { logger.error(`Amount ${transactionAmount} invalid.`); return; } - const entryExists = await balancesKit.addressExists(address); + for (let coin of coins) { + const { amount, denom } = coin; + const entryExists = await balancesKit.addressExists(address, denom); - if (!entryExists) { - await balancesKit.createBalancesEntry(address); - } + if (!entryExists) { + const primaryKey = `${block.block.id}-${denom}`; + await balancesKit.createBalancesEntry(address, denom, primaryKey); + } - const formattedAmount = BigInt(Math.round(Number(amount.slice(0, -4)))); - await balancesKit.updateBalance(address, formattedAmount, operation); + const formattedAmount = BigInt(Math.round(Number(amount))); + await balancesKit.updateBalance(address, denom, formattedAmount, operation); + } } export async function initiateBalancesTable(block: CosmosBlock): Promise { @@ -239,14 +243,15 @@ export async function initiateBalancesTable(block: CosmosBlock): Promise { process.env.network === 'main' ? mainnetGenesisData : localGenesisData; for (let element of data.balances) { - const newBalance = new Balances(element.address); - newBalance.address = element.address; + let newBalance; for (const coin of element.coins) { + newBalance = new Balances(`${block.block.id}-${coin.denom}`); + newBalance.address = element.address; newBalance.balance = BigInt(coin.amount); newBalance.denom = coin.denom; - } - await newBalance.save(); + await newBalance.save(); + } } logger.info(`Balances Table Initiated`);