Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor velodrome/aerodrome #1930

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 24 additions & 68 deletions dexs/aerodrome-slipstream/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { FetchOptions, FetchResult, SimpleAdapter } from "../../adapters/types"
import { CHAIN } from "../../helpers/chains"

const gurar = '0xe521fc2C55AF632cdcC3D69E7EFEd93d56c89015';
const abis: any = {
"forSwaps": "function forSwaps(uint256 _limit, uint256 _offset) view returns ((address lp, int24 type, address token0, address token1, address factory, uint256 pool_fee)[])"
}

interface IForSwap {
lp: string;
token0: string;
token1: string;
pool_fee: string;
}
import { addOneToken } from "../../helpers/prices";
import { filterPools2 } from "../../helpers/uniswap";

interface ILog {
address: string;
Expand All @@ -21,71 +11,37 @@ interface ILog {
}
const event_swap = 'event Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)'

const fetch = async (timestamp: number, _: any, { api, getLogs, createBalances, }: FetchOptions): Promise<FetchResult> => {
const fetch = async (fetchOptions: FetchOptions): Promise<FetchResult> => {
const { api, getLogs, createBalances, } = fetchOptions
const chain = api.chain
const dailyVolume = createBalances()
const dailyFees = createBalances()
const chunkSize = 400;
let currentOffset = 965; // Slipstream launched after ~970 v2 pools were already created
const allForSwaps: IForSwap[] = [];
let unfinished = true;

while (unfinished) {
const forSwapsUnfiltered: IForSwap[] = (await api.call({
target: gurar,
params: [chunkSize, currentOffset],
abi: abis.forSwaps,
chain: CHAIN.BASE,
}));

const forSwaps: IForSwap[] = forSwapsUnfiltered.filter(t => Number(t.type) > 0).map((e: any) => {
return {
lp: e.lp,
token0: e.token0,
token1: e.token1,
pool_fee: e.pool_fee,
}
});

unfinished = forSwapsUnfiltered.length !== 0;
currentOffset += chunkSize;
allForSwaps.push(...forSwaps);
}

const targets = allForSwaps.map((forSwap: IForSwap) => forSwap.lp)

let logs: ILog[][] = [];
const targetChunkSize = 5;
let currentTargetOffset = 0;
unfinished = true;

while (unfinished) {
let endOffset = currentTargetOffset + targetChunkSize;
if (endOffset >= targets.length) {
unfinished = false;
endOffset = targets.length;
}

let currentLogs: ILog[][] = await getLogs({
targets: targets.slice(currentTargetOffset, endOffset),
eventAbi: event_swap,
flatten: false,
})

logs.push(...currentLogs);
currentTargetOffset += targetChunkSize;
}

let pairs = await api.fetchList({ lengthAbi: 'allPoolsLength', itemAbi: 'allPools', target: '0x5e7BB104d84c7CB9B682AaC2F3d509f5F406809A' })
let token0s = await api.multiCall({ abi: 'address:token0', calls: pairs })
let token1s = await api.multiCall({ abi: 'address:token1', calls: pairs })
const res = await filterPools2({ fetchOptions, pairs, token0s, token1s })
api.log(res.pairs.length, 'pairs out of', pairs.length, chain, 'aerodrome')
pairs = res.pairs
token0s = res.token0s
token1s = res.token1s
const fees = await api.multiCall({ abi: 'uint256:fee', calls: pairs })

let logs: ILog[][] = await getLogs({ targets: pairs, eventAbi: event_swap, flatten: false, })
logs.forEach((logs: ILog[], idx: number) => {
const { token1, pool_fee } = allForSwaps[idx]
const token0 = token0s[idx]
const token1 = token1s[idx]
const fee = fees[idx]/1e6
logs.forEach((log: any) => {
dailyVolume.add(token1, BigInt(Math.abs(Number(log.amount1))))
dailyFees.add(token1, BigInt( Math.round((((Math.abs(Number(log.amount1))) * Number(pool_fee)) / 1000000)))) // 1% fee represented as pool_fee=10000
addOneToken({ chain, balances: dailyVolume, token0, token1, amount0: log.amount0, amount1: log.amount1 })
addOneToken({ chain, balances: dailyFees, token0, token1, amount0: Number(log.amount0) * fee, amount1: Number(log.amount1) * fee })
})
})

return { dailyVolume, timestamp, dailyFees, dailyRevenue: dailyFees, dailyHoldersRevenue: dailyFees }
return { dailyVolume, dailyFees, dailyRevenue: dailyFees, dailyHoldersRevenue: dailyFees } as any
}

const adapters: SimpleAdapter = {
version: 2,
adapter: {
[CHAIN.BASE]: {
fetch: fetch as any,
Expand Down
144 changes: 6 additions & 138 deletions dexs/aerodrome/index.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,8 @@
import { FetchOptions, FetchResultV2, IJSON, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { addOneToken } from "../../helpers/prices";
import { filterPools } from "../../helpers/uniswap";
import { CHAIN } from "../../helpers/chains"
import { uniV2Exports } from "../../helpers/uniswap";

const sugar = '0xe521fc2C55AF632cdcC3D69E7EFEd93d56c89015';
const sugarOld = '0x2073D8035bB2b0F2e85aAF5a8732C6f397F9ff9b';
const abis: any = {
"forSwaps": "function forSwaps(uint256 _limit, uint256 _offset) view returns ((address lp, int24 type, address token0, address token1, address factory, uint256 pool_fee)[])"
}
const abisOld: any = {
"forSwaps": "function forSwaps() view returns ((address lp, bool stable, address token0, address token1, address factory)[])"
}
const swapEvent = 'event Swap(address indexed sender, address indexed to, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out)'

interface IForSwap {
lp: string;
token0: string;
token1: string;
pool_fee: string;
}

interface ILog {
address: string;
data: string;
transactionHash: string;
topics: string[];
}
const event_swap = 'event Swap(address indexed sender,address indexed to,uint256 amount0In,uint256 amount1In,uint256 amount0Out,uint256 amount1Out)'

const fetch = async (options: FetchOptions): Promise<FetchResultV2> => {
const dailyVolume = options.createBalances()
const dailyFees = options.createBalances()
const chunkSize = 500;
let currentOffset = 0;
const allForSwaps: IForSwap[] = [];
let unfinished = true;

if (options.startOfDay > 1714743000) {

while (unfinished && currentOffset < 970) {
const forSwaps: IForSwap[] = (await options.api.call({
target: sugar,
params: [chunkSize, currentOffset],
abi: abis.forSwaps,
chain: CHAIN.BASE,
})).filter(t => Number(t.type) < 1).map((e: any) => { // Regular v2 pool are types 0 and -1
return {
lp: e.lp,
token0: e.token0,
token1: e.token1,
pool_fee: e.pool_fee,
}
});

unfinished = forSwaps.length !== 0;
currentOffset += chunkSize;
if (currentOffset > 970) { // slipstream launched after ~970 v2 then we need to start from the beginning
currentOffset = 970;
}
allForSwaps.push(...forSwaps.sort(() => Math.random() - 0.5)); // shuffle the array to avoid getting stuck on a single pool
}

const pairObject: IJSON<string[]> = {}
const pairs = allForSwaps.map((forSwap: IForSwap) => forSwap.lp)
pairs.forEach((pair: string, i: number) => {
pairObject[pair] = [allForSwaps[i].token0, allForSwaps[i].token1]
})
// filter out the pairs with less than 1000 USD pooled value
const filteredPairs = await filterPools({ api: options.api, pairs: pairObject, createBalances: options.createBalances })
const dailyVolume = options.createBalances()
await Promise.all(Object.keys(filteredPairs).map(async (pair) => {
const [token0, token1] = pairObject[pair]
const logs = await options.getLogs({ target: pair, eventAbi: event_swap })
logs.forEach(log => {
addOneToken({ chain: options.chain, balances: dailyVolume, token0, token1, amount0: log.amount0Out, amount1: log.amount1Out })
})
}))
return { dailyVolume }
}
else {
const forSwapsOld: IForSwap[] = (await options.api.call({
target: sugarOld,
abi: abisOld.forSwaps,
chain: CHAIN.BASE,
})).map((e: any) => {
return {
lp: e.lp,
token0: e.token0,
token1: e.token1,
pool_fee: e.stable ? 5 : 30, // v2 0.05% stable swap fees, 0.3% volatile fees
}
})

const targets = forSwapsOld.map((forSwap: IForSwap) => forSwap.lp)

let logs: ILog[][] = [];
const targetChunkSize = 5;
let currentTargetOffset = 0;
unfinished = true;

while (unfinished) {
let endOffset = currentTargetOffset + targetChunkSize;
if (endOffset >= targets.length) {
unfinished = false;
endOffset = targets.length;
}

let currentLogs: ILog[][] = await options.getLogs({
targets: targets.slice(currentTargetOffset, endOffset),
eventAbi: event_swap,
flatten: false,
})

logs.push(...currentLogs);
currentTargetOffset += targetChunkSize;
}

logs.forEach((logs: ILog[], idx: number) => {
const { token0, token1, pool_fee } = forSwapsOld[idx]
logs.forEach((log: any) => {
dailyVolume.add(token0, log.amount0Out)
dailyVolume.add(token1, log.amount1Out)
dailyFees.add(token0, BigInt( Math.round((((Math.abs(Number(log.amount0Out))) * Number(pool_fee)) / 10000))))
dailyFees.add(token1, BigInt( Math.round((((Math.abs(Number(log.amount1Out))) * Number(pool_fee)) / 10000))))
})
})

return { dailyVolume, dailyFees, dailyRevenue: dailyFees, dailyHoldersRevenue: dailyFees }
}
}
const adapters: SimpleAdapter = {
version: 2,
adapter: {
[CHAIN.BASE]: {
fetch: fetch as any,
start: 1693180800,
}
}
}
export default adapters;
export default uniV2Exports({
[CHAIN.BASE]: { factory: '0x420DD381b31aEf6683db6B902084cB0FFECe40Da', swapEvent, },
})
85 changes: 0 additions & 85 deletions fees/Omnidrome/bribes.ts

This file was deleted.

Loading
Loading