2021-11-11 17:46:45 +01:00
|
|
|
import * as ethers from 'ethers';
|
2022-07-30 07:37:29 +02:00
|
|
|
import { getContractFactory } from '@eth-optimism/contracts/dist/contract-defs';
|
|
|
|
import { predeploys } from '@eth-optimism/contracts/dist/predeploys';
|
2021-11-11 17:46:45 +01:00
|
|
|
import buildUnserializedTransaction from './buildUnserializedTransaction';
|
|
|
|
|
|
|
|
// The code in this file is largely drawn from https://community.optimism.io/docs/developers/l2/new-fees.html#for-frontend-and-wallet-developers
|
|
|
|
|
|
|
|
function buildOVMGasPriceOracleContract(eth) {
|
2022-07-30 07:37:29 +02:00
|
|
|
const OVMGasPriceOracle = getContractFactory('OVM_GasPriceOracle').attach(
|
|
|
|
predeploys.OVM_GasPriceOracle,
|
|
|
|
);
|
2021-11-11 17:46:45 +01:00
|
|
|
const abi = JSON.parse(
|
|
|
|
OVMGasPriceOracle.interface.format(ethers.utils.FormatTypes.json),
|
|
|
|
);
|
|
|
|
return eth.contract(abi).at(OVMGasPriceOracle.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function fetchEstimatedL1Fee(eth, txMeta) {
|
|
|
|
const contract = buildOVMGasPriceOracleContract(eth);
|
2022-07-31 20:26:40 +02:00
|
|
|
const serializedTransaction =
|
|
|
|
buildUnserializedTransaction(txMeta).serialize();
|
2021-11-11 17:46:45 +01:00
|
|
|
const result = await contract.getL1Fee(serializedTransaction);
|
|
|
|
return result?.[0]?.toString(16);
|
|
|
|
}
|