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

fix: Don't force add direct swap AMPL pools for V3 in candidate pools #764

Merged
merged 3 commits into from
Nov 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
7 changes: 7 additions & 0 deletions src/providers/token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export const DAI_MAINNET = new Token(
'DAI',
'Dai Stablecoin'
);
export const AMPL_MAINNET = new Token(
ChainId.MAINNET,
'0xD46bA6D942050d489DBd938a2C909A5d5039A161',
9,
'AMPL',
'AMPL'
);
export const FEI_MAINNET = new Token(
ChainId.MAINNET,
'0x956F47F50A910163D8BF957Cf5846D573E7f87CA',
Expand Down
64 changes: 38 additions & 26 deletions src/routers/alpha-router/functions/get-candidate-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,32 +992,44 @@ export async function getV3CandidatePools({
.value();

if (top2DirectSwapPool.length == 0 && topNDirectSwaps > 0) {
// If we requested direct swap pools but did not find any in the subgraph query.
// Optimistically add them into the query regardless. Invalid pools ones will be dropped anyway
// when we query the pool on-chain. Ensures that new pools for new pairs can be swapped on immediately.
top2DirectSwapPool = _.map(
getApplicableV3FeeAmounts(chainId),
(feeAmount) => {
const { token0, token1, poolAddress } = poolProvider.getPoolAddress(
tokenIn,
tokenOut,
feeAmount
);
return {
id: poolAddress,
feeTier: unparseFeeAmount(feeAmount),
liquidity: '10000',
token0: {
id: token0.address,
},
token1: {
id: token1.address,
},
tvlETH: 10000,
tvlUSD: 10000,
};
}
);
// We don't want to re-add AMPL token pools for V3 in Mainnet.
// TODO: ROUTE-347, Remove this check once we have a better way to sync filters from subgraph cronjob <> routing path.
if (
!(
chainId == ChainId.MAINNET &&
(tokenIn.address.toLowerCase() ===
'0xd46ba6d942050d489dbd938a2c909a5d5039a161' ||
tokenOut.address.toLowerCase() ===
'0xd46ba6d942050d489dbd938a2c909a5d5039a161')
)
) {
// If we requested direct swap pools but did not find any in the subgraph query.
// Optimistically add them into the query regardless. Invalid pools ones will be dropped anyway
// when we query the pool on-chain. Ensures that new pools for new pairs can be swapped on immediately.
top2DirectSwapPool = _.map(
getApplicableV3FeeAmounts(chainId),
(feeAmount) => {
const { token0, token1, poolAddress } = poolProvider.getPoolAddress(
tokenIn,
tokenOut,
feeAmount
);
return {
id: poolAddress,
feeTier: unparseFeeAmount(feeAmount),
liquidity: '10000',
token0: {
id: token0.address,
},
token1: {
id: token1.address,
},
tvlETH: 10000,
tvlUSD: 10000,
};
}
);
}
}

addToAddressSet(top2DirectSwapPool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import sinon from 'sinon';
import { USDC_BASE } from '../../../../../build/main';
import {
AlphaRouterConfig,
AMPL_MAINNET,
CachingTokenListProvider,
DAI_MAINNET as DAI, sortsBefore,
TokenProvider,
Expand Down Expand Up @@ -458,6 +459,58 @@ describe('get candidate pools', () => {
}
});

test(`AMPL token pools are not added by force to direct swap pools even if they dont exist in the subgraph for protocol ${protocol}`, async () => {
if (protocol === Protocol.V3) {
// Mock so that DAI_WETH exists on chain, but not in the subgraph
const poolsOnSubgraph = [
USDC_DAI_LOW,
USDC_DAI_MEDIUM,
USDC_WETH_LOW,
WETH9_USDT_LOW,
DAI_USDT_LOW,
];

const subgraphPools: V3SubgraphPool[] = _.map(
poolsOnSubgraph,
poolToV3SubgraphPool
);

mockV3SubgraphProvider.getPools.resolves(subgraphPools);

const DAI_WETH_LOW = new V3Pool(
DAI,
WRAPPED_NATIVE_CURRENCY[1]!,
FeeAmount.LOW,
encodeSqrtRatioX96(1, 1),
10,
0
);
mockV3PoolProvider.getPools.resolves(
buildMockV3PoolAccessor([...poolsOnSubgraph, DAI_WETH_LOW])
);

const candidatePools = await getV3CandidatePools({
tokenIn: WRAPPED_NATIVE_CURRENCY[1]!,
tokenOut: AMPL_MAINNET,
routeType: TradeType.EXACT_INPUT,
routingConfig: {
...ROUTING_CONFIG,
v3PoolSelection: {
...ROUTING_CONFIG.v3PoolSelection,
topNDirectSwaps: 1,
},
},
poolProvider: mockV3PoolProvider,
subgraphProvider: mockV3SubgraphProvider,
tokenProvider: mockTokenProvider,
blockedTokenListProvider: mockBlockTokenListProvider,
chainId: ChainId.MAINNET,
});

expect(candidatePools.candidatePools.selections.topByDirectSwapPool.length).toEqual(0);
}
});

test(`succeeds to get direct swap pools even if they dont exist in the subgraph for protocol ${protocol} but includes default LOW_200 for Base`, async () => {
if (protocol === Protocol.V3) {
const poolsOnSubgraph: V3Pool[] = [];
Expand Down
Loading