From b2b6201f8ac389d9e1ef54fff5da9d8e17a45fa3 Mon Sep 17 00:00:00 2001 From: Papa Smurf Date: Mon, 9 Oct 2023 17:24:45 +0100 Subject: [PATCH 1/4] feat: added router config entry for tokens to exclude from route search --- cli/commands/quote.ts | 11 ++++++++ src/routers/alpha-router/alpha-router.ts | 4 +++ .../functions/get-candidate-pools.ts | 17 ++++++++++- src/routers/router.ts | 2 +- .../functions/get-candidate-pools.test.ts | 28 +++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/cli/commands/quote.ts b/cli/commands/quote.ts index ab0cb6a..9ac8abd 100644 --- a/cli/commands/quote.ts +++ b/cli/commands/quote.ts @@ -39,6 +39,7 @@ export class Quote extends BaseCommand { required: false, default: false, }), + excludeTokens: flags.string({ required: false }), simulate: flags.boolean({ required: false, default: false }), }; @@ -67,6 +68,7 @@ export class Quote extends BaseCommand { protocols: protocolsStr, forceCrossProtocol, forceMixedRoutes, + excludeTokens, simulate, } = flags; @@ -87,6 +89,13 @@ export class Quote extends BaseCommand { } } + let excludeTokensFromRoute: string[] | undefined = undefined; + if (excludeTokens) + excludeTokensFromRoute = excludeTokens + .toLowerCase() + .replace(' ', '') + .split(','); + const chainId = ID_TO_CHAIN_ID(chainIdNumb); const log = this.logger; @@ -142,6 +151,7 @@ export class Quote extends BaseCommand { protocols, forceCrossProtocol, forceMixedRoutes, + excludeTokens: excludeTokensFromRoute, } ); } else { @@ -176,6 +186,7 @@ export class Quote extends BaseCommand { protocols, forceCrossProtocol, forceMixedRoutes, + excludeTokens: excludeTokensFromRoute, } ); } diff --git a/src/routers/alpha-router/alpha-router.ts b/src/routers/alpha-router/alpha-router.ts index cda2a6e..982e383 100644 --- a/src/routers/alpha-router/alpha-router.ts +++ b/src/routers/alpha-router/alpha-router.ts @@ -255,6 +255,10 @@ export type AlphaRouterConfig = { * This parameter should always be false. It is only included for testing purposes. */ forceCrossProtocol: boolean; + /** + * Force the alpha router to exclude specific tokens and their associated pools from the route. + */ + excludeTokens?: string[]; /** * The minimum percentage of the input token to use for each route in a split route. * All routes will have a multiple of this value. For example is distribution percentage is 5, diff --git a/src/routers/alpha-router/functions/get-candidate-pools.ts b/src/routers/alpha-router/functions/get-candidate-pools.ts index c819aac..a0372a3 100644 --- a/src/routers/alpha-router/functions/get-candidate-pools.ts +++ b/src/routers/alpha-router/functions/get-candidate-pools.ts @@ -210,12 +210,27 @@ export async function getV3CandidatePools({ blockNumber, }); + // Only consider pools where neither tokens are to be excluded. + let prunedPoolsRaw: V3SubgraphPool[] = allPoolsRaw; + const excludeTokens = routingConfig.excludeTokens; + if (excludeTokens && excludeTokens.length > 0) { + prunedPoolsRaw = []; + for (const pool of allPoolsRaw) { + const excludeToken0 = excludeTokens.find((ex) => pool.token0.id == ex); + const excludeToken1 = excludeTokens.find((ex) => pool.token1.id == ex); + + if (excludeToken0 || excludeToken1) continue; + + prunedPoolsRaw.push(pool); + } + } + log.info( { samplePools: allPoolsRaw.slice(0, 3) }, 'Got all pools from V3 subgraph provider' ); - const allPools = _.map(allPoolsRaw, (pool) => { + const allPools = _.map(prunedPoolsRaw, (pool) => { return { ...pool, token0: { diff --git a/src/routers/router.ts b/src/routers/router.ts index 2b54c2f..f2c7b8b 100644 --- a/src/routers/router.ts +++ b/src/routers/router.ts @@ -14,9 +14,9 @@ import { } from '@violetprotocol/mauve-sdk-core'; import { Route as V2RouteRaw } from '@violetprotocol/mauve-v2-sdk'; import { + MethodParameters as SDKMethodParameters, Pool, Position, - MethodParameters as SDKMethodParameters, Route as V3RouteRaw, } from '@violetprotocol/mauve-v3-sdk'; diff --git a/test/unit/routers/alpha-router/functions/get-candidate-pools.test.ts b/test/unit/routers/alpha-router/functions/get-candidate-pools.test.ts index 971ecdc..f81a3f8 100644 --- a/test/unit/routers/alpha-router/functions/get-candidate-pools.test.ts +++ b/test/unit/routers/alpha-router/functions/get-candidate-pools.test.ts @@ -170,6 +170,34 @@ describe('get candidate pools', () => { ).toBeTruthy(); }); + test('succeeds to get top pools while excluding WETH', async () => { + await getV3CandidatePools({ + tokenIn: USDC, + tokenOut: DAI, + routeType: TradeType.EXACT_INPUT, + routingConfig: { + ...ROUTING_CONFIG, + v3PoolSelection: { + ...ROUTING_CONFIG.v3PoolSelection, + topNTokenInOut: 1, + }, + excludeTokens: [WRAPPED_NATIVE_CURRENCY[1].address], + }, + poolProvider: mockV3PoolProvider, + subgraphProvider: mockV3SubgraphProvider, + tokenProvider: mockTokenProvider, + blockedTokenListProvider: mockBlockTokenListProvider, + chainId: ChainId.MAINNET, + }); + + expect( + mockV3PoolProvider.getPools.calledWithExactly([ + [DAI, USDC, FeeAmount.LOW], + [DAI, USDT, FeeAmount.LOW], + ]) + ).toBeTruthy(); + }); + test('succeeds to get direct swap pools even if they dont exist in the subgraph', async () => { // Mock so that DAI_WETH exists on chain, but not in the subgraph const poolsOnSubgraph = [ From 0f4bbe37eab93cbf64b6324e7085009e616f3393 Mon Sep 17 00:00:00 2001 From: Papa Smurf Date: Mon, 9 Oct 2023 18:45:53 +0100 Subject: [PATCH 2/4] feat: version bump to 4.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2d77b95..8d4cca9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@violetprotocol/mauve-smart-order-router", - "version": "4.0.10", + "version": "4.1.0", "description": "Mauve Smart Order Router", "main": "build/main/index.js", "typings": "build/main/index.d.ts", From e2f3d6e04873bf3a1754b586ed0ceba1e96eb7fe Mon Sep 17 00:00:00 2001 From: Papa Smurf Date: Wed, 11 Oct 2023 16:47:12 +0100 Subject: [PATCH 3/4] feat: poolAccessor now filtered with excluded tokens --- .../functions/get-candidate-pools.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/routers/alpha-router/functions/get-candidate-pools.ts b/src/routers/alpha-router/functions/get-candidate-pools.ts index a0372a3..485c738 100644 --- a/src/routers/alpha-router/functions/get-candidate-pools.ts +++ b/src/routers/alpha-router/functions/get-candidate-pools.ts @@ -584,9 +584,26 @@ export async function getV3CandidatePools({ const tokenPairs = _.compact(tokenPairsRaw); + // Only consider pairs where neither tokens are to be excluded. + let filteredTokenPairs: [Token, Token, FeeAmount][] = tokenPairs; + if (excludeTokens && excludeTokens.length > 0) { + filteredTokenPairs = []; + for (const [token0, token1, fee] of tokenPairs) { + const excludeToken0 = excludeTokens.find((ex) => token0.address == ex); + const excludeToken1 = excludeTokens.find((ex) => token1.address == ex); + + console.log(excludeToken0); + console.log(excludeToken1); + + if (excludeToken0 || excludeToken1) continue; + + filteredTokenPairs.push([token0, token1, fee]); + } + } + const beforePoolsLoad = Date.now(); - const poolAccessor = await poolProvider.getPools(tokenPairs); + const poolAccessor = await poolProvider.getPools(filteredTokenPairs); metric.putMetric( 'V3PoolsLoad', From dc1d4258c50b18e8250b154c54aef43ef6b4e5c8 Mon Sep 17 00:00:00 2001 From: Papa Smurf Date: Wed, 11 Oct 2023 18:30:18 +0100 Subject: [PATCH 4/4] feat: removed console logs --- package.json | 2 +- src/routers/alpha-router/functions/get-candidate-pools.ts | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index 8d4cca9..836ee5c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@violetprotocol/mauve-smart-order-router", - "version": "4.1.0", + "version": "4.1.2", "description": "Mauve Smart Order Router", "main": "build/main/index.js", "typings": "build/main/index.d.ts", diff --git a/src/routers/alpha-router/functions/get-candidate-pools.ts b/src/routers/alpha-router/functions/get-candidate-pools.ts index 485c738..6ebe0a6 100644 --- a/src/routers/alpha-router/functions/get-candidate-pools.ts +++ b/src/routers/alpha-router/functions/get-candidate-pools.ts @@ -592,9 +592,6 @@ export async function getV3CandidatePools({ const excludeToken0 = excludeTokens.find((ex) => token0.address == ex); const excludeToken1 = excludeTokens.find((ex) => token1.address == ex); - console.log(excludeToken0); - console.log(excludeToken1); - if (excludeToken0 || excludeToken1) continue; filteredTokenPairs.push([token0, token1, fee]);