From 81fdb029da5f189ff7678b1b497cdd03113157d6 Mon Sep 17 00:00:00 2001 From: Dian Fishekqi Date: Fri, 31 May 2024 10:29:56 +0200 Subject: [PATCH] group by user/token --- adapters/xfai/src/index.ts | 101 +++++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 33 deletions(-) diff --git a/adapters/xfai/src/index.ts b/adapters/xfai/src/index.ts index 5e8ae3fb..6f96c701 100644 --- a/adapters/xfai/src/index.ts +++ b/adapters/xfai/src/index.ts @@ -146,6 +146,41 @@ function getLiquidityFromTransfers( return groupedTransfers; } +function groupLiquidityByUserAndToken( + block: BlockData, + liquidities: Omit[] +): OutputDataSchemaRow[] { + const groupedLiquidity: OutputDataSchemaRow[] = []; + const liquidityMap: Map> = new Map(); + + for (const liquidity of liquidities) { + const { user_address, token_address, token_balance } = liquidity; + const userMap = liquidityMap.get(user_address) || new Map(); + const existingBalance = userMap.get(token_address) || 0n; + userMap.set(token_address, existingBalance + token_balance); + liquidityMap.set(user_address, userMap); + } + + for (const [user, tokenMap] of liquidityMap) { + for (const [token, balance] of tokenMap) { + if (balance === 0n) { + continue; + } + groupedLiquidity.push({ + block_number: Number(block.blockNumber), + timestamp: block.blockTimestamp, + user_address: user, + token_address: token, + token_balance: balance, + token_symbol: "", + usd_price: 0, + }); + } + } + + return groupedLiquidity; +} + export async function getUserTVLByBlock( block: BlockData ): Promise { @@ -204,40 +239,40 @@ export async function getUserTVLByBlock( ]) ); - const result: OutputDataSchemaRow[] = liquiditiesRows.flatMap( - ({ owner, token, pool: poolAddress, liquidity }) => { - const poolSupply = poolSupplies[poolAddress]; - const poolReserve = poolRes[poolAddress]; - const tokenBalance = - (liquidity * poolReserve.reserve.toBigInt()) / poolSupply.toBigInt(); - const ethBalance = - (liquidity * poolReserve.ethReserve.toBigInt()) / poolSupply.toBigInt(); - return [ - // Token reserve - { - block_number: Number(block.blockNumber), - timestamp: block.blockTimestamp, - user_address: owner, - token_address: token, - token_balance: tokenBalance, - token_symbol: "", - usd_price: 0, - }, - // WETH Reserve - { - block_number: Number(block.blockNumber), - timestamp: block.blockTimestamp, - user_address: owner, - token_address: WETH, - token_balance: ethBalance, - token_symbol: "WETH", - usd_price: 0, - }, - ]; - } - ); + const result: Omit[] = + liquiditiesRows.flatMap( + ({ owner, token, pool: poolAddress, liquidity }) => { + const poolSupply = poolSupplies[poolAddress]; + const poolReserve = poolRes[poolAddress]; + const tokenBalance = + (liquidity * poolReserve.reserve.toBigInt()) / poolSupply.toBigInt(); + const ethBalance = + (liquidity * poolReserve.ethReserve.toBigInt()) / + poolSupply.toBigInt(); + return [ + // Token reserve + { + user_address: owner, + token_address: token, + token_balance: tokenBalance, + token_symbol: "", + usd_price: 0, + }, + // WETH Reserve + { + user_address: owner, + token_address: WETH, + token_balance: ethBalance, + token_symbol: "WETH", + usd_price: 0, + }, + ]; + } + ); - return result; + // + + return groupLiquidityByUserAndToken(block, result); } const readBlocksFromCSV = async (filePath: string): Promise => {