Skip to content

Commit

Permalink
chore: bump sor to 4.7.1 - fix: v4 subgraph provider add ETH base tok…
Browse files Browse the repository at this point in the history
…en, and custom fee tier and tick spacings (#751)

* fix: v4 subgraph provider add ETH base token, and custom fee tier and tick spacings

* merge conflict

* 4.7.1

* add sepolia v4 1 eth -> usdc quote test

* remove fee parsing in v4

* also fix unichain sepolia exact out quote test setup
  • Loading branch information
jsy1218 authored Oct 15, 2024
1 parent a65a782 commit f7d78d5
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uniswap/smart-order-router",
"version": "4.7.0",
"version": "4.7.1",
"description": "Uniswap Smart Order Router",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
41 changes: 33 additions & 8 deletions src/providers/caching-subgraph-provider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Protocol } from '@uniswap/router-sdk';
import { ChainId, Token } from '@uniswap/sdk-core';
import { ChainId, Currency, Token } from '@uniswap/sdk-core';

import { SubgraphPool } from '../routers/alpha-router/functions/get-candidate-pools';
import { WRAPPED_NATIVE_CURRENCY } from '../util';
import { nativeOnChain, WRAPPED_NATIVE_CURRENCY } from '../util';

import { ICache } from './cache';
import { ProviderConfig } from './provider';
Expand Down Expand Up @@ -53,11 +53,12 @@ import {
import { V3SubgraphPool } from './v3/subgraph-provider';

type ChainTokenList = {
readonly [chainId in ChainId]: Token[];
readonly [chainId in ChainId]: Currency[];
};

export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
[ChainId.MAINNET]: [
nativeOnChain(ChainId.MAINNET),
WRAPPED_NATIVE_CURRENCY[ChainId.MAINNET]!,

Check warning on line 62 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
DAI_MAINNET,
USDC_MAINNET,
Expand All @@ -66,9 +67,13 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
WSTETH_MAINNET,
],
[ChainId.GOERLI]: [WRAPPED_NATIVE_CURRENCY[ChainId.GOERLI]!],

Check warning on line 69 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
[ChainId.SEPOLIA]: [WRAPPED_NATIVE_CURRENCY[ChainId.SEPOLIA]!],
[ChainId.SEPOLIA]: [
nativeOnChain(ChainId.SEPOLIA),
WRAPPED_NATIVE_CURRENCY[ChainId.SEPOLIA]!,

Check warning on line 72 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
],
//v2 not deployed on [arbitrum, polygon, celo, gnosis, moonbeam, bnb, avalanche] and their testnets
[ChainId.OPTIMISM]: [
nativeOnChain(ChainId.OPTIMISM),
WRAPPED_NATIVE_CURRENCY[ChainId.OPTIMISM]!,

Check warning on line 77 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
USDC_OPTIMISM,
DAI_OPTIMISM,
Expand All @@ -77,6 +82,7 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
OP_OPTIMISM,
],
[ChainId.ARBITRUM_ONE]: [
nativeOnChain(ChainId.ARBITRUM_ONE),
WRAPPED_NATIVE_CURRENCY[ChainId.ARBITRUM_ONE]!,

Check warning on line 86 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
WBTC_ARBITRUM,
DAI_ARBITRUM,
Expand All @@ -89,7 +95,12 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
[ChainId.ARBITRUM_SEPOLIA]: [],
[ChainId.OPTIMISM_GOERLI]: [],
[ChainId.OPTIMISM_SEPOLIA]: [],
[ChainId.POLYGON]: [USDC_POLYGON, WETH_POLYGON, WMATIC_POLYGON],
[ChainId.POLYGON]: [
nativeOnChain(ChainId.POLYGON),
USDC_POLYGON,
WETH_POLYGON,
WMATIC_POLYGON,
],
[ChainId.POLYGON_MUMBAI]: [],
[ChainId.CELO]: [CELO, CUSD_CELO, CEUR_CELO, DAI_CELO],
[ChainId.CELO_ALFAJORES]: [],
Expand All @@ -101,6 +112,7 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
WBTC_MOONBEAM,
],
[ChainId.BNB]: [
nativeOnChain(ChainId.BNB),
WRAPPED_NATIVE_CURRENCY[ChainId.BNB],
BUSD_BNB,
DAI_BNB,
Expand All @@ -115,23 +127,36 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
DAI_AVAX,
],
[ChainId.BASE_GOERLI]: [],
[ChainId.BASE]: [WRAPPED_NATIVE_CURRENCY[ChainId.BASE], USDC_BASE],
[ChainId.ZORA]: [WRAPPED_NATIVE_CURRENCY[ChainId.ZORA]!],
[ChainId.BASE]: [
nativeOnChain(ChainId.BASE),
WRAPPED_NATIVE_CURRENCY[ChainId.BASE],
USDC_BASE,
],
[ChainId.ZORA]: [
nativeOnChain(ChainId.ZORA),
WRAPPED_NATIVE_CURRENCY[ChainId.ZORA]!,

Check warning on line 137 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
],
[ChainId.ZORA_SEPOLIA]: [WRAPPED_NATIVE_CURRENCY[ChainId.ZORA_SEPOLIA]!],

Check warning on line 139 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
[ChainId.ROOTSTOCK]: [WRAPPED_NATIVE_CURRENCY[ChainId.ROOTSTOCK]!],

Check warning on line 140 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
[ChainId.BLAST]: [WRAPPED_NATIVE_CURRENCY[ChainId.BLAST]!, USDB_BLAST],
[ChainId.BLAST]: [
nativeOnChain(ChainId.BLAST),
WRAPPED_NATIVE_CURRENCY[ChainId.BLAST]!,

Check warning on line 143 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
USDB_BLAST,
],
[ChainId.ZKSYNC]: [
WRAPPED_NATIVE_CURRENCY[ChainId.ZKSYNC]!,

Check warning on line 147 in src/providers/caching-subgraph-provider.ts

View workflow job for this annotation

GitHub Actions / Run linters 2

Forbidden non-null assertion
USDCE_ZKSYNC,
USDC_ZKSYNC,
],
[ChainId.WORLDCHAIN]: [
nativeOnChain(ChainId.WORLDCHAIN),
WRAPPED_NATIVE_CURRENCY[ChainId.WORLDCHAIN]!,
USDC_WORLDCHAIN,
WLD_WORLDCHAIN,
WBTC_WORLDCHAIN,
],
[ChainId.ASTROCHAIN_SEPOLIA]: [
nativeOnChain(ChainId.ASTROCHAIN_SEPOLIA),
WRAPPED_NATIVE_CURRENCY[ChainId.ASTROCHAIN_SEPOLIA]!,
USDC_ASTROCHAIN_SEPOLIA,
],
Expand Down
48 changes: 28 additions & 20 deletions src/providers/v4/static-subgraph-provider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ChainId, Token } from '@uniswap/sdk-core';
import { ADDRESS_ZERO, FeeAmount } from '@uniswap/v3-sdk';
import { ChainId, Currency } from '@uniswap/sdk-core';
import { Pool } from '@uniswap/v4-sdk';
import _ from 'lodash';

import { getAddress, log, unparseFeeAmount } from '../../util';
import {
getAddress,
getApplicableV4FeesTickspacingsHooks,
log,
} from '../../util';
import { BASES_TO_CHECK_TRADES_AGAINST } from '../caching-subgraph-provider';

import JSBI from 'jsbi';
Expand All @@ -14,46 +17,51 @@ import { IV4SubgraphProvider, V4SubgraphPool } from './subgraph-provider';
export class StaticV4SubgraphProvider implements IV4SubgraphProvider {
constructor(
private chainId: ChainId,
private poolProvider: IV4PoolProvider
private poolProvider: IV4PoolProvider,
private v4PoolParams: Array<
[number, number, string]
> = getApplicableV4FeesTickspacingsHooks(chainId)
) {}

public async getPools(
currencyIn?: Token,
currencyOut?: Token,
currencyIn?: Currency,
currencyOut?: Currency,
providerConfig?: ProviderConfig
): Promise<V4SubgraphPool[]> {
log.info('In static subgraph provider for V4');
const bases = BASES_TO_CHECK_TRADES_AGAINST[this.chainId];

const basePairs: [Token, Token][] = _.flatMap(
const basePairs: [Currency, Currency][] = _.flatMap(
bases,
(base): [Token, Token][] => bases.map((otherBase) => [base, otherBase])
(base): [Currency, Currency][] =>
bases.map((otherBase) => [base, otherBase])
);

if (currencyIn && currencyOut) {
basePairs.push(
[currencyIn, currencyOut],
...bases.map((base): [Token, Token] => [currencyIn, base]),
...bases.map((base): [Token, Token] => [currencyOut, base])
...bases.map((base): [Currency, Currency] => [currencyIn, base]),
...bases.map((base): [Currency, Currency] => [currencyOut, base])
);
}

const pairs: V4PoolConstruct[] = _(basePairs)
.filter((tokens): tokens is [Token, Token] =>
.filter((tokens): tokens is [Currency, Currency] =>
Boolean(tokens[0] && tokens[1])
)
.filter(
([tokenA, tokenB]) =>
tokenA.address !== tokenB.address && !tokenA.equals(tokenB)
tokenA.wrapped.address !== tokenB.wrapped.address &&
!tokenA.equals(tokenB)
)
.flatMap<V4PoolConstruct>(([tokenA, tokenB]) => {
// TODO: we will follow up with expanding the fee tiers and tick spacing from just hard-coding from v3 for now.
return [
[tokenA, tokenB, FeeAmount.LOWEST, 1, ADDRESS_ZERO],
[tokenA, tokenB, FeeAmount.LOW, 10, ADDRESS_ZERO],
[tokenA, tokenB, FeeAmount.MEDIUM, 60, ADDRESS_ZERO],
[tokenA, tokenB, FeeAmount.HIGH, 200, ADDRESS_ZERO],
];
const tokensWithPoolParams: Array<
[Currency, Currency, number, number, string]
> = this.v4PoolParams.map(([feeAmount, tickSpacing, hooks]) => {
return [tokenA, tokenB, feeAmount, tickSpacing, hooks];
});

return tokensWithPoolParams;
})
.value();

Expand Down Expand Up @@ -88,7 +96,7 @@ export class StaticV4SubgraphProvider implements IV4SubgraphProvider {

return {
id: poolAddress,
feeTier: unparseFeeAmount(fee),
feeTier: fee.toString(),
tickSpacing: tickSpacing.toString(),
hooks: hooks,
liquidity: liquidity.toString(),
Expand Down
18 changes: 17 additions & 1 deletion src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import { Erc20__factory } from '../../types/other/factories/Erc20__factory';
import {
getAddress,
getAddressLowerCase,
getApplicableV4FeesTickspacingsHooks,
MIXED_SUPPORTED,
shouldWipeoutCachedRoutes,
SWAP_ROUTER_02_ADDRESSES,
Expand Down Expand Up @@ -327,6 +328,12 @@ export type AlphaRouterParams = {
* The version of the universal router to use.
*/
universalRouterVersion?: UniversalRouterVersion;

/**
* The v4 pool params to be used for the v4 pool provider.
* fee tiers, tickspacings, and hook addresses
*/
v4PoolParams?: Array<[number, number, string]>;
};

export class MapWithLowerCaseKey<V> extends Map<string, V> {
Expand Down Expand Up @@ -544,6 +551,7 @@ export class AlphaRouter
protected v4Supported?: ChainId[];
protected mixedSupported?: ChainId[];
protected universalRouterVersion?: UniversalRouterVersion;
protected v4PoolParams?: Array<[number, number, string]>;

constructor({
chainId,
Expand Down Expand Up @@ -575,6 +583,7 @@ export class AlphaRouter
v4Supported,
mixedSupported,
universalRouterVersion,
v4PoolParams,
}: AlphaRouterParams) {
this.chainId = chainId;
this.provider = provider;
Expand Down Expand Up @@ -919,6 +928,8 @@ export class AlphaRouter
]);
}

this.v4PoolParams =
v4PoolParams ?? getApplicableV4FeesTickspacingsHooks(chainId);
if (v4SubgraphProvider) {
this.v4SubgraphProvider = v4SubgraphProvider;
} else {
Expand All @@ -933,7 +944,11 @@ export class AlphaRouter
),
new NodeJSCache(new NodeCache({ stdTTL: 300, useClones: false }))
),
new StaticV4SubgraphProvider(chainId, this.v4PoolProvider),
new StaticV4SubgraphProvider(
chainId,
this.v4PoolProvider,
this.v4PoolParams
),
]);
}

Expand Down Expand Up @@ -2137,6 +2152,7 @@ export class AlphaRouter
subgraphProvider: this.v4SubgraphProvider,
routingConfig,
chainId: this.chainId,
v4PoolParams: this.v4PoolParams,
}).then((candidatePools) => {
metric.putMetric(
'GetV4CandidatePools',
Expand Down
14 changes: 6 additions & 8 deletions src/routers/alpha-router/functions/get-candidate-pools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Protocol } from '@uniswap/router-sdk';
import { ChainId, Currency, Token, TradeType } from '@uniswap/sdk-core';
import { ADDRESS_ZERO, FeeAmount } from '@uniswap/v3-sdk';
import { FeeAmount } from '@uniswap/v3-sdk';
import _ from 'lodash';

import { isNativeCurrency } from '@uniswap/universal-router-sdk';
Expand Down Expand Up @@ -82,6 +82,7 @@ import {
getAddress,
getAddressLowerCase,
getApplicableV3FeeAmounts,
getApplicableV4FeesTickspacingsHooks,
nativeOnChain,
unparseFeeAmount,
WRAPPED_NATIVE_CURRENCY,
Expand Down Expand Up @@ -135,6 +136,7 @@ export type V4GetCandidatePoolsParams = {
poolProvider: IV4PoolProvider;
blockedTokenListProvider?: ITokenListProvider;
chainId: ChainId;
v4PoolParams?: Array<[number, number, string]>;
};

export type V3GetCandidatePoolsParams = {
Expand Down Expand Up @@ -414,6 +416,7 @@ export async function getV4CandidatePools({
poolProvider,
blockedTokenListProvider,
chainId,
v4PoolParams = getApplicableV4FeesTickspacingsHooks(chainId),
}: V4GetCandidatePoolsParams): Promise<V4CandidatePools> {
const {
blockNumber,
Expand Down Expand Up @@ -549,12 +552,7 @@ export async function getV4CandidatePools({
// 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, 200, ADDRESS_ZERO],
[FeeAmount.MEDIUM, 60, ADDRESS_ZERO],
[FeeAmount.LOW, 10, ADDRESS_ZERO],
[FeeAmount.LOWEST, 1, ADDRESS_ZERO],
] as Array<[number, number, string]>,
v4PoolParams as Array<[number, number, string]>,
(poolParams) => {
const [fee, tickSpacing, hooks] = poolParams;

Expand All @@ -567,7 +565,7 @@ export async function getV4CandidatePools({
);
return {
id: poolId,
feeTier: unparseFeeAmount(fee),
feeTier: fee.toString(),
tickSpacing: tickSpacing.toString(),
hooks: hooks,
liquidity: '10000',
Expand Down
24 changes: 23 additions & 1 deletion src/util/amounts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { parseUnits } from '@ethersproject/units';
import { ADDRESS_ZERO } from '@uniswap/router-sdk';
import {
ChainId,
Currency,
CurrencyAmount as CurrencyAmountRaw,
} from '@uniswap/sdk-core';
import { FeeAmount } from '@uniswap/v3-sdk';
import { FeeAmount, TICK_SPACINGS } from '@uniswap/v3-sdk';
import JSBI from 'jsbi';

export class CurrencyAmount extends CurrencyAmountRaw<Currency> {}
Expand Down Expand Up @@ -73,3 +74,24 @@ export function getApplicableV3FeeAmounts(chainId: ChainId): FeeAmount[] {

return feeAmounts;
}

export function getApplicableV4FeesTickspacingsHooks(
chainId: ChainId
): Array<[number, number, string]> {
const feeAmounts = [
FeeAmount.HIGH,
FeeAmount.MEDIUM,
FeeAmount.LOW,
FeeAmount.LOWEST,
];

if (chainId === ChainId.BASE) {
feeAmounts.push(FeeAmount.LOW_200, FeeAmount.LOW_300, FeeAmount.LOW_400);
}

return feeAmounts.map((feeAmount) => [
feeAmount as number,
TICK_SPACINGS[feeAmount],
ADDRESS_ZERO,
]);
}
Loading

0 comments on commit f7d78d5

Please sign in to comment.