|
| 1 | +defmodule Ethers.Transaction.Eip4844 do |
| 2 | + @moduledoc """ |
| 3 | + Transaction struct and protocol implementation for Ethereum Improvement Proposal (EIP) 4844 |
| 4 | + transactions. EIP-4844 introduced "blob-carrying transactions" which contain a large amount |
| 5 | + of data that cannot be accessed by EVM execution, but whose commitment can be accessed. |
| 6 | +
|
| 7 | + See: https://eips.ethereum.org/EIPS/eip-4844 |
| 8 | + """ |
| 9 | + |
| 10 | + alias Ethers.Types |
| 11 | + alias Ethers.Utils |
| 12 | + |
| 13 | + @behaviour Ethers.Transaction |
| 14 | + |
| 15 | + @type_id 3 |
| 16 | + |
| 17 | + @enforce_keys [ |
| 18 | + :chain_id, |
| 19 | + :nonce, |
| 20 | + :max_priority_fee_per_gas, |
| 21 | + :max_fee_per_gas, |
| 22 | + :gas, |
| 23 | + :max_fee_per_blob_gas |
| 24 | + ] |
| 25 | + defstruct [ |
| 26 | + :chain_id, |
| 27 | + :nonce, |
| 28 | + :max_priority_fee_per_gas, |
| 29 | + :max_fee_per_gas, |
| 30 | + :gas, |
| 31 | + :to, |
| 32 | + :value, |
| 33 | + :input, |
| 34 | + :max_fee_per_blob_gas, |
| 35 | + access_list: [], |
| 36 | + blob_versioned_hashes: [] |
| 37 | + ] |
| 38 | + |
| 39 | + @typedoc """ |
| 40 | + A transaction type following EIP-4844 (Type-3) and incorporating the following fields: |
| 41 | + - `chain_id` - chain ID of network where the transaction is to be executed |
| 42 | + - `nonce` - sequence number for the transaction from this sender |
| 43 | + - `max_priority_fee_per_gas` - maximum fee per gas (in wei) to give to validators as priority fee (introduced in EIP-1559) |
| 44 | + - `max_fee_per_gas` - maximum total fee per gas (in wei) willing to pay (introduced in EIP-1559) |
| 45 | + - `gas` - maximum amount of gas allowed for transaction execution |
| 46 | + - `to` - destination address for transaction, nil for contract creation |
| 47 | + - `value` - amount of ether (in wei) to transfer |
| 48 | + - `input` - data payload of the transaction |
| 49 | + - `access_list` - list of addresses and storage keys to warm up (introduced in EIP-2930) |
| 50 | + - `max_fee_per_blob_gas` - maximum fee per blob gas (in wei) willing to pay (introduced in EIP-4844) |
| 51 | + - `blob_versioned_hashes` - list of versioned hashes of the blobs (introduced in EIP-4844) |
| 52 | + """ |
| 53 | + @type t :: %__MODULE__{ |
| 54 | + chain_id: non_neg_integer(), |
| 55 | + nonce: non_neg_integer(), |
| 56 | + max_priority_fee_per_gas: non_neg_integer(), |
| 57 | + max_fee_per_gas: non_neg_integer(), |
| 58 | + gas: non_neg_integer(), |
| 59 | + to: Types.t_address() | nil, |
| 60 | + value: non_neg_integer(), |
| 61 | + input: binary(), |
| 62 | + access_list: [{binary(), [binary()]}], |
| 63 | + max_fee_per_blob_gas: non_neg_integer(), |
| 64 | + blob_versioned_hashes: [{binary(), [binary()]}] |
| 65 | + } |
| 66 | + |
| 67 | + @impl Ethers.Transaction |
| 68 | + def new(params) do |
| 69 | + to = params[:to] |
| 70 | + |
| 71 | + {:ok, |
| 72 | + %__MODULE__{ |
| 73 | + chain_id: params.chain_id, |
| 74 | + nonce: params.nonce, |
| 75 | + max_priority_fee_per_gas: params.max_priority_fee_per_gas, |
| 76 | + max_fee_per_gas: params.max_fee_per_gas, |
| 77 | + gas: params.gas, |
| 78 | + to: to && Utils.to_checksum_address(to), |
| 79 | + value: params[:value] || 0, |
| 80 | + input: params[:input] || params[:data] || "", |
| 81 | + access_list: params[:access_list] || [], |
| 82 | + max_fee_per_blob_gas: params.max_fee_per_blob_gas, |
| 83 | + blob_versioned_hashes: params[:blob_versioned_hashes] || [] |
| 84 | + }} |
| 85 | + end |
| 86 | + |
| 87 | + @impl Ethers.Transaction |
| 88 | + def auto_fetchable_fields do |
| 89 | + [:chain_id, :nonce, :max_priority_fee_per_gas, :max_fee_per_gas, :gas, :max_fee_per_blob_gas] |
| 90 | + end |
| 91 | + |
| 92 | + @impl Ethers.Transaction |
| 93 | + def type_envelope, do: <<type_id()>> |
| 94 | + |
| 95 | + @impl Ethers.Transaction |
| 96 | + def type_id, do: @type_id |
| 97 | + |
| 98 | + @impl Ethers.Transaction |
| 99 | + def from_rlp_list([ |
| 100 | + chain_id, |
| 101 | + nonce, |
| 102 | + max_priority_fee_per_gas, |
| 103 | + max_fee_per_gas, |
| 104 | + gas, |
| 105 | + to, |
| 106 | + value, |
| 107 | + input, |
| 108 | + access_list, |
| 109 | + max_fee_per_blob_gas, |
| 110 | + blob_versioned_hashes |
| 111 | + | rest |
| 112 | + ]) do |
| 113 | + {:ok, |
| 114 | + %__MODULE__{ |
| 115 | + chain_id: :binary.decode_unsigned(chain_id), |
| 116 | + nonce: :binary.decode_unsigned(nonce), |
| 117 | + max_priority_fee_per_gas: :binary.decode_unsigned(max_priority_fee_per_gas), |
| 118 | + max_fee_per_gas: :binary.decode_unsigned(max_fee_per_gas), |
| 119 | + gas: :binary.decode_unsigned(gas), |
| 120 | + to: (to != "" && Utils.encode_address!(to)) || nil, |
| 121 | + value: :binary.decode_unsigned(value), |
| 122 | + input: input, |
| 123 | + access_list: access_list, |
| 124 | + max_fee_per_blob_gas: :binary.decode_unsigned(max_fee_per_blob_gas), |
| 125 | + blob_versioned_hashes: blob_versioned_hashes |
| 126 | + }, rest} |
| 127 | + end |
| 128 | + |
| 129 | + def from_rlp_list(_rlp_list), do: {:error, :transaction_decode_failed} |
| 130 | + |
| 131 | + defimpl Ethers.Transaction.Protocol do |
| 132 | + def type_id(_transaction), do: @for.type_id() |
| 133 | + |
| 134 | + def type_envelope(_transaction), do: @for.type_envelope() |
| 135 | + |
| 136 | + def to_rlp_list(tx, _mode) do |
| 137 | + # Eip4844 requires Eip1559 fields |
| 138 | + [ |
| 139 | + tx.chain_id, |
| 140 | + tx.nonce, |
| 141 | + tx.max_priority_fee_per_gas, |
| 142 | + tx.max_fee_per_gas, |
| 143 | + tx.gas, |
| 144 | + (tx.to && Utils.decode_address!(tx.to)) || "", |
| 145 | + tx.value, |
| 146 | + tx.input, |
| 147 | + tx.access_list || [], |
| 148 | + tx.max_fee_per_blob_gas, |
| 149 | + tx.blob_versioned_hashes || [] |
| 150 | + ] |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments