1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/ui/helpers/utils/optimism/fetchEstimatedL1Fee.js

48 lines
1.5 KiB
JavaScript

import { Contract } from '@ethersproject/contracts';
import { Web3Provider } from '@ethersproject/providers';
import buildUnserializedTransaction from './buildUnserializedTransaction';
// Snippet of the ABI that we need
// Should we need more of it at some point, the full ABI can be found here:
// https://github.com/ethereum-optimism/optimism/blob/develop/gas-oracle/abis/OVM_GasPriceOracle.json
const OPTIMISM_GAS_PRICE_ORACLE_ABI = [
{
inputs: [{ internalType: 'bytes', name: '_data', type: 'bytes' }],
name: 'getL1Fee',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
];
// BlockExplorer link: https://optimistic.etherscan.io/address/0x420000000000000000000000000000000000000f#code
const OPTIMISM_GAS_PRICE_ORACLE_ADDRESS =
'0x420000000000000000000000000000000000000F';
export default async function fetchEstimatedL1Fee(
chainId,
txMeta,
ethersProvider,
) {
const networkId = Number(chainId);
const provider = global.ethereumProvider
? new Web3Provider(global.ethereumProvider, networkId)
: ethersProvider;
if (process.env.IN_TEST) {
provider.detectNetwork = async () => ({
name: 'optimism',
chainId: networkId,
});
}
const contract = new Contract(
OPTIMISM_GAS_PRICE_ORACLE_ADDRESS,
OPTIMISM_GAS_PRICE_ORACLE_ABI,
provider,
);
const serializedTransaction =
buildUnserializedTransaction(txMeta).serialize();
const result = await contract.getL1Fee(serializedTransaction);
return result?.toHexString();
}