Skip to content

Commit

Permalink
Merge pull request #904 from SamarendraGouda/master
Browse files Browse the repository at this point in the history
fix(fluid-dex): resolve failing e2e tests
  • Loading branch information
JackieJoo authored Feb 14, 2025
2 parents 2626fb1 + 8388411 commit 585e499
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 216 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paraswap/dex-lib",
"version": "4.1.3",
"version": "4.1.4",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
30 changes: 30 additions & 0 deletions src/abi/fluid-dex/resolver.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@
"name": "fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -395,6 +400,11 @@
"name": "fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1192,6 +1202,11 @@
"name": "fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1392,6 +1407,11 @@
"name": "fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1616,6 +1636,11 @@
"name": "fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1816,6 +1841,11 @@
"name": "fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down
4 changes: 2 additions & 2 deletions src/dex/fluid-dex/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const FluidDexConfig: DexConfigMap<DexParams> = {
[Network.MAINNET]: {
commonAddresses: {
liquidityProxy: '0x52aa899454998be5b000ad077a46bbe360f4e497',
resolver: '0x45f4ad57e300da55c33dea579a40fcee000d7b94',
resolver: '0xb387f9C2092cF7c4943F97842887eBff7AE96EB3',
dexFactory: '0x91716C4EDA1Fb55e84Bf8b4c7085f84285c19085',
},
},
[Network.ARBITRUM]: {
commonAddresses: {
liquidityProxy: '0x52Aa899454998Be5b000Ad077a46Bbe360F4e497',
resolver: '0xb8f526718FF58758E256D9aD86bC194a9ff5986D',
resolver: '0xCe6F4E40152a1DF97ae95a7e4F60D944871A4060',
dexFactory: '0x91716C4EDA1Fb55e84Bf8b4c7085f84285c19085',
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/dex/fluid-dex/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const MIN_SWAP_LIQUIDITY = 10n ** 4n;
// on-chain we use 1e4 but use extra buffer to avoid reverts
export const MIN_SWAP_LIQUIDITY = 8500n;
178 changes: 61 additions & 117 deletions src/dex/fluid-dex/fluid-dex-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,13 @@ dotenv.config();

import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { testE2E } from '../../../tests/utils-e2e';
import {
Tokens,
Holders,
NativeTokenSymbols,
} from '../../../tests/constants-e2e';
import { Tokens, Holders } 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
======
The template already enumerates the basic structure which involves
testing simpleSwap, multiSwap, megaSwap contract methods for
ETH <> TOKEN and TOKEN <> TOKEN swaps. You should replace tokenA and
tokenB with any two highly liquid tokens on FluidDex for the tests
to work. If the tokens that you would like to use are not defined in
Tokens or Holders map, you can update the './tests/constants-e2e'
Other than the standard cases that are already added by the template
it is highly recommended to add test cases which could be specific
to testing FluidDex (Eg. Tests based on poolType, special tokens,
etc).
You can run this individual test script by running:
`npx jest src/dex/<dex-name>/<dex-name>-e2e.test.ts`
e2e tests use the Tenderly fork api. Please add the following to your
.env file:
TENDERLY_TOKEN=Find this under Account>Settings>Authorization.
TENDERLY_ACCOUNT_ID=Your Tenderly account name.
TENDERLY_PROJECT=Name of a Tenderly project you have created in your
dashboard.
(This comment should be removed from the final implementation)
*/

function testForNetwork(
network: Network,
dexKey: string,
Expand All @@ -56,9 +23,14 @@ function testForNetwork(
generateConfig(network).privateHttpProvider,
network,
);

const tokens = Tokens[network];
const holders = Holders[network];

// Create FluidDex instance to check reserves
const dexHelper = new DummyDexHelper(network);
const fluidDex = new FluidDex(network, dexKey, dexHelper);

const sideToContractMethods = new Map([
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
[SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
Expand All @@ -69,11 +41,11 @@ function testForNetwork(
describe(`${side}`, () => {
contractMethods.forEach((contractMethod: string) => {
describe(`${contractMethod}`, () => {
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {
await testE2E(
tokens[tokenASymbol],
tokens[tokenBSymbol],
holders[tokenASymbol],
tokens[tokenASymbol],
holders[tokenBSymbol],
tokenBAmount,
side,
dexKey,
Expand All @@ -82,12 +54,13 @@ function testForNetwork(
provider,
);
});
it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {

it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
await testE2E(
tokens[tokenBSymbol],
tokens[tokenASymbol],
holders[tokenBSymbol],
tokenBAmount,
tokens[tokenBSymbol],
holders[tokenASymbol],
tokenAAmount,
side,
dexKey,
contractMethod as ContractMethod,
Expand All @@ -108,30 +81,11 @@ describe('FluidDex E2E', () => {
describe('Mainnet', () => {
const network = Network.MAINNET;

// TODO: Uncomment & test when the issue is resolved. Currently no price as expected.
// describe.only('FLUID -> ETH', () => {
// const tokenASymbol: string = 'FLUID';
// const tokenBSymbol: string = 'ETH';
//
// const tokenAAmount: string = '160097047322810379';
// const tokenBAmount: string = '79923068733005505624';
//
// testForNetwork(
// network,
// dexKey,
// tokenASymbol,
// tokenBSymbol,
// tokenAAmount,
// tokenBAmount,
// );
// });

describe('ETH -> INST', () => {
const tokenASymbol: string = 'ETH';
const tokenBSymbol: string = 'INST';

const tokenAAmount: string = '100000000000000';
const tokenBAmount: string = '100000000000000';
describe('limit:FLUID -> ETH', () => {
const tokenASymbol: string = 'FLUID';
const tokenBSymbol: string = 'ETH';
const tokenAAmount: string = '16009704732281037900';
const tokenBAmount: string = '7992306873300550562400';

testForNetwork(
network,
Expand All @@ -148,7 +102,7 @@ describe('FluidDex E2E', () => {
const tokenBSymbol: string = 'ETH';

const tokenAAmount: string = '100000000000000';
const tokenBAmount: string = '100000000000000';
const tokenBAmount: string = '3500000000000000000';

testForNetwork(
network,
Expand Down Expand Up @@ -237,6 +191,9 @@ function NewDebtReservesOne(): DebtReserves {
};
}

const centerPriceTight: bigint = 3401944443797854601871360n;
const centerPriceWide: bigint = 1190081051944310368000000000n;

const limitsTight: DexLimits = {
withdrawableToken0: {
available: 456740438880263n,
Expand Down Expand Up @@ -283,10 +240,6 @@ const limitsWide: DexLimits = {
},
};

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);
Expand All @@ -295,21 +248,18 @@ describe('TestPoolSimulator_SwapInLimits', () => {

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);
}
outAmt = fluidDex.swapInAdjusted(
true,
BigInt(1e15),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsTight,
BigInt(centerPriceTight),
Math.floor(Date.now() / 1000) - 10,
);
expect(outAmt).toEqual(0n);
});

it('when expanded', () => {
Expand All @@ -321,47 +271,41 @@ describe('TestPoolSimulator_SwapInLimits', () => {
100n,
18,
limitsTight,
BigInt(centerPriceTight),
Math.floor(Date.now() / 1000) - 6000,
);
console.log('outAmt: ', outAmt);
expect(outAmt?.toString()).toEqual('998262697204710');
expect(outAmt?.toString()).toEqual('998163044346107');
});

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);
}
outAmt = fluidDex.swapInAdjusted(
true,
BigInt(3e16),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsWide,
BigInt(centerPriceWide),
Math.floor(Date.now() / 1000) - 10,
);
expect(outAmt).toEqual(0n);
});

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);
}
outAmt = fluidDex.swapInAdjusted(
true,
BigInt(5e16),
NewColReservesOne(),
NewDebtReservesOne(),
100n,
18,
limitsWide,
BigInt(centerPriceWide),
Math.floor(Date.now() / 1000) - 10,
);
expect(outAmt).toEqual(0n);
});
});
3 changes: 3 additions & 0 deletions src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
token0,
token1,
feeHex,
centerPriceHex,
collateralReservesHex,
debtReservesHex,
dexLimitsHex,
] = poolReserve;

const fee = Number(feeHex.toString());
const centerPrice = Number(centerPriceHex.toString());

const collateralReserves = {
token0RealReserves: bigIntify(collateralReservesHex[0]),
Expand Down Expand Up @@ -196,6 +198,7 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
token0,
token1,
fee,
centerPrice,
collateralReserves,
debtReserves,
dexLimits,
Expand Down
Loading

0 comments on commit 585e499

Please sign in to comment.