1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/ui/helpers/utils/optimism/fetchEstimatedL1Fee.js
2023-06-09 15:48:48 -05:00

49 lines
1.7 KiB
JavaScript

import { Contract } from '@ethersproject/contracts';
import { Web3Provider } from '@ethersproject/providers';
import { hexToDecimal } from '../../../../shared/modules/conversion.utils';
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 chainIdAsDecimalNumber = Number(hexToDecimal(chainId));
const provider = global.ethereumProvider
? new Web3Provider(global.ethereumProvider, chainIdAsDecimalNumber)
: ethersProvider;
if (process.env.IN_TEST) {
provider.detectNetwork = async () => ({
name: 'optimism',
chainId: chainIdAsDecimalNumber,
});
}
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();
}