Ethers.Event (Ethers v1.0.0)

Copy Markdown View Source

EVM Event struct and helpers

ETH transfer logs (EIP-7708)

On networks with 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.

Summary

Types

t()

A decoded event log.

Functions

Decodes a log entry with the given Event function selector and returns an Event struct

Returns true if the event is an EIP-7708 ETH transfer log: a Transfer(address,address,uint256) log emitted by system_address/0.

Given a log entry and an EventFilters module (e.g. Ethers.Contracts.ERC20.EventFilters) will find a matching event filter in the given module and decodes the log using the event selector.

Returns SYSTEM_ADDRESS, the address emitting EIP-7708 ETH transfer logs, EIP-55 checksummed like the address of decoded events.

Types

t()

@type t() :: %Ethers.Event{
  address: Ethers.Types.t_address(),
  block_hash: Ethers.Types.t_hash(),
  block_number: non_neg_integer(),
  data: [term()],
  data_raw: String.t(),
  log_index: non_neg_integer(),
  removed: boolean(),
  topics: [term(), ...],
  topics_raw: [String.t(), ...],
  transaction_hash: Ethers.Types.t_hash(),
  transaction_index: non_neg_integer()
}

A decoded event log.

address is the EIP-55 checksummed address of the contract which emitted the log, and the addresses decoded into topics and data are checksummed as well. topics_raw and data_raw hold the log values as the node returned them.

Functions

decode(log, selector)

@spec decode(map(), ABI.FunctionSelector.t()) :: t()

Decodes a log entry with the given Event function selector and returns an Event struct

The emitter address is returned EIP-55 checksummed. A value which is not a well-formed address is kept as the node returned it.

eth_transfer_log?(event)

@spec eth_transfer_log?(t()) :: boolean()

Returns true if the event is an 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)

find_and_decode(log, event_filters_module)

@spec find_and_decode(map(), module()) :: {:ok, t()} | {:error, :not_found}

Given a log entry and an EventFilters module (e.g. Ethers.Contracts.ERC20.EventFilters) will find a matching event filter in the given module and decodes the log using the event selector.

system_address()

@spec system_address() :: Ethers.Types.t_address()

Returns SYSTEM_ADDRESS, the address emitting 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"