@tevm/tx
Generated API Documentation: View the full API documentation in the evmts/tevm-monorepo/packages/tx/docs folder.
The @tevm/tx
package provides a custom implementation of Ethereum transactions, extending the functionality of @ethereumjs/tx
. It includes support for various transaction types and introduces impersonated transactions unique to Tevm.
Installation
npm install @tevm/tx
API Reference
Enumerations
- Capability - Transaction capabilities
- TransactionType - Supported transaction types
Classes
- AccessListEIP2930Transaction - Type 1 transaction implementation
- BlobEIP4844Transaction - Type 3 transaction implementation
- FeeMarketEIP1559Transaction - Type 2 transaction implementation
- LegacyTransaction - Legacy transaction implementation
- TransactionFactory - Factory for creating transactions
Interfaces
- EIP1559CompatibleTx - EIP-1559 transaction interface
- EIP4844CompatibleTx - EIP-4844 transaction interface
- ImpersonatedTx - Impersonated transaction interface
- JsonRpcTx - JSON-RPC transaction format
- JsonTx - JSON transaction format
- TxData - Transaction data interface
- TxOptions - Transaction options interface
Type Aliases
- AccessList - Access list type definition
- AccessListItem - Access list item type
- TypedTransaction - Union type of all transaction types
Functions
- createImpersonatedTx - Create an impersonated transaction
- isAccessListEIP2930Tx - Type guard for EIP-2930 transactions
- isBlobEIP4844Tx - Type guard for EIP-4844 transactions
- isFeeMarketEIP1559Tx - Type guard for EIP-1559 transactions
- isLegacyTx - Type guard for legacy transactions
Main Components
Transaction Types
The package supports multiple transaction types:
- LegacyTransaction: Pre-EIP-2718 transactions
- AccessListEIP2930Transaction: Type 1 transactions with access lists (EIP-2930)
- FeeMarketEIP1559Transaction: Type 2 transactions with fee market (EIP-1559)
- BlobEIP4844Transaction: Type 3 transactions with blob data (EIP-4844)
- ImpersonatedTx: Tevm-specific transaction type for impersonating accounts
TransactionFactory
A utility class for creating transactions from various data formats:
import { TransactionFactory } from '@tevm/tx'
// Create from serialized data
const tx = TransactionFactory.fromSerializedTx(serializedData)
// Create from RPC data
const tx = await TransactionFactory.fromRPC(rpcTxData)
// Create from block body data
const tx = TransactionFactory.fromBlockBodyData(blockData)
Impersonated Transactions
A unique feature of Tevm that allows simulating transactions as if they were sent from any address:
import { createImpersonatedTx } from '@tevm/tx'
const tx = createImpersonatedTx({
impersonatedAddress: address,
to: recipient,
value: value,
data: data,
// ... other EIP-1559 transaction fields
})
Transaction Types
Legacy Transactions
Pre-EIP-2718 transactions with basic fields:
interface LegacyTxData {
nonce: bigint
gasPrice: bigint
gasLimit: bigint
to?: Address
value: bigint
data: Uint8Array
v?: bigint
r?: bigint
s?: bigint
}
EIP-2930 Transactions
Type 1 transactions with access lists:
interface AccessListEIP2930TxData extends LegacyTxData {
chainId: bigint
accessList: AccessList
}
EIP-1559 Transactions
Type 2 transactions with fee market:
interface FeeMarketEIP1559TxData extends AccessListEIP2930TxData {
maxFeePerGas: bigint
maxPriorityFeePerGas: bigint
}
EIP-4844 Transactions
Type 3 transactions with blob data:
interface BlobEIP4844TxData extends FeeMarketEIP1559TxData {
maxFeePerBlobGas: bigint
blobVersionedHashes: Uint8Array[]
blobs?: Uint8Array[]
kzgCommitments?: Uint8Array[]
kzgProofs?: Uint8Array[]
}
Common Operations
Creating Transactions
import { TransactionFactory, LegacyTransaction } from '@tevm/tx'
// Using factory
const tx = TransactionFactory.fromTxData({
nonce: 0n,
gasPrice: 20000000000n,
gasLimit: 21000n,
to: '0x...',
value: 1000000000000000000n,
data: new Uint8Array()
})
// Direct instantiation
const legacyTx = new LegacyTransaction({
nonce: 0n,
gasPrice: 20000000000n,
gasLimit: 21000n,
to: '0x...',
value: 1000000000000000000n,
data: new Uint8Array()
})
Signing Transactions
const signedTx = tx.sign(privateKey)
Transaction Methods
All transaction types provide common methods:
hash()
: Get transaction hashgetBaseFee()
: Get minimum required gasgetDataFee()
: Get gas cost for datagetUpfrontCost()
: Get total required balanceisSigned()
: Check if transaction is signedserialize()
: Get RLP encoded transactiontoJSON()
: Get JSON representation
Error Handling
The package includes custom error types:
import { InvalidGasLimitError } from '@tevm/tx'
try {
const tx = createImpersonatedTx(txData)
} catch (e) {
if (e instanceof InvalidGasLimitError) {
// Handle invalid gas limit
}
}