-
Notifications
You must be signed in to change notification settings - Fork 388
/
gas-data-provider.ts
56 lines (49 loc) · 1.51 KB
/
gas-data-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
47
48
49
50
51
52
53
54
55
56
import { BigNumber, providers } from 'ethers';
import { OptimismGasData } from '../../routers';
import { GasPriceOracle, GasPriceOracle__factory } from '../../types/other';
import { ChainId, OVM_GASPRICE_ADDRESS } from '../../util';
/**
* Provider for getting Optimism gas constants.
*
* @export
* @interface IGasDataProvider
*/
export interface IGasDataProvider {
/**
* Gets the data constants needed to calculate the l1 security fee on Optimism.
* @returns An OptimismGasData object that includes the l1BaseFee,
* scalar, decimals, and overhead values.
*/
getGasData(): Promise<OptimismGasData>;
}
export class GasDataProvider implements IGasDataProvider {
protected gasOracleAddress: string;
private gasOracleContract: GasPriceOracle;
constructor(
protected chainId: ChainId,
protected provider: providers.BaseProvider,
gasPriceAddress?: string
) {
this.gasOracleAddress = gasPriceAddress ?? OVM_GASPRICE_ADDRESS;
this.gasOracleContract = GasPriceOracle__factory.connect(
this.gasOracleAddress,
provider
);
}
public async getGasData(): Promise<OptimismGasData> {
const [l1BaseFee, scalar, decimals, overhead]: BigNumber[] =
await Promise.all([
this.gasOracleContract.l1BaseFee(),
this.gasOracleContract.scalar(),
this.gasOracleContract.decimals(),
this.gasOracleContract.overhead(),
]);
const data: OptimismGasData = {
l1BaseFee,
scalar,
decimals,
overhead,
};
return data;
}
}