From 48c1908c49fbacb7246f5531bb968e22a1a18607 Mon Sep 17 00:00:00 2001 From: ponyjackal Date: Mon, 29 Jan 2024 04:04:09 -0800 Subject: [PATCH] feat: use BigInt L2 fee logic --- src/transformers/ethereum/transactionFees.ts | 45 +++++++++----------- src/types/log.ts | 3 ++ src/types/transaction.ts | 1 + 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/transformers/ethereum/transactionFees.ts b/src/transformers/ethereum/transactionFees.ts index 9c5c43a..b05dbc2 100644 --- a/src/transformers/ethereum/transactionFees.ts +++ b/src/transformers/ethereum/transactionFees.ts @@ -1,39 +1,34 @@ import type { RawBlock, RawTransaction } from '../../types'; -import { FORKS } from '../../helpers/constants'; export function transform(block: RawBlock) { const newTxs: Partial[] = []; for (const tx of block.transactions) { - const transactionFee = ( - BigInt(tx.receipt.gasUsed) * BigInt(tx.receipt.effectiveGasPrice) - ).toString(); - - let burntFees = '0'; - let minerFees = transactionFee; - - /*** - * Legacy tx after EIP1559 https://legacy.ethgasstation.info/blog/eip-1559/ - * "EIP-1559 will still be able to facilitate legacy style transactions. - * When these transactions come in their gas prices are simply converted into fee caps, - * including respective base fees and tips." - */ - if (tx.type === 2 && block.number >= FORKS.london) { - burntFees = ( - BigInt(block.baseFeePerGas) * BigInt(tx.receipt.gasUsed) - ).toString(); - minerFees = ( - (BigInt(tx.receipt.effectiveGasPrice) - BigInt(block.baseFeePerGas)) * - BigInt(tx.receipt.gasUsed) - ).toString(); + let totalL2FeeWei = BigInt(0); + if (tx.gasPrice) { + const l2GasPrice = BigInt(tx.gasPrice); + const l2GasUsed = BigInt(tx.receipt?.gasUsed ?? 0); + + const tenToTheEighteenth = BigInt('1000000000000000000'); + + const l1FeeContribution = !tx.receipt?.l1GasUsed + ? BigInt(0) + : (BigInt(tx.receipt?.l1GasPrice ?? 0) * + BigInt(tx.receipt.l1GasUsed) * + BigInt( + parseFloat(tx.receipt?.l1FeeScalar ?? '0') * Math.pow(10, 18), + )) / + tenToTheEighteenth; + + const l2FeeContribution = l2GasPrice * l2GasUsed; + + totalL2FeeWei = l2FeeContribution + l1FeeContribution; } newTxs.push({ hash: tx.hash, baseFeePerGas: block.baseFeePerGas, - burntFees, - minerFees, - transactionFee, + transactionFee: totalL2FeeWei.toString(), }); } diff --git a/src/types/log.ts b/src/types/log.ts index 8a99e7b..bc0e854 100644 --- a/src/types/log.ts +++ b/src/types/log.ts @@ -25,6 +25,9 @@ export type RawReceipt = StdObj & { logs: RawLog[]; gasUsed: number | string; effectiveGasPrice: number | string; + l1GasPrice?: string; + l1GasUsed?: string; + l1FeeScalar?: string; }; export type EventLogTopics = [ diff --git a/src/types/transaction.ts b/src/types/transaction.ts index 6f18298..8744703 100644 --- a/src/types/transaction.ts +++ b/src/types/transaction.ts @@ -11,6 +11,7 @@ export type RawTransaction = StdObj & { input: string; value: string; receipt: RawReceipt; + gasPrice: string; to: string; traces: RawTrace[]; contracts?: Contract[];