Skip to content

Commit

Permalink
Add Ethers.chain_id/1
Browse files Browse the repository at this point in the history
  • Loading branch information
alisinabh committed Dec 8, 2024
1 parent 85f0f5c commit ce88ac6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/ethers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ defmodule Ethers do

@option_keys [:rpc_client, :rpc_opts, :signer, :signer_opts, :tx_type]
@hex_decode_post_process [
:chain_id,
:current_block_number,
:current_gas_price,
:estimate_gas,
Expand All @@ -95,6 +96,13 @@ defmodule Ethers do

defguardp valid_result(bin) when bin != "0x"

def chain_id(opts \\ []) do
{rpc_client, rpc_opts} = get_rpc_client(opts)

rpc_client.eth_chain_id(rpc_opts)
|> post_process(nil, :chain_id)
end

@doc """
Returns the current gas price from the RPC API
"""
Expand Down Expand Up @@ -733,7 +741,7 @@ defmodule Ethers do
case errors_module.find_and_decode(error_data) do
{:ok, error} -> {:error, error}
{:error, :undefined_error} -> {:error, full_error}
e -> e
{:error, reason} -> {:error, reason}
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/ethers/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ defmodule Ethers.Transaction do
end
end

defp do_post_process(:chain_id, {:ok, v_int}) when is_integer(v_int) do
{:ok, {:chain_id, Utils.integer_to_hex(v_int)}}
end

defp do_post_process(:max_fee_per_gas, {:ok, v_hex}) do
with {:ok, v} <- Utils.hex_to_integer(v_hex) do
# Setting a higher value for max_fee_per gas since the actual base fee is
Expand Down

0 comments on commit ce88ac6

Please sign in to comment.