eth_event v0.1.1 EthEvent.Schema behaviour View Source

Behaviour to define Solidity like events e.g:

In Solidity you would declare an event as:

Transfer(address indexed from, address indexed to, uint value)

Using this library, you would do the equivalent as follows:

defmodule Transfer do
  use EthEvent.Schema

  event "Transfer" do
    address :from
    address :to
    uint :value
  end
end

Then is possible to query the Transfer(address,address,uint256) event as follows:

> {:ok, results} = Transfer.query(%Transfer{}, [from_block: 0, to_block: 100])

By default the options Keyword list is [] and from_block and to_block are set to "latest".

In order to filter the results, the indexed fields can be used:

> Transfer.query(%Transfer{from: "0x93ecb3962981e1ba2928297cb09c1932aa2c9c51"})

The previous query would search for all the Transfers events from the address "0x93ecb3962981e1ba2928297cb09c1932aa2c9c51" in the "latest" block.

In essence the queries will return a list of the following struct:

%Transfer{
  address: "0xd09de8b6b510aecd508a22811398f468e75c8c4d", # Contract address
  block_hash: "0x15feeab052b4bd65c8e3a2e3efab391debb9d8b5def6ced89ea7727f26790bd8",
  block_number: 42,
  index: 0, # Index of the log in the block
  type: "mined",
  ...
  from: "0x93ecb3962981e1ba2928297cb09c1932aa2c9c51",
  to: "0x1e529de18f95ad5a4f41ac5e159fa307d5a85967",
  value: 100
}

Link to this section Summary

Functions

Adds the necessary macros and functions to define an Ethereum event. Receives a list of options. The only option available is :method and expects a string with the JSON RPC call name for the Ethereum node API e.g

Build the parameters for an Ethereum event

Builds the results as an event

Copies the header of an event to the module event

Receives the name of the event and the definition block. Accepts bool/1, address/1, uint/1, int/1, uint<M>/1 and int<M> where 0 < <M> <= 256 and Integer.mod(<M>, 8) == 0, and bytes<N> where 0 < <N> <= 32

Callbacks

Callback to do the appropriate modifications to the query parameters to request an event with a list of options

Callback to build a result from an defined event

Link to this section Types

Link to this section Functions

Link to this macro __using__(options) View Source (macro)

Adds the necessary macros and functions to define an Ethereum event. Receives a list of options. The only option available is :method and expects a string with the JSON RPC call name for the Ethereum node API e.g:

defmodule Balance do
  use EthEvent.Schema, method: "eth_getBalance"

  event "Balance" do
    address :address
  end

  (...)
end
Link to this function build_query(event, options) View Source
build_query(t(), Keyword.t()) :: {:ok, term()} | {:error, term()}

Build the parameters for an Ethereum event.

The available options are:

  • from_block - Block number from which it will search for logs of the event. Defaults to "latest".
  • to_block - Block number until it will search for logs of the event. Defaults to "latest".

To look for specific topics, add them as values for the indexed arguments in the event struct.

Link to this function build_result(event, results) View Source
build_result(t(), term()) :: {:ok, t()} | {:error, term()}

Builds the results as an event.

Link to this function copy_header(module, event) View Source
copy_header(module(), map() | t() | list()) :: t()

Copies the header of an event to the module event.

Link to this macro event(name, list) View Source (macro)

Receives the name of the event and the definition block. Accepts bool/1, address/1, uint/1, int/1, uint<M>/1 and int<M> where 0 < <M> <= 256 and Integer.mod(<M>, 8) == 0, and bytes<N> where 0 < <N> <= 32.

event "Approval" do
  address :owner, indexed: true
  address :spender, indexed: true
  uint :amount
end

Link to this section Callbacks

Link to this callback build_query(event, options) View Source
build_query(event :: t(), options :: list()) ::
  {:ok, {t(), map()}} | {:error, term()}

Callback to do the appropriate modifications to the query parameters to request an event with a list of options.

Link to this callback build_result(event, result) View Source
build_result(event :: t(), result :: term()) ::
  {:ok, t()} | {:error, term()}

Callback to build a result from an defined event.