-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add an adapter interface * add rpc_client module * add ethereumex http client adapter implementation * apply changes to ethers facade * backwards compatibility * backwards compatibility on the rpc_client configuration * Add missing rpc calls and default opt * Add test and improve naming * Add moduledoc false to private modules --------- Co-authored-by: Alisina Bahadori <[email protected]>
- Loading branch information
Showing
5 changed files
with
128 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Ethers.RpcClient do | ||
@moduledoc false | ||
|
||
@doc false | ||
@spec rpc_client() :: atom() | ||
def rpc_client do | ||
case Application.get_env(:ethers, :rpc_client, Ethereumex.HttpClient) do | ||
Ethereumex.HttpClient -> Ethers.RpcClient.EthereumexHttpClient | ||
module when is_atom(module) -> module | ||
_ -> raise ArgumentError, "Invalid ethers configuration. :rpc_client must be a module" | ||
end | ||
end | ||
|
||
@doc false | ||
@spec get_rpc_client(Keyword.t()) :: {atom(), Keyword.t()} | ||
def get_rpc_client(opts) do | ||
module = | ||
case Keyword.fetch(opts, :rpc_client) do | ||
{:ok, module} when is_atom(module) -> module | ||
:error -> Ethers.rpc_client() | ||
end | ||
|
||
{module, Keyword.get(opts, :rpc_opts, [])} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Ethers.RpcClient.Adapter do | ||
@moduledoc false | ||
|
||
@type error :: {:error, map() | binary() | atom()} | ||
|
||
@callback batch_request([{atom(), list(boolean() | binary())}], keyword()) :: | ||
{:ok, [any()]} | error | ||
|
||
@callback eth_block_number(keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_call(map(), binary(), keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_estimate_gas(map(), keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_gas_price(keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_get_balance(binary(), binary(), keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_get_block_by_number(binary() | non_neg_integer(), boolean(), keyword()) :: | ||
{:ok, map()} | error() | ||
|
||
@callback eth_get_transaction_by_hash(binary(), keyword()) :: {:ok, map()} | error() | ||
|
||
@callback eth_get_transaction_count(binary(), binary(), keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_get_transaction_receipt(binary(), keyword()) :: {:ok, map()} | error() | ||
|
||
@callback eth_max_priority_fee_per_gas(keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_get_logs(map(), keyword()) :: {:ok, [binary()] | [map()]} | error() | ||
|
||
@callback eth_send_transaction(map(), keyword()) :: {:ok, binary()} | error() | ||
|
||
@callback eth_send_raw_transaction(binary(), keyword()) :: {:ok, binary()} | error() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Ethers.RpcClient.EthereumexHttpClient do | ||
@moduledoc false | ||
|
||
alias Ethers.RpcClient.Adapter | ||
|
||
@behaviour Ethers.RpcClient.Adapter | ||
|
||
@exclude_delegation [:eth_get_logs] | ||
|
||
for {func, arity} <- Adapter.behaviour_info(:callbacks), func not in @exclude_delegation do | ||
args = Macro.generate_arguments(arity - 1, __MODULE__) | ||
|
||
@impl true | ||
def unquote(func)(unquote_splicing(args), opts \\ []) do | ||
apply(Ethereumex.HttpClient, unquote(func), [unquote_splicing(args), opts]) | ||
end | ||
end | ||
|
||
@impl true | ||
def eth_get_logs(params, opts \\ []) do | ||
params | ||
|> replace_key(:from_block, :fromBlock) | ||
|> replace_key(:to_block, :toBlock) | ||
|> Ethereumex.HttpClient.eth_get_logs(opts) | ||
end | ||
|
||
defp replace_key(map, ethers_key, ethereumex_key) do | ||
case Map.fetch(map, ethers_key) do | ||
{:ok, value} -> | ||
map | ||
|> Map.put(ethereumex_key, value) | ||
|> Map.delete(ethers_key) | ||
|
||
:error -> | ||
map | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters