### Installation Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Add Ethereumex and a JSON library to your Mix dependencies. ```APIDOC ## Installation Add Ethereumex and a JSON library to your Mix dependencies. ```elixir def deps do [ {:ethereumex, "~> 0.14.0"}, {:jason, "~> 1.4"} ] end ``` ``` -------------------------------- ### Setup Ethereum Ex Project Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Use this command to download parity and initialize the password file for the project. ```bash $ make setup ``` -------------------------------- ### Add Ethereumex and ExAbi Dependencies Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Example of how to add the ethereumex and ex_abi libraries to your Elixir project's dependencies. ```elixir defp deps do [ ..., {:ethereumex, "~> 0.9"}, {:ex_abi, "~> 0.5"} ... ] end ``` -------------------------------- ### Get Account Balance Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Fetches the balance of a given Ethereum account address. ```elixir iex> Ethereumex.HttpClient.eth_get_balance("0x407d73d8a49eeb85d32cf465507dd71d507100c1") {:ok, "0x0"} ``` -------------------------------- ### Get Ethereum Client Version Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Retrieves the version of the Ethereum client. Can specify a custom URL to override configuration. ```elixir 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"} ``` -------------------------------- ### Get storage value at address Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the value from a specific storage position at a given address. ```elixir contract_address = "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC" {:ok, value} = Ethereumex.HttpClient.eth_get_storage_at(contract_address, "0x0", "latest") ``` -------------------------------- ### Get logs for specific contract and topics Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves logs matching the provided filter criteria. ```elixir filter = %{ fromBlock: "0x10d00", toBlock: "latest", address: "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC", topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] } {:ok, logs} = Ethereumex.HttpClient.eth_get_logs(filter) ``` -------------------------------- ### Get contract bytecode Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves the code at a given address. Returns "0x" for non-contract addresses. ```elixir contract_address = "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC" {:ok, code} = Ethereumex.HttpClient.eth_get_code(contract_address) {:ok, code} = Ethereumex.HttpClient.eth_get_code("0x407d73d8a49eeb85d32cf465507dd71d507100c1") ``` -------------------------------- ### Get Account Balance Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Use `eth_get_balance/3` to fetch the balance of an Ethereum address at a specific block. The balance is returned in wei and can be converted to ether. ```elixir # Get balance at latest block (default) address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" {:ok, balance} = Ethereumex.HttpClient.eth_get_balance(address) # => {:ok, "0x0"} # Get balance at specific block {:ok, balance} = Ethereumex.HttpClient.eth_get_balance(address, "0x10d4f") # => {:ok, "0x1bc16d674ec80000"} # Convert wei to ether wei = balance |> String.slice(2..-1) |> String.to_integer(16) ether = wei / 1_000_000_000_000_000_000 # => 2.0 ``` -------------------------------- ### Get Logs Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves logs that match a given filter object. ```elixir # Example for eth_get_logs would go here, but is not provided in the source text. ``` -------------------------------- ### Get recommended max priority fee Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the current recommended max priority fee per gas for EIP-1559 transactions. ```elixir {:ok, priority_fee} = Ethereumex.HttpClient.eth_max_priority_fee_per_gas() ``` -------------------------------- ### Get Transaction by Hash Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Fetches detailed information about a specific transaction using its hash. ```elixir # Get transaction details tx_hash = "0xabc123def456789abc123def456789abc123def456789abc123def456789abc1" {:ok, transaction} = Ethereumex.HttpClient.eth_get_transaction_by_hash(tx_hash) # => {:ok, %{ # "hash" => "0xabc123...", # "from" => "0x407d73d8a49eeb85d32cf465507dd71d507100c1", # "to" => "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC", # "value" => "0x1bc16d674ec80000", # "gas" => "0x5208", # "gasPrice" => "0x3b9aca00", # "nonce" => "0x1a", # "blockNumber" => "0x10d4f", # "blockHash" => "0x9f8a..." # }} ``` -------------------------------- ### Get Transaction Count (Nonce) Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Use `eth_get_transaction_count/3` to get the number of transactions sent from an address. Specify 'latest' for confirmed transactions or 'pending' for the next transaction's nonce. ```elixir # Get nonce for address address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" {:ok, nonce} = Ethereumex.HttpClient.eth_get_transaction_count(address, "latest") # => {:ok, "0x1a"} # Use pending for next transaction {:ok, pending_nonce} = Ethereumex.HttpClient.eth_get_transaction_count(address, "pending") # => {:ok, "0x1b"} ``` -------------------------------- ### Get Ethereum Chain ID Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Use `eth_chain_id/1` to get the chain ID of the connected network. The ID is returned as a hex string and can be converted to an integer. ```elixir # Get the chain ID {:ok, chain_id} = Ethereumex.HttpClient.eth_chain_id() # => {:ok, "0x1"} # Mainnet # Convert to integer chain = chain_id |> String.slice(2..-1) |> String.to_integer(16) # => 1 ``` -------------------------------- ### Get historical gas fee data Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns historical gas fee data for EIP-1559 transactions. ```elixir {:ok, fee_history} = Ethereumex.HttpClient.eth_fee_history( "0xa", # block_count: 10 blocks "latest", # newest_block [25, 50, 75] # reward_percentiles ) ``` -------------------------------- ### Get Transaction Receipt Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves the receipt of a transaction, including status, gas used, and logs, by its hash. ```elixir # Get transaction receipt tx_hash = "0xabc123def456789abc123def456789abc123def456789abc123def456789abc1" {:ok, receipt} = Ethereumex.HttpClient.eth_get_transaction_receipt(tx_hash) # => {:ok, %{ # "transactionHash" => "0xabc123...", # "blockNumber" => "0x10d4f", # "blockHash" => "0x9f8a...", # "status" => "0x1", # "gasUsed" => "0x5208", # "logs" => [%{"address" => "0x...", "topics" => [...], "data" => "0x..."}], # "contractAddress" => nil # }} ``` -------------------------------- ### Get Block by Number Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Fetches block information by its number. Set the second argument to `true` to include full transaction objects. ```elixir # Get block with transaction hashes only {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number("0x10d4f") # => {:ok, %{ # "number" => "0x10d4f", # "hash" => "0x9f8a...", # "parentHash" => "0x7b8c...", # "transactions" => ["0xabc...", "0xdef..."], # "gasUsed" => "0x5208", # "timestamp" => "0x64a12345" # }} # Get block with full transaction objects {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number("0x10d4f", true) # => {:ok, %{ # "number" => "0x10d4f", # "transactions" => [ # %{"hash" => "0xabc...", "from" => "0x123...", "to" => "0x456...", "value" => "0x1"} # ] # }} ``` -------------------------------- ### Get Current Gas Price Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieve the current gas price in wei using `eth_gas_price/1`. The result can be converted to Gwei for easier readability. ```elixir # Get current gas price {:ok, gas_price} = Ethereumex.HttpClient.eth_gas_price() # => {:ok, "0x3b9aca00"} # Convert to Gwei gas_wei = gas_price |> String.slice(2..-1) |> String.to_integer(16) gas_gwei = gas_wei / 1_000_000_000 # => 1.0 ``` -------------------------------- ### Get Block by Hash Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves block details using its hash. The second argument, when `true`, includes full transaction objects. ```elixir # Get block by hash block_hash = "0x9f8a0e7b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f" {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_hash(block_hash, true) # => {:ok, %{ # "number" => "0x10d4f", # "hash" => "0x9f8a...", # "transactions" => [%{...}] # }} ``` -------------------------------- ### Get Latest Block Number Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieve the number of the most recent block using `eth_block_number/1`. The result is a hex string that can be converted to an integer. ```elixir # Get current block number {:ok, block_number} = Ethereumex.HttpClient.eth_block_number() # => {:ok, "0x10d4f"} # Convert hex to integer block_num = block_number |> String.slice(2..-1) |> String.to_integer(16) # => 68943 ``` -------------------------------- ### Get Block by Number Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieve block information using `eth_get_block_by_number/3`. You can specify 'latest' for the most recent block and choose whether to include full transaction objects. ```elixir # Get block without full transaction objects {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number("latest", false) # => {:ok, %{ # "number" => "0x10d4f", ``` -------------------------------- ### Run Ethereum Ex Project Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Execute this command to run the parity client for the project. ```bash $ make run ``` -------------------------------- ### Run Tests for Ethereum Ex Project Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Use this command to execute the test suite for the project. ```bash $ make test ``` -------------------------------- ### Add Ethereumex and Jason to Mix Dependencies Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Include Ethereumex and a JSON library like Jason in your Mix dependencies to use the client. ```elixir def deps do [ {:ethereumex, "~> 0.14.0"}, {:jason, "~> 1.4"} ] end ``` -------------------------------- ### Add Ethereumex to Mix Dependencies Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Add the :ethereumex package and a JSON library (like :jason) to your project's dependencies in `mix.exs`. ```elixir def deps do [ {:ethereumex, "~> 0.14.0"}, # json library is configurable {:jason, "~> 1.4"} ] end ``` -------------------------------- ### eth_get_code Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the bytecode at a given address. ```APIDOC ## eth_get_code ### Description Returns the contract bytecode at a given address. Returns '0x' if no code exists at the address. ### Method POST ### Endpoint eth_get_code ### Parameters - **address** (string) - Required - The contract address to query. ``` -------------------------------- ### Configure WebSocket Connection Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Set the WebSocket URL and client type in `config/config.exs` to enable WebSocket connections. ```elixir config :ethereumex, websocket_url: "ws://localhost:8545", client_type: :websocket ``` -------------------------------- ### WebSocket Client Configuration Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Configure WebSocket connection for real-time subscriptions and persistent connections to Ethereum nodes. ```APIDOC ## WebSocket Client Configuration Configure WebSocket connection for real-time subscriptions and persistent connections to Ethereum nodes. ```elixir # config/config.exs config :ethereumex, client_type: :websocket, websocket_url: "ws://localhost:8545" ``` ``` -------------------------------- ### HTTP Client Configuration Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Configure the HTTP client with endpoint URL and optional timeout settings in your config file. ```APIDOC ## HTTP Client Configuration Configure the HTTP client with endpoint URL and optional timeout settings in your config file. ```elixir # config/config.exs config :ethereumex, url: "http://localhost:8545", http_options: [pool_timeout: 5000, receive_timeout: 15_000], http_headers: [{"Content-Type", "application/json"}], enable_request_error_logs: false ``` ``` -------------------------------- ### Configure WebSocket Client Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Configure the WebSocket client by providing the WebSocket URL. This is used for real-time event subscriptions and persistent connections. ```elixir config :ethereumex, client_type: :websocket, websocket_url: "ws://localhost:8545" ``` -------------------------------- ### Configure HTTP Client Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Configure the HTTP client with the node's endpoint URL and optional timeout settings in your config file. Ensure the Content-Type header is set to application/json. ```elixir config :ethereumex, url: "http://localhost:8545", http_options: [pool_timeout: 5000, receive_timeout: 15_000], http_headers: [{"Content-Type", "application/json"}], enable_request_error_logs: false ``` -------------------------------- ### Configure HTTP Request Options Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Customize HTTP request timeouts and headers in `config/config.exs`. These can be overridden in client calls. ```elixir config :ethereumex, http_options: [pool_timeout: 5000, receive_timeout: 15_000], http_headers: [{"Content-Type", "application/json"}] ``` -------------------------------- ### Subscribe to New Block Headers via WebSocket Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Initiate a subscription to new block headers using `Ethereumex.WebsocketClient.subscribe(:newHeads)`. The subscription ID is returned. ```elixir # Subscribe to new block headers iex> {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:newHeads) {:ok, "0x9cef478923ff08bf67fde6c64013158d"} ``` -------------------------------- ### web3_client_version/1 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the current client version of the connected Ethereum node. ```APIDOC ## web3_client_version/1 Returns the current client version of the connected Ethereum node. ### Example ```elixir # Get the client version {:ok, version} = Ethereumex.HttpClient.web3_client_version() # => {:ok, "Geth/v1.10.26-stable/linux-amd64/go1.18.5"} # With custom URL option {:ok, version} = Ethereumex.HttpClient.web3_client_version(url: "http://mainnet.example.com:8545") # => {:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"} ``` ``` -------------------------------- ### Configure HTTP Connection URL Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Set the Ethereum JSON-RPC host URL in `config/config.exs` for HTTP connections. ```elixir config :ethereumex, url: "http://localhost:8545" ``` -------------------------------- ### Configure IPC Path Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Specify the path to the IPC socket file in `config/config.exs` when using the IPC client type. ```elixir config :ethereumex, ipc_path: "/path/to/ipc" ``` -------------------------------- ### Configure Custom JSON Library Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Replace the default JSON encoder/decoder with a custom implementation. ```elixir # config/config.exs config :ethereumex, json_module: MyCustomJson # Your custom module must implement these functions: defmodule MyCustomJson do def encode(term), do: {:ok, encoded_string} def encode!(term), do: encoded_string def decode(string, opts \ []), do: {:ok, decoded_term} def decode!(string, opts \ []), do: decoded_term end ``` -------------------------------- ### Configure IPC Client Type Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Set the client type to `:ipc` in `config/config.exs` to enable Inter-Process Communication. ```elixir config :ethereumex, client_type: :ipc ``` -------------------------------- ### Integrate Telemetry Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Attach handlers to monitor RPC calls for observability. ```elixir # Attach to all RPC methods :telemetry.attach( "ethereumex-handler", [:ethereumex], fn [:ethereumex], %{counter: 1}, %{method_name: method}, _config -> Statix.increment("ethereum.rpc.#{method}") end, nil ) # Or attach to specific methods :telemetry.attach( "eth-call-handler", [:ethereumex, :eth_call], fn [:ethereumex, :eth_call], %{counter: 1}, _metadata, _config -> Statix.increment("ethereum.rpc.eth_call") end, nil ) ``` -------------------------------- ### IPC Client Configuration Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Configure IPC connection for local node communication with Unix socket path and worker pool settings. ```APIDOC ## IPC Client Configuration Configure IPC connection for local node communication with Unix socket path and worker pool settings. ```elixir # config/config.exs config :ethereumex, client_type: :ipc, ipc_path: "/path/to/geth.ipc", ipc_worker_size: 5, ipc_max_worker_overflow: 2, ipc_request_timeout: 60_000 ``` ``` -------------------------------- ### Manage log filters Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Creates, polls, and uninstalls a log filter for specific events. ```elixir filter_params = %{ fromBlock: "latest", address: "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC", topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] } {:ok, filter_id} = Ethereumex.HttpClient.eth_new_filter(filter_params) {:ok, logs} = Ethereumex.HttpClient.eth_get_filter_changes(filter_id) {:ok, true} = Ethereumex.HttpClient.eth_uninstall_filter(filter_id) ``` -------------------------------- ### Configure IPC Client Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Configure the IPC client for local node communication by specifying the Unix socket path and worker pool settings. Adjust worker size and overflow for performance. ```elixir config :ethereumex, client_type: :ipc, ipc_path: "/path/to/geth.ipc", ipc_worker_size: 5, ipc_max_worker_overflow: 2, ipc_request_timeout: 60_000 ``` -------------------------------- ### Use IPC Client for Local Communication Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Communicate with a local node via Unix socket for improved performance. ```elixir # All HttpClient methods are available on IpcClient {:ok, version} = Ethereumex.IpcClient.web3_client_version() # => {:ok, "Geth/v1.10.26-stable/linux-amd64/go1.18.5"} {:ok, balance} = Ethereumex.IpcClient.eth_get_balance("0x407d73d8a49eeb85d32cf465507dd71d507100c1") # => {:ok, "0x1bc16d674ec80000"} # Batch requests work the same way requests = [ {:eth_block_number, []}, {:eth_gas_price, []} ] {:ok, results} = Ethereumex.IpcClient.batch_request(requests) ``` -------------------------------- ### Hash Data with Keccak-256 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Use `web3_sha3/2` to compute the Keccak-256 hash of a hex-encoded string. Ensure the input is correctly formatted; otherwise, an error will be returned. ```elixir # Hash a hex-encoded string {:ok, hash} = Ethereumex.HttpClient.web3_sha3("0x68656c6c6f20776f726c64") # => {:ok, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"} # Invalid parameter returns an error {:error, error} = Ethereumex.HttpClient.web3_sha3("wrong_param") # => {:error, %{"code" => -32602, "message" => "Invalid params: invalid format."}} ``` -------------------------------- ### Make Standard RPC Call via WebSocket Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Execute standard JSON-RPC methods like `eth_block_number` using the `Ethereumex.WebsocketClient`. ```elixir iex> Ethereumex.WebsocketClient.eth_block_number() {:ok, "0x1234"} ``` -------------------------------- ### batch_request Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Executes multiple RPC calls in a single HTTP request. ```APIDOC ## batch_request ### Description Sends multiple JSON-RPC calls in a single HTTP request for improved efficiency. ### Method POST ### Endpoint batch_request ### Request Body - **requests** (array) - Required - A list of tuples containing the method name and arguments. ``` -------------------------------- ### Configure Custom JSON Module Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Override the default JSON library (e.g., `jason`) by specifying a custom module that implements `encode/1` and `decode/2` (and their `!/1` variants) in `config/config.exs`. ```elixir config :ethereumex, json_module: MyCustomJson ``` -------------------------------- ### Receive WebSocket Notifications Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Handle incoming subscription notifications by using `receive` to match the expected message format, which includes the subscription ID and the result. ```elixir # Receive notifications in your process receive do %{ "method" => "eth_subscription", "params" => %{ "subscription" => subscription_id, "result" => result } } -> handle_notification(result) end ``` -------------------------------- ### Execute Batch Requests Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Perform multiple RPC calls in a single request using the HttpClient. ```elixir requests = [ {:eth_get_balance, ["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]}, {:eth_get_transaction_count, ["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]} ] {:ok, [balance_result, nonce_result]} = Ethereumex.HttpClient.batch_request(requests) ``` -------------------------------- ### eth_get_balance/3 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the balance of an account at a specified block. ```APIDOC ## eth_get_balance/3 Returns the balance of an account at a specified block. ### Example ```elixir # Get balance at latest block (default) address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" {:ok, balance} = Ethereumex.HttpClient.eth_get_balance(address) # => {:ok, "0x0"} # Get balance at specific block {:ok, balance} = Ethereumex.HttpClient.eth_get_balance(address, "0x10d4f") # => {:ok, "0x1bc16d674ec80000"} # Convert wei to ether wei = balance |> String.slice(2..-1) |> String.to_integer(16) ether = wei / 1_000_000_000_000_000_000 # => 2.0 ``` ``` -------------------------------- ### Execute batch requests Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Sends multiple RPC calls in a single HTTP request for efficiency. ```elixir requests = [ {:web3_client_version, []}, {:net_version, []}, {:eth_block_number, []}, {:eth_gas_price, []}, {:web3_sha3, ["0x68656c6c6f20776f726c64"]} ] {:ok, results} = Ethereumex.HttpClient.batch_request(requests) ``` -------------------------------- ### web3_sha3/2 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the Keccak-256 hash of the given data. ```APIDOC ## web3_sha3/2 Returns the Keccak-256 hash of the given data. ### Example ```elixir # Hash a hex-encoded string {:ok, hash} = Ethereumex.HttpClient.web3_sha3("0x68656c6c6f20776f726c64") # => {:ok, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"} # Invalid parameter returns an error {:error, error} = Ethereumex.HttpClient.web3_sha3("wrong_param") # => {:error, %{"code" => -32602, "message" => "Invalid params: invalid format."}} ``` ``` -------------------------------- ### Use WebSocket Client for Standard RPC Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Perform standard RPC calls over a persistent WebSocket connection. ```elixir # Standard RPC methods work identically to HttpClient {:ok, block_number} = Ethereumex.WebsocketClient.eth_block_number() # => {:ok, "0x10d4f"} {:ok, balance} = Ethereumex.WebsocketClient.eth_get_balance("0x407d73d8a49eeb85d32cf465507dd71d507100c1") # => {:ok, "0x1bc16d674ec80000"} {:ok, block} = Ethereumex.WebsocketClient.eth_get_block_by_number("latest", true) # => {:ok, %{...}} ``` -------------------------------- ### Make Custom JSON-RPC Request Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Uses the generic `request/3` method to call non-standard JSON-RPC methods, such as parity's personal_listAccounts. ```elixir iex> Ethereumex.HttpClient.request("personal_listAccounts", [], []) {:ok, ["0x71cf0b576a95c347078ec2339303d13024a26910", "0x7c12323a4fff6df1a25d38319d5692982f48ec2e"]} ``` -------------------------------- ### Subscribe to Contract Logs Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Monitor specific contract events using filters. ```elixir # Subscribe to Transfer events from a specific contract filter = %{ address: "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd", topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] } {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:logs, filter) # => {:ok, "0x4a8a4c0517381924f9838102c5a4dcb7"} # Receive log notifications receive do %{ "method" => "eth_subscription", "params" => %{ "subscription" => ^subscription_id, "result" => %{ "address" => "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd", "topics" => ["0xddf252ad...", "0x000...from", "0x000...to"], "data" => "0x...", "blockNumber" => "0x10d50" } } } -> IO.puts("Transfer event detected!") end {:ok, true} = Ethereumex.WebsocketClient.unsubscribe(subscription_id) ``` -------------------------------- ### Execute custom JSON-RPC requests Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Executes non-standard or node-specific methods. ```elixir {:ok, accounts} = Ethereumex.HttpClient.request("personal_listAccounts", [], []) {:ok, trace} = Ethereumex.HttpClient.request( "debug_traceTransaction", ["0xabc123...", %{tracer: "callTracer"}], [] ) ``` -------------------------------- ### Subscribe to Contract Logs via WebSocket Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Subscribe to contract events or logs by providing an address and topics filter to `Ethereumex.WebsocketClient.subscribe(:logs, filter)`. The subscription ID is returned. ```elixir # 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 New Block Headers Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Receive real-time notifications for new blocks via WebSocket. ```elixir # Subscribe to new block headers {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:newHeads) # => {:ok, "0x9cef478923ff08bf67fde6c64013158d"} # Receive notifications in your process receive do %{ "method" => "eth_subscription", "params" => %{ "subscription" => "0x9cef478923ff08bf67fde6c64013158d", "result" => %{ "number" => "0x10d50", "hash" => "0x9f8a...", "parentHash" => "0x7b8c...", "timestamp" => "0x64a12345" } } } -> IO.puts("New block received!") end # Unsubscribe when done {:ok, true} = Ethereumex.WebsocketClient.unsubscribe(subscription_id) ``` -------------------------------- ### Manage block filters Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Creates, polls, and uninstalls a filter for new block notifications. ```elixir {:ok, filter_id} = Ethereumex.HttpClient.eth_new_block_filter() {:ok, block_hashes} = Ethereumex.HttpClient.eth_get_filter_changes(filter_id) {:ok, true} = Ethereumex.HttpClient.eth_uninstall_filter(filter_id) ``` -------------------------------- ### eth_gas_price/1 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the current gas price in wei. ```APIDOC ## eth_gas_price/1 Returns the current gas price in wei. ### Example ```elixir # Get current gas price {:ok, gas_price} = Ethereumex.HttpClient.eth_gas_price() # => {:ok, "0x3b9aca00"} # Convert to Gwei gas_wei = gas_price |> String.slice(2..-1) |> String.to_integer(16) gas_gwei = gas_wei / 1_000_000_000 # => 1.0 ``` ``` -------------------------------- ### Perform Contract Call Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Executes a read-only smart contract function call. For complex calls, use `ex_abi` for encoding. ```elixir # Simple contract call call_params = %{ to: "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC", data: "0x70a08231000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" } {:ok, result} = Ethereumex.HttpClient.eth_call(call_params) # => {:ok, "0x0000000000000000000000000000000000000000000000001bc16d674ec80000"} # Call at specific block {:ok, result} = Ethereumex.HttpClient.eth_call(call_params, "0x10d4f") # Using ex_abi for encoding (recommended approach) # Add {:ex_abi, "~> 0.5"} to deps 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) {:ok, balance_hex} = Ethereumex.HttpClient.eth_call(%{ data: "0x" <> abi_encoded_data, to: contract_address }) # => {:ok, "0x0000000000000000000000000000000000000000000000001bc16d674ec80000"} ``` -------------------------------- ### Send Batch JSON-RPC Requests Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Sends multiple JSON-RPC requests simultaneously using `batch_request/1` and displays the aggregated results. ```elixir requests = [ {:web3_client_version, []}, {:net_version, []}, {:web3_sha3, ["0x68656c6f20776f726c64"]} ] Ethereumex.HttpClient.batch_request(requests) {:ok, [ {:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"}, {:ok, "42"}, {:ok, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"} ]} ``` -------------------------------- ### Access Mining and Account Information Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Methods for retrieving node-specific data including account addresses, mining status, hashrate, and coinbase address. ```elixir # Get accounts (requires node to have accounts) {:ok, accounts} = Ethereumex.HttpClient.eth_accounts() # => {:ok, ["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]} # Check mining status {:ok, is_mining} = Ethereumex.HttpClient.eth_mining() # => {:ok, false} # Get hashrate {:ok, hashrate} = Ethereumex.HttpClient.eth_hashrate() # => {:ok, "0x0"} # Get coinbase address {:ok, coinbase} = Ethereumex.HttpClient.eth_coinbase() # => {:ok, "0x407d73d8a49eeb85d32cf465507dd71d507100c1"} ``` -------------------------------- ### Attach to Telemetry Events Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Monitor RPC calls by attaching to `[:ethereumex]` or `[:ethereumex, ]` telemetry events. Events carry a counter and method name metadata. ```elixir Emitted event: `{:event, [:ethereumex], %{counter: 1}, %{method_name: "method_name"}}` or more granular `{:event, [:ethereumex, :method_name_as_atom], %{counter: 1}, %{}}` ``` -------------------------------- ### eth_get_logs Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves logs for a specific contract and set of topics. ```APIDOC ## eth_get_logs ### Description Retrieves logs based on a filter object containing block ranges, contract address, and topics. ### Method POST ### Endpoint eth_get_logs ### Request Body - **filter** (object) - Required - Filter criteria including fromBlock, toBlock, address, and topics. ### Response #### Success Response (200) - **logs** (array) - List of log objects matching the filter criteria. ``` -------------------------------- ### Subscribe to Pending Transactions Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Track incoming pending transactions. ```elixir # Subscribe to pending transactions {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:newPendingTransactions) # => {:ok, "0x1234567890abcdef1234567890abcdef"} # Receive pending transaction hashes receive do %{ "method" => "eth_subscription", "params" => %{ "subscription" => ^subscription_id, "result" => tx_hash } } -> IO.puts("Pending transaction: #{tx_hash}") end # Unsubscribe from multiple subscriptions at once {:ok, true} = Ethereumex.WebsocketClient.unsubscribe([subscription_id, other_subscription_id]) ``` -------------------------------- ### Send Raw Transaction Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Broadcasts a pre-signed raw transaction to the Ethereum network. ```elixir # Send pre-signed transaction signed_tx = "0xf86c0a8502540be40082520894..." {:ok, tx_hash} = Ethereumex.HttpClient.eth_send_raw_transaction(signed_tx) # => {:ok, "0xabc123def456789abc123def456789abc123def456789abc123def456789abc1"} ``` -------------------------------- ### Execute eth_call for Smart Contract Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Executes a read-only smart contract method call using the eth_call JSON-RPC method. ```elixir balance_bytes = Ethereumex.HttpClient.eth_call(%{ data: "0x" <> abi_encoded_data, to: contract_address }) ``` -------------------------------- ### Handle Invalid Parameters Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Demonstrates the error response when invalid parameters are provided to a JSON-RPC method. ```elixir iex> Ethereumex.HttpClient.web3_sha3("wrong_param") {:error, %{"code" => -32602, "message" => "Invalid params: invalid format."}} ``` -------------------------------- ### eth_get_block_by_number/3 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns information about a block by block number. ```APIDOC ## eth_get_block_by_number/3 Returns information about a block by block number. ### Example ```elixir # Get block without full transaction objects {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number("latest", false) # => {:ok, %{ # "number" => "0x10d4f", ``` ``` -------------------------------- ### Manage pending transaction filters Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Creates, polls, and uninstalls a filter for pending transaction notifications. ```elixir {:ok, filter_id} = Ethereumex.HttpClient.eth_new_pending_transaction_filter() {:ok, tx_hashes} = Ethereumex.HttpClient.eth_get_filter_changes(filter_id) {:ok, true} = Ethereumex.HttpClient.eth_uninstall_filter(filter_id) ``` -------------------------------- ### Send Transaction Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Creates and broadcasts a new transaction to the Ethereum network. Requires an unlocked account on the node. ```elixir # Send ether transaction transaction = %{ from: "0x407d73d8a49eeb85d32cf465507dd71d507100c1", to: "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC", value: "0x1bc16d674ec80000", gas: "0x5208", gasPrice: "0x3b9aca00" } {:ok, tx_hash} = Ethereumex.HttpClient.eth_send_transaction(transaction) # => {:ok, "0xabc123def456789abc123def456789abc123def456789abc123def456789abc1"} ``` -------------------------------- ### Subscribe to Pending Transactions via WebSocket Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Subscribe to pending transaction hashes using `Ethereumex.WebsocketClient.subscribe(:newPendingTransactions)`. The subscription ID is returned. ```elixir # Subscribe to pending transactions iex> {:ok, subscription_id} = Ethereumex.WebsocketClient.subscribe(:newPendingTransactions) {:ok, "0x1234567890abcdef1234567890abcdef"} ``` -------------------------------- ### Decode Smart Contract Balance Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Converts the raw byte response from eth_call into a human-readable integer balance. ```elixir balance_bytes |> String.slice(2..-1) |> Base.decode16!(case: :lower) |> TypeDecoder.decode_raw([{:uint, 256}]) |> List.first ``` -------------------------------- ### eth_fee_history Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns historical gas fee data for EIP-1559 transactions. ```APIDOC ## eth_fee_history ### Description Returns historical gas fee data for EIP-1559 transactions over a specified number of blocks. ### Method POST ### Endpoint eth_fee_history ### Parameters - **block_count** (string) - Required - Number of blocks to retrieve history for. - **newest_block** (string) - Required - The block number or tag to start from. - **reward_percentiles** (array) - Required - List of reward percentiles to calculate. ``` -------------------------------- ### Query Network Status Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Methods for retrieving network-level information such as ID, listening status, peer count, and synchronization state. ```elixir # Get network ID {:ok, network_id} = Ethereumex.HttpClient.net_version() # => {:ok, "1"} # Mainnet # Check if node is listening for connections {:ok, is_listening} = Ethereumex.HttpClient.net_listening() # => {:ok, true} # Get peer count {:ok, peer_count} = Ethereumex.HttpClient.net_peer_count() # => {:ok, "0x19"} # Check sync status {:ok, syncing} = Ethereumex.HttpClient.eth_syncing() # => {:ok, false} # or sync status map when syncing ``` -------------------------------- ### Configure IPC Worker Pool Size Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Adjust the worker pool size and overflow for IPC connections in `config/config.exs`. The request timeout can also be set. ```elixir config :ethereumex, ipc_worker_size: 5, ipc_max_worker_overflow: 2, ipc_request_timeout: 60_000 ``` -------------------------------- ### eth_send_transaction Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Submits a new transaction to the network for processing. ```APIDOC ## eth_send_transaction ### Description Creates and sends a new transaction (requires unlocked account on node). ### Method POST ### Endpoint eth_send_transaction ### Parameters #### Request Body - **transaction** (object) - Required - Contains from, to, value, gas, and gasPrice. ### Response #### Success Response (200) - **tx_hash** (string) - The hash of the submitted transaction. ``` -------------------------------- ### Unsubscribe from WebSocket Subscription Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Cancel an active subscription by calling `Ethereumex.WebsocketClient.unsubscribe/1` with the subscription ID. ```elixir # Unsubscribe when done iex> Ethereumex.WebsocketClient.unsubscribe(subscription_id) {:ok, true} ``` -------------------------------- ### eth_get_block_by_hash Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves block information from the blockchain using its unique hash. ```APIDOC ## eth_get_block_by_hash ### Description Returns information about a block by block hash. ### Method POST ### Endpoint eth_get_block_by_hash ### Parameters #### Request Body - **block_hash** (string) - Required - The hash of the block. - **full_tx** (boolean) - Required - If true, returns full transaction objects; if false, returns transaction hashes. ### Response #### Success Response (200) - **block** (object) - The block object containing details like number, hash, and transactions. ``` -------------------------------- ### eth_call Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Executes a read-only smart contract call without creating a transaction on the blockchain. ```APIDOC ## eth_call ### Description Executes a read-only smart contract call without creating a transaction. ### Method POST ### Endpoint eth_call ### Parameters #### Request Body - **call_params** (object) - Required - Contains 'to' (contract address) and 'data' (encoded function call). - **block_number** (string) - Optional - The block number to execute the call at. ### Response #### Success Response (200) - **result** (string) - The hex-encoded return value of the contract call. ``` -------------------------------- ### eth_get_transaction_by_hash Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Retrieves details of a specific transaction using its hash. ```APIDOC ## eth_get_transaction_by_hash ### Description Returns the information about a transaction by transaction hash. ### Method POST ### Endpoint eth_get_transaction_by_hash ### Parameters #### Request Body - **tx_hash** (string) - Required - The hash of the transaction. ### Response #### Success Response (200) - **transaction** (object) - The transaction object including from, to, value, gas, and block information. ``` -------------------------------- ### eth_get_transaction_count/3 Source: https://context7.com/mana-ethereum/ethereumex/llms.txt Returns the number of transactions sent from an address (nonce). ```APIDOC ## eth_get_transaction_count/3 Returns the number of transactions sent from an address (nonce). ### Example ```elixir # Get nonce for address address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" {:ok, nonce} = Ethereumex.HttpClient.eth_get_transaction_count(address, "latest") # => {:ok, "0x1a"} # Use pending for next transaction {:ok, pending_nonce} = Ethereumex.HttpClient.eth_get_transaction_count(address, "pending") # => {:ok, "0x1b"} ``` ``` -------------------------------- ### Encode Smart Contract Call Data Source: https://github.com/mana-ethereum/ethereumex/blob/master/README.md Encodes a smart contract method call with its arguments into ABI-encoded hexadecimal data. ```elixir 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) ```