Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sbvegan authored Dec 21, 2023
2 parents ed399e3 + a6cf7e2 commit bdc0be7
Show file tree
Hide file tree
Showing 33 changed files with 1,113 additions and 559 deletions.
10 changes: 9 additions & 1 deletion cli/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,12 @@ export abstract class BaseCommand extends Command {
quoteGasAdjusted: CurrencyAmount<Currency>,
estimatedGasUsedQuoteToken: CurrencyAmount<Currency>,
estimatedGasUsedUSD: CurrencyAmount<Currency>,
estimatedGasUsedGasToken: CurrencyAmount<Currency> | undefined,
methodParameters: MethodParameters | undefined,
blockNumber: BigNumber,
estimatedGasUsed: BigNumber,
gasPriceWei: BigNumber,
simulationStatus?: SimulationStatus
simulationStatus?: SimulationStatus,
) {
this.logger.info(`Best Route:`);
this.logger.info(`${routeAmountsToString(routeAmounts)}`);
Expand All @@ -385,6 +386,13 @@ export abstract class BaseCommand extends Command {
Math.min(estimatedGasUsedUSD.currency.decimals, 6)
)}`
);
if(estimatedGasUsedGasToken) {
this.logger.info(
`Gas Used gas token: ${estimatedGasUsedGasToken.toFixed(
Math.min(estimatedGasUsedGasToken.currency.decimals, 6)
)}`
);
}
this.logger.info(`Calldata: ${methodParameters?.calldata}`);
this.logger.info(`Value: ${methodParameters?.value}`);
this.logger.info({
Expand Down
2 changes: 2 additions & 0 deletions cli/commands/quote-to-ratio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class QuoteToRatio extends BaseCommand {
estimatedGasUsed,
estimatedGasUsedQuoteToken,
estimatedGasUsedUSD,
estimatedGasUsedGasToken,
gasPriceWei,
methodParameters,
quote,
Expand All @@ -159,6 +160,7 @@ export class QuoteToRatio extends BaseCommand {
quoteGasAdjusted,
estimatedGasUsedQuoteToken,
estimatedGasUsedUSD,
estimatedGasUsedGasToken,
methodParameters,
blockNumber,
estimatedGasUsed,
Expand Down
8 changes: 7 additions & 1 deletion cli/commands/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Quote extends BaseCommand {
debugRouting: flags.boolean({ required: false, default: true }),
enableFeeOnTransferFeeFetching: flags.boolean({ required: false, default: false }),
requestBlockNumber: flags.integer({ required: false }),
gasToken: flags.string({ required: false }),
};

async run() {
Expand Down Expand Up @@ -68,7 +69,8 @@ export class Quote extends BaseCommand {
simulate,
debugRouting,
enableFeeOnTransferFeeFetching,
requestBlockNumber
requestBlockNumber,
gasToken
} = flags;

const topNSecondHopForTokenAddress = new MapWithLowerCaseKey();
Expand Down Expand Up @@ -159,6 +161,7 @@ export class Quote extends BaseCommand {
forceMixedRoutes,
debugRouting,
enableFeeOnTransferFeeFetching,
gasToken
}
);
} else {
Expand Down Expand Up @@ -196,6 +199,7 @@ export class Quote extends BaseCommand {
forceMixedRoutes,
debugRouting,
enableFeeOnTransferFeeFetching,
gasToken
}
);
}
Expand All @@ -214,6 +218,7 @@ export class Quote extends BaseCommand {
estimatedGasUsed,
estimatedGasUsedQuoteToken,
estimatedGasUsedUSD,
estimatedGasUsedGasToken,
gasPriceWei,
methodParameters,
quote,
Expand All @@ -228,6 +233,7 @@ export class Quote extends BaseCommand {
quoteGasAdjusted,
estimatedGasUsedQuoteToken,
estimatedGasUsedUSD,
estimatedGasUsedGasToken,
methodParameters,
blockNumber,
estimatedGasUsed,
Expand Down
22 changes: 17 additions & 5 deletions src/providers/caching-gas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { GasPrice, IGasPriceProvider } from './gas-price-provider';
* @class CachingV3SubgraphProvider
*/
export class CachingGasStationProvider extends IGasPriceProvider {
private GAS_KEY = (chainId: ChainId, blockNumber: number) => `gasPrice-${chainId}-${blockNumber}`;
private GAS_KEY = (chainId: ChainId, blockNumber: number) =>
`gasPrice-${chainId}-${blockNumber}`;

/**
* Creates an instance of CachingGasStationProvider.
Expand All @@ -28,11 +29,16 @@ export class CachingGasStationProvider extends IGasPriceProvider {
super();
}

public override async getGasPrice(latestBlockNumber: number, requestBlockNumber?: number): Promise<GasPrice> {
public override async getGasPrice(
latestBlockNumber: number,
requestBlockNumber?: number
): Promise<GasPrice> {
// If block number is specified in the request, we have to use that block number find any potential cache hits.
// Otherwise, we can use the latest block number.
const targetBlockNumber = requestBlockNumber ?? latestBlockNumber;
const cachedGasPrice = await this.cache.get(this.GAS_KEY(this.chainId, targetBlockNumber));
const cachedGasPrice = await this.cache.get(
this.GAS_KEY(this.chainId, targetBlockNumber)
);

if (cachedGasPrice) {
log.info(
Expand All @@ -43,8 +49,14 @@ export class CachingGasStationProvider extends IGasPriceProvider {
return cachedGasPrice;
}

const gasPrice = await this.gasPriceProvider.getGasPrice(latestBlockNumber, requestBlockNumber);
await this.cache.set(this.GAS_KEY(this.chainId, targetBlockNumber), gasPrice);
const gasPrice = await this.gasPriceProvider.getGasPrice(
latestBlockNumber,
requestBlockNumber
);
await this.cache.set(
this.GAS_KEY(this.chainId, targetBlockNumber),
gasPrice
);

return gasPrice;
}
Expand Down
9 changes: 7 additions & 2 deletions src/providers/eip-1559-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export class EIP1559GasPriceProvider extends IGasPriceProvider {
super();
}

public override async getGasPrice(_latestBlockNumber: number, requestBlockNumber?: number): Promise<GasPrice> {
public override async getGasPrice(
_latestBlockNumber: number,
requestBlockNumber?: number
): Promise<GasPrice> {
const feeHistoryRaw = (await this.provider.send('eth_feeHistory', [
/**
* @fix Use BigNumber.from(this.blocksToConsider).toHexString() after hardhat adds support
Expand All @@ -53,7 +56,9 @@ export class EIP1559GasPriceProvider extends IGasPriceProvider {
// If the block number is not specified, we have to send hardcoded 'latest' to infura RPC
// because Infura node pool is eventually consistent and may not have the latest block from our block number.
// See https://uniswapteam.slack.com/archives/C023A7JDTJP/p1702485038251449?thread_ts=1702471203.519869&cid=C023A7JDTJP
requestBlockNumber ? BigNumber.from(requestBlockNumber).toHexString().replace('0x0', '0x') : 'latest',
requestBlockNumber
? BigNumber.from(requestBlockNumber).toHexString().replace('0x0', '0x')
: 'latest',
[this.priorityFeePercentile],
])) as RawFeeHistoryResponse;

Expand Down
14 changes: 10 additions & 4 deletions src/providers/eth-estimate-gas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { BigNumber } from '@ethersproject/bignumber';
import { JsonRpcProvider } from '@ethersproject/providers';
import { ChainId } from '@uniswap/sdk-core';

import { SwapOptions, SwapRoute, SwapType } from '../routers';
import {
GasModelProviderConfig,
SwapOptions,
SwapRoute,
SwapType,
} from '../routers';
import { BEACON_CHAIN_DEPOSIT_ADDRESS, log } from '../util';
import {
calculateGasUsed,
Expand Down Expand Up @@ -107,6 +112,7 @@ export class EthEstimateGasSimulator extends Simulator {
const {
estimatedGasUsedUSD,
estimatedGasUsedQuoteToken,
estimatedGasUsedGasToken,
quoteGasAdjusted,
} = await calculateGasUsed(
route.quote.currency.chainId,
Expand All @@ -127,7 +133,8 @@ export class EthEstimateGasSimulator extends Simulator {
estimatedGasUsed,
estimatedGasUsedQuoteToken,
estimatedGasUsedUSD,
swapOptions
swapOptions,
estimatedGasUsedGasToken
),
simulationStatus: SimulationStatus.Succeeded,
};
Expand All @@ -150,8 +157,7 @@ export class EthEstimateGasSimulator extends Simulator {
swapOptions: SwapOptions,
swapRoute: SwapRoute,
l2GasData?: OptimismGasData | ArbitrumGasData | undefined,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_providerConfig?: ProviderConfig | undefined
_providerConfig?: GasModelProviderConfig | undefined
): Promise<SwapRoute> {
const inputAmount = swapRoute.trade.inputAmount;
if (
Expand Down
6 changes: 4 additions & 2 deletions src/providers/eth-gas-station-info-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ export class ETHGasStationInfoProvider extends IGasPriceProvider {
this.url = url;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public override async getGasPrice(_latestBlockNumber: number, _requestBlockNumber?: number): Promise<GasPrice> {
public override async getGasPrice(
_latestBlockNumber: number,
_requestBlockNumber?: number
): Promise<GasPrice> {
const response = await retry(
async () => {
return axios.get<ETHGasStationResponse>(this.url);
Expand Down
5 changes: 4 additions & 1 deletion src/providers/gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export type GasPrice = {
* Provider for getting gas prices.
*/
export abstract class IGasPriceProvider {
public abstract getGasPrice(latestBlockNumber: number, requestBlockNumber?: number): Promise<GasPrice>;
public abstract getGasPrice(
latestBlockNumber: number,
requestBlockNumber?: number
): Promise<GasPrice>;
}
6 changes: 4 additions & 2 deletions src/providers/legacy-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export class LegacyGasPriceProvider extends IGasPriceProvider {
super();
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public override async getGasPrice(_latestBlockNumber: number, _requestBlockNumber?: number): Promise<GasPrice> {
public override async getGasPrice(
_latestBlockNumber: number,
_requestBlockNumber?: number
): Promise<GasPrice> {
const gasPriceWei = await this.provider.getGasPrice();
return {
gasPriceWei,
Expand Down
15 changes: 12 additions & 3 deletions src/providers/on-chain-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ export class OnChainGasPriceProvider extends IGasPriceProvider {
super();
}

public override async getGasPrice(latestBlockNumber: number, requestBlockNumber?: number): Promise<GasPrice> {
public override async getGasPrice(
latestBlockNumber: number,
requestBlockNumber?: number
): Promise<GasPrice> {
if (this.eipChains.includes(this.chainId)) {
return this.eip1559GasPriceProvider.getGasPrice(latestBlockNumber, requestBlockNumber);
return this.eip1559GasPriceProvider.getGasPrice(
latestBlockNumber,
requestBlockNumber
);
}

return this.legacyGasPriceProvider.getGasPrice(latestBlockNumber, requestBlockNumber);
return this.legacyGasPriceProvider.getGasPrice(
latestBlockNumber,
requestBlockNumber
);
}
}
6 changes: 0 additions & 6 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { BigNumber } from '@ethersproject/bignumber';

export type ProviderConfig = {
/**
* The block number to use when getting data on-chain.
*/
blockNumber?: number | Promise<number>;
/*
* Any additional overhead to add to the gas estimate
*/
additionalGasOverhead?: BigNumber;
/*
* Debug flag to test some codepaths
*/
Expand Down
12 changes: 8 additions & 4 deletions src/providers/simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { ChainId, TradeType } from '@uniswap/sdk-core';
import { PERMIT2_ADDRESS } from '@uniswap/universal-router-sdk';
import { BigNumber } from 'ethers/lib/ethers';

import { SwapOptions, SwapRoute, SwapType } from '../routers';
import {
GasModelProviderConfig,
SwapOptions,
SwapRoute,
SwapType,
} from '../routers';
import { Erc20__factory } from '../types/other/factories/Erc20__factory';
import { Permit2__factory } from '../types/other/factories/Permit2__factory';
import { CurrencyAmount, log, SWAP_ROUTER_02_ADDRESSES } from '../util';

import { IPortionProvider } from './portion-provider';
import { ProviderConfig } from './provider';
import { ArbitrumGasData, OptimismGasData } from './v3/gas-data-provider';

export type SimulationResult = {
Expand Down Expand Up @@ -60,7 +64,7 @@ export abstract class Simulator {
amount: CurrencyAmount,
quote: CurrencyAmount,
l2GasData?: OptimismGasData | ArbitrumGasData,
providerConfig?: ProviderConfig
providerConfig?: GasModelProviderConfig
): Promise<SwapRoute> {
const neededBalance =
swapRoute.trade.tradeType == TradeType.EXACT_INPUT ? amount : quote;
Expand Down Expand Up @@ -105,7 +109,7 @@ export abstract class Simulator {
swapOptions: SwapOptions,
swapRoute: SwapRoute,
l2GasData?: OptimismGasData | ArbitrumGasData,
providerConfig?: ProviderConfig
providerConfig?: GasModelProviderConfig
): Promise<SwapRoute>;

protected async userHasSufficientBalance(
Expand Down
7 changes: 5 additions & 2 deletions src/providers/static-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { GasPrice, IGasPriceProvider } from './gas-price-provider';

export class StaticGasPriceProvider implements IGasPriceProvider {
constructor(private gasPriceWei: BigNumber) {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getGasPrice(_latestBlockNumber: number, _requestBlockNumber?: number): Promise<GasPrice> {

async getGasPrice(
_latestBlockNumber: number,
_requestBlockNumber?: number
): Promise<GasPrice> {
return { gasPriceWei: this.gasPriceWei };
}
}
Loading

0 comments on commit bdc0be7

Please sign in to comment.