UNPKG

blockchain-api

Version:

API utilities for interacting with the Exatechl2 blockchain

134 lines (113 loc) 3.46 kB
import { Block } from '../../types'; import { parseTimestampToMillis, calculateAgeInMillis, parseHexToDecimal, formatGasValue, calculateGasPercentage } from '../../utils/formatters'; export interface FormattedBlock { id: string; number: string; numberDecimal: number; hash: string; timestamp: number; age: number; txCount: number; size: string; sizeBytes: number; status: string; gasLimit: string; gasLimitDecimal: number; gasUsed: string; gasUsedDecimal: number; gasUsedPercentage: number; baseFee: string; stateRoot?: string; receiptsRoot?: string; extraData?: string; logsBloom?: string; difficulty?: string; totalDifficulty?: string; nonce?: string; mixHash?: string; sha3Uncles?: string; sendCount?: string; sendRoot?: string; l1BlockNumber?: number; validator?: string; parentHash?: string; rootHash?: string; transactions?: any[]; batchId?: string | number; batchStatus?: string; } /** * * @param {Block} block * @returns {FormattedBlock} */ export function formatBlockForDisplay(block: Block): FormattedBlock { const numberDecimal = parseHexToDecimal(block.number); const id = `B-${numberDecimal}`; const timestamp = parseTimestampToMillis(block.timestamp); const age = calculateAgeInMillis(block.timestamp); const txCount = typeof block.txCount !== 'undefined' && block.txCount !== null ? Number(block.txCount) : Array.isArray(block.transactions) ? block.transactions.length : 0; const sizeBytes = parseHexToDecimal(block.size); const size = sizeBytes > 0 ? `${(sizeBytes / 1024).toFixed(2)} KB` : 'N/A'; let statusValue = block.status || block.batchStatus || 'N/A'; if (statusValue && typeof statusValue === 'string') { statusValue = statusValue.charAt(0).toUpperCase() + statusValue.slice(1).toLowerCase(); } const gasLimitDecimal = parseHexToDecimal(block.gasLimit); const gasLimit = gasLimitDecimal > 0 ? gasLimitDecimal.toString() : 'N/A'; const gasUsedDecimal = parseHexToDecimal(block.gasUsed); const gasUsed = gasUsedDecimal > 0 ? gasUsedDecimal.toString() : 'N/A'; const gasUsedPercentage = calculateGasPercentage(block.gasUsed, block.gasLimit); const gasPriceField = block.baseFeePerGas || block.baseFee || block.minimumGasPrice || block.gasPrice; const baseFee = gasPriceField ? formatGasValue(gasPriceField, 'gwei', 2).replace(' Gwei', '') : 'N/A'; let l1BlockNumber: number | undefined = undefined; if (block.l1BlockNumber) { l1BlockNumber = parseHexToDecimal(block.l1BlockNumber); } return { id, number: block.number, numberDecimal, hash: block.hash, timestamp, age, txCount, size, sizeBytes, status: statusValue, gasLimit, gasLimitDecimal, gasUsed, gasUsedDecimal, gasUsedPercentage, baseFee, stateRoot: block.stateRoot, receiptsRoot: block.receiptsRoot, extraData: block.extraData, logsBloom: block.logsBloom, difficulty: block.difficulty, totalDifficulty: block.totalDifficulty, nonce: block.nonce, mixHash: block.mixHash, sha3Uncles: block.sha3Uncles, sendCount: block.sendCount, sendRoot: block.sendRoot, l1BlockNumber, validator: block.validator, parentHash: block.parentHash, rootHash: block.rootHash, transactions: block.transactions, batchId: block.batchId, batchStatus: block.batchStatus }; }