Skip to content

Commit

Permalink
address feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Feb 12, 2024
1 parent 97a983e commit 410f4d3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 39 deletions.
49 changes: 49 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 @@ -323,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 @@ -440,6 +454,20 @@ 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_NATIVE_CELO = new Token(
ChainId.CELO,
'0x765DE816845861e75A25fCA122bb6898B8B1282a',
18,
'USDC',
'USD//C'
);

export const CEUR_CELO = new Token(
ChainId.CELO,
Expand Down Expand Up @@ -497,6 +525,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 @@ -506,6 +548,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
15 changes: 8 additions & 7 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ import {
V3CandidatePools,
} from './functions/get-candidate-pools';
import {
GasModelProviderConfig,
GasModelProviderConfig, GasModelType,
IGasModel,
IOnChainGasModelFactory,
IV2GasModelFactory,
LiquidityCalculationPools,
LiquidityCalculationPools
} from './gas-models/gas-model';
import { MixedRouteHeuristicGasModelFactory } from './gas-models/mixedRoute/mixed-route-heuristic-gas-model';
import { V2HeuristicGasModelFactory } from './gas-models/v2/v2-heuristic-gas-model';
Expand Down Expand Up @@ -1120,7 +1120,10 @@ export class AlphaRouter
gasToken,
};

const [v2GasModel, v3GasModel, mixedRouteGasModel] = await this.getGasModels(
const {
v2GasModel: v2GasModel,
v3GasModel: v3GasModel,
mixedRouteGasModel: mixedRouteGasModel} = await this.getGasModels(
gasPriceWei,
amount.currency.wrapped,
quoteToken,
Expand Down Expand Up @@ -2018,9 +2021,7 @@ export class AlphaRouter
amountToken: Token,
quoteToken: Token,
providerConfig?: GasModelProviderConfig
): Promise<
[IGasModel<V2RouteWithValidQuote> | undefined, IGasModel<V3RouteWithValidQuote>, IGasModel<MixedRouteWithValidQuote>]
> {
): Promise<GasModelType> {
const beforeGasModel = Date.now();

const usdPoolPromise = getHighestLiquidityV3USDPool(
Expand Down Expand Up @@ -2120,7 +2121,7 @@ export class AlphaRouter
MetricLoggerUnit.Milliseconds
);

return [v2GasModel, 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
9 changes: 8 additions & 1 deletion src/routers/alpha-router/functions/best-swap-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { AlphaRouterConfig } from '../alpha-router';
import { IGasModel, L1ToL2GasCosts, usdGasTokensByChain } from '../gas-models';

import {
RouteWithValidQuote, V2RouteWithValidQuote,
RouteWithValidQuote,
V2RouteWithValidQuote,
V3RouteWithValidQuote
} from './../entities/route-with-valid-quote';

Expand Down Expand Up @@ -465,12 +466,18 @@ export async function getBestSwapRouteBy(
if (v2GasModel == undefined && v3GasModel == undefined) {
throw new Error("Can't compute L1 gas fees.");
} else {
// Now after v2 deploy everywhere, it's possible to to have v2 and v3 routes on L2 swap.
// So that we need to add both gas cost one by one
const v2Routes = bestSwap.filter((routes) => routes.protocol === Protocol.V2);
if (v2Routes.length > 0 && V2_SUPPORTED.includes(chainId)) {
if (v2GasModel) {
const v2GasCostL1 = await v2GasModel.calculateL1GasFees!(v2Routes as V2RouteWithValidQuote[]);
gasCostsL1ToL2.gasUsedL1 = gasCostsL1ToL2.gasUsedL1.add(v2GasCostL1.gasUsedL1);
gasCostsL1ToL2.gasUsedL1OnL2 = gasCostsL1ToL2.gasUsedL1OnL2.add(v2GasCostL1.gasUsedL1OnL2);
// It's possible that the gasCostL1USD currency is different from gasCostL1USD
// (https://github.com/Uniswap/smart-order-router/blob/b970aedfec8a9509f9e22f14cc5c11be54d47b35/src/routers/alpha-router/gas-models/gas-model.ts#L62)
// so we need to make sure they are the same.
// If they are different, the best we can do is to re-assign the gasCostL1USD to gasCostL1USD
if (gasCostsL1ToL2.gasCostL1USD.currency.equals(v2GasCostL1.gasCostL1USD.currency)) {
gasCostsL1ToL2.gasCostL1USD = gasCostsL1ToL2.gasCostL1USD.add(v2GasCostL1.gasCostL1USD);
} else {
Expand Down
32 changes: 24 additions & 8 deletions src/routers/alpha-router/gas-models/gas-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ import {
DAI_GOERLI,
DAI_MAINNET,
DAI_OPTIMISM,
DAI_OPTIMISM_GOERLI, DAI_OPTIMISM_SEPOLIA,
DAI_OPTIMISM_GOERLI,
DAI_OPTIMISM_SEPOLIA,
DAI_POLYGON_MUMBAI,
DAI_SEPOLIA,
USDC_ARBITRUM,
USDC_ARBITRUM_GOERLI,
USDC_AVAX,
USDC_BASE,
USDC_BNB,
USDC_BRIDGED_AVAX,
USDC_CELO,
USDC_ETHEREUM_GNOSIS,
USDC_GOERLI,
USDC_MAINNET,
USDC_MOONBEAM, USDC_NATIVE_ARBITRUM,
USDC_MOONBEAM,
USDC_NATIVE_ARBITRUM,
USDC_NATIVE_AVAX,
USDC_NATIVE_BASE,
USDC_NATIVE_CELO,
USDC_NATIVE_OPTIMISM,
USDC_NATIVE_POLYGON,
USDC_OPTIMISM,
USDC_OPTIMISM_GOERLI, USDC_OPTIMISM_SEPOLIA,
USDC_OPTIMISM_GOERLI,
USDC_OPTIMISM_SEPOLIA,
USDC_POLYGON,
USDC_SEPOLIA,
USDT_ARBITRUM,
Expand Down Expand Up @@ -63,21 +73,21 @@ import {
export const usdGasTokensByChain: { [chainId in ChainId]?: Token[] } = {
[ChainId.MAINNET]: [DAI_MAINNET, USDC_MAINNET, USDT_MAINNET],
[ChainId.ARBITRUM_ONE]: [DAI_ARBITRUM, USDC_ARBITRUM, USDC_NATIVE_ARBITRUM, USDT_ARBITRUM],
[ChainId.OPTIMISM]: [DAI_OPTIMISM, USDC_OPTIMISM, USDT_OPTIMISM],
[ChainId.OPTIMISM]: [DAI_OPTIMISM, USDC_OPTIMISM, USDC_NATIVE_OPTIMISM, USDT_OPTIMISM],
[ChainId.OPTIMISM_GOERLI]: [DAI_OPTIMISM_GOERLI, USDC_OPTIMISM_GOERLI, USDT_OPTIMISM_GOERLI],
[ChainId.OPTIMISM_SEPOLIA]: [DAI_OPTIMISM_SEPOLIA, USDC_OPTIMISM_SEPOLIA, USDT_OPTIMISM_SEPOLIA],
[ChainId.ARBITRUM_GOERLI]: [USDC_ARBITRUM_GOERLI],
[ChainId.GOERLI]: [DAI_GOERLI, USDC_GOERLI, USDT_GOERLI, WBTC_GOERLI],
[ChainId.SEPOLIA]: [USDC_SEPOLIA, DAI_SEPOLIA],
[ChainId.POLYGON]: [USDC_POLYGON],
[ChainId.POLYGON]: [USDC_POLYGON, USDC_NATIVE_POLYGON],
[ChainId.POLYGON_MUMBAI]: [DAI_POLYGON_MUMBAI],
[ChainId.CELO]: [CUSD_CELO],
[ChainId.CELO]: [CUSD_CELO, USDC_CELO, USDC_NATIVE_CELO],
[ChainId.CELO_ALFAJORES]: [CUSD_CELO_ALFAJORES],
[ChainId.GNOSIS]: [USDC_ETHEREUM_GNOSIS],
[ChainId.MOONBEAM]: [USDC_MOONBEAM],
[ChainId.BNB]: [USDT_BNB, USDC_BNB, DAI_BNB],
[ChainId.AVALANCHE]: [DAI_AVAX, USDC_AVAX],
[ChainId.BASE]: [USDC_BASE],
[ChainId.AVALANCHE]: [DAI_AVAX, USDC_AVAX, USDC_NATIVE_AVAX, USDC_BRIDGED_AVAX],
[ChainId.BASE]: [USDC_BASE, USDC_NATIVE_BASE],
};

export type L1ToL2GasCosts = {
Expand Down Expand Up @@ -127,6 +137,12 @@ export type LiquidityCalculationPools = {
nativeAndSpecifiedGasTokenV3Pool: Pool | null;
};

export type GasModelType = {
v2GasModel?: IGasModel<V2RouteWithValidQuote>,
v3GasModel: IGasModel<V3RouteWithValidQuote>,
mixedRouteGasModel: IGasModel<MixedRouteWithValidQuote>,
}

/**
* Contains functions for generating gas estimates for given routes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ export class V2HeuristicGasModelFactory extends IV2GasModelFactory {
gasCostL1USD: CurrencyAmount;
gasCostL1QuoteToken: CurrencyAmount;
}> => {
const nativePool = !token.equals(WRAPPED_NATIVE_CURRENCY[chainId]) ?
await getV2NativePool(token, poolProvider, providerConfig) : null;

return await calculateL1GasFeesHelper(
route,
chainId,
usdPool,
token,
!token.equals(WRAPPED_NATIVE_CURRENCY[chainId]) ?
await getV2NativePool(token, poolProvider, providerConfig) : null,
nativePool,
l2GasData
);
};
Expand Down
33 changes: 12 additions & 21 deletions src/util/gas-factory-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../routers';
import { CurrencyAmount, log, WRAPPED_NATIVE_CURRENCY } from '../util';

import { opStackChains } from './l2FeeChains';
import { buildSwapMethodParameters, buildTrade } from './methodParameters';

export async function getV2NativePool(
Expand Down Expand Up @@ -564,18 +565,11 @@ export const calculateL1GasFeesHelper = async (
deadlineOrPreviousBlockhash: 100,
slippageTolerance: new Percent(5, 10_000),
};
let l1Used = BigNumber.from(0);
let l1FeeInWei = BigNumber.from(0);
let mainnetGasUsed = BigNumber.from(0);
let mainnetFeeInWei = BigNumber.from(0);
let gasUsedL1OnL2 = BigNumber.from(0);
const opStackChains = [
ChainId.OPTIMISM,
ChainId.OPTIMISM_GOERLI,
ChainId.OPTIMISM_SEPOLIA,
ChainId.BASE,
ChainId.BASE_GOERLI,
];
if (opStackChains.includes(chainId)) {
[l1Used, l1FeeInWei] = calculateOptimismToL1SecurityFee(
[mainnetGasUsed, mainnetFeeInWei] = calculateOptimismToL1SecurityFee(
route,
swapOptions,
l2GasData as OptimismGasData,
Expand All @@ -585,7 +579,7 @@ export const calculateL1GasFeesHelper = async (
chainId == ChainId.ARBITRUM_ONE ||
chainId == ChainId.ARBITRUM_GOERLI
) {
[l1Used, l1FeeInWei, gasUsedL1OnL2] =
[mainnetGasUsed, mainnetFeeInWei, gasUsedL1OnL2] =
calculateArbitrumToL1SecurityFee(
route,
swapOptions,
Expand All @@ -598,17 +592,15 @@ export const calculateL1GasFeesHelper = async (
const nativeCurrency = WRAPPED_NATIVE_CURRENCY[chainId];
const costNativeCurrency = CurrencyAmount.fromRawAmount(
nativeCurrency,
l1FeeInWei.toString()
mainnetFeeInWei.toString()
);

// convert fee into usd
const nativeTokenPrice =
usdPool.token0.address == nativeCurrency.address
? usdPool.token0Price
: usdPool.token1Price;

const gasCostL1USD: CurrencyAmount =
nativeTokenPrice.quote(costNativeCurrency);
const gasCostL1USD: CurrencyAmount = getQuoteThroughNativePool(
chainId,
costNativeCurrency,
usdPool
);

let gasCostL1QuoteToken = costNativeCurrency;
// if the inputted token is not in the native currency, quote a native/quote token pool to get the gas cost in terms of the quote token
Expand All @@ -629,7 +621,7 @@ export const calculateL1GasFeesHelper = async (
// gasUsedL1 is the gas units used calculated from the bytes of the calldata
// gasCostL1USD and gasCostL1QuoteToken is the cost of gas in each of those tokens
return {
gasUsedL1: l1Used,
gasUsedL1: mainnetGasUsed,
gasUsedL1OnL2,
gasCostL1USD,
gasCostL1QuoteToken,
Expand Down Expand Up @@ -671,7 +663,6 @@ function calculateOptimismToL1SecurityFee(
// scaled = unscaled / (10 ** decimals)
const scaledConversion = BigNumber.from(10).pow(decimals);
const scaled = unscaled.div(scaledConversion);
// TODO: also return the gasUsedL1OnL2 because the final estimateGasUsed should include L1 calldata posting fee
return [l1GasUsed, scaled];
}

Expand Down
9 changes: 9 additions & 0 deletions src/util/l2FeeChains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ChainId } from '@uniswap/sdk-core';

export const opStackChains = [
ChainId.OPTIMISM,
ChainId.OPTIMISM_GOERLI,
ChainId.OPTIMISM_SEPOLIA,
ChainId.BASE,
ChainId.BASE_GOERLI,
];

0 comments on commit 410f4d3

Please sign in to comment.