Etherex (Etherex v1.0.0-rc19) View Source
Elixir high level library for the Ethereum blockchain. This library is based on the methods of the Json RPC API (https://eth.wiki/json-rpc/api).
All functions returns {:error, :econnrefused}
if no client is
listening in the configured URL.
You can directly use Elixir integers for quantities or atoms for tags. The library encode/decode Elixir primitive data to/from the blockchain.
Compiled code and contract instances are abstracted. The library can
compile code and generate a value of type t:compiled_code/0
and
that representation can be used to deploy the contact and get a
value of contract/0
that can be used in call/5
and
call_transaction/5
.
Some operations allow options. Options are Keyword.t/0
that
accepts optional data accepted by the API (see
https://eth.wiki/json-rpc/api for details when no details are given
in this documentation).
The result of some operations are maps with a direct representation of the returns of the operations in the API (see https://eth.wiki/json-rpc/api for details when no details are given in this documentation).
Link to this section Summary
Functions
Returns a list of addresses owned by client.
Works like accounts/0
but a exception is raised on error.
Returns the number of most recent block.
Works like block_number/0
but a exception is raised on error.
Performs a call to a function of a deployed contract. This call does not generate a transaction in the blockchain.
Works like call/6
but a exception is raised on error.
Performs a call to a function of a deployed contract, This call generates a transaction in the blockchain.
Works like call_transaction/5
but a exception is raised on error.
Returns the current client version.
Works like client_version/0
but a exception is raised on error.
Returns the byte code after compiling Solidity source
code. Parameter source_or_filename
can be the source code or a
filename with the source code.
Works like compile_solidity/1
but a exception is raised on error.
Deploys a contract to the blockchain.
Works like deploy/4
but a exception is raised on error.
Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain. Note that the estimate may be significantly more than the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
Works like estimate_gas/5
but a exception is raised on error.
Generates and returns an estimate the gas cost of a transaction. It
uses estimate_gas/5
and gas_price/0
.
Works like estimate_gas_cost/5
but a exception is raised on error.
Returns the gas cost of a transaction. If the transaction hasn't
been processed yet (is pending), {:ok, nil}
is returned.
Works like gas_cost/1
but a exception is raised on error.
Returns the current price per gas in wei.
Works like gas_price/0
but a exception is raised on error.
Returns the balance of the account of given address.
Works like get_balance/2
but a exception is raised on error.
Returns information about a block by block number or by block hash.
Works like get_block/1
but a exception is raised on error.
Returns the deployed bytecode of a contract.
Returns the address of a contract. If the creation transaction has not been mined yet, nil
is returned.
Works like get_contract_creation_address/1
but a exception is raised on error.
Returns the hash of the creation transaction of a contract.
Returns the contract creator address.
Given a contract and a transaction of the contract, retrieves the
list of events logged in it. Returns []
if no events. Returns
nil
if transaction is pending.
Works like get_events/2
but a exception is raised on error.
Returns the information about a transaction requested by transaction hash.
Works like get_transaction/1
but a exception is raised on error.
Returns the receipt of a transaction by transaction hash. The
receipt is not available for pending transactions and {:ok, nil}
is
returned.
Works like get_transaction_receipt/1
but a exception is raised on error.
Returns true if client is actively listening for network connections, false otherwise.
Works like net_listening/0
but a exception is raised on error.
Returns the current network id.
Works like net_version/0
but a exception is raised on error.
Returns the current ethereum protocol version.
Works like protocol_version/0
but a exception is raised on error.
Returns syncing status.
Works like syncing/0
but a exception is raised on error.
Transfer funds.
Decimal representation of a value given an Ether unit.
String representation of a value given an Ether unit.
Link to this section Types
Specs
address() :: Etherex.Type.address()
Specs
block_parameter() :: Etherex.Type.block_parameter()
Specs
bytecode()
Specs
contract()
Specs
error() :: Etherex.Type.error()
Specs
event() :: Etherex.Type.event()
Specs
hash() :: Etherex.Type.hash()
Specs
hex() :: Etherex.Type.hex()
Specs
opts() :: Etherex.Type.opts()
Specs
quantity() :: Etherex.Type.quantity()
Specs
tag() :: Etherex.Type.tag()
Specs
unit() :: Etherex.Type.unit()
Link to this section Functions
Specs
Returns a list of addresses owned by client.
Specs
accounts!() :: [address()]
Works like accounts/0
but a exception is raised on error.
Specs
Returns the number of most recent block.
Specs
block_number!() :: quantity()
Works like block_number/0
but a exception is raised on error.
call(contract, caller, function, arguments, block \\ :latest, opts \\ [])
View SourceSpecs
call( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), block :: block_parameter(), opts :: Keyword.t() ) :: {:ok, any()} | {:error, error()}
Performs a call to a function of a deployed contract. This call does not generate a transaction in the blockchain.
Examples
iex> owner = List.last(Etherex.accounts!())
iex> {:ok, [value]} = Etherex.compile_solidity!("priv/solidity/Counter.sol")
...> |> Etherex.deploy!(owner, [], gas: 1_000_000)
...> |> Etherex.call(owner, "get", [])
iex> value
0
call!(contract, caller, function, arguments, block \\ :latest, opts \\ [])
View SourceSpecs
call!( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), block :: block_parameter(), opts :: Keyword.t() ) :: any()
Works like call/6
but a exception is raised on error.
call_transaction(contract, caller, function, arguments, opts \\ [])
View SourceSpecs
call_transaction( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), opts :: opts() ) :: {:ok, hash()} | {:error, error()}
Performs a call to a function of a deployed contract, This call generates a transaction in the blockchain.
Examples
iex> owner = List.last(Etherex.accounts!())
iex> {:ok, "0x" <> _} = Etherex.compile_solidity!("priv/solidity/Counter.sol")
...> |> Etherex.deploy!(owner, [], gas: 1_000_000)
...> |> Etherex.call_transaction(owner, "get", [])
call_transaction!(contract, caller, function, arguments, opts \\ [])
View SourceSpecs
call_transaction!( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), opts :: opts() ) :: hash()
Works like call_transaction/5
but a exception is raised on error.
Specs
Returns the current client version.
Specs
client_version!() :: String.t()
Works like client_version/0
but a exception is raised on error.
Specs
Returns the byte code after compiling Solidity source
code. Parameter source_or_filename
can be the source code or a
filename with the source code.
If source code is passed, the function tries to send the code to the client to get the compiled contract.
If a filename is passed, a local compiler is used to compile the code. If no local compiler can be used or compilation fails then the source code in file is sent to the client.
Specs
Works like compile_solidity/1
but a exception is raised on error.
Specs
deploy( contract :: bytecode(), creator :: address(), arguments :: list(), opts :: opts() ) :: {:ok, contract()} | {:error, error()}
Deploys a contract to the blockchain.
Examples
iex> owner = List.last(Etherex.accounts!())
iex> {:ok, _contract} = Etherex.compile_solidity!("priv/solidity/Counter.sol")
...> |> Etherex.deploy(owner, [], gas: 1_000_000)
Specs
deploy!( code :: bytecode(), creator :: address(), arguments :: list(), opts :: opts() ) :: contract()
Works like deploy/4
but a exception is raised on error.
Specs
estimate_gas( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), opts :: opts() ) :: {:ok, any()} | {:error, error()}
Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain. Note that the estimate may be significantly more than the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
Examples
iex> owner = List.last(Etherex.accounts!())
iex> {:ok, estimated_gas} = Etherex.compile_solidity!("priv/solidity/Counter.sol")
...> |> Etherex.deploy!(owner, [], gas: 1_000_000)
...> |> Etherex.estimate_gas(owner, "get", [])
iex> is_integer(estimated_gas)
true
Specs
estimate_gas!( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), opts :: Keyword.t() ) :: any()
Works like estimate_gas/5
but a exception is raised on error.
estimate_gas_cost(contract, caller, function, arguments, opts \\ [])
View SourceSpecs
estimate_gas_cost( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), opts :: opts() ) :: {:ok, any()} | {:error, error()}
Generates and returns an estimate the gas cost of a transaction. It
uses estimate_gas/5
and gas_price/0
.
Examples
iex> owner = List.last(Etherex.accounts!())
iex> {:ok, gas_cost} = Etherex.compile_solidity!("priv/solidity/Counter.sol")
...> |> Etherex.deploy!(owner, [], gas: 1_000_000)
...> |> Etherex.estimate_gas_cost(owner, "get", [])
iex> is_integer(gas_cost)
true
estimate_gas_cost!(contract, caller, function, arguments, opts \\ [])
View SourceSpecs
estimate_gas_cost!( contract :: contract(), caller :: address(), function :: String.t(), arguments :: list(), opts :: Keyword.t() ) :: any()
Works like estimate_gas_cost/5
but a exception is raised on error.
Specs
Returns the gas cost of a transaction. If the transaction hasn't
been processed yet (is pending), {:ok, nil}
is returned.
Specs
Works like gas_cost/1
but a exception is raised on error.
Specs
Returns the current price per gas in wei.
Specs
gas_price!() :: quantity()
Works like gas_price/0
but a exception is raised on error.
Specs
get_balance(address(), block_parameter()) :: {:ok, quantity()} | {:error, error()}
Returns the balance of the account of given address.
Specs
get_balance!(address(), block_parameter()) :: quantity()
Works like get_balance/2
but a exception is raised on error.
Specs
get_block(hash() | block_parameter()) :: {:ok, map()} | {:error, error()}
Returns information about a block by block number or by block hash.
Specs
Works like get_block/1
but a exception is raised on error.
Specs
Returns the deployed bytecode of a contract.
Specs
Returns the address of a contract. If the creation transaction has not been mined yet, nil
is returned.
Specs
Works like get_contract_creation_address/1
but a exception is raised on error.
Specs
Returns the hash of the creation transaction of a contract.
Specs
Returns the contract creator address.
Specs
Given a contract and a transaction of the contract, retrieves the
list of events logged in it. Returns []
if no events. Returns
nil
if transaction is pending.
Specs
Works like get_events/2
but a exception is raised on error.
Specs
Returns the information about a transaction requested by transaction hash.
Specs
Works like get_transaction/1
but a exception is raised on error.
Specs
get_transaction_receipt(Etherex.Type.hash()) :: {:ok, map() | nil} | {:error, Etherex.Type.error()}
Returns the receipt of a transaction by transaction hash. The
receipt is not available for pending transactions and {:ok, nil}
is
returned.
Specs
Works like get_transaction_receipt/1
but a exception is raised on error.
Specs
Returns true if client is actively listening for network connections, false otherwise.
Specs
net_listening!() :: boolean()
Works like net_listening/0
but a exception is raised on error.
Specs
Returns the current network id.
Specs
net_version!() :: String.t()
Works like net_version/0
but a exception is raised on error.
Specs
Returns the current ethereum protocol version.
Specs
protocol_version!() :: String.t()
Works like protocol_version/0
but a exception is raised on error.
Specs
Returns syncing status.
Specs
syncing!() :: map()
Works like syncing/0
but a exception is raised on error.
Specs
transfer( from :: address(), to :: address(), value :: quantity(), opts :: opts() ) :: {:ok, hash()} | {:error, error()}
Transfer funds.
Specs
Decimal representation of a value given an Ether unit.
Examples
iex> Etherex.wei_to_decimal(0)
Decimal.new("0.0")
iex> Etherex.wei_to_decimal(0, :WEI)
Decimal.new("0.0")
iex> Etherex.wei_to_decimal(0, :ETH)
Decimal.new("0.0")
iex> Etherex.wei_to_decimal(20000000000)
Decimal.new("2E+10")
iex> Etherex.wei_to_decimal(20000000000, :WEI)
Decimal.new("2E+10")
iex> Etherex.wei_to_decimal(20000000000, :GWEI)
Decimal.new("20.0")
iex> Etherex.wei_to_decimal(20000000000, :PWEI)
Decimal.new("0.00002")
iex> Etherex.wei_to_decimal(20000000000, :ETH)
Decimal.new("2E-8")
Specs
String representation of a value given an Ether unit.
Examples
iex> Etherex.wei_to_string(0)
"0 WEI"
iex> Etherex.wei_to_string(0, :WEI)
"0 WEI"
iex> Etherex.wei_to_string(0, :GWEI)
"0.000000000 GWEI"
iex> Etherex.wei_to_string(0, :PWEI)
"0.000000000000000 PWEI"
iex> Etherex.wei_to_string(0, :ETH)
"0.000000000000000000 ETH"
iex> Etherex.wei_to_string(20000000000)
"20,000,000,000 WEI"
iex> Etherex.wei_to_string(20000000000, :WEI)
"20,000,000,000 WEI"
iex> Etherex.wei_to_string(20000000000, :GWEI)
"20.000000000 GWEI"
iex> Etherex.wei_to_string(20000000000, :PWEI)
"0.000020000000000 PWEI"
iex> Etherex.wei_to_string(20000000000, :ETH)
"0.000000020000000000 ETH"