Skip to content

Commit

Permalink
Merge pull request #296 from Blobscan/fix/blob-gas-as-caldata
Browse files Browse the repository at this point in the history
fix(api): calculate blob gas as calldata for all the blobs in a tx
  • Loading branch information
0xGabi authored Mar 13, 2024
2 parents 9ed91fc + 93eb90a commit e94f31b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-llamas-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/api": patch
---

Fixed blob gas as calldata calculation
28 changes: 15 additions & 13 deletions packages/api/src/routers/indexer/indexData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Prisma } from "@blobscan/db";

import type { IndexDataInput } from "./indexData.schema";

const MIN_BLOB_GASPRICE = BigInt(1);
const BLOB_GASPRICE_UPDATE_FRACTION = BigInt(3_338_477);
const MIN_BLOB_BASE_FEE = BigInt(1);
const BLOB_BASE_FEE_UPDATE_FRACTION = BigInt(3_338_477);

export const GAS_PER_BLOB = 2 ** 17; // 131_072

Expand Down Expand Up @@ -56,12 +56,12 @@ export function calculateBlobSize(blob: string): number {
return blob.slice(2).length / 2;
}

export function calculateBlobGasPrice(excessDataGas: bigint): bigint {
export function calculateBlobGasPrice(excessBlobGas: bigint): bigint {
return BigInt(
fakeExponential(
MIN_BLOB_GASPRICE,
excessDataGas,
BLOB_GASPRICE_UPDATE_FRACTION
MIN_BLOB_BASE_FEE,
excessBlobGas,
BLOB_BASE_FEE_UPDATE_FRACTION
)
);
}
Expand All @@ -73,16 +73,18 @@ export function createDBTransactions({
}: IndexDataInput): WithoutTimestampFields<Transaction>[] {
return transactions.map<WithoutTimestampFields<Transaction>>(
({ from, gasPrice, hash, maxFeePerBlobGas, to }) => {
const txBlob: IndexDataInput["blobs"][0] | undefined = blobs.find(
(b) => b.txHash === hash
);
const txBlobs = blobs.filter((b) => b.txHash === hash);

if (!txBlob) {
throw new Error(`Blob for transaction ${hash} not found`);
if (txBlobs.length === 0) {
throw new Error(`Blobs for transaction ${hash} not found`);
}

const blobGasAsCalldataUsed = txBlobs.reduce(
(acc, b) => acc + getEIP2028CalldataGas(b.data),
BigInt(0)
);

const blobGasPrice = calculateBlobGasPrice(block.excessBlobGas);
const blobAsCalldataGasUsed = getEIP2028CalldataGas(txBlob.data);

return {
blockHash: block.hash,
Expand All @@ -92,7 +94,7 @@ export function createDBTransactions({
gasPrice: bigIntToDecimal(gasPrice),
blobGasPrice: bigIntToDecimal(blobGasPrice),
maxFeePerBlobGas: bigIntToDecimal(maxFeePerBlobGas),
blobAsCalldataGasUsed: bigIntToDecimal(blobAsCalldataGasUsed),
blobAsCalldataGasUsed: bigIntToDecimal(blobGasAsCalldataUsed),
};
}
);
Expand Down

0 comments on commit e94f31b

Please sign in to comment.