UNPKG

viem

Version:

TypeScript Interface for Ethereum

87 lines 3.62 kB
import { hexToNumber } from '../encoding/fromHex.js'; import { defineFormatter } from './formatter.js'; export const transactionType = { '0x0': 'legacy', '0x1': 'eip2930', '0x2': 'eip1559', '0x3': 'eip4844', '0x4': 'eip7702', }; export function formatTransaction(transaction) { const transaction_ = { ...transaction, blockHash: transaction.blockHash ? transaction.blockHash : null, blockNumber: transaction.blockNumber ? BigInt(transaction.blockNumber) : null, chainId: transaction.chainId ? hexToNumber(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 ? hexToNumber(transaction.nonce) : undefined, to: transaction.to ? transaction.to : null, transactionIndex: transaction.transactionIndex ? Number(transaction.transactionIndex) : null, type: transaction.type ? transactionType[transaction.type] : undefined, typeHex: transaction.type ? transaction.type : undefined, value: transaction.value ? BigInt(transaction.value) : undefined, v: transaction.v ? BigInt(transaction.v) : undefined, }; if (transaction.authorizationList) transaction_.authorizationList = formatAuthorizationList(transaction.authorizationList); 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 === 'bigint') { if (transaction_.v === 0n || transaction_.v === 27n) return 0; if (transaction_.v === 1n || transaction_.v === 28n) return 1; if (transaction_.v >= 35n) return transaction_.v % 2n === 0n ? 1 : 0; } return undefined; })(); if (transaction_.type === 'legacy') { delete transaction_.accessList; delete transaction_.maxFeePerBlobGas; delete transaction_.maxFeePerGas; delete transaction_.maxPriorityFeePerGas; delete transaction_.yParity; } if (transaction_.type === 'eip2930') { delete transaction_.maxFeePerBlobGas; delete transaction_.maxFeePerGas; delete transaction_.maxPriorityFeePerGas; } if (transaction_.type === 'eip1559') { delete transaction_.maxFeePerBlobGas; } return transaction_; } export const defineTransaction = /*#__PURE__*/ defineFormatter('transaction', formatTransaction); ////////////////////////////////////////////////////////////////////////////// function formatAuthorizationList(authorizationList) { return authorizationList.map((authorization) => ({ address: authorization.address, chainId: Number(authorization.chainId), nonce: Number(authorization.nonce), r: authorization.r, s: authorization.s, yParity: Number(authorization.yParity), })); } //# sourceMappingURL=transaction.js.map