Skip to content

Commit

Permalink
feat: add L2 gas estimate on v2, as well as enable Arbitrum on v2 sup…
Browse files Browse the repository at this point in the history
…port (#485)

* add native usdc arbitrum, and usdc/weth arbitrum v2 swap integ-test

* add v2 console log pool reserve

* fix gas model for v2 with security fees

* make sure the route length is non-zero before calculating L1 security fee

* fix another best swap route missing v2 gas model

* fix arb integ-test

* fix native pool quote token is wrapped native token error

* v2 gas model behind v2 supported chains check

* fix usd gas token invariant currency

* gas model check fix

* 3.21.1

* remove console.log for debugging

* remove other console.log

* add native arb usdc whale

* add weth/usdc v2 only swap and tenderly simulation integ-test

* increase base gas estimate percent deviation to 34%

* address feedbacks

* lint prettier

* address feedbacks
  • Loading branch information
jsy1218 authored Feb 13, 2024
1 parent b970aed commit dff9d8d
Show file tree
Hide file tree
Showing 14 changed files with 660 additions and 214 deletions.
63 changes: 63 additions & 0 deletions src/providers/token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ export const USDC_OPTIMISM = new Token(
'0x7F5c764cBc14f9669B88837ca1490cCa17c31607',
6,
'USDC',
'USD//C.e'
);
export const USDC_NATIVE_OPTIMISM = new Token(
ChainId.OPTIMISM,
'0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85',
6,
'USDC',
'USD//C'
);
export const USDT_OPTIMISM = new Token(
Expand Down Expand Up @@ -245,6 +252,13 @@ export const USDC_ARBITRUM = new Token(
'0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8',
6,
'USDC',
'USD//C.e'
);
export const USDC_NATIVE_ARBITRUM = new Token(
ChainId.ARBITRUM_ONE,
'0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
6,
'USDC',
'USD//C'
);
export const USDT_ARBITRUM = new Token(
Expand Down Expand Up @@ -316,6 +330,13 @@ export const USDC_POLYGON = new Token(
'0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
6,
'USDC',
'USD//C.e'
);
export const USDC_NATIVE_POLYGON = new Token(
ChainId.POLYGON,
'0x3c499c542cef5e3811e1192ce70d8cc03d5c3359',
6,
'USDC',
'USD//C'
);

Expand Down Expand Up @@ -433,6 +454,27 @@ export const CUSD_CELO = new Token(
'CUSD',
'Celo Dollar Stablecoin'
);
export const USDC_CELO = new Token(
ChainId.CELO,
'0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664',
18,
'USDC',
'USD//C.e'
);
export const USDC_WORMHOLE_CELO = new Token(
ChainId.CELO,
'0x37f750B7cC259A2f741AF45294f6a16572CF5cAd',
18,
'USDC',
'USD//C.e'
);
export const USDC_NATIVE_CELO = new Token(
ChainId.CELO,
'0x765DE816845861e75A25fCA122bb6898B8B1282a',
18,
'USDC',
'USD//C'
);

export const CEUR_CELO = new Token(
ChainId.CELO,
Expand Down Expand Up @@ -490,6 +532,20 @@ export const USDC_AVAX = new Token(
'USDC',
'USDC Token'
);
export const USDC_BRIDGED_AVAX = new Token(
ChainId.AVALANCHE,
'0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664',
6,
'USDC',
'USDC Token'
);
export const USDC_NATIVE_AVAX = new Token(
ChainId.AVALANCHE,
'0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
6,
'USDC',
'USDC Token'
);

// Base Tokens
export const USDC_BASE = new Token(
Expand All @@ -499,6 +555,13 @@ export const USDC_BASE = new Token(
'USDbC',
'USD Base Coin'
);
export const USDC_NATIVE_BASE = new Token(
ChainId.BASE,
'0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
6,
'USDbC',
'USD Base Coin'
);

// Base Goerli Tokens
export const USDC_BASE_GOERLI = new Token(
Expand Down
6 changes: 4 additions & 2 deletions src/providers/v2/static-subgraph-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
USDC_BNB,
USDC_MAINNET,
USDC_MOONBEAM,
USDC_NATIVE_ARBITRUM,
USDC_OPTIMISM,
USDC_POLYGON,
USDT_ARBITRUM,
Expand Down Expand Up @@ -63,13 +64,14 @@ const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
DAI_OPTIMISM,
USDT_OPTIMISM,
WBTC_OPTIMISM,
OP_OPTIMISM
OP_OPTIMISM,
],
[ChainId.ARBITRUM_ONE]: [
WRAPPED_NATIVE_CURRENCY[ChainId.ARBITRUM_ONE]!,
WBTC_ARBITRUM,
DAI_ARBITRUM,
USDC_ARBITRUM,
USDC_NATIVE_ARBITRUM,
USDT_ARBITRUM,
ARB_ARBITRUM,
],
Expand Down Expand Up @@ -102,7 +104,7 @@ const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
DAI_AVAX,
],
[ChainId.BASE_GOERLI]: [],
[ChainId.BASE]: [WRAPPED_NATIVE_CURRENCY[ChainId.BASE], USDC_BASE ],
[ChainId.BASE]: [WRAPPED_NATIVE_CURRENCY[ChainId.BASE], USDC_BASE],
};

/**
Expand Down
43 changes: 35 additions & 8 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import {
import {
MixedRouteWithValidQuote,
RouteWithValidQuote,
V2RouteWithValidQuote,
V3RouteWithValidQuote,
} from './entities/route-with-valid-quote';
import { BestSwapRoute, getBestSwapRoute } from './functions/best-swap-route';
Expand All @@ -138,6 +139,7 @@ import {
} from './functions/get-candidate-pools';
import {
GasModelProviderConfig,
GasModelType,
IGasModel,
IOnChainGasModelFactory,
IV2GasModelFactory,
Expand Down Expand Up @@ -804,7 +806,8 @@ export class AlphaRouter
this.tokenProvider,
this.chainId,
this.blockedTokenListProvider,
this.tokenValidatorProvider
this.tokenValidatorProvider,
this.l2GasDataProvider
);

this.v3Quoter = new V3Quoter(
Expand Down Expand Up @@ -1118,7 +1121,11 @@ export class AlphaRouter
gasToken,
};

const [v3GasModel, mixedRouteGasModel] = await this.getGasModels(
const {
v2GasModel: v2GasModel,
v3GasModel: v3GasModel,
mixedRouteGasModel: mixedRouteGasModel,
} = await this.getGasModels(
gasPriceWei,
amount.currency.wrapped,
quoteToken,
Expand Down Expand Up @@ -1229,6 +1236,7 @@ export class AlphaRouter
v3GasModel,
mixedRouteGasModel,
gasPriceWei,
v2GasModel,
swapConfig
);
}
Expand All @@ -1247,6 +1255,7 @@ export class AlphaRouter
v3GasModel,
mixedRouteGasModel,
gasPriceWei,
v2GasModel,
swapConfig
);
}
Expand Down Expand Up @@ -1539,6 +1548,7 @@ export class AlphaRouter
v3GasModel: IGasModel<V3RouteWithValidQuote>,
mixedRouteGasModel: IGasModel<MixedRouteWithValidQuote>,
gasPriceWei: BigNumber,
v2GasModel?: IGasModel<V2RouteWithValidQuote>,
swapConfig?: SwapOptions
): Promise<BestSwapRoute | null> {
log.info(
Expand Down Expand Up @@ -1697,6 +1707,7 @@ export class AlphaRouter
this.chainId,
routingConfig,
this.portionProvider,
v2GasModel,
v3GasModel,
swapConfig
);
Expand All @@ -1713,6 +1724,7 @@ export class AlphaRouter
v3GasModel: IGasModel<V3RouteWithValidQuote>,
mixedRouteGasModel: IGasModel<MixedRouteWithValidQuote>,
gasPriceWei: BigNumber,
v2GasModel?: IGasModel<V2RouteWithValidQuote>,
swapConfig?: SwapOptions
): Promise<BestSwapRoute | null> {
// Generate our distribution of amounts, i.e. fractions of the input amount.
Expand Down Expand Up @@ -1857,7 +1869,7 @@ export class AlphaRouter
v2CandidatePools!,
tradeType,
routingConfig,
undefined,
v2GasModel,
gasPriceWei
)
.then((result) => {
Expand Down Expand Up @@ -1940,6 +1952,7 @@ export class AlphaRouter
this.chainId,
routingConfig,
this.portionProvider,
v2GasModel,
v3GasModel,
swapConfig
);
Expand Down Expand Up @@ -2010,9 +2023,7 @@ export class AlphaRouter
amountToken: Token,
quoteToken: Token,
providerConfig?: GasModelProviderConfig
): Promise<
[IGasModel<V3RouteWithValidQuote>, IGasModel<MixedRouteWithValidQuote>]
> {
): Promise<GasModelType> {
const beforeGasModel = Date.now();

const usdPoolPromise = getHighestLiquidityV3USDPool(
Expand Down Expand Up @@ -2069,6 +2080,17 @@ export class AlphaRouter
nativeAndSpecifiedGasTokenV3Pool: nativeAndSpecifiedGasTokenV3Pool,
};

const v2GasModelPromise = V2_SUPPORTED.includes(this.chainId)
? this.v2GasModelFactory.buildGasModel({
chainId: this.chainId,
gasPriceWei,
poolProvider: this.v2PoolProvider,
token: quoteToken,
l2GasDataProvider: this.l2GasDataProvider,
providerConfig: providerConfig,
})
: Promise.resolve(undefined);

const v3GasModelPromise = this.v3GasModelFactory.buildGasModel({
chainId: this.chainId,
gasPriceWei,
Expand All @@ -2091,7 +2113,8 @@ export class AlphaRouter
providerConfig: providerConfig,
});

const [v3GasModel, mixedRouteGasModel] = await Promise.all([
const [v2GasModel, v3GasModel, mixedRouteGasModel] = await Promise.all([
v2GasModelPromise,
v3GasModelPromise,
mixedRouteGasModelPromise,
]);
Expand All @@ -2102,7 +2125,11 @@ export class AlphaRouter
MetricLoggerUnit.Milliseconds
);

return [v3GasModel, mixedRouteGasModel];
return {
v2GasModel: v2GasModel,
v3GasModel: v3GasModel,
mixedRouteGasModel: mixedRouteGasModel,
} as GasModelType;
}

// Note multiplications here can result in a loss of precision in the amounts (e.g. taking 50% of 101)
Expand Down
Loading

0 comments on commit dff9d8d

Please sign in to comment.