Skip to content

Commit

Permalink
feat: changes to index IST transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed May 3, 2024
1 parent acaf2a7 commit 1ce0cf8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
49 changes: 31 additions & 18 deletions src/mappings/events/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -66,30 +66,40 @@ export const balancesEventKit = () => {
return decodedData;
}

async function addressExists(address: string): Promise<boolean> {
const balance = await Balances.getByAddress(address);
async function addressExists(
address: string,
denom: string
): Promise<boolean> {
const balance = await Balances.getByFields([
['address', '=', address],
['denom', '=', denom],
]);

if (!balance || balance.length === 0) {
return false;
}
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) {
Expand All @@ -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) });
}
}

Expand All @@ -110,10 +119,14 @@ export const balancesEventKit = () => {

async function updateBalance(
address: string,
denom: string,
amount: bigint,
operation: Operation
): Promise<void> {
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}`);
Expand Down Expand Up @@ -148,7 +161,7 @@ export const balancesEventKit = () => {
}

return {
validateBLDTransaction,
validateTransaction,
getAttributeValue,
decodeEvent,
getData,
Expand Down
33 changes: 19 additions & 14 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export async function handleStateChangeEvent(cosmosEvent: CosmosEvent): Promise<
export async function handleBalanceEvent(
cosmosEvent: CosmosEvent
): Promise<void> {
const { event } = cosmosEvent;
const { event, block } = cosmosEvent;

const incrementEventTypes = [
EVENT_TYPES.COMMISSION,
Expand Down Expand Up @@ -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<void> {
Expand All @@ -239,14 +243,15 @@ export async function initiateBalancesTable(block: CosmosBlock): Promise<void> {
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`);
Expand Down

0 comments on commit 1ce0cf8

Please sign in to comment.