Skip to content

Commit

Permalink
disable getTopPoolsForToken
Browse files Browse the repository at this point in the history
  • Loading branch information
aburkut committed Oct 29, 2024
1 parent ced8c6c commit 0b6559d
Show file tree
Hide file tree
Showing 10 changed files with 2,161 additions and 102 deletions.
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,3 @@ tests/configs.json
.idea/
local-scripts
tests/debug-price-route.json
tests/constants-e2e.d.ts
tests/constants-e2e.js
tests/constants-e2e.js.map
tests/smart-tokens.d.ts
tests/smart-tokens.js
tests/smart-tokens.js.map
4 changes: 2 additions & 2 deletions src/dex/fluid-dex/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DexParams } from './types';
import { DexConfigMap, AdapterMappings } from '../../types';
import { Network, SwapSide } from '../../constants';
import { DexConfigMap } from '../../types';
import { Network } from '../../constants';

export const FluidDexConfig: DexConfigMap<DexParams> = {
FluidDex: {
Expand Down
186 changes: 93 additions & 93 deletions src/dex/fluid-dex/fluid-dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
import { extractReturnAmountPosition } from '../../executor/utils';
import { MultiResult } from '../../lib/multi-wrapper';
import { generalDecoder } from '../../lib/decoders';
// @ts-ignore
import { Tokens } from '../../../tests/constants-e2e';

export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
eventPools: { [id: string]: FluidDexEventPool } = {};
Expand Down Expand Up @@ -285,97 +283,99 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
tokenAddress: Address,
limit: number,
): Promise<PoolLiquidity[]> {
const latestBlockNumber_ = await this.dexHelper.provider.getBlockNumber();
let liquidityAmounts: { [id: string]: bigint } = {};
for (const pool of this.pools) {
if (
pool.token0 === tokenAddress.toLowerCase() ||
pool.token1 === tokenAddress.toLowerCase()
) {
const state: FluidDexPoolState = await this.eventPools[
pool.id
].getStateOrGenerate(latestBlockNumber_, false);

liquidityAmounts[pool.id] =
pool.token0 === tokenAddress
? state.collateralReserves.token0RealReserves +
state.debtReserves.token0RealReserves
: state.collateralReserves.token1RealReserves +
state.debtReserves.token1RealReserves;
}
}

const entries = Object.entries(liquidityAmounts);

// Sort the entries based on the values in descending order
entries.sort((a, b) => {
if (b[1] > a[1]) return 1;
if (b[1] < a[1]) return -1;
return 0;
});

// Take the top k entries
const topKEntries = entries.slice(0, limit);

// Convert the array back to an object
const sortedAmounts = Object.fromEntries(topKEntries);

const poolLiquidities: PoolLiquidity[] = [];

for (const [id, amount] of Object.entries(sortedAmounts)) {
const pool = this.pools.find(p => p.id === id);
if (!pool) continue; // Skip if pool not found

const state: FluidDexPoolState = await this.eventPools[
pool.id
].getStateOrGenerate(latestBlockNumber_, false);

let token0decimals: number;
for (const [networkStr, symbolMapping] of Object.entries(Tokens)) {
let found = false;
for (const [symbol, tokenParams] of Object.entries(symbolMapping)) {
if (tokenParams.address.toLowerCase() === pool.token0.toLowerCase()) {
token0decimals = tokenParams.decimals;
found = true;
break;
}
if (found) break;
}
}

let token1decimals: number;
for (const [networkStr, symbolMapping] of Object.entries(Tokens)) {
let found = false;
for (const [symbol, tokenParams] of Object.entries(symbolMapping)) {
if (tokenParams.address.toLowerCase() === pool.token1.toLowerCase()) {
token1decimals = tokenParams.decimals;
found = true;
break;
}
if (found) break;
}
}

const usd0 = await this.dexHelper.getTokenUSDPrice(
{ address: pool.token0, decimals: token0decimals! },
state.collateralReserves.token0RealReserves +
state.debtReserves.token0RealReserves,
);

const usd1 = await this.dexHelper.getTokenUSDPrice(
{ address: pool.token1, decimals: token1decimals! },
state.collateralReserves.token1RealReserves +
state.debtReserves.token1RealReserves,
);

poolLiquidities.push({
exchange: 'FluidDex',
address: pool.address,
connectorTokens: [],
liquidityUSD: Number(usd0 + usd1), // converted to number
});
}
return poolLiquidities;
// const latestBlockNumber_ = await this.dexHelper.provider.getBlockNumber();
// let liquidityAmounts: { [id: string]: bigint } = {};
// for (const pool of this.pools) {
// if (
// pool.token0 === tokenAddress.toLowerCase() ||
// pool.token1 === tokenAddress.toLowerCase()
// ) {
// const state: FluidDexPoolState = await this.eventPools[
// pool.id
// ].getStateOrGenerate(latestBlockNumber_, false);
//
// liquidityAmounts[pool.id] =
// pool.token0 === tokenAddress
// ? state.collateralReserves.token0RealReserves +
// state.debtReserves.token0RealReserves
// : state.collateralReserves.token1RealReserves +
// state.debtReserves.token1RealReserves;
// }
// }
//
// const entries = Object.entries(liquidityAmounts);
//
// // Sort the entries based on the values in descending order
// entries.sort((a, b) => {
// if (b[1] > a[1]) return 1;
// if (b[1] < a[1]) return -1;
// return 0;
// });
//
// // Take the top k entries
// const topKEntries = entries.slice(0, limit);
//
// // Convert the array back to an object
// const sortedAmounts = Object.fromEntries(topKEntries);
//
// const poolLiquidities: PoolLiquidity[] = [];
//
// for (const [id, amount] of Object.entries(sortedAmounts)) {
// const pool = this.pools.find(p => p.id === id);
// if (!pool) continue; // Skip if pool not found
//
// const state: FluidDexPoolState = await this.eventPools[
// pool.id
// ].getStateOrGenerate(latestBlockNumber_, false);
//
// // let token0decimals: number;
// // for (const [networkStr, symbolMapping] of Object.entries(Tokens)) {
// // let found = false;
// // for (const [symbol, tokenParams] of Object.entries(symbolMapping)) {
// // if (tokenParams.address.toLowerCase() === pool.token0.toLowerCase()) {
// // token0decimals = tokenParams.decimals;
// // found = true;
// // break;
// // }
// // if (found) break;
// // }
// // }
// //
// // let token1decimals: number;
// // for (const [networkStr, symbolMapping] of Object.entries(Tokens)) {
// // let found = false;
// // for (const [symbol, tokenParams] of Object.entries(symbolMapping)) {
// // if (tokenParams.address.toLowerCase() === pool.token1.toLowerCase()) {
// // token1decimals = tokenParams.decimals;
// // found = true;
// // break;
// // }
// // if (found) break;
// // }
// // }
//
// const usd0 = await this.dexHelper.getTokenUSDPrice(
// { address: pool.token0, decimals: token0decimals! },
// state.collateralReserves.token0RealReserves +
// state.debtReserves.token0RealReserves,
// );
//
// const usd1 = await this.dexHelper.getTokenUSDPrice(
// { address: pool.token1, decimals: token1decimals! },
// state.collateralReserves.token1RealReserves +
// state.debtReserves.token1RealReserves,
// );
//
// poolLiquidities.push({
// exchange: 'FluidDex',
// address: pool.address,
// connectorTokens: [],
// liquidityUSD: Number(usd0 + usd1), // converted to number
// });
// }
// return poolLiquidities;

return [];
}

async getDexParam(
Expand Down
22 changes: 22 additions & 0 deletions tests/constants-e2e.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SmartTokenParams, SmartToken } from '../tests/smart-tokens';
import { Address } from '../src/types';
export declare const GIFTER_ADDRESS = "0xb22fC4eC94D555A5049593ca4552c810Fb8a6d00";
export declare const GENERIC_ADDR1 = "0xbe9317f6711e2da074fe1f168fd9c402bc0a9d1b";
export declare const GENERIC_ADDR2 = "0x230a1ac45690b9ae1176389434610b9526d2f21b";
export declare const Tokens: {
[network: number]: {
[symbol: string]: SmartTokenParams;
};
};
export declare const Holders: {
[network: number]: {
[tokenAddress: string]: Address;
};
};
export declare const SmartTokens: Record<number, Record<string, SmartToken>>;
export declare const NativeTokenSymbols: {
[network: number]: string;
};
export declare const WrappedNativeTokenSymbols: {
[network: number]: string;
};
Loading

0 comments on commit 0b6559d

Please sign in to comment.