Skip to content

Commit

Permalink
Merge pull request #2056 from 0xpeluche/fluid-dex
Browse files Browse the repository at this point in the history
Feat: Fluid-dex (Volume&Fees)
  • Loading branch information
dtmkeng authored Nov 4, 2024
2 parents a6d3464 + 72d799b commit 48044cc
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions dexs/fluid-dex/index.ts
Original file line number Diff line number Diff line change
@@ -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<FetchResultV2> => {
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, token0, token1, fee }) => {
const feesCollected = (amountIn * fee) / 1000000n // 1000000n = 100%
if (isSwap0to1) {
dailyVolume.add(token0, amountIn);
dailyFees.add(token0, feesCollected);
} else {
dailyVolume.add(token1, amountIn);
dailyFees.add(token1, feesCollected);
}
});
};

processSwapEvents(swapEvents0to1, true);
processSwapEvents(swapEvents1to0, false);

return { dailyVolume, dailyFees };
};

const adapter: Adapter = {
version: 2,
adapter: {
[CHAIN.ETHEREUM]: { fetch, start: 1729969200 },
},
};

export default adapter;

0 comments on commit 48044cc

Please sign in to comment.