From 1f91bd94403cfa63bdd5ce8ced81395c7caef23e Mon Sep 17 00:00:00 2001 From: 0xpeluche <0xpeluche@proton.me> Date: Wed, 30 Oct 2024 17:27:14 +0100 Subject: [PATCH 1/2] Feat: Fluid-dex (Volume&Fees) --- dexs/fluid-dex/index.ts | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 dexs/fluid-dex/index.ts diff --git a/dexs/fluid-dex/index.ts b/dexs/fluid-dex/index.ts new file mode 100644 index 0000000000..611e492a17 --- /dev/null +++ b/dexs/fluid-dex/index.ts @@ -0,0 +1,83 @@ +import { Adapter, FetchOptions, FetchResultV2 } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; + +interface PoolInfo { + pool: string; + token0: string; + token1: string; + fee: number; +} + +interface SwapEventArgs { + swap0to1: boolean; + amountIn: bigint; + amountOut: bigint; + to: string; + pool: string; + token0: string; + token1: string; + fee: bigint; +} + +const dexReservesResolver = "0xE8a07a32489BD9d5a00f01A55749Cf5cB854Fd13"; + +const abi = { + getAllPools: "function getAllPools() view returns (tuple(address pool, address token0, address token1, uint256 fee)[])", + swap: "event Swap(bool swap0to1, uint256 amountIn, uint256 amountOut, address to)", +}; + +const fetch = async ({ api, createBalances, getLogs }: FetchOptions): Promise => { + const dailyVolume = createBalances(); + const dailyFees = createBalances() + const rawPools: PoolInfo[] = await api.call({ target: dexReservesResolver, abi: abi.getAllPools }); + const pools = rawPools.map(({ pool, token0, token1, fee }) => ({ pool, token0, token1, fee: BigInt(fee) })); + + const logsByPool: SwapEventArgs[][]= await Promise.all( + pools.map(async pool => { + const logs: SwapEventArgs[] = await getLogs({ + targets: [pool.pool], + onlyArgs: true, + eventAbi: abi.swap, + }); + + return logs.map(log => ({ + swap0to1: log[0] as boolean, + amountIn: log[1] as bigint, + amountOut: log[2] as bigint, + to: log[3] as string, + ...pool + })); + }) + ); + + const allSwapEvents = logsByPool.flat(); + const swapEvents0to1 = allSwapEvents.filter(event => event.swap0to1); + const swapEvents1to0 = allSwapEvents.filter(event => !event.swap0to1); + + const processSwapEvents = (events: SwapEventArgs[], isSwap0to1: boolean) => { + events.forEach(({ amountIn, amountOut, token0, token1, fee }) => { + const feesCollected = amountOut * 1000000n / (1000000n - fee) - amountOut; // 1000000n = 100% + if (isSwap0to1) { + dailyVolume.add(token0, amountIn); + dailyFees.add(token1, feesCollected); + } else { + dailyVolume.add(token1, amountIn); + dailyFees.add(token0, feesCollected); + } + }); + }; + + processSwapEvents(swapEvents0to1, true); + processSwapEvents(swapEvents1to0, false); + + return { dailyVolume, dailyFees }; +}; + +const adapter: Adapter = { + version: 2, + adapter: { + [CHAIN.ETHEREUM]: { fetch, start: 1729969200 }, + }, +}; + +export default adapter; From 72d799bf54f1ae4c862b8c5e174dd0e76b420643 Mon Sep 17 00:00:00 2001 From: 0xpeluche <0xpeluche@proton.me> Date: Thu, 31 Oct 2024 15:35:43 +0100 Subject: [PATCH 2/2] fix formula --- dexs/fluid-dex/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dexs/fluid-dex/index.ts b/dexs/fluid-dex/index.ts index 611e492a17..2d0cf86456 100644 --- a/dexs/fluid-dex/index.ts +++ b/dexs/fluid-dex/index.ts @@ -55,14 +55,14 @@ const fetch = async ({ api, createBalances, getLogs }: FetchOptions): Promise !event.swap0to1); const processSwapEvents = (events: SwapEventArgs[], isSwap0to1: boolean) => { - events.forEach(({ amountIn, amountOut, token0, token1, fee }) => { - const feesCollected = amountOut * 1000000n / (1000000n - fee) - amountOut; // 1000000n = 100% + events.forEach(({ amountIn, token0, token1, fee }) => { + const feesCollected = (amountIn * fee) / 1000000n // 1000000n = 100% if (isSwap0to1) { dailyVolume.add(token0, amountIn); - dailyFees.add(token1, feesCollected); + dailyFees.add(token0, feesCollected); } else { dailyVolume.add(token1, amountIn); - dailyFees.add(token0, feesCollected); + dailyFees.add(token1, feesCollected); } }); };