Ethereumex
View SourceElixir JSON-RPC client for the Ethereum blockchain.
Check out the documentation here.
Installation
Add :ethereumex to your list of dependencies in mix.exs:
def deps do
[
{:ethereumex, "~> 0.12.2"},
# json library is configurable
{:jason, "~> 1.4"}
]
endConfiguration
HTTP
In config/config.exs, add Ethereum protocol host params to your config file
config :ethereumex,
url: "http://localhost:8545"You can also configure the HTTP request timeout for requests sent to the Ethereum JSON-RPC
(you can also overwrite this configuration in opts used when calling the client).
config :ethereumex,
http_options: [pool_timeout: 5000, receive_timeout: 15_000],
http_headers: [{"Content-Type", "application/json"}]:pool_timeout - This timeout is applied when we check out a connection from the pool. Default value is 5_000.
:receive_timeout - The maximum time to wait for a response before returning an error. Default value is 15_000
:enable_request_error_logs - Optional request error logs. Default value is false
IPC
If you want to use IPC you will need to set a few things in your config.
First, specify the :client_type:
config :ethereumex,
client_type: :ipcThis will resolve to :http by default.
Second, specify the :ipc_path:
config :ethereumex,
ipc_path: "/path/to/ipc"The IPC client type mode opens a pool of connection workers (default is 5 and 2, respectively). You can configure the pool size.
config :ethereumex,
ipc_worker_size: 5,
ipc_max_worker_overflow: 2,
ipc_request_timeout: 60_000WebSocket
The WebSocket client supports both standard JSON-RPC requests and real-time subscriptions. To use it, configure your application with:
config :ethereumex,
websocket_url: "ws://localhost:8545",
client_type: :websocketStandard RPC Calls
All standard RPC methods work the same as with HTTP:
iex> Ethereumex.WebsocketClient.eth_block_number()
{:ok, "0x1234"}Real-time Subscriptions
Subscribe to various blockchain events:
# Subscribe to new block headers
iex> {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:newHeads)
{:ok, "0x9cef478923ff08bf67fde6c64013158d"}
# Subscribe to logs/events from specific contracts
iex> filter = %{
...> address: "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd",
...> topics: ["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"]
...> }
iex> {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:logs, filter)
{:ok, "0x4a8a4c0517381924f9838102c5a4dcb7"}
# Subscribe to pending transactions
iex> {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:newPendingTransactions)
{:ok, "0x1234567890abcdef1234567890abcdef"}
# Receive notifications in your process
receive do
%{
"method" => "eth_subscription",
"params" => %{
"subscription" => subscription_id,
"result" => result
}
} -> handle_notification(result)
end
# Unsubscribe when done
iex> Ethereumex.WebsocketClient.unsubscribe(subscription_id)
{:ok, true}Available subscription types:
:newHeads- New block headers:logs- Contract events/logs with optional filtering:newPendingTransactions- Pending transaction hashes
Telemetry
If you want to count the number of RPC calls per RPC method or overall,
you can attach yourself to executed telemetry events.
There are two events you can attach yourself to:
[:ethereumex] # has RPC method name in metadata
Emitted event: {:event, [:ethereumex], %{counter: 1}, %{method_name: "method_name"}}
or more granular
[:ethereumex, <rpc_method>] # %{} metadata
Emitted event: {:event, [:ethereumex, :method_name_as_atom], %{counter: 1}, %{}}
Each event caries a single ticker that you can pass into your counters (like Statix.increment/2).
Be sure to add :telemetry as project dependency.
Json library
The default json library is set to jason but that can be overridden with a different module. The module should implement functions encode/1, decode/2, encode!/, decode!/1
config :ethereumex, json_module: MyCustomJsonTest
Download parity and initialize the password file
$ make setup
Run parity
$ make run
Run tests
$ make test
Usage
Available methods:
web3_clientVersionweb3_sha3net_versionnet_peerCountnet_listeningeth_protocolVersioneth_syncingeth_coinbaseeth_chainIdeth_miningeth_hashrateeth_gasPriceeth_accountseth_blockNumbereth_getBalanceeth_getStorageAteth_getTransactionCounteth_getBlockTransactionCountByHasheth_getBlockTransactionCountByNumbereth_getUncleCountByBlockHasheth_getUncleCountByBlockNumbereth_getCodeeth_signeth_sendTransactioneth_sendRawTransactioneth_calleth_estimateGaseth_getBlockByHasheth_getBlockByNumbereth_getTransactionByHasheth_getTransactionByBlockHashAndIndexeth_getTransactionByBlockNumberAndIndexeth_getTransactionReceipteth_getUncleByBlockHashAndIndexeth_getUncleByBlockNumberAndIndexeth_getCompilerseth_compileLLLeth_compileSolidityeth_compileSerpenteth_newFiltereth_newBlockFiltereth_newPendingTransactionFiltereth_uninstallFiltereth_getFilterChangeseth_getFilterLogseth_getLogs- eth_getProof
eth_getWorketh_submitWorketh_submitHashratedb_putStringdb_getStringdb_putHexdb_getHexshh_postshh_versionshh_newIdentityshh_hasIdentityshh_newGroupshh_addToGroupshh_newFiltershh_uninstallFiltershh_getFilterChangesshh_getMessages
WebSocket Subscription Methods
eth_subscribe- Subscribe to real-time eventseth_unsubscribe- Unsubscribe from events
IpcClient
You can follow along with any of these examples using IPC by replacing HttpClient with IpcClient.
Examples
iex> Ethereumex.HttpClient.web3_client_version
{:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"}
# Using the url option will overwrite the configuration
iex> Ethereumex.HttpClient.web3_client_version(url: "http://localhost:8545")
{:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"}
iex> Ethereumex.HttpClient.web3_sha3("wrong_param")
{:error, %{"code" => -32602, "message" => "Invalid params: invalid format."}}
iex> Ethereumex.HttpClient.eth_get_balance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
{:ok, "0x0"}Note that all method names are snakecases, so, for example, shh_getMessages method has corresponding Ethereumex.HttpClient.shh_get_messages/1 method. Signatures can be found in Ethereumex.Client.Behaviour. There are more examples in tests.
eth_call example - Read only smart contract calls
In order to call a smart contract using the JSON-RPC interface you need to properly hash the data attribute (this will need to include the contract method signature along with arguments if any). You can do this manually or use a hex package like ABI to parse your smart contract interface or encode individual calls.
defp deps do
[
...
{:ethereumex, "~> 0.9"},
{:ex_abi, "~> 0.5"}
...
]
endNow load the ABI and pass the method signature. Note that the address needs to be converted to bytes:
address = "0xF742d4cE7713c54dD701AA9e92101aC42D63F895" |> String.slice(2..-1) |> Base.decode16!(case: :mixed)
contract_address = "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC"
abi_encoded_data = ABI.encode("balanceOf(address)", [address]) |> Base.encode16(case: :lower)Now you can use eth_call to execute this smart contract command:
balance_bytes = Ethereumex.HttpClient.eth_call(%{
data: "0x" <> abi_encoded_data,
to: contract_address
})To convert the balance into an integer:
balance_bytes
|> String.slice(2..-1)
|> Base.decode16!(case: :lower)
|> TypeDecoder.decode_raw([{:uint, 256}])
|> List.firstCustom requests
Many Ethereum protocol implementations support additional JSON-RPC API methods. To use them, you should call Ethereumex.HttpClient.request/3 method.
For example, let's call parity's personal_listAccounts method.
iex> Ethereumex.HttpClient.request("personal_listAccounts", [], [])
{:ok,
["0x71cf0b576a95c347078ec2339303d13024a26910",
"0x7c12323a4fff6df1a25d38319d5692982f48ec2e"]}Batch requests
To send batch requests use Ethereumex.HttpClient.batch_request/1 or Ethereumex.HttpClient.batch_request/2 method.
requests = [
{:web3_client_version, []},
{:net_version, []},
{:web3_sha3, ["0x68656c6c6f20776f726c64"]}
]
Ethereumex.HttpClient.batch_request(requests)
{
:ok,
[
{:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"},
{:ok, "42"},
{:ok, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}
]
}Built on Ethereumex
If you are curious what others are building with ethereumex, you might want to take a look at these projects:
- ethers - Interacting with EVM contracts like first-class Elixir functions similar to Ethers.js
Contributing
- Fork it!
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Copyright and License
Copyright (c) 2018 Ayrat Badykov
Released under the MIT License, which can be found in the repository in LICENSE.md.