forked from ethereum-optimism/optimism-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate-fees-v2.ts
41 lines (38 loc) · 1.08 KB
/
estimate-fees-v2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {
serializeTransaction,
Transaction,
TransactionSerializedEIP1559,
PublicClient,
} from "viem";
import { getGasPriceOracleContract } from "./estimate-fees";
interface EstimateFeesResponse {
l1Fee: bigint;
l2Fee: bigint;
total: bigint;
}
// Inspired by Optimism's estimateFees in `estimate-fees.ts`; adapted to my needs.
export async function estimateFees(
tx: Transaction,
publicClient: PublicClient,
): Promise<EstimateFeesResponse> {
const contract = getGasPriceOracleContract(publicClient);
console.log(contract.address);
const serializedTransaction = serializeTransaction({
chainId: tx.chainId!,
to: tx.to!,
account: tx.from!,
accessList: tx.accessList,
data: tx.input,
value: tx.value,
type: tx.type,
}) as TransactionSerializedEIP1559;
const l1Fee: bigint = await contract.read.getL1Fee([serializedTransaction]);
const l2Fee = await publicClient.estimateGas({
to: tx.to!,
account: tx.from!,
accessList: tx.accessList,
data: tx.input,
value: tx.value,
});
return { l1Fee, l2Fee, total: l1Fee + l2Fee };
}