From ce88ac66c50307744d7d431cf12e4d79fede56f2 Mon Sep 17 00:00:00 2001 From: Alisina Bahadori Date: Sat, 7 Dec 2024 21:11:41 -0500 Subject: [PATCH] Add `Ethers.chain_id/1` --- lib/ethers.ex | 10 +++++++++- lib/ethers/transaction.ex | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/ethers.ex b/lib/ethers.ex index 462b57e..90ec6d1 100644 --- a/lib/ethers.ex +++ b/lib/ethers.ex @@ -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, @@ -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 """ @@ -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 diff --git a/lib/ethers/transaction.ex b/lib/ethers/transaction.ex index 375a9b1..79e5ed6 100644 --- a/lib/ethers/transaction.ex +++ b/lib/ethers/transaction.ex @@ -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