2022-12-20 21:05:07 +01:00
|
|
|
import { Contract } from '@ethersproject/contracts';
|
|
|
|
import { Web3Provider } from '@ethersproject/providers';
|
2021-11-11 17:46:45 +01:00
|
|
|
import buildUnserializedTransaction from './buildUnserializedTransaction';
|
|
|
|
|
2022-12-20 21:05:07 +01:00
|
|
|
// 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',
|
|
|
|
},
|
|
|
|
];
|
2021-11-11 17:46:45 +01:00
|
|
|
|
2022-12-20 21:05:07 +01:00
|
|
|
// BlockExplorer link: https://optimistic.etherscan.io/address/0x420000000000000000000000000000000000000f#code
|
|
|
|
const OPTIMISM_GAS_PRICE_ORACLE_ADDRESS =
|
|
|
|
'0x420000000000000000000000000000000000000F';
|
2021-11-11 17:46:45 +01:00
|
|
|
|
2022-12-21 20:59:34 +01:00
|
|
|
export default async function fetchEstimatedL1Fee(txMeta, ethersProvider) {
|
|
|
|
const provider = global.ethereumProvider
|
|
|
|
? new Web3Provider(global.ethereumProvider, 10)
|
|
|
|
: ethersProvider;
|
2022-12-20 21:05:07 +01:00
|
|
|
if (process.env.IN_TEST) {
|
|
|
|
provider.detectNetwork = async () => ({
|
|
|
|
name: 'optimism',
|
|
|
|
chainId: 10,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const contract = new Contract(
|
|
|
|
OPTIMISM_GAS_PRICE_ORACLE_ADDRESS,
|
|
|
|
OPTIMISM_GAS_PRICE_ORACLE_ABI,
|
|
|
|
provider,
|
|
|
|
);
|
2022-07-31 20:26:40 +02:00
|
|
|
const serializedTransaction =
|
|
|
|
buildUnserializedTransaction(txMeta).serialize();
|
2022-12-20 21:05:07 +01:00
|
|
|
|
2021-11-11 17:46:45 +01:00
|
|
|
const result = await contract.getL1Fee(serializedTransaction);
|
2022-12-20 21:05:07 +01:00
|
|
|
return result?.toHexString();
|
2021-11-11 17:46:45 +01:00
|
|
|
}
|