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
Reverse of prepare_arg/2
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
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"}
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"
Encode to hex with 0x prefix.
examples
Examples
iex> Ethers.Utils.hex_encode("ethers_ex")
"0x6574686572735f6578"
Converts a hexadecimal integer to integer form
examples
Examples
iex> Ethers.Utils.hex_to_integer("0x11111")
{:ok, 69905}
Same as hex_to_integer/1
but raises on error
examples
Examples
iex> Ethers.Utils.hex_to_integer!("0x11111")
69905
@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"
Converts integer to its hexadecimal form
examples
Examples
iex> Ethers.Utils.integer_to_hex(69905)
"0x11111"
Adds gas limit estimation to the parameters if not already exists
@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