Skip to content

Commit

Permalink
Fix: Gas price block aware (Uniswap#451)
Browse files Browse the repository at this point in the history
* gas price block aware

* fully implemented block-aware gas price

* fix the block number to be hex string
  • Loading branch information
jsy1218 authored Nov 22, 2023
1 parent 9691f22 commit 89c9917
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/providers/caching-gas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { GasPrice, IGasPriceProvider } from './gas-price-provider';
* @export
* @class CachingV3SubgraphProvider
*/
export class CachingGasStationProvider implements IGasPriceProvider {
private GAS_KEY = (chainId: ChainId) => `gasPrice-${chainId}`;
export class CachingGasStationProvider extends IGasPriceProvider {
private GAS_KEY = (chainId: ChainId, blockNumber: number) => `gasPrice-${chainId}-${blockNumber}`;

/**
* Creates an instance of CachingGasStationProvider.
Expand All @@ -24,10 +24,12 @@ export class CachingGasStationProvider implements IGasPriceProvider {
protected chainId: ChainId,
private gasPriceProvider: IGasPriceProvider,
private cache: ICache<GasPrice>
) {}
) {
super();
}

public async getGasPrice(): Promise<GasPrice> {
const cachedGasPrice = await this.cache.get(this.GAS_KEY(this.chainId));
public override async getGasPrice(requestBlockNumber: number): Promise<GasPrice> {
const cachedGasPrice = await this.cache.get(this.GAS_KEY(this.chainId, requestBlockNumber));

if (cachedGasPrice) {
log.info(
Expand All @@ -39,8 +41,8 @@ export class CachingGasStationProvider implements IGasPriceProvider {
}

log.info('Gas station price local cache miss.');
const gasPrice = await this.gasPriceProvider.getGasPrice();
await this.cache.set(this.GAS_KEY(this.chainId), gasPrice);
const gasPrice = await this.gasPriceProvider.getGasPrice(requestBlockNumber);
await this.cache.set(this.GAS_KEY(this.chainId, requestBlockNumber), gasPrice);

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

public async getGasPrice(): Promise<GasPrice> {
public override async getGasPrice(requestBlockNumber: number): Promise<GasPrice> {
const feeHistoryRaw = (await this.provider.send('eth_feeHistory', [
/**
* @fix Use BigNumber.from(this.blocksToConsider).toHexString() after hardhat adds support
* @see https://github.com/NomicFoundation/hardhat/issues/1585 .___.
*/
BigNumber.from(this.blocksToConsider).toHexString().replace('0x0', '0x'),
'latest',
BigNumber.from(requestBlockNumber).toHexString().replace('0x0', '0x'),
[this.priorityFeePercentile],
])) as RawFeeHistoryResponse;

Expand Down
3 changes: 2 additions & 1 deletion src/providers/eth-gas-station-info-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class ETHGasStationInfoProvider extends IGasPriceProvider {
this.url = url;
}

public async getGasPrice(): Promise<GasPrice> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public override async getGasPrice(_requestBlockNumber: number): Promise<GasPrice> {
log.info(`About to get gas prices from gas station ${this.url}`);
const response = await retry(
async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export type GasPrice = {
* Provider for getting gas prices.
*/
export abstract class IGasPriceProvider {
public abstract getGasPrice(): Promise<GasPrice>;
public abstract getGasPrice(requestBlockNumber: number): Promise<GasPrice>;
}
3 changes: 2 additions & 1 deletion src/providers/legacy-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class LegacyGasPriceProvider extends IGasPriceProvider {
super();
}

public async getGasPrice(): Promise<GasPrice> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public override async getGasPrice(_requestBlockNumber: number): Promise<GasPrice> {
const gasPriceWei = await this.provider.getGasPrice();
log.info(
{ gasPriceWei },
Expand Down
6 changes: 3 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,11 @@ export class OnChainGasPriceProvider extends IGasPriceProvider {
super();
}

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

return this.legacyGasPriceProvider.getGasPrice();
return this.legacyGasPriceProvider.getGasPrice(requestBlockNumber);
}
}
3 changes: 2 additions & 1 deletion src/providers/static-gas-price-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { GasPrice, IGasPriceProvider } from './gas-price-provider';

export class StaticGasPriceProvider implements IGasPriceProvider {
constructor(private gasPriceWei: BigNumber) {}
async getGasPrice(): Promise<GasPrice> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getGasPrice(_requestBlockNumber: number): Promise<GasPrice> {
return { gasPriceWei: this.gasPriceWei };
}
}
6 changes: 3 additions & 3 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ export class AlphaRouter
log.warn(`Finalized routing config is ${JSON.stringify(routingConfig)}`);
}

const gasPriceWei = await this.getGasPriceWei();
const gasPriceWei = await this.getGasPriceWei(await blockNumber);

const quoteToken = quoteCurrency.wrapped;
const providerConfig: ProviderConfig = {
Expand Down Expand Up @@ -1947,12 +1947,12 @@ export class AlphaRouter
}
}

private async getGasPriceWei(): Promise<BigNumber> {
private async getGasPriceWei(blockNumber: number): Promise<BigNumber> {
// Track how long it takes to resolve this async call.
const beforeGasTimestamp = Date.now();

// Get an estimate of the gas price to use when estimating gas cost of different routes.
const { gasPriceWei } = await this.gasPriceProvider.getGasPrice();
const { gasPriceWei } = await this.gasPriceProvider.getGasPrice(blockNumber);

metric.putMetric(
'GasPriceLoad',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/providers/gas-price-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('gas price provider', () => {
});

test('succeeds to get gas price and converts it to wei', async () => {
await expect(ethGasStationInfo.getGasPrice()).resolves.toMatchObject({
await expect(ethGasStationInfo.getGasPrice(10000000)).resolves.toMatchObject({
gasPriceWei: BigNumber.from('1000000000000000'),
});
});
Expand Down

0 comments on commit 89c9917

Please sign in to comment.