Skip to content

Commit

Permalink
Merge branch 'main' into jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Pote committed Nov 30, 2021
2 parents d29dd35 + f648158 commit 1dd1866
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 58 deletions.
35 changes: 17 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uniswap/smart-order-router",
"version": "2.0.0-beta.12",
"version": "2.0.0-beta.15",
"description": "Uniswap Smart Order Router",
"main": "build/main/src/index.js",
"typings": "build/main/src/index.d.ts",
Expand Down Expand Up @@ -42,7 +42,7 @@
"@types/sinon": "^10.0.2",
"@types/stats-lite": "^2.2.0",
"@uniswap/default-token-list": "^2.0.0",
"@uniswap/router-sdk": "^1.0.0-beta.6",
"@uniswap/router-sdk": "^1.0.0-beta.7",
"@uniswap/token-lists": "^1.0.0-beta.25",
"@uniswap/v2-core": "^1.0.1",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
Expand Down
9 changes: 7 additions & 2 deletions src/providers/v2/subgraph-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ export class V2SubgraphProvider implements IV2SubgraphProvider {
}
);

// filter pools that have liquidity less than threshold
// Filter pools that have tracked reserve ETH less than threshold.
// trackedReserveETH filters pools that do not involve a pool from this
// allowlist: https://github.com/Uniswap/v2-subgraph/blob/7c82235cad7aee4cfce8ea82f0030af3d224833e/src/mappings/pricing.ts#L43
// Which helps filter pools with manipulated prices/liquidity.
const poolsSanitized: V2SubgraphPool[] = pools
.filter((pool) => parseFloat(pool.trackedReserveETH) > threshold)
.filter((pool) => {
return parseFloat(pool.trackedReserveETH) > threshold;
})
.map((pool) => {
return {
...pool,
Expand Down
22 changes: 11 additions & 11 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class AlphaRouter
} else {
if (!V2_IPFS_POOL_CACHE_URL_BY_CHAIN[chainId]) {
throw new Error(
`No IPFS pool cache for V2 on ${chainId}. Provide your own provider.`
`Can not create a default subgraph provider for V2 on ${chainId}. Provide your own V2SubgraphProvider.`
);
}

Expand Down Expand Up @@ -356,7 +356,7 @@ export class AlphaRouter
} else {
if (!V3_IPFS_POOL_CACHE_URL_BY_CHAIN[chainId]) {
throw new Error(
`No IPFS pool cache for V3 on ${chainId}. Provide your own provider.`
`Can not create a default subgraph provider for V3 on ${chainId}. Provide your own V3SubgraphProvider.`
);
}

Expand Down Expand Up @@ -595,11 +595,13 @@ export class AlphaRouter
candidatePools: CandidatePoolsBySelectionCriteria;
}>[] = [];

if (!protocols || protocols.length == 0) {
log.info(
{ protocols, swapType: tradeType },
'Routing across V3 and V2 protocols'
);
const protocolsSet = new Set(protocols ?? []);

if (
protocolsSet.size == 0 ||
(protocolsSet.has(Protocol.V2) && protocolsSet.has(Protocol.V3))
) {
log.info({ protocols, tradeType }, 'Routing across all protocols');
quotePromises.push(
this.getV3Quotes(
tokenIn,
Expand All @@ -625,7 +627,7 @@ export class AlphaRouter
)
);
} else {
if (protocols.includes(Protocol.V3)) {
if (protocolsSet.has(Protocol.V3)) {
log.info({ protocols, swapType: tradeType }, 'Routing across V3');
quotePromises.push(
this.getV3Quotes(
Expand All @@ -640,7 +642,7 @@ export class AlphaRouter
)
);
}
if (protocols.includes(Protocol.V2)) {
if (protocolsSet.has(Protocol.V2)) {
log.info({ protocols, swapType: tradeType }, 'Routing across V2');
quotePromises.push(
this.getV2Quotes(
Expand All @@ -657,9 +659,7 @@ export class AlphaRouter
}
}

log.info('Waiting for quotes');
const routesWithValidQuotesByProtocol = await Promise.all(quotePromises);
log.info('Waiting for quotes promise resolved');

let allRoutesWithValidQuotes: RouteWithValidQuote[] = [];
let allCandidatePools: CandidatePoolsBySelectionCriteria[] = [];
Expand Down
69 changes: 55 additions & 14 deletions src/routers/alpha-router/functions/get-candidate-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
V3SubgraphPool,
} from '../../../providers/v3/subgraph-provider';
import { ChainId } from '../../../util';
import { parseFeeAmount } from '../../../util/amounts';
import { parseFeeAmount, unparseFeeAmount } from '../../../util/amounts';
import { log } from '../../../util/log';
import { metric, MetricLoggerUnit } from '../../../util/metric';
import { AlphaRouterConfig } from '../alpha-router';
Expand Down Expand Up @@ -218,7 +218,7 @@ export async function getV3CandidatePools({
.slice(0, topNWithBaseToken)
.value();

const top2DirectSwapPool = _(subgraphPoolsSorted)
let top2DirectSwapPool = _(subgraphPoolsSorted)
.filter((subgraphPool) => {
return (
!poolAddressesSoFar.has(subgraphPool.id) &&
Expand All @@ -231,6 +231,35 @@ export async function getV3CandidatePools({
.slice(0, topNDirectSwaps)
.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(
[FeeAmount.HIGH, FeeAmount.MEDIUM, FeeAmount.LOW, FeeAmount.LOWEST],
(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);

const wethAddress = WETH9[chainId]!.address;
Expand Down Expand Up @@ -594,18 +623,30 @@ export async function getV2CandidatePools({
.slice(0, topNWithBaseToken)
.value();

const top2DirectSwapPool = _(subgraphPoolsSorted)
.filter((subgraphPool) => {
return (
!poolAddressesSoFar.has(subgraphPool.id) &&
((subgraphPool.token0.id == tokenInAddress &&
subgraphPool.token1.id == tokenOutAddress) ||
(subgraphPool.token1.id == tokenInAddress &&
subgraphPool.token0.id == tokenOutAddress))
);
})
.slice(0, topNDirectSwaps)
.value();
// Always add the direct swap pool into the mix regardless of if it exists in the subgraph pool list.
// Ensures that new pools can be swapped on immediately, and that if a pool was filtered out of the
// subgraph query for some reason (e.g. trackedReserveETH was 0), then we still consider it.
let top2DirectSwapPool: V2SubgraphPool[] = [];
if (topNDirectSwaps != 0) {
const { token0, token1, poolAddress } = poolProvider.getPoolAddress(
tokenIn,
tokenOut
);

top2DirectSwapPool = [
{
id: poolAddress,
token0: {
id: token0.address,
},
token1: {
id: token1.address,
},
supply: 10000, // Not used. Set to arbitrary number.
reserve: 10000, // Not used. Set to arbitrary number.
},
];
}

addToAddressSet(top2DirectSwapPool);

Expand Down
Loading

0 comments on commit 1dd1866

Please sign in to comment.