diff --git a/CHANGELOG.md b/CHANGELOG.md index e35835daa9..c9c11688e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - [#2803](https://github.com/poanetwork/blockscout/pull/2803) - Fix block validator custom tooltip ### Chore +- [#2859](https://github.com/poanetwork/blockscout/pull/2859) - Add eth_blockNumber API endpoint to eth_rpc section - [#2846](https://github.com/poanetwork/blockscout/pull/2846) - Remove networks images preload - [#2845](https://github.com/poanetwork/blockscout/pull/2845) - Set outline none for nav dropdown item in mobile view (fix for Safari) - [#2844](https://github.com/poanetwork/blockscout/pull/2844) - Extend external reward types up to 20 diff --git a/apps/block_scout_web/lib/block_scout_web/etherscan.ex b/apps/block_scout_web/lib/block_scout_web/etherscan.ex index eadaf0fff5..1037013d66 100644 --- a/apps/block_scout_web/lib/block_scout_web/etherscan.ex +++ b/apps/block_scout_web/lib/block_scout_web/etherscan.ex @@ -299,7 +299,7 @@ defmodule BlockScoutWeb.Etherscan do @block_eth_block_number_example_value %{ "jsonrpc" => "2.0", - "result" => "767969", + "result" => "0xb33bf1", "id" => 1 } diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex index 5c98794b2a..1fc3af19cc 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex @@ -3,6 +3,7 @@ defmodule BlockScoutWeb.API.RPC.BlockView do alias BlockScoutWeb.API.RPC.{EthRPCView, RPCView} alias Explorer.Chain.{Hash, Wei} + alias Explorer.EthRPC, as: EthRPC def render("block_reward.json", %{block: block, reward: reward}) do reward_as_string = @@ -23,7 +24,7 @@ defmodule BlockScoutWeb.API.RPC.BlockView do end def render("eth_block_number.json", %{number: number, id: id}) do - result = encode_quantity(number) + result = EthRPC.encode_quantity(number) EthRPCView.render("show.json", %{result: result, id: id}) end @@ -31,24 +32,4 @@ defmodule BlockScoutWeb.API.RPC.BlockView do def render("error.json", %{error: error}) do RPCView.render("error.json", error: error) end - - defp encode_quantity(binary) when is_binary(binary) do - hex_binary = Base.encode16(binary, case: :lower) - - result = String.replace_leading(hex_binary, "0", "") - - final_result = if result == "", do: "0", else: result - - "0x#{final_result}" - end - - defp encode_quantity(value) when is_integer(value) do - value - |> :binary.encode_unsigned() - |> encode_quantity() - end - - defp encode_quantity(value) when is_nil(value) do - nil - end end diff --git a/apps/explorer/lib/explorer/eth_rpc.ex b/apps/explorer/lib/explorer/eth_rpc.ex index 04d257ffb1..4ae8311150 100644 --- a/apps/explorer/lib/explorer/eth_rpc.ex +++ b/apps/explorer/lib/explorer/eth_rpc.ex @@ -6,9 +6,21 @@ defmodule Explorer.EthRPC do alias Ecto.Type, as: EctoType alias Explorer.{Chain, Repo} alias Explorer.Chain.{Block, Data, Hash, Hash.Address, Wei} + alias Explorer.Chain.Cache.BlockNumber alias Explorer.Etherscan.Logs @methods %{ + "eth_blockNumber" => %{ + action: :eth_block_number, + notes: nil, + example: """ + {"id": 0, "jsonrpc": "2.0", "method": "eth_blockNumber", "params": []} + """, + params: [], + result: """ + {"id": 0, "jsonrpc": "2.0", "result": "0xb3415c"} + """ + }, "eth_getBalance" => %{ action: :eth_get_balance, notes: """ @@ -94,6 +106,16 @@ defmodule Explorer.EthRPC do end) end + def eth_block_number do + max_block_number = BlockNumber.get_max() + + max_block_number_hex = + max_block_number + |> encode_quantity() + + {:ok, max_block_number_hex} + end + def eth_get_balance(address_param, block_param \\ nil) do with {:address, {:ok, address}} <- {:address, Chain.string_to_address_hash(address_param)}, {:block, {:ok, block}} <- {:block, block_param(block_param)}, @@ -405,5 +427,25 @@ defmodule Explorer.EthRPC do defp block_param(nil), do: {:ok, :latest} defp block_param(_), do: :error + def encode_quantity(binary) when is_binary(binary) do + hex_binary = Base.encode16(binary, case: :lower) + + result = String.replace_leading(hex_binary, "0", "") + + final_result = if result == "", do: "0", else: result + + "0x#{final_result}" + end + + def encode_quantity(value) when is_integer(value) do + value + |> :binary.encode_unsigned() + |> encode_quantity() + end + + def encode_quantity(value) when is_nil(value) do + nil + end + def methods, do: @methods end