eth_event v0.1.1 EthEvent.Decode View Source

Module with the basic functions to decode event’s data.

The available types are:

  • :bool - Boolean type (1 byte).
  • {:uint, n} - Unsigned integer of size n bits, where 0 < n <= 256 and Integer.mod(n, 8) == 0.
  • {:int, n} - Two’s complement integer of size n bits, where 0 < n <= 256 and Integer.mod(n, 8) == 0
  • :address - Ethereum address (20 bytes).
  • {:bytes, n} - n bytes where 0 < n <= 32.

Decode packed values

When given a string with packed data, use the function EthEvent.Decode.decode/2 to decode it to the correct types e.g:

> data = "0x6481"
> types = [{:uint, 8}, {:int, 8}]
> EthEvent.Decode.decode(types, data)
{[100, -1], nil}

The data contains two numbers 0x64 as uint8 (decimal 100) and 0x81 as int8 (decimal -1).

Cast values

When given a string, use the function EthEvent.Decode.cast/2 to cast the data to the correct type e.g:

> data = "0x0000000000000000000000000000000000000000000000000000000000000081"
> EthEvent.Decode.cast({:int, 8}, data)
{:ok, -1}

The function ignores the zero padding and converts 0x81 to the decimal -1.

Link to this section Summary

Functions

Casts a value to the given type

Decodes data according to a list of types

Link to this section Types

Link to this type basic_type() View Source
basic_type() ::
  :bool
  | :address
  | {:uint, integer()}
  | {:int, integer()}
  | {:bytes, integer()}
Link to this type basic_types() View Source
basic_types() :: [basic_type()]

Link to this section Functions

Link to this function cast(type, value) View Source
cast(basic_type(), binary()) :: {:ok, term()} | :error

Casts a value to the given type.

> data = "0x0000000000000000000000000000000000000000000000000000000000000081"
> EthEvent.Decode.cast({:int, 8}, data)
{:ok, -1}
Link to this function decode(types, data) View Source
decode(basic_types(), binary()) :: {list(), term()} | :error

Decodes data according to a list of types.

> data = "0x6481"
> types = [{:uint, 8}, {:int, 8}]
> EthEvent.Decode.decode(types, data)
{[100, -1], nil}