Transaction struct and helper functions for handling EVM transactions.
This module provides functionality to:
- Create and manipulate transaction structs
- Encode transactions for network transmission
- Handle different transaction types
Supported transaction types:
Ethers.Transaction.Legacy- Legacy (type 0) transactions, with EIP-155 replay protection whenchain_idis setEthers.Transaction.Eip2930- EIP-2930 access list (type 1) transactionsEthers.Transaction.Eip1559- EIP-1559 fee market (type 2) transactions, the default when sending or signing transactionsEthers.Transaction.Eip4844- EIP-4844 blob (type 3) transactions. Transaction envelope only: blob sidecars are not supported, so these cannot be broadcastEthers.Transaction.Eip7702- EIP-7702 set code (type 4) transactions
Signed transactions of any type are wrapped in Ethers.Transaction.Signed.
Transactions of any other type (for example OP-stack deposit transactions, type 0x7e) read
with from_rpc_map/1 or decode/1 are returned undecoded as Ethers.Transaction.Unknown.
Summary
Callbacks
Returns a list of fields that can be auto-fetched from the network.
Constructs a transaction from a decoded RLP list
Creates a new transaction struct with the given parameters.
Returns the type envelope for the transaction.
Returns the type ID for the transaction. e.g Legacy: 0, EIP-1559: 2
Functions
Fills missing transaction fields with default values from the network based on transaction type.
When params has no :type, it is set to Ethers.Transaction.Eip1559.
Decodes a raw transaction from a binary or hex-encoded string.
Encodes a transaction for network transmission.
Converts a map (typically from JSON-RPC response) into a Transaction struct.
Creates a new transaction struct with the given parameters.
Converts a Transaction struct into a map suitable for JSON-RPC.
Calculates the transaction hash.
Types
@type t() :: t_payload() | Ethers.Transaction.Signed.t() | Ethers.Transaction.Unknown.t()
EVM Transaction type
@type t_payload() :: Ethers.Transaction.Legacy.t() | Ethers.Transaction.Eip7702.t() | Ethers.Transaction.Eip4844.t() | Ethers.Transaction.Eip2930.t() | Ethers.Transaction.Eip1559.t()
EVM Transaction payload type
Callbacks
@callback auto_fetchable_fields() :: [atom()]
Returns a list of fields that can be auto-fetched from the network.
@callback from_rlp_list([binary() | [binary()]]) :: {:ok, t(), rest :: [binary() | [binary()]]} | {:error, reason :: term()}
Constructs a transaction from a decoded RLP list
Creates a new transaction struct with the given parameters.
@callback type_envelope() :: binary()
Returns the type envelope for the transaction.
@callback type_id() :: non_neg_integer()
Returns the type ID for the transaction. e.g Legacy: 0, EIP-1559: 2
Functions
Fills missing transaction fields with default values from the network based on transaction type.
When params has no :type, it is set to Ethers.Transaction.Eip1559.
Missing max_fee_per_gas and max_priority_fee_per_gas values are estimated from recent
blocks with Ethers.estimate_fees/1 (eth_feeHistory). When the RPC client does not
support eth_feeHistory (or the node returns no usable data), they fall back to the
legacy estimation based on eth_gasPrice and eth_maxPriorityFeePerGas.
A missing gas is filled with the eth_estimateGas result plus the :default_gas_margin.
The gas limit, whether filled or given, is checked against the per-transaction gas limit cap
of EIP-7825 as set with the :max_transaction_gas
option or application config (see the configuration guide):
- Not configured: a gas limit above 16,777,216 (the EIP-7825 cap) logs a warning.
- A positive integer: a gas limit above it returns
{:error, :gas_limit_exceeds_cap}. The margin never pushes an estimate at or below the cap above it, the filled gas is clamped to the cap instead. false: no check and no warning.
Parameters
params- Updated Transaction paramsopts- Options to pass to the RPC client, and:max_transaction_gas
Returns
{:ok, params}- Filled transaction params map{:error, :gas_limit_exceeds_cap}- If the gas limit exceeds a configured cap{:error, reason}- If fetching defaults fails
Decodes a raw transaction from a binary or hex-encoded string.
Transaction strings must be prefixed with "0x" for hex-encoded inputs.
Handles legacy and all supported typed transactions (EIP-2930, EIP-1559, EIP-4844 and
EIP-7702). Signed transactions are returned wrapped in Ethers.Transaction.Signed, unsigned
ones (including the EIP-155 legacy signing payload, see encode/1) as the bare transaction
struct. EIP-4844 transactions are decoded from the transaction envelope only (without a blob
sidecar). Malformed input returns an error instead of raising.
A first byte in 0x01..0x7f that is not a registered transaction type (for example an
OP-stack deposit transaction, type 0x7e) returns an Ethers.Transaction.Unknown with the
rest of the bytes, undecoded and unvalidated, in raw_payload.
Parameters
raw_transaction- Raw transaction data as a binary or hex string starting with "0x"
Returns
{:ok, transaction}- Decoded transaction struct{:error, reason}- The input is not a valid transaction, withreasonone of::invalid_hex- invalid hex string:invalid_rlp- invalid RLP, or trailing bytes after the transaction:unsupported_transaction_type- a first byte of0x00(legacy transactions have no type byte):transaction_decode_failed- fields of the wrong shape, kind or size:invalid_address- atoaddress that is not 20 bytes:missing_to_address- an EIP-7702 transaction withoutto:signature_decode_failed- malformed signature fields:authorization_decode_failed- a malformed EIP-7702 authorization
Encodes a transaction for network transmission.
Handles all supported transaction types: Legacy (including EIP-155), EIP-2930, EIP-1559,
EIP-4844 and EIP-7702. An Ethers.Transaction.Signed transaction is encoded with its
signature. An unsigned transaction is encoded as its signing payload, whose keccak hash is
what gets signed (for EIP-155 legacy transactions it ends with chain_id, 0, 0, which
decode/1 reads back as the unsigned transaction). EIP-4844 transactions are encoded without
a blob sidecar (see Ethers.Transaction.Eip4844).
An Ethers.Transaction.Unknown transaction is encoded as its type byte followed by its
raw_payload, the bytes it was decoded from. One without raw_payload (read with
from_rpc_map/1) raises ArgumentError.
Parameters
transaction- Transaction struct to encode
Returns
binary- RLP encoded transaction with appropriate type envelope
Converts a map (typically from JSON-RPC response) into a Transaction struct.
Handles different field naming conventions and transaction types.
A transaction of a type that is not registered (for example an OP-stack deposit transaction,
type 0x7e) is returned as an Ethers.Transaction.Unknown holding the map, unchanged, in
rpc_map and its block metadata in metadata.
Parameters
tx- Map containing transaction data. Keys can be camelCase strings or atoms. (e.g:chainId,"gasPrice")
Returns
{:ok, transaction}- Converted transaction struct{:error, :invalid_type}- If the transaction type is not a hex quantity{:error, reason}- If transaction fields are missing or invalid (seenew/1)
Creates a new transaction struct with the given parameters.
Type of transaction is determined by the required type field in the params map. Returns
{:error, :missing_type} when it is missing, {:error, :missing_<field>} (e.g.
{:error, :missing_chain_id}) when a field required by the type is missing and
{:error, reason} when a field is invalid.
Examples
iex> Ethers.Transaction.new(%{
...> type: Ethers.Transaction.Eip1559,
...> chain_id: 1,
...> nonce: 0,
...> gas: 21_000,
...> max_fee_per_gas: 30_000_000_000,
...> max_priority_fee_per_gas: 1_000_000_000,
...> to: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1",
...> value: 1
...> })
{:ok,
%Ethers.Transaction.Eip1559{
chain_id: 1,
nonce: 0,
max_priority_fee_per_gas: 1_000_000_000,
max_fee_per_gas: 30_000_000_000,
gas: 21_000,
to: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1",
value: 1,
input: "",
access_list: []
}}
Converts a Transaction struct into a map suitable for JSON-RPC.
A signed transaction is converted to the payload's map plus its signature (r, s,
yParity and v, or only v for legacy transactions) and any block metadata it carries
(blockHash, blockNumber, transactionIndex), so it reads back with from_rpc_map/1.
Parameters
transaction- Transaction struct to convert
Returns
- map containing transaction parameters with RPC field names and "0x" prefixed hex values
Raises ArgumentError if a field holds a value that has no JSON-RPC representation, and for an
Ethers.Transaction.Unknown transaction (one read with from_rpc_map/1 keeps the map in
rpc_map).
Calculates the transaction hash.
Parameters
transaction- Transaction struct to hashformat- Format to return the hash in. Either:hexor:bin. (default::hex)
Returns Either
binary- Transaction hash in binary format (whenformatis:bin)String.t()- Transaction hash in hex format prefixed with "0x" (whenformatis:hex)
The hash of an Ethers.Transaction.Unknown transaction is the keccak hash of its encoding
(see encode/1). One without raw_payload (read with from_rpc_map/1) raises
ArgumentError, the hash reported by the node is in its rpc_map.