View Source Ethers.Utils (Ethers v0.0.2)

Utilities for interacting with ethereum blockchain

Link to this section Summary

Functions

Convert WEI to ETH

Decode from hex with (or without) 0x prefix.

Same as hex_decode/1 but raises on error

Encode to hex with 0x prefix.

Converts a hexadecimal integer to integer form

Same as hex_to_integer/1 but raises on error

Converts integer to its hexadecimal form

Adds gas limit estimation to the parameters if not already exists

Converts human readable argument to the form required for ABI encoding.

Converts ETH to WEI

Link to this section Functions

@spec from_wei(non_neg_integer()) :: float()

Convert WEI to ETH

examples

Examples

iex> Ethers.Utils.from_wei(1000000000000000000)
1.0

iex> Ethers.Utils.from_wei(3140000000000000000)
3.14
@spec hex_decode(String.t()) :: {:ok, binary()} | :error

Decode from hex with (or without) 0x prefix.

examples

Examples

iex> Ethers.Utils.hex_decode("0x6574686572735f6578")
{:ok, "ethers_ex"}

iex> Ethers.Utils.hex_decode("6574686572735f6578")
{:ok, "ethers_ex"}
@spec hex_decode!(String.t()) :: binary() | no_return()

Same as hex_decode/1 but raises on error

examples

Examples

iex> Ethers.Utils.hex_decode!("0x6574686572735f6578")
"ethers_ex"

iex> Ethers.Utils.hex_decode!("6574686572735f6578")
"ethers_ex"
Link to this function

hex_encode(bin, include_prefix \\ true)

View Source

Encode to hex with 0x prefix.

examples

Examples

iex> Ethers.Utils.hex_encode("ethers_ex")
"0x6574686572735f6578"
@spec hex_to_integer(String.t()) :: {:ok, integer()} | {:error, :invalid_hex}

Converts a hexadecimal integer to integer form

examples

Examples

iex> Ethers.Utils.hex_to_integer("0x11111")
{:ok, 69905}
Link to this function

hex_to_integer!(encoded)

View Source
@spec hex_to_integer!(String.t()) :: integer() | no_return()

Same as hex_to_integer/1 but raises on error

examples

Examples

iex> Ethers.Utils.hex_to_integer!("0x11111")
69905
Link to this function

human_arg(argument, arg2)

View Source
@spec human_arg(term(), ABI.FunctionSelector.type()) :: term()

Reverse of prepare_arg/2

examples

Examples

iex> Ethers.Utils.human_arg(<<192, 42, 170, 57, 178, 35, 254, 141, 10, 14, 92, 79, 39, 
...> 234, 217, 8, 60, 117, 108, 194>>, :address)
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" 
@spec integer_to_hex(integer()) :: String.t()

Converts integer to its hexadecimal form

examples

Examples

iex> Ethers.Utils.integer_to_hex(69905)
"0x11111"
Link to this function

maybe_add_gas_limit(params, opts \\ [])

View Source

Adds gas limit estimation to the parameters if not already exists

Link to this function

prepare_arg(argument, arg2)

View Source
@spec prepare_arg(term(), ABI.FunctionSelector.type()) :: term()

Converts human readable argument to the form required for ABI encoding.

For example the addresses in Ethereum are represented by hex strings in human readable format but are in 160-bit binaries in ABI form.

examples

Examples

iex> Ethers.Utils.prepare_arg("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", :address)
<<192, 42, 170, 57, 178, 35, 254, 141, 10, 14, 92, 79, 39, 234, 217, 8, 60, 117, 108, 194>> 
@spec to_wei(number()) :: non_neg_integer()

Converts ETH to WEI

examples

Examples

iex> Ethers.Utils.to_wei(1)
1000000000000000000

iex> Ethers.Utils.to_wei(3.14)
3140000000000000000