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 sizen
bits, where0 < n <= 256
andInteger.mod(n, 8) == 0
.{:int, n}
- Two’s complement integer of sizen
bits, where0 < n <= 256
andInteger.mod(n, 8) == 0
:address
- Ethereum address (20 bytes).{:bytes, n}
-n
bytes where0 < 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
Link to this section Types
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}