import { toHex } from '../encoding/toHex.js' import type { ErrorType } from '../errors/utils.js' import { vToYParity } from '../signature/vToYParity.js' import type { BlockTag } from '../types/block.js' import type { Chain } from '../types/chain.js' import type { ExtractChainFormatterType } from '../types/chain.js' import type { RpcTransaction } from '../types/transaction.js' import type { Transaction } from '../types/transaction.js' import type { PartialBy, UnionLooseOmit } from '../types/utils.js' import { fromRpcAuthorizationList, toRpcAuthorizationList, } from './authorization.js' import { type DefineFormatterErrorType, defineFormatter, } from './defineFormatter.js' type TransactionPendingDependencies = | 'blockHash' | 'blockNumber' | 'transactionIndex' export type FormattedTransaction< chain extends Chain | undefined = undefined, blockTag extends BlockTag = BlockTag, // _formatterReturnType = ExtractChainFormatterType< chain, 'transaction', Transaction >, _excludedPendingDependencies extends string = TransactionPendingDependencies, > = UnionLooseOmit<_formatterReturnType, TransactionPendingDependencies> & Pick< Transaction, TransactionPendingDependencies > /** @internal */ export const transactionType = { '0x0': 'legacy', '0x1': 'eip2930', '0x2': 'eip1559', '0x3': 'eip4844', '0x4': 'eip7702', } as const /** @internal */ export const rpcTransactionType = { legacy: '0x0', eip2930: '0x1', eip1559: '0x2', eip4844: '0x3', eip7702: '0x4', } as const export type FromRpcTransactionErrorType = ErrorType export function fromRpcTransaction( transaction: Omit, 'type'> & { type: string }, ) { const transaction_ = { ...transaction, authorizationList: transaction.authorizationList ? fromRpcAuthorizationList(transaction.authorizationList) : undefined, blockHash: transaction.blockHash ? transaction.blockHash : null, blockNumber: transaction.blockNumber ? BigInt(transaction.blockNumber) : null, chainId: transaction.chainId ? Number(transaction.chainId) : undefined, gas: transaction.gas ? BigInt(transaction.gas) : undefined, gasPrice: transaction.gasPrice ? BigInt(transaction.gasPrice) : undefined, maxFeePerBlobGas: transaction.maxFeePerBlobGas ? BigInt(transaction.maxFeePerBlobGas) : undefined, maxFeePerGas: transaction.maxFeePerGas ? BigInt(transaction.maxFeePerGas) : undefined, maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ? BigInt(transaction.maxPriorityFeePerGas) : undefined, nonce: transaction.nonce ? BigInt(transaction.nonce) : undefined, to: transaction.to ? transaction.to : null, transactionIndex: transaction.transactionIndex ? Number(transaction.transactionIndex) : null, type: transaction.type ? (transactionType as any)[transaction.type] : undefined, typeHex: transaction.type ? transaction.type : undefined, value: transaction.value ? BigInt(transaction.value) : undefined, v: transaction.v ? Number(transaction.v) : undefined, } as Transaction transaction_.yParity = (() => { // If `yParity` is provided, we will use it. if (transaction.yParity) return Number(transaction.yParity) // If no `yParity` provided, try derive from `v`. if (typeof transaction_.v === 'number') return vToYParity(transaction_.v) return undefined })() if (transaction_.type === 'legacy') { delete transaction_.accessList delete transaction_.yParity } if (transaction_.type === 'legacy' || transaction_.type === 'eip2930') { delete transaction_.maxFeePerGas delete transaction_.maxPriorityFeePerGas } if (transaction_.type !== 'eip4844') delete transaction_.maxFeePerBlobGas if (transaction_.type !== 'eip7702') delete transaction_.authorizationList return transaction_ } export type ToRpcTransactionErrorType = ErrorType export function toRpcTransaction( transaction: Omit & { type: string }, ): RpcTransaction { const yParity = (() => { if (transaction.type === 'legacy') return undefined // If `yParity` is provided, we will use it. if (transaction.yParity) return toHex(transaction.yParity) // If no `yParity` provided, try derive from `v`. if (typeof transaction.v === 'number') return toHex(vToYParity(transaction.v)) return undefined })() const rpcTransaction = { blockHash: transaction.blockHash, blockNumber: typeof transaction.blockNumber === 'bigint' ? toHex(transaction.blockNumber) : null, from: transaction.from, gas: toHex(transaction.gas), hash: transaction.hash, input: transaction.input, nonce: toHex(transaction.nonce), r: transaction.r, s: transaction.s, to: transaction.to, transactionIndex: typeof transaction.transactionIndex === 'number' ? toHex(transaction.transactionIndex) : null, type: (rpcTransactionType as any)[transaction.type] || transaction.type, value: toHex(transaction.value), ...(transaction.authorizationList ? { authorizationList: toRpcAuthorizationList( transaction.authorizationList, ), } : {}), ...(transaction.accessList ? { accessList: transaction.accessList } : {}), ...(transaction.blobVersionedHashes ? { blobVersionedHashes: transaction.blobVersionedHashes } : {}), ...(typeof transaction.chainId === 'number' ? { chainId: toHex(transaction.chainId) } : {}), ...(typeof transaction.gasPrice === 'bigint' ? { gasPrice: toHex(transaction.gasPrice) } : {}), ...(typeof transaction.maxFeePerBlobGas === 'bigint' ? { maxFeePerBlobGas: toHex(transaction.maxFeePerBlobGas) } : {}), ...(typeof transaction.maxFeePerGas === 'bigint' ? { maxFeePerGas: toHex(transaction.maxFeePerGas) } : {}), ...(typeof transaction.maxPriorityFeePerGas === 'bigint' ? { maxPriorityFeePerGas: toHex(transaction.maxPriorityFeePerGas) } : {}), ...(typeof transaction.v === 'number' ? { v: toHex(transaction.v) } : {}), ...(yParity ? { yParity } : {}), } as unknown as RpcTransaction return rpcTransaction } export type DefineTransactionErrorType = DefineFormatterErrorType | ErrorType export const defineTransaction = /*#__PURE__*/ defineFormatter({ fromRpc: fromRpcTransaction, toRpc: toRpcTransaction, })