-
Notifications
You must be signed in to change notification settings - Fork 390
/
on-chain-gas-price-provider.ts
46 lines (41 loc) · 1.32 KB
/
on-chain-gas-price-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { ChainId } from '@uniswap/sdk-core';
import { EIP1559GasPriceProvider } from './eip-1559-gas-price-provider';
import { GasPrice, IGasPriceProvider } from './gas-price-provider';
import { LegacyGasPriceProvider } from './legacy-gas-price-provider';
const DEFAULT_EIP_1559_SUPPORTED_CHAINS = [
ChainId.MAINNET,
ChainId.GOERLI,
ChainId.POLYGON_MUMBAI,
];
/**
* Gets gas prices on chain. If the chain supports EIP-1559 and has the feeHistory API,
* uses the EIP1559 provider. Otherwise it will use a legacy provider that uses eth_gasPrice
*
* @export
* @class OnChainGasPriceProvider
*/
export class OnChainGasPriceProvider extends IGasPriceProvider {
constructor(
protected chainId: ChainId,
protected eip1559GasPriceProvider: EIP1559GasPriceProvider,
protected legacyGasPriceProvider: LegacyGasPriceProvider,
protected eipChains: ChainId[] = DEFAULT_EIP_1559_SUPPORTED_CHAINS
) {
super();
}
public override async getGasPrice(
latestBlockNumber: number,
requestBlockNumber?: number
): Promise<GasPrice> {
if (this.eipChains.includes(this.chainId)) {
return this.eip1559GasPriceProvider.getGasPrice(
latestBlockNumber,
requestBlockNumber
);
}
return this.legacyGasPriceProvider.getGasPrice(
latestBlockNumber,
requestBlockNumber
);
}
}