From 0a016d2ff3e7b2232e9d5b4958d82bd9873871de Mon Sep 17 00:00:00 2001 From: Alisina Bahadori Date: Fri, 10 Jan 2025 02:17:44 -0500 Subject: [PATCH] Add `Ethres.blob_fee_per_gas/1` --- CHANGELOG.md | 1 + lib/ethers.ex | 25 +++++++++++++++++++++++++ lib/ethers/rpc_client/adapter.ex | 2 ++ lib/ethers/transaction.ex | 1 + mix.exs | 2 +- mix.lock | 4 ++-- test/ethers_test.exs | 11 +++++++++++ 7 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f111c0..b03b7d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Support [EIP-1191](https://eips.ethereum.org/EIPS/eip-1191): Add chain id to mixed-case checksum address encoding - Add [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) transaction support - Add [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) transaction support +- Add `Ethers.blob_fee_per_gas/1` to calculate blob fee per gas ## v0.6.1 (2025-01-02) diff --git a/lib/ethers.ex b/lib/ethers.ex index a80cf07..5bca12c 100644 --- a/lib/ethers.ex +++ b/lib/ethers.ex @@ -77,6 +77,7 @@ defmodule Ethers do :get_balance, :get_transaction_count, :max_priority_fee_per_gas, + :blob_base_fee, :gas_price ] @rpc_actions_map %{ @@ -91,6 +92,7 @@ defmodule Ethers do get_transaction: :eth_get_transaction_by_hash, max_priority_fee_per_gas: :eth_max_priority_fee_per_gas, send_transaction: :eth_send_transaction, + blob_base_fee: :eth_blob_base_fee, # Deprecated, kept for backward compatibility send: :eth_send_transaction } @@ -525,6 +527,29 @@ defmodule Ethers do end end + @doc """ + Returns the current blob base fee from the RPC API + """ + @spec blob_base_fee(Keyword.t()) :: + {:ok, non_neg_integer()} | {:error, reason :: term()} + def blob_base_fee(opts \\ []) do + {rpc_client, rpc_opts} = get_rpc_client(opts) + + rpc_client.eth_blob_base_fee(rpc_opts) + |> post_process(nil, :blob_base_fee) + end + + @doc """ + Same as `Ethers.blob_base_fee/1` but raises on error. + """ + @spec blob_base_fee!(Keyword.t()) :: non_neg_integer() | no_return() + def blob_base_fee!(opts \\ []) do + case blob_base_fee(opts) do + {:ok, fee} -> fee + {:error, reason} -> raise ExecutionError, reason + end + end + @doc """ Fetches the event logs with the given filter. diff --git a/lib/ethers/rpc_client/adapter.ex b/lib/ethers/rpc_client/adapter.ex index bdf44cf..fc0e3d4 100644 --- a/lib/ethers/rpc_client/adapter.ex +++ b/lib/ethers/rpc_client/adapter.ex @@ -29,6 +29,8 @@ defmodule Ethers.RpcClient.Adapter do @callback eth_max_priority_fee_per_gas(keyword()) :: {:ok, binary()} | error() + @callback eth_blob_base_fee(keyword()) :: {:ok, binary()} | error() + @callback eth_get_logs(map(), keyword()) :: {:ok, [binary()] | [map()]} | error() @callback eth_send_transaction(map(), keyword()) :: {:ok, binary()} | error() diff --git a/lib/ethers/transaction.ex b/lib/ethers/transaction.ex index 9fd86e1..9f5e7f7 100644 --- a/lib/ethers/transaction.ex +++ b/lib/ethers/transaction.ex @@ -351,6 +351,7 @@ defmodule Ethers.Transaction do defp fill_action(:nonce, tx), do: {:get_transaction_count, tx.from, block: "latest"} defp fill_action(:max_fee_per_gas, _tx), do: :gas_price defp fill_action(:max_priority_fee_per_gas, _tx), do: :max_priority_fee_per_gas + defp fill_action(:max_fee_per_blob_gas, _tx), do: :blob_base_fee defp fill_action(:gas_price, _tx), do: :gas_price defp fill_action(:gas, tx), do: {:estimate_gas, tx} diff --git a/mix.exs b/mix.exs index 4a900aa..b8f7bf2 100644 --- a/mix.exs +++ b/mix.exs @@ -104,7 +104,7 @@ defmodule Ethers.MixProject do [ {:credo, "~> 1.7", only: [:dev, :test], runtime: false}, {:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false}, - {:ethereumex, "~> 0.10.6"}, + {:ethereumex, "~> 0.10", github: "alisinabh/ethereumex", branch: "add-blob-base-fee"}, {:ex_abi, "~> 0.8.0", optional: System.get_env("SKIP_EX_KECCAK") == "true"}, {:ex_doc, "~> 0.32", only: :dev, runtime: false}, {:ex_keccak, "~> 0.7.5"}, diff --git a/mix.lock b/mix.lock index 00b631f..cb8e023 100644 --- a/mix.lock +++ b/mix.lock @@ -5,7 +5,7 @@ "dialyxir": {:hex, :dialyxir, "1.4.5", "ca1571ac18e0f88d4ab245f0b60fa31ff1b12cbae2b11bd25d207f865e8ae78a", [:mix], [{:erlex, ">= 0.2.7", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "b0fb08bb8107c750db5c0b324fa2df5ceaa0f9307690ee3c1f6ba5b9eb5d35c3"}, "earmark_parser": {:hex, :earmark_parser, "1.4.42", "f23d856f41919f17cd06a493923a722d87a2d684f143a1e663c04a2b93100682", [:mix], [], "hexpm", "6915b6ca369b5f7346636a2f41c6a6d78b5af419d61a611079189233358b8b8b"}, "erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"}, - "ethereumex": {:hex, :ethereumex, "0.10.6", "6d75cac39b5b7a720b064fe48563f205d3d9784e5bde25f983dd07cf306c2a6d", [:make, :mix], [{:finch, "~> 0.16", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "58cf926239dabf8bd1fc6cf50a37b926274240b7f58ba5b235a20b5500a9a7e1"}, + "ethereumex": {:git, "https://github.com/alisinabh/ethereumex.git", "f0c1c1f3300ffb61af3b594b487b1997c6072181", [branch: "add-blob-base-fee"]}, "ex_abi": {:hex, :ex_abi, "0.8.1", "451fa960ddc4dfbb350e13509f3dd64ca586b8484a77aad9f7d778161b5eab79", [:mix], [{:ex_keccak, "~> 0.7.5", [hex: :ex_keccak, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "abcf53d556c2948e5c1241340afd4a72cdf93ab6daef16fc200c16ca1183cdca"}, "ex_doc": {:hex, :ex_doc, "0.36.1", "4197d034f93e0b89ec79fac56e226107824adcce8d2dd0a26f5ed3a95efc36b1", [:mix], [{:earmark_parser, "~> 1.4.42", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "d7d26a7cf965dacadcd48f9fa7b5953d7d0cfa3b44fa7a65514427da44eafd89"}, "ex_keccak": {:hex, :ex_keccak, "0.7.6", "110c3ed76b55265975d9ae6628205b8a026f11fe081f3073e00c29aab2e91473", [:mix], [{:rustler, ">= 0.0.0", [hex: :rustler, repo: "hexpm", optional: true]}, {:rustler_precompiled, "~> 0.8", [hex: :rustler_precompiled, repo: "hexpm", optional: false]}], "hexpm", "9d1568424eb7b995e480d1b7f0c1e914226ee625496600abb922bba6f5cdc5e4"}, @@ -14,7 +14,7 @@ "excoveralls": {:hex, :excoveralls, "0.18.3", "bca47a24d69a3179951f51f1db6d3ed63bca9017f476fe520eb78602d45f7756", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "746f404fcd09d5029f1b211739afb8fb8575d775b21f6a3908e7ce3e640724c6"}, "file_system": {:hex, :file_system, "1.0.1", "79e8ceaddb0416f8b8cd02a0127bdbababe7bf4a23d2a395b983c1f8b3f73edd", [:mix], [], "hexpm", "4414d1f38863ddf9120720cd976fce5bdde8e91d8283353f0e31850fa89feb9e"}, "finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"}, - "hpax": {:hex, :hpax, "1.0.1", "c857057f89e8bd71d97d9042e009df2a42705d6d690d54eca84c8b29af0787b0", [:mix], [], "hexpm", "4e2d5a4f76ae1e3048f35ae7adb1641c36265510a2d4638157fbcb53dda38445"}, + "hpax": {:hex, :hpax, "1.0.2", "762df951b0c399ff67cc57c3995ec3cf46d696e41f0bba17da0518d94acd4aac", [:mix], [], "hexpm", "2f09b4c1074e0abd846747329eaa26d535be0eb3d189fa69d812bfb8bfefd32f"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, diff --git a/test/ethers_test.exs b/test/ethers_test.exs index a31bfa7..2eb1e6b 100644 --- a/test/ethers_test.exs +++ b/test/ethers_test.exs @@ -43,6 +43,17 @@ defmodule EthersTest do end end + describe "blob_base_fee" do + test "returns the correct blob base fee" do + assert {:ok, blob_base_fee} = Ethers.blob_base_fee() + assert is_integer(blob_base_fee) + end + + test "bang version returns unwrapped value" do + assert is_integer(Ethers.blob_base_fee!()) + end + end + describe "current_block_number" do test "returns the current block number" do assert {:ok, n} = Ethers.current_block_number()