Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
- Warn about gas limits above the [EIP-7825](https://eips.ethereum.org/EIPS/eip-7825) cap, enforce a cap with `:max_transaction_gas`
- Add `Ethers.Receipt` struct with decoded transaction receipt fields
- Add `Ethers.Receipt.decode_logs/2` to decode receipt logs with event filters
- Add `Ethers.Event.eth_transfer_log?/1` and `Ethers.Event.system_address/0` to identify [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) ETH transfer logs
- Document that `Transfer` event filters also match EIP-7708 ETH transfer logs

### Bug Fixes

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ iex> Ethers.get_logs(filter)
]}
```

#### Native ETH transfer logs (EIP-7708)

On networks with [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) active (the Glamsterdam
upgrade), every ETH transfer with a non-zero value emits a log from
`0xfffffffffffffffffffffffffffffffffffffffe` that has the same topics and data as an ERC-20
`Transfer` event. A `Transfer` filter without an address returns these logs too, decoded as token
transfers. Filter by the token address or tell them apart with `Ethers.Event.eth_transfer_log?/1`:

```elixir
{:ok, events} = Ethers.get_logs(Ethers.Contracts.ERC20.EventFilters.transfer(nil, nil))
token_transfers = Enum.reject(events, &Ethers.Event.eth_transfer_log?/1)
```

### Resolving Ethereum names (ENS domains) using Ethers

To resolve ENS or any other name service provider (which are ENS compatible) in the blockchain
Expand Down
13 changes: 13 additions & 0 deletions lib/ethers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,19 @@ defmodule Ethers do
- An EventFilters module of a contract (e.g. `MyContract.EventFilters`) to match all
events of that contract.

> #### ETH transfers match `Transfer` filters (EIP-7708) {: .warning}
>
> On networks with [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) active (the
> Glamsterdam upgrade), every ETH transfer with a non-zero value emits a log from
> `0xfffffffffffffffffffffffffffffffffffffffe` with the same topics and data as the
> `Transfer(address,address,uint256)` event of ERC-20 and ERC-777 tokens. A `Transfer` filter
> without an address (neither `:address` nor a default address, e.g.
> `Ethers.Contracts.ERC20.EventFilters.transfer(nil, nil)` or a combined filter including it)
> therefore also returns native ETH transfers, decoded as token transfers.
>
> Pass the token contract as `:address` to only get its transfers, or use
> `Ethers.Event.eth_transfer_log?/1` to tell the ETH transfer logs apart.

## Overrides and Options

- `:address`: Indicates event emitter contract address. (nil means all contracts)
Expand Down
4 changes: 4 additions & 0 deletions lib/ethers/combined_event_filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule Ethers.CombinedEventFilter do
topics and match unintended logs.
- All combined filters must belong to the same address. Filters with conflicting
default addresses cannot be combined since `eth_getLogs` accepts a single address.
- Without an address, a combined filter including the `Transfer(address,address,uint256)`
event (e.g. `Ethers.Contracts.ERC20.EventFilters`) also matches the native ETH transfer
logs of [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708). See `Ethers.get_logs/2` and
`Ethers.Event.eth_transfer_log?/1`.

## Examples

Expand Down
2 changes: 2 additions & 0 deletions lib/ethers/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ defmodule Ethers.Contract do
For each indexed parameter you can either pass in the value you want to
filter or `nil` if you don't want to filter.

#{unquote(document_eth_transfer_logs(abi.selectors))}

#{unquote(document_parameters(abi.selectors))}

#{unquote(document_returns(abi.selectors))}
Expand Down
17 changes: 17 additions & 0 deletions lib/ethers/contract_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Ethers.ContractHelpers do
@moduledoc false

@transfer_signature "Transfer(address,address,uint256)"

@spec read_abi(Keyword.t()) :: {abi :: [...], file_path :: String.t() | nil}
def read_abi(opts) do
case Keyword.take(opts, [:abi, :abi_file]) do
Expand Down Expand Up @@ -163,6 +165,21 @@ defmodule Ethers.ContractHelpers do
"""
end

def document_eth_transfer_logs(selectors) do
if Enum.any?(selectors, &(ABI.FunctionSelector.encode(&1) == @transfer_signature)) do
"""
> #### ETH transfer logs (EIP-7708) {: .warning}
>
> On networks with [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) active, native ETH
> transfers emit logs with this event signature. Without an address (the `:address` option
> or a default address), `Ethers.get_logs/2` can return them as well. See
> `Ethers.Event.eth_transfer_log?/1`.
"""
else
""
end
end

defp document_state_mutabilities(state_mutabilities) do
Enum.map_join(state_mutabilities, " OR ", &"`#{&1}`")
end
Expand Down
48 changes: 48 additions & 0 deletions lib/ethers/event.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule Ethers.Event do
@moduledoc """
EVM Event struct and helpers

## ETH transfer logs (EIP-7708)

On networks with [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) active (the Glamsterdam
upgrade), every ETH transfer with a non-zero value emits a log from `system_address/0`. The log
has the same topics and data layout as the ERC-20 `Transfer(address,address,uint256)` event, so
`Transfer` event filters decode it as a token transfer. Use `eth_transfer_log?/1` to tell these
logs apart.
"""

alias ABI.FunctionSelector
Expand All @@ -10,6 +18,10 @@ defmodule Ethers.Event do
alias Ethers.Types
alias Ethers.Utils

@system_address "0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE"
@system_address_downcased String.downcase(@system_address)
@transfer_topic "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"

defstruct [
:address,
:block_hash,
Expand Down Expand Up @@ -123,6 +135,42 @@ defmodule Ethers.Event do
end
end

@doc """
Returns `SYSTEM_ADDRESS`, the address emitting [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708)
ETH transfer logs, EIP-55 checksummed like the `address` of decoded events.

Pass it as `:address` to `Ethers.get_logs/2` with an ERC-20 `Transfer` event filter to fetch
only native ETH transfers.

## Examples

iex> Ethers.Event.system_address()
"0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE"
"""
@spec system_address() :: Types.t_address()
def system_address, do: @system_address

@doc """
Returns `true` if the event is an [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) ETH
transfer log: a `Transfer(address,address,uint256)` log emitted by `system_address/0`.

Such logs are decoded like ERC-20 `Transfer` events (`topics` holds `from` and `to`, `data`
holds the amount in wei), so use this to keep or drop native ETH transfers in the results of
`Ethers.get_logs/2`.

## Examples

# Token transfers only, without the native ETH transfers
Ethers.Contracts.ERC20.EventFilters.transfer(nil, nil)
|> Ethers.get_logs!(from_block: from_block, to_block: to_block)
|> Enum.reject(&Ethers.Event.eth_transfer_log?/1)
"""
@spec eth_transfer_log?(t()) :: boolean()
def eth_transfer_log?(%__MODULE__{address: address, topics_raw: [topic_0 | _]}) do
String.downcase(address) == @system_address_downcased and
String.downcase(topic_0) == @transfer_topic
end

defp decode_sub_topic(raw, type) when type in [:string, :bytes], do: Utils.hex_encode(raw)
defp decode_sub_topic(raw, {:array, _}), do: Utils.hex_encode(raw)
defp decode_sub_topic(raw, {:array, _, _}), do: Utils.hex_encode(raw)
Expand Down
6 changes: 6 additions & 0 deletions lib/ethers/event_filter.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
defmodule Ethers.EventFilter do
@moduledoc """
Event Filter struct and helper functions to work with the event filters

> #### ETH transfers match `Transfer` filters (EIP-7708) {: .warning}
>
> On networks with [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708) active, native ETH
> transfers emit `Transfer(address,address,uint256)` logs. Filters for that event without an
> address also match them. See `Ethers.get_logs/2` and `Ethers.Event.eth_transfer_log?/1`.
"""

@typedoc """
Expand Down
20 changes: 20 additions & 0 deletions test/ethers/contract_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ defmodule Ethers.ContractHelpersTest do
end
end

describe "document_eth_transfer_logs/1" do
test "warns on Transfer(address,address,uint256) event filters only" do
assert event_filter_doc(Ethers.Contracts.ERC20.EventFilters, :transfer, 2) =~ "EIP-7708"
assert event_filter_doc(Ethers.Contracts.ERC777.EventFilters, :transfer, 2) =~ "EIP-7708"
assert event_filter_doc(Ethers.Contracts.ERC721.EventFilters, :transfer, 3) =~ "EIP-7708"

refute event_filter_doc(Ethers.Contracts.ERC20.EventFilters, :approval, 2) =~ "EIP-7708"
refute event_filter_doc(Ethers.Contracts.ERC777.EventFilters, :sent, 3) =~ "EIP-7708"
end
end

describe "generate_arguments" do
test "works with correct names" do
assert [{:amount, [], _}, {:sender, [], _}] =
Expand Down Expand Up @@ -116,4 +127,13 @@ defmodule Ethers.ContractHelpersTest do
})
end
end

defp event_filter_doc(module, function, arity) do
{:docs_v1, _, _, _, _, _, docs} = Code.fetch_docs(module)

Enum.find_value(docs, fn
{{:function, ^function, ^arity}, _, _, %{"en" => doc}, _} -> doc
_ -> nil
end)
end
end
90 changes: 90 additions & 0 deletions test/ethers/event_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Ethers.EventTest do
use ExUnit.Case
alias Ethers.CombinedEventFilter
alias Ethers.Contracts.ERC20
alias Ethers.Event
doctest Event

Expand Down Expand Up @@ -290,6 +292,75 @@ defmodule Ethers.EventTest do
end
end

describe "EIP-7708 ETH transfer logs" do
@from "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
@to "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"
@token "0xaa107ccfe230a29c345fd97bc6eb9bd2fccd0750"
# As the node returns it
@system_address "0xfffffffffffffffffffffffffffffffffffffffe"

test "system_address/0 is the checksummed EIP-7708 log emitter" do
assert Event.system_address() == "0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE"
assert Event.system_address() == Ethers.Utils.to_checksum_address(@system_address)
end

test "decode/2 decodes an ETH transfer log as an ERC-20 Transfer event" do
%Ethers.EventFilter{selector: selector} = ERC20.EventFilters.transfer(nil, nil)

assert %Event{
address: "0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE",
topics: ["Transfer(address,address,uint256)", @from, @to],
data: [1_000_000_000_000_000_000]
} = event = Event.decode(transfer_log(@system_address), selector)

assert event.address == Event.system_address()
assert Event.eth_transfer_log?(event)
end

test "CombinedEventFilter.decode_logs/2 decodes ETH and token transfer logs alike" do
logs = [transfer_log(@system_address), transfer_log(@token)]

assert [eth_transfer, token_transfer] =
CombinedEventFilter.decode_logs(logs, ERC20.EventFilters.__all__())

for event <- [eth_transfer, token_transfer] do
assert %Event{
topics: ["Transfer(address,address,uint256)", @from, @to],
data: [1_000_000_000_000_000_000]
} = event
end

assert Event.eth_transfer_log?(eth_transfer)
refute Event.eth_transfer_log?(token_transfer)
end

test "eth_transfer_log?/1 ignores the address case" do
%Ethers.EventFilter{selector: selector} = ERC20.EventFilters.transfer(nil, nil)
event = Event.decode(transfer_log(@system_address), selector)

assert Event.eth_transfer_log?(%{event | address: @system_address})
end

test "eth_transfer_log?/1 is false for other events emitted by the system address" do
%Ethers.EventFilter{selector: selector, topics: [approval_topic | _]} =
ERC20.EventFilters.approval(nil, nil)

log =
@system_address
|> transfer_log()
|> Map.update!("topics", fn [_ | sub_topics] -> [approval_topic | sub_topics] end)

refute Event.eth_transfer_log?(Event.decode(log, selector))
end

test "the ETH transfer log topic is the ERC-20 Transfer topic" do
assert %Ethers.EventFilter{topics: [topic_0, nil, nil]} =
ERC20.EventFilters.transfer(nil, nil)

assert [^topic_0 | _] = transfer_log(@system_address)["topics"]
end
end

describe "find_and_decode/2" do
test "returns the emitter address checksummed" do
assert {:ok, %Ethers.Event{address: "0xAA107cCFe230a29C345Fd97bc6eb9Bd2fccD0750"}} =
Expand Down Expand Up @@ -339,4 +410,23 @@ defmodule Ethers.EventTest do
)
end
end

# An RPC log of a 1 ETH transfer from @from to @to, in the EIP-7708 layout
defp transfer_log(emitter) do
%{
"address" => emitter,
"blockHash" => "0xe8885761ec559c5e267c48f44b4b12e4169f7d3a116f5e8f43314147722f0d83",
"blockNumber" => "0x1138b39",
"data" => "0x0000000000000000000000000000000000000000000000000de0b6b3a7640000",
"logIndex" => "0x0",
"removed" => false,
"topics" => [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8"
],
"transactionHash" => "0xf6e06e4f3fbd67088e8278843e55862957537760c63bae7b682a0e39da75b45d",
"transactionIndex" => "0x0"
}
end
end
Loading