Skip to content

Commit

Permalink
solve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
aburkut committed Nov 29, 2024
2 parents 3c04b1e + 29db1d1 commit 9fd584c
Show file tree
Hide file tree
Showing 12 changed files with 1,361 additions and 139 deletions.
735 changes: 731 additions & 4 deletions src/abi/fluid-dex/resolver.abi.json

Large diffs are not rendered by default.

39 changes: 20 additions & 19 deletions src/dex/algebra/algebra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type PoolPairsInfo = {

const PoolsRegistryHashKey = `${CACHE_PREFIX}_poolsRegistry`;

// const ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS = 3 * 24 * 60 * 60 * 1000; // 3 days
// const ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS = 24 * 60 * 60 * 1000; // Once in a day
const ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS = 3 * 24 * 60 * 60 * 1000; // 3 days
const ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS = 24 * 60 * 60 * 1000; // Once in a day
const ALGEBRA_EFFICIENCY_FACTOR = 3;
const ALGEBRA_TICK_GAS_COST = 24_000; // Ceiled
const ALGEBRA_TICK_BASE_OVERHEAD = 75_000;
Expand Down Expand Up @@ -168,23 +168,24 @@ export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
// Init listening to new pools creation
await this.factory.initialize(blockNumber);

//// COMMENTING DEPRECATED LOGIC: as we now invalidate pools on creation this is not needed anymore
// if (!this.dexHelper.config.isSlave) {
// const cleanExpiredNotExistingPoolsKeys = async () => {
// const maxTimestamp =
// Date.now() - ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS;
// await this.dexHelper.cache.zremrangebyscore(
// this.notExistingPoolSetKey,
// 0,
// maxTimestamp,
// );
// };

// this.intervalTask = setInterval(
// cleanExpiredNotExistingPoolsKeys.bind(this),
// ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS,
// );
// }
if (!this.dexHelper.config.isSlave) {
const cleanExpiredNotExistingPoolsKeys = async () => {
const maxTimestamp =
Date.now() - ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS;
await this.dexHelper.cache.zremrangebyscore(
this.notExistingPoolSetKey,
0,
maxTimestamp,
);
};

void cleanExpiredNotExistingPoolsKeys();

this.intervalTask = setInterval(
cleanExpiredNotExistingPoolsKeys.bind(this),
ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS,
);
}
}

/*
Expand Down
3 changes: 1 addition & 2 deletions src/dex/algebra/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export const AlgebraConfig: DexConfigMap<DexParams> = {
chunksCount: 3,
initRetryFrequency: 30,
algebraStateMulticall: '0xa6bc273A238867dD74F2bBbD5fBbA3c941C939B9',
subgraphURL:
'https://api.studio.thegraph.com/query/44554/quickswap-v3-02/0.0.7',
subgraphURL: '3L5Y5brtgvzDoAFGaPs63xz27KdviCdzRuY12spLSBGU',
uniswapMulticall: '0x61530d6E1c7A47BBB3e48e8b8EdF7569DcFeE121',
deployer: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270',
version: 'v1.1',
Expand Down
2 changes: 1 addition & 1 deletion src/dex/fluid-dex/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const FluidDexConfig: DexConfigMap<DexParams> = {
[Network.MAINNET]: {
commonAddresses: {
liquidityProxy: '0x52aa899454998be5b000ad077a46bbe360f4e497',
resolver: '0xE8a07a32489BD9d5a00f01A55749Cf5cB854Fd13',
resolver: '0x45f4ad57e300da55c33dea579a40fcee000d7b94',
dexFactory: '0x91716C4EDA1Fb55e84Bf8b4c7085f84285c19085',
},
},
Expand Down
163 changes: 157 additions & 6 deletions src/dex/fluid-dex/fluid-dex-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import dotenv from 'dotenv';
dotenv.config();

import { StaticJsonRpcProvider } from '@ethersproject/providers';

import { testE2E } from '../../../tests/utils-e2e';
import {
Tokens,
Expand All @@ -12,6 +11,9 @@ import {
} from '../../../tests/constants-e2e';
import { Network, ContractMethod, SwapSide } from '../../constants';
import { generateConfig } from '../../config';
import { CollateralReserves, DebtReserves, DexLimits } from './types';
import { DummyDexHelper } from '../../dex-helper/index';
import { FluidDex } from './fluid-dex';

/*
README
Expand Down Expand Up @@ -66,10 +68,10 @@ function testForNetwork(
);
const tokens = Tokens[network];
const holders = Holders[network];
const nativeTokenSymbol = NativeTokenSymbols[network];

const sideToContractMethods = new Map([
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
[SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
]);

describe(`${network}`, () => {
Expand All @@ -83,7 +85,7 @@ function testForNetwork(
tokens[tokenBSymbol],
holders[tokenASymbol],
tokenBAmount,
SwapSide.SELL,
side,
dexKey,
contractMethod as ContractMethod,
network,
Expand All @@ -96,7 +98,7 @@ function testForNetwork(
tokens[tokenASymbol],
holders[tokenBSymbol],
tokenBAmount,
SwapSide.SELL,
side,
dexKey,
contractMethod as ContractMethod,
network,
Expand Down Expand Up @@ -137,8 +139,8 @@ describe('FluidDex E2E', () => {
const tokenASymbol: string = 'USDC';
const tokenBSymbol: string = 'USDT';

const tokenAAmount: string = '1000000';
const tokenBAmount: string = '100000000';
const tokenAAmount: string = '10000';
const tokenBAmount: string = '1000000';

testForNetwork(
network,
Expand All @@ -151,3 +153,152 @@ describe('FluidDex E2E', () => {
});
});
});

function NewColReservesOne(): CollateralReserves {
return {
token0RealReserves: BigInt(20000000006000000),
token1RealReserves: BigInt(20000000000500000),
token0ImaginaryReserves: BigInt(389736659726997981),
token1ImaginaryReserves: BigInt(389736659619871949),
};
}

function NewDebtReservesOne(): DebtReserves {
return {
token0Debt: BigInt(1e18),
token1Debt: BigInt(1e18),
token0RealReserves: BigInt(9486832995556050),
token1RealReserves: BigInt(9486832993079885),
token0ImaginaryReserves: BigInt(184868330099560759),
token1ImaginaryReserves: BigInt(184868330048879109),
};
}

const limitsTight: DexLimits = {
withdrawableToken0: {
available: 456740438880263n,
expandsTo: 711907234052361388866n,
expandsDuration: 600n,
},
withdrawableToken1: {
available: 825179383432029n,
expandsTo: 711907234052361388866n,
expandsDuration: 600n,
},
borrowableToken0: {
available: 941825058374170n,
expandsTo: 711907234052361388866n,
expandsDuration: 600n,
},
borrowableToken1: {
available: 941825058374170n,
expandsTo: 711907234052361388866n,
expandsDuration: 600n,
},
};

const limitsWide: DexLimits = {
withdrawableToken0: {
available: BigInt(34242332879776515083099999),
expandsTo: BigInt(34242332879776515083099999),
expandsDuration: 0n,
},
withdrawableToken1: {
available: BigInt(34242332879776515083099999),
expandsTo: BigInt(34242332879776515083099999),
expandsDuration: 22n,
},
borrowableToken0: {
available: BigInt(34242332879776515083099999),
expandsTo: BigInt(34242332879776515083099999),
expandsDuration: 0n,
},
borrowableToken1: {
available: BigInt(34242332879776515083099999),
expandsTo: BigInt(34242332879776515083099999),
expandsDuration: 308n,
},
};

const ErrInsufficientBorrowable = new Error('insufficient borrowable');
const ErrInsufficientMaxPrice = new Error('insufficient max price');
const ErrInsufficientReserve = new Error('insufficient reserve');

describe('TestPoolSimulator_SwapInLimits', () => {
const network = Network.MAINNET;
const dexHelper = new DummyDexHelper(network);
const dexKey = 'FluidDex';
const fluidDex = new FluidDex(network, dexKey, dexHelper);

it('when limits hit', () => {
let outAmt;
try {
outAmt = fluidDex.swapInAdjusted(
true,
BigInt(1e15),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsTight,
Math.floor(Date.now() / 1000) - 10,
);
expect(outAmt).toEqual(0n);
} catch (err: any) {
expect(err.message).toEqual(ErrInsufficientBorrowable.message);
}
});

it('when expanded', () => {
const outAmt = fluidDex.swapInAdjusted(
true,
BigInt(1e15),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsTight,
Math.floor(Date.now() / 1000) - 6000,
);
console.log('outAmt: ', outAmt);
expect(outAmt?.toString()).toEqual('998262697204710');
});

it('when price diff hit', () => {
let outAmt;
try {
outAmt = fluidDex.swapInAdjusted(
true,
BigInt(3e16),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsWide,
Math.floor(Date.now() / 1000) - 10,
);
expect(outAmt).toEqual(0n);
} catch (err: any) {
expect(err.message).toEqual(ErrInsufficientMaxPrice.message);
}
});

it('when reserves limit is hit', () => {
let outAmt;
try {
outAmt = fluidDex.swapInAdjusted(
true,
BigInt(5e16),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsWide,
Math.floor(Date.now() / 1000) - 10,
);
expect(outAmt).toEqual(0n);
} catch (err: any) {
expect(err.message).toEqual(ErrInsufficientReserve.message);
}
});
});
6 changes: 2 additions & 4 deletions src/dex/fluid-dex/fluid-dex-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ describe('FluidDex EventPool Mainnet', function () {
const eventsToTest: Record<Address, EventMappings> = {
'0x52aa899454998be5b000ad077a46bbe360f4e497': {
LogOperate: [
21091850, 21091882, 21091897, 21091915, 21092008, 21092022, 21092039,
21092142, 21092176, 21092187, 21092230, 21092286, 21092289, 21092295,
21092319, 21092352, 21092360, 21092368, 21092378, 21092383,
21190399, 21190405, 21190420, 21190452, 21190454, 21190465, 21190506,
],
},
};
Expand Down Expand Up @@ -86,7 +84,7 @@ describe('FluidDex EventPool Mainnet', function () {

const eventsToTest: Record<Address, EventMappings> = {
'0x91716C4EDA1Fb55e84Bf8b4c7085f84285c19085': {
LogDexDeployed: [21105297, 21105362, 21105366, 21105370],
LogDexDeployed: [21199929],
},
};

Expand Down
Loading

0 comments on commit 9fd584c

Please sign in to comment.