Behaviour of the JSON-RPC clients Ethers sends its requests with.
Ethers talks to a node through an RPC client module. The default one sends requests over HTTP with Ethereumex. Implement this behaviour to use another transport, to route requests between several endpoints, or to add retries, rate limiting or instrumentation.
Using an adapter
Set it as the default client:
config :ethers, rpc_client: MyApp.RpcClientOr choose it for a single request with the :rpc_client option:
Ethers.call(tx_data, rpc_client: MyApp.RpcClient, rpc_opts: [url: "https://..."])Implementing an adapter
Each callback sends the JSON-RPC method it is named after, eth_get_balance/3 for
eth_getBalance.
defmodule MyApp.RpcClient do
@behaviour Ethers.RpcClient.Adapter
@impl true
def eth_block_number(opts), do: request("eth_blockNumber", [], opts)
@impl true
def eth_get_balance(account, block, opts) do
request("eth_getBalance", [account, block], opts)
end
# ... the other callbacks
defp request(method, params, opts) do
# Send the request, then return {:ok, result} or {:error, reason}
end
end- Arguments are the parameters of the method, in order and already encoded for JSON-RPC:
quantities and data are
0xprefixed hex strings, blocks are a hex quantity or a tag such as"latest", and transactions, filters and state overrides are maps. - The last argument is the
:rpc_optskeyword list of the request ([]if not given). It is passed as is, so an adapter defines its own options. - The return value is
{:ok, result}with theresultmember of the JSON-RPC response, decoded from JSON and otherwise untouched, or{:error, reason}. Return the JSON-RPCerrormember asreasonwhen the node responds with one. Ethers decodes the custom errors of a contract from its"data"member.
Optional callbacks
An adapter may leave out the callbacks below. The functions that need them then return an error:
| Callback | Without it |
|---|---|
eth_call/4, eth_estimate_gas/4 | :state_overrides returns {:error, :state_overrides_not_supported} |
eth_config/1 | Ethers.config/1 returns {:error, :not_supported} |
eth_create_access_list/3 | Ethers.create_access_list/2 returns {:error, :not_supported} |
eth_fee_history/4 | Ethers.fee_history/4 returns {:error, :not_supported}, and missing transaction fees are filled in from eth_gasPrice and eth_maxPriorityFeePerGas |
eth_get_code/3 | Ethers.get_code/2 returns {:error, :not_supported} |
Summary
Callbacks
Sends several requests as one JSON-RPC batch.
Sends eth_blobBaseFee.
Sends eth_blockNumber.
Sends eth_call.
Sends eth_call with state overrides.
Sends eth_chainId.
Sends eth_config.
Sends eth_createAccessList.
Sends eth_estimateGas.
Sends eth_estimateGas with state overrides.
Sends eth_feeHistory.
Sends eth_gasPrice.
Sends eth_getBalance.
Sends eth_getBlockByNumber.
Sends eth_getCode.
Sends eth_getLogs.
Sends eth_getTransactionByHash.
Sends eth_getTransactionCount.
Sends eth_getTransactionReceipt.
Sends eth_maxPriorityFeePerGas.
Sends eth_sendRawTransaction.
Sends eth_sendTransaction.
Types
Callbacks
@callback batch_request([{method :: atom(), params :: [term()]}], keyword()) :: {:ok, ok: term(), error: term()} | error()
Sends several requests as one JSON-RPC batch.
Each request is a {method, params} tuple. method is the name of a callback of this
behaviour, or of any other method given to Ethers.batch/2 (such as :net_version), and
params are its arguments without the options.
Returns the {:ok, result} or {:error, reason} of every request, in the order of the
requests.
Sends eth_blobBaseFee.
Sends eth_blockNumber.
Sends eth_call.
@callback eth_call(map(), binary(), state_overrides :: map(), keyword()) :: {:ok, binary()} | error()
Sends eth_call with state overrides.
Sends eth_chainId.
Sends eth_config.
Sends eth_createAccessList.
Sends eth_estimateGas.
@callback eth_estimate_gas(map(), binary(), state_overrides :: map(), keyword()) :: {:ok, binary()} | error()
Sends eth_estimateGas with state overrides.
@callback eth_fee_history( block_count :: binary(), newest_block :: binary(), reward_percentiles :: [number()], keyword() ) :: {:ok, map()} | error()
Sends eth_feeHistory.
Sends eth_gasPrice.
Sends eth_getBalance.
@callback eth_get_block_by_number(binary() | non_neg_integer(), boolean(), keyword()) :: {:ok, map() | nil} | error()
Sends eth_getBlockByNumber.
The result is nil if the block does not exist.
Sends eth_getCode.
Sends eth_getLogs.
Sends eth_getTransactionByHash.
The result is nil if the transaction is not found.
Sends eth_getTransactionCount.
Sends eth_getTransactionReceipt.
The result is nil if the transaction is not found or not mined yet.
Sends eth_maxPriorityFeePerGas.
Sends eth_sendRawTransaction.
Sends eth_sendTransaction.