-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Dynamically generate t_payload typespec * Add EIP-2930 support * Fix typedoc * Improve tests * Update CHANGELOG
- Loading branch information
Showing
4 changed files
with
195 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
defmodule Ethers.Transaction.Eip2930 do | ||
@moduledoc """ | ||
Transaction struct and protocol implementation for Ethereum Improvement Proposal (EIP) 2930 | ||
transactions. EIP-2930 introduced a new transaction type that includes an access list, | ||
allowing transactions to pre-specify and pre-pay for account and storage access to mitigate | ||
gas cost changes from EIP-2929 and prevent contract breakage. The access list format also | ||
enables future use cases like block-wide witnesses and static state access patterns. | ||
See: https://eips.ethereum.org/EIPS/eip-2930 | ||
""" | ||
|
||
alias Ethers.Types | ||
alias Ethers.Utils | ||
|
||
@behaviour Ethers.Transaction | ||
|
||
@type_id 1 | ||
|
||
@enforce_keys [:chain_id, :nonce, :gas_price, :gas] | ||
defstruct [ | ||
:chain_id, | ||
:nonce, | ||
:gas_price, | ||
:gas, | ||
:to, | ||
:value, | ||
:input, | ||
access_list: [] | ||
] | ||
|
||
@typedoc """ | ||
A transaction type following EIP-2930 (Type-1) and incorporating the following fields: | ||
- `chain_id` - chain ID of network where the transaction is to be executed | ||
- `nonce` - sequence number for the transaction from this sender | ||
- `gas_price`: Price willing to pay for each unit of gas (in wei) | ||
- `gas` - maximum amount of gas allowed for transaction execution | ||
- `to` - destination address for transaction, nil for contract creation | ||
- `value` - amount of ether (in wei) to transfer | ||
- `input` - data payload of the transaction | ||
- `access_list` - list of addresses and storage keys to warm up (introduced in EIP-2930) | ||
""" | ||
@type t :: %__MODULE__{ | ||
chain_id: non_neg_integer(), | ||
nonce: non_neg_integer(), | ||
gas_price: non_neg_integer(), | ||
gas: non_neg_integer(), | ||
to: Types.t_address() | nil, | ||
value: non_neg_integer(), | ||
input: binary(), | ||
access_list: [{binary(), [binary()]}] | ||
} | ||
|
||
@impl Ethers.Transaction | ||
def new(params) do | ||
to = params[:to] | ||
|
||
{:ok, | ||
%__MODULE__{ | ||
chain_id: params.chain_id, | ||
nonce: params.nonce, | ||
gas_price: params.gas_price, | ||
gas: params.gas, | ||
to: to && Utils.to_checksum_address(to), | ||
value: params[:value] || 0, | ||
input: params[:input] || params[:data] || "", | ||
access_list: params[:access_list] || [] | ||
}} | ||
end | ||
|
||
@impl Ethers.Transaction | ||
def auto_fetchable_fields do | ||
[:chain_id, :nonce, :gas_price, :gas] | ||
end | ||
|
||
@impl Ethers.Transaction | ||
def type_envelope, do: <<type_id()>> | ||
|
||
@impl Ethers.Transaction | ||
def type_id, do: @type_id | ||
|
||
@impl Ethers.Transaction | ||
def from_rlp_list([ | ||
chain_id, | ||
nonce, | ||
gas_price, | ||
gas, | ||
to, | ||
value, | ||
input, | ||
access_list | rest | ||
]) do | ||
{:ok, | ||
%__MODULE__{ | ||
chain_id: :binary.decode_unsigned(chain_id), | ||
nonce: :binary.decode_unsigned(nonce), | ||
gas_price: :binary.decode_unsigned(gas_price), | ||
gas: :binary.decode_unsigned(gas), | ||
to: (to != "" && Utils.encode_address!(to)) || nil, | ||
value: :binary.decode_unsigned(value), | ||
input: input, | ||
access_list: access_list | ||
}, rest} | ||
end | ||
|
||
def from_rlp_list(_rlp_list), do: {:error, :transaction_decode_failed} | ||
|
||
defimpl Ethers.Transaction.Protocol do | ||
def type_id(_transaction), do: @for.type_id() | ||
|
||
def type_envelope(_transaction), do: @for.type_envelope() | ||
|
||
def to_rlp_list(tx, _mode) do | ||
# Eip2930 does not discriminate in RLP encoding between payload and hash | ||
[ | ||
tx.chain_id, | ||
tx.nonce, | ||
tx.gas_price, | ||
tx.gas, | ||
(tx.to && Utils.decode_address!(tx.to)) || "", | ||
tx.value, | ||
tx.input, | ||
tx.access_list || [] | ||
] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters