import type { ErrorType } from '../errors/utils.js' import type { Block, BlockTag } from '../types/block.js' import type { RpcBlock } from '../types/block.js' import type { Chain } from '../types/chain.js' import type { ExtractChainFormatterType } from '../types/chain.js' import type { Hex } from '../types/data.js' import type { Prettify } from '../types/utils.js' import { toHex } from '../encoding/toHex.js' import type { RpcTransaction } from '../types/transaction.js' import { type DefineFormatterErrorType, defineFormatter, } from './defineFormatter.js' import { type FormattedTransaction, fromRpcTransaction, toRpcTransaction, } from './transaction.js' type BlockPendingDependencies = 'hash' | 'logsBloom' | 'nonce' | 'number' export type FormattedBlock< chain extends Chain | undefined = undefined, includeTransactions extends boolean = boolean, blockTag extends BlockTag = BlockTag, // _formatterReturnType = ExtractChainFormatterType< chain, 'block', Block >, _excludedPendingDependencies extends string = BlockPendingDependencies, _formatted = Omit<_formatterReturnType, BlockPendingDependencies> & { [_key in _excludedPendingDependencies]: never } & Pick< Block, BlockPendingDependencies >, _transactions = includeTransactions extends true ? Prettify>[] : Hex[], > = Omit<_formatted, 'transactions'> & { transactions: _transactions } export type FromRpcBlockErrorType = ErrorType export function fromRpcBlock(block: RpcBlock): Block { const transactions = block.transactions?.map((transaction) => { if (typeof transaction === 'string') return transaction return fromRpcTransaction(transaction) }) return { ...block, baseFeePerGas: block.baseFeePerGas ? BigInt(block.baseFeePerGas) : null, blobGasUsed: block.blobGasUsed ? BigInt(block.blobGasUsed) : undefined, difficulty: block.difficulty ? BigInt(block.difficulty) : undefined, excessBlobGas: block.excessBlobGas ? BigInt(block.excessBlobGas) : undefined, gasLimit: block.gasLimit ? BigInt(block.gasLimit) : undefined, gasUsed: block.gasUsed ? BigInt(block.gasUsed) : undefined, hash: block.hash ? block.hash : null, logsBloom: block.logsBloom ? block.logsBloom : null, nonce: block.nonce ? block.nonce : null, number: block.number ? BigInt(block.number) : null, size: block.size ? BigInt(block.size) : undefined, timestamp: block.timestamp ? BigInt(block.timestamp) : undefined, transactions, totalDifficulty: block.totalDifficulty ? BigInt(block.totalDifficulty) : null, } as Block } export type ToRpcBlockErrorType = ErrorType export function toRpcBlock(block: Block): RpcBlock { return { baseFeePerGas: typeof block.baseFeePerGas === 'bigint' ? toHex(block.baseFeePerGas) : null, blobGasUsed: typeof block.blobGasUsed === 'bigint' ? toHex(block.blobGasUsed) : undefined, difficulty: typeof block.difficulty === 'bigint' ? toHex(block.difficulty) : undefined, excessBlobGas: typeof block.excessBlobGas === 'bigint' ? toHex(block.excessBlobGas) : undefined, totalDifficulty: typeof block.totalDifficulty === 'bigint' ? toHex(block.totalDifficulty) : null, extraData: block.extraData, hash: block.hash, logsBloom: block.logsBloom, miner: block.miner, mixHash: block.mixHash, nonce: block.nonce, number: typeof block.number === 'bigint' ? toHex(block.number) : null, parentHash: block.parentHash, receiptsRoot: block.receiptsRoot, sealFields: block.sealFields, sha3Uncles: block.sha3Uncles, stateRoot: block.stateRoot, transactions: block.transactions.map((transaction) => { if (typeof transaction === 'string') return transaction return toRpcTransaction(transaction) }) as RpcTransaction[], transactionsRoot: block.transactionsRoot, uncles: block.uncles, withdrawals: block.withdrawals, withdrawalsRoot: block.withdrawalsRoot, ...(typeof block.gasLimit === 'bigint' ? { gasLimit: toHex(block.gasLimit) } : {}), ...(typeof block.gasUsed === 'bigint' ? { gasUsed: toHex(block.gasUsed) } : {}), ...(typeof block.size === 'bigint' ? { size: toHex(block.size) } : {}), ...(typeof block.timestamp === 'bigint' ? { timestamp: toHex(block.timestamp) } : {}), } as RpcBlock } export type DefineBlockErrorType = DefineFormatterErrorType | ErrorType export const defineBlock = /*#__PURE__*/ defineFormatter({ fromRpc: fromRpcBlock, toRpc: toRpcBlock, })