-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2056 from 0xpeluche/fluid-dex
Feat: Fluid-dex (Volume&Fees)
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |