Exth is an Elixir client for interacting with EVM-compatible blockchain nodes via JSON-RPC. It provides a robust, type-safe interface for making Ethereum RPC calls.
- π Type Safety: Comprehensive type specs and validation
- π Transport Agnostic: Pluggable transport system (HTTP, WebSocket, IPC)
- π― Smart Defaults: Sensible defaults with full configurability
- π‘οΈ Error Handling: Detailed error reporting and recovery
- π¦ Batch Support: Efficient batch request processing
- π Protocol Compliance: Full JSON-RPC 2.0 specification support
- βοΈ Dynamic Configuration: Flexible configuration through both inline options and application config
Add exth to your list of dependencies in mix.exs:
defdepsdo[{:exth,"~> 0.1.0"},# Optional dependencies:# Mint for Tesla adapter{:mint,"~> 1.7"}]endExth offers two ways to interact with EVM nodes:
- Provider (High-Level): Define a provider module with convenient function names and no need to pass client references.
- RPC Client (Low-Level): Direct client usage with more control, requiring explicit client handling.
# Basic usage with inline configurationdefmoduleMyProviderdouseExth.Provider,otp_app: :your_otp_app,transport_type: :http,rpc_url: "https://YOUR-RPC-URL"end# Dynamic configuration through application config# In your config/config.exs or similar:config:your_otp_app,MyProvider,rpc_url: "https://YOUR-RPC-URL",timeout: 30_000,max_retries: 3# Then in your provider module:defmoduleMyProviderdouseExth.Provider,otp_app: :your_otp_app,transport_type: :httpend# Configuration is merged with inline options taking precedencedefmoduleMyProviderdouseExth.Provider,otp_app: :your_otp_app,transport_type: :http,rpc_url: "https://OVERRIDE-RPC-URL"# This will override the config valueend# Use the provider{:ok,block_number}=MyProvider.block_number(){:ok,balance}=MyProvider.get_balance("0x742d35Cc6634C0532925a3b844Bc454e4438f44e","latest"){:ok,block}=MyProvider.get_block_by_number("0x1",true){:ok,tx_hash}=MyProvider.send_raw_transaction("0x...")The Provider approach is recommended for most use cases as it provides:
- β¨ Clean, intuitive function names
- π Type-safe parameters
- π Better documentation and IDE support
- π― No need to manage client references
- βοΈ Flexible configuration through both inline options and application config
Providers can be configured through both inline options and application config. Inline options take precedence over application config. Here are the available options:
# Required optionstransport_type: :http|:websocket|:ipc|:custom# Transport type to userpc_url: "https://..."# RPC endpoint URL (for HTTP/WebSocket)path: "/tmp/ethereum.ipc"# Socket path (for IPC)# Required inline optionotp_app: :your_otp_app# Application name for config lookup# Custom transport optionsmodule: MyCustomTransport# Required when transport_type is :custom# Optional HTTP optionstimeout: 30_000# Request timeout in millisecondsheaders: [{"header","value"}]# Custom headers for HTTP transportadapter: Tesla.Adapter.Mint# HTTP adapter (defaults to Mint)# Optional WebSocket optionsdispatch_callback: fnresponse->handle_response(response)end# Required for WebSocket# Optional IPC optionspool_size: 10# Connection pool sizesocket_opts: [:binary,active: false,reuseaddr: true]# Socket optionsaliasExth.Rpc# 1. Define a client{:ok,client}=Rpc.new_client(transport_type: :http,rpc_url: "https://YOUR-RPC-URL")# 2.1. Make RPC calls with explicit clientrequest1=Rpc.request(client,"eth_blockNumber",[]){:ok,block_number}=Rpc.send(client,request1)# 2.2. Or make RPC calls without a clientrequest2=Rpc.request("eth_getBalance",["0x742d35Cc6634C0532925a3b844Bc454e4438f44e","latest"]){:ok,balance}=Rpc.send(client,request2)# 3. You can also send multiple requests in one callrequests=[request1,request2]{:ok,responses}=Rpc.send(client,requests)# 4. You can invert the order of the arguments and pipeRpc.request("eth_blockNumber",[])|>Rpc.send(client)# OR[request1,request2]|>Rpc.send(client)Use the RPC Client approach when you need:
- π§ Direct control over RPC calls
- π Dynamic method names
- π οΈ Custom parameter handling
- ποΈ Flexible client management (multiple clients, runtime configuration)
Exth uses a pluggable transport system that supports different communication protocols. Each transport type can be configured with specific options:
The HTTP transport provides robust HTTP/HTTPS communication with configurable middleware:
# Provider configurationdefmoduleMyProviderdouseExth.Provider,transport_type: :http,rpc_url: "https://eth-mainnet.example.com",# Optional HTTP-specific configurationadapter: Tesla.Adapter.Mint,# Default HTTP adapterheaders: [{"authorization","Bearer token"}],timeout: 30_000# Request timeout in msend# Direct client configuration{:ok,client}=Exth.Rpc.new_client(transport_type: :http,rpc_url: "https://eth-mainnet.example.com",adapter: Tesla.Adapter.Mint,headers: [{"authorization","Bearer token"}],timeout: 30_000)HTTP Features:
- Built on Tesla HTTP client with middleware support
- Configurable adapters (Mint, Hackney, etc.)
- Configurable headers and timeouts
- Automatic URL validation and formatting
The WebSocket transport provides full-duplex communication for real-time updates and subscriptions:
# Provider configurationdefmoduleMyProviderdouseExth.Provider,transport_type: :websocket,rpc_url: "wss://eth-mainnet.example.com",dispatch_callback: fnresponse->handle_response(response)endend# Direct client configuration{:ok,client}=Exth.Rpc.new_client(transport_type: :websocket,rpc_url: "wss://eth-mainnet.example.com",dispatch_callback: fnresponse->handle_response(response)end)# Example subscriptionrequest=Rpc.request("eth_subscribe",["newHeads"]){:ok,response}=Rpc.send(client,request)WebSocket Features:
- Full-duplex communication
- Support for subscriptions and real-time updates
- Automatic connection management and lifecycle
- Asynchronous message handling via dispatch callbacks
- Connection state management and supervision
The IPC transport provides communication with local Ethereum nodes via Unix domain sockets:
# Provider configurationdefmoduleMyProviderdouseExth.Provider,transport_type: :ipc,path: "/tmp/ethereum.ipc",# Optional IPC-specific configurationtimeout: 30_000,# Request timeout in mspool_size: 10,# Number of connections in the poolsocket_opts: [:binary,active: false,reuseaddr: true]end# Direct client configuration{:ok,client}=Exth.Rpc.new_client(transport_type: :ipc,path: "/tmp/ethereum.ipc",timeout: 30_000,pool_size: 5)# Make requestsrequest=Rpc.request("eth_blockNumber",[]){:ok,response}=Rpc.send(client,request)IPC Features:
- Unix domain socket communication
- Connection pooling with NimblePool for efficient resource management
- Low latency for local nodes
- Automatic connection lifecycle management
- Note: Only available on Unix-like systems
IPC Configuration Options:
:path- (required) The Unix domain socket path (e.g., "/tmp/ethereum.ipc"):timeout- Request timeout in milliseconds (default: 30,000ms):socket_opts- TCP socket options (default: [:binary, active: false, reuseaddr: true]):pool_size- Number of connections in the pool (default: 10):pool_lazy_workers- Whether to create workers lazily (default: true):pool_worker_idle_timeout- Worker idle timeout (default: nil):pool_max_idle_pings- Maximum idle pings before worker termination (default: -1)
Implement your own transport by creating a module and implementing the
Exth.Transport behaviour:
defmoduleMyCustomTransportdouseExth.Transport@implExth.Transportdefinit(opts)do# Initialize your transport{:ok,transport_state}end@implExth.Transportdefhandle_request(transport_state,request)do# Handle the JSON-RPC request# Return {:ok, response} or {:error, reason}endend# Use your custom transportdefmoduleMyProviderdouseExth.Provider,transport_type: :custom,module: MyCustomTransport,rpc_url: "custom://endpoint",# Additional custom optionscustom_option: "value"end# Direct client configuration{:ok,client}=Exth.Rpc.new_client(transport_type: :custom,module: MyCustomTransport,custom_option: "value")Custom Transport Features:
- Full control over transport implementation
- Custom state management
- Behaviour-based implementation for consistency
Check out our examples directory for practical usage examples.
- Elixir ~> 1.18
- Erlang/OTP 26 or later
- Fork it
- Create your feature branch (
git checkout -b feature/my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/my-new-feature) - Create new Pull Request
This project is licensed under the MIT License. See LICENSE for details.