Skip to content

Commit

Permalink
Added support for EIP-7702 transactions following the approach used b…
Browse files Browse the repository at this point in the history
…y 1559.
  • Loading branch information
rojotek committed Feb 13, 2025
1 parent b96e883 commit 4c1e10c
Show file tree
Hide file tree
Showing 3 changed files with 785 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/eth/tx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require "eth/chain"
require "eth/tx/eip1559"
require "eth/tx/eip2930"
require "eth/tx/eip7702"
require "eth/tx/legacy"
require "eth/unit"

Expand Down Expand Up @@ -72,6 +73,9 @@ class ParameterError < TypeError; end
# The EIP-1559 transaction type is 2.
TYPE_1559 = 0x02.freeze

# The EIP-7702 transaction type is 4.
TYPE_7702 = 0x04.freeze

# The zero byte is 0x00.
ZERO_BYTE = "\x00".freeze

Expand All @@ -80,6 +84,8 @@ class ParameterError < TypeError; end

# Creates a new transaction of any type for given parameters and chain ID.
# Required parameters are (optional in brackets):
# - EIP-7702: chain_id, nonce, priority_fee, max_gas_fee, gas_limit, authorizations(, from, to,
# value, data, access_list)
# - EIP-1559: chain_id, nonce, priority_fee, max_gas_fee, gas_limit(, from, to,
# value, data, access_list)
# - EIP-2930: chain_id, nonce, gas_price, gas_limit, access_list(, from, to,
Expand All @@ -90,6 +96,12 @@ class ParameterError < TypeError; end
# @param chain_id [Integer] the EIP-155 Chain ID (legacy transactions only).
def new(params, chain_id = Chain::ETHEREUM)

# if we deal with authorizations, attempt EIP-7702
unless params[:authorization_list].nil?
params[:chain_id] = chain_id if params[:chain_id].nil?
return Tx::Eip7702.new params
end

# if we deal with max gas fee parameter, attempt EIP-1559
unless params[:max_gas_fee].nil?
params[:chain_id] = chain_id if params[:chain_id].nil?
Expand All @@ -114,8 +126,14 @@ def new(params, chain_id = Chain::ETHEREUM)
# @raise [TransactionTypeError] if the transaction type is unknown.
def decode(hex)
hex = Util.remove_hex_prefix hex
puts hex[0, 2].to_i(16)
type = hex[0, 2].to_i(16)

case type
when TYPE_7702

# EIP-1559 transaction (type 2)
return Tx::Eip7702.decode hex
when TYPE_1559

# EIP-1559 transaction (type 2)
Expand All @@ -142,6 +160,8 @@ def decode(hex)
# @raise [TransactionTypeError] if the transaction type is unknown.
def unsigned_copy(tx)
case tx.type
when TYPE_7702
return Tx::Eip7702.unsigned_copy tx
when TYPE_1559

# EIP-1559 transaction (type 2)
Expand Down
Loading

0 comments on commit 4c1e10c

Please sign in to comment.