View Source Ethers.Transaction behaviour (Ethers v0.6.3)
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 (legacy, EIP-1559, etc.)
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.
Decodes a raw transaction from a binary or hex-encoded string.
Encodes a transaction for network transmission following EIP-155/EIP-1559.
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()
EVM Transaction type
@type t_payload() :: Ethers.Transaction.Legacy.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.
Parameters
params
- Updated Transaction paramsopts
- Options to pass to the RPC client
Returns
{:ok, params}
- Filled transaction struct{: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 both legacy and typed transactions (EIP-1559, etc).
Parameters
raw_transaction
- Raw transaction data as a binary or hex string starting with "0x"
Returns
{:ok, transaction}
- Decoded transaction struct{:error, reason}
- Error decoding transaction
Encodes a transaction for network transmission following EIP-155/EIP-1559.
Handles both legacy and EIP-1559 transaction types, including signature data if present.
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.
Parameters
tx
- Map containing transaction data. Keys can be snakeCase strings or atoms. (e.g:chainId
,"gasPrice"
)
Returns
{:ok, transaction}
- Converted transaction struct{:error, :unsupported_type}
- If transaction type is not supported
Creates a new transaction struct with the given parameters.
Type of transaction is determined by the type
field in the params map or defaults to EIP-1559.
Examples
iex> Ethers.Transaction.new(%{type: Ethers.Transaction.Eip1559, from: "0x123...", to: "0x456...", value: "0x0"})
{:ok, %Ethers.Transaction.Eip1559{from: "0x123...", to: "0x456...", value: "0x0"}}
Converts a Transaction struct into a map suitable for JSON-RPC.
Parameters
transaction
- Transaction struct to convert
Returns
- map containing transaction parameters with RPC field names and "0x" prefixed hex values
Calculates the transaction hash.
Parameters
transaction
- Transaction struct to hashformat
- Format to return the hash in. Either:hex
or:bin
. (default::hex
)
Returns Either
binary
- Transaction hash in binary format (whenformat
is:bin
)String.t()
- Transaction hash in hex format prefixed with "0x" (whenformat
is:hex
)