2022-12-20 21:05:07 +01:00
|
|
|
import { Contract } from '@ethersproject/contracts';
|
|
|
|
import { Web3Provider } from '@ethersproject/providers';
|
2023-06-09 22:48:48 +02:00
|
|
|
import { hexToDecimal } from '../../../../shared/modules/conversion.utils';
|
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
|
|
|
|
2023-04-06 13:50:52 +02:00
|
|
|
export default async function fetchEstimatedL1Fee(
|
|
|
|
chainId,
|
|
|
|
txMeta,
|
|
|
|
ethersProvider,
|
|
|
|
) {
|
2023-06-09 22:48:48 +02:00
|
|
|
const chainIdAsDecimalNumber = Number(hexToDecimal(chainId));
|
2022-12-21 20:59:34 +01:00
|
|
|
const provider = global.ethereumProvider
|
2023-06-09 22:48:48 +02:00
|
|
|
? new Web3Provider(global.ethereumProvider, chainIdAsDecimalNumber)
|
2022-12-21 20:59:34 +01:00
|
|
|
: ethersProvider;
|
2023-04-06 13:50:52 +02:00
|
|
|
|
2022-12-20 21:05:07 +01:00
|
|
|
if (process.env.IN_TEST) {
|
|
|
|
provider.detectNetwork = async () => ({
|
|
|
|
name: 'optimism',
|
2023-06-09 22:48:48 +02:00
|
|
|
chainId: chainIdAsDecimalNumber,
|
2022-12-20 21:05:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
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();
|
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
|
|
|
}
|