import { bytesToHex, toHex } from '../encoding/toHex.js' import type { ErrorType } from '../errors/utils.js' import type { Chain, ExtractChainFormatterType } from '../types/chain.js' import type { Bytes } from '../types/data.js' import type { RpcTransactionRequest, TransactionRequest, } from '../types/transactionRequest.js' import { fromRpcAuthorizationList, toRpcAuthorizationList, } from './authorization.js' import { type DefineFormatterErrorType, defineFormatter, } from './defineFormatter.js' import { rpcTransactionType, transactionType } from './transaction.js' export type FormattedTransactionRequest< chain extends Chain | undefined = Chain | undefined, > = ExtractChainFormatterType export type FromRpcTransactionRequestErrorType = ErrorType export function fromRpcTransactionRequest( request: RpcTransactionRequest, ): TransactionRequest { return { from: request.from, ...(request.accessList ? { accessList: request.accessList } : {}), ...(request.authorizationList ? { authorizationList: fromRpcAuthorizationList( request.authorizationList, ), } : {}), ...(request.blobVersionedHashes ? { blobVersionedHashes: request.blobVersionedHashes } : {}), ...(request.blobs ? { blobs: request.blobs } : {}), ...(request.data ? { data: request.data } : {}), ...(request.gas ? { gas: BigInt(request.gas) } : {}), ...(request.gasPrice ? { gasPrice: BigInt(request.gasPrice) } : {}), ...(request.maxFeePerBlobGas ? { maxFeePerBlobGas: BigInt(request.maxFeePerBlobGas) } : {}), ...(request.maxFeePerGas ? { maxFeePerGas: BigInt(request.maxFeePerGas) } : {}), ...(request.maxPriorityFeePerGas ? { maxPriorityFeePerGas: BigInt(request.maxPriorityFeePerGas) } : {}), ...(request.nonce ? { nonce: BigInt(request.nonce) } : {}), ...(request.to ? { to: request.to } : {}), ...(request.type ? { type: transactionType[request.type] as any } : {}), ...(request.value ? { value: BigInt(request.value) } : {}), } as unknown as TransactionRequest } export type ToRpcTransactionRequestErrorType = ErrorType export function toRpcTransactionRequest( request: TransactionRequest, ): RpcTransactionRequest { const rpcRequest = {} as RpcTransactionRequest if (typeof request.authorizationList !== 'undefined') rpcRequest.authorizationList = toRpcAuthorizationList( request.authorizationList, ) if (typeof request.accessList !== 'undefined') rpcRequest.accessList = request.accessList if (typeof request.blobVersionedHashes !== 'undefined') rpcRequest.blobVersionedHashes = request.blobVersionedHashes if (typeof request.blobs !== 'undefined') { if (typeof request.blobs[0] !== 'string') rpcRequest.blobs = (request.blobs as Bytes[]).map((x) => bytesToHex(x)) else rpcRequest.blobs = request.blobs } if (typeof request.data !== 'undefined') rpcRequest.data = request.data if (typeof request.from !== 'undefined') rpcRequest.from = request.from if (typeof request.gas !== 'undefined') rpcRequest.gas = toHex(request.gas) if (typeof request.gasPrice !== 'undefined') rpcRequest.gasPrice = toHex(request.gasPrice) if (typeof request.maxFeePerBlobGas !== 'undefined') rpcRequest.maxFeePerBlobGas = toHex(request.maxFeePerBlobGas) if (typeof request.maxFeePerGas !== 'undefined') rpcRequest.maxFeePerGas = toHex(request.maxFeePerGas) if (typeof request.maxPriorityFeePerGas !== 'undefined') rpcRequest.maxPriorityFeePerGas = toHex(request.maxPriorityFeePerGas) if (typeof request.nonce !== 'undefined') rpcRequest.nonce = toHex(request.nonce) if (typeof request.to !== 'undefined') rpcRequest.to = request.to if (typeof request.type !== 'undefined') rpcRequest.type = rpcTransactionType[request.type] if (typeof request.value !== 'undefined') rpcRequest.value = toHex(request.value) return rpcRequest } export type DefineTransactionRequestErrorType = | DefineFormatterErrorType | ErrorType export const defineTransactionRequest = /*#__PURE__*/ defineFormatter({ fromRpc: fromRpcTransactionRequest, toRpc: toRpcTransactionRequest, })