Add eth_blockNumber API endpoint to eth_rpc section

pull/2859/head
Victor Baranov 5 years ago
parent 7286cbfa60
commit 28acc6da0f
  1. 2
      apps/block_scout_web/lib/block_scout_web/etherscan.ex
  2. 23
      apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex
  3. 42
      apps/explorer/lib/explorer/eth_rpc.ex

@ -299,7 +299,7 @@ defmodule BlockScoutWeb.Etherscan do
@block_eth_block_number_example_value %{ @block_eth_block_number_example_value %{
"jsonrpc" => "2.0", "jsonrpc" => "2.0",
"result" => "767969", "result" => "0xb33bf1",
"id" => 1 "id" => 1
} }

@ -3,6 +3,7 @@ defmodule BlockScoutWeb.API.RPC.BlockView do
alias BlockScoutWeb.API.RPC.{EthRPCView, RPCView} alias BlockScoutWeb.API.RPC.{EthRPCView, RPCView}
alias Explorer.Chain.{Hash, Wei} alias Explorer.Chain.{Hash, Wei}
alias Explorer.EthRPC, as: EthRPC
def render("block_reward.json", %{block: block, reward: reward}) do def render("block_reward.json", %{block: block, reward: reward}) do
reward_as_string = reward_as_string =
@ -23,7 +24,7 @@ defmodule BlockScoutWeb.API.RPC.BlockView do
end end
def render("eth_block_number.json", %{number: number, id: id}) do 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}) EthRPCView.render("show.json", %{result: result, id: id})
end end
@ -31,24 +32,4 @@ defmodule BlockScoutWeb.API.RPC.BlockView do
def render("error.json", %{error: error}) do def render("error.json", %{error: error}) do
RPCView.render("error.json", error: error) RPCView.render("error.json", error: error)
end 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 end

@ -6,9 +6,21 @@ defmodule Explorer.EthRPC do
alias Ecto.Type, as: EctoType alias Ecto.Type, as: EctoType
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
alias Explorer.Chain.{Block, Data, Hash, Hash.Address, Wei} alias Explorer.Chain.{Block, Data, Hash, Hash.Address, Wei}
alias Explorer.Chain.Cache.BlockNumber
alias Explorer.Etherscan.Logs alias Explorer.Etherscan.Logs
@methods %{ @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" => %{ "eth_getBalance" => %{
action: :eth_get_balance, action: :eth_get_balance,
notes: """ notes: """
@ -94,6 +106,16 @@ defmodule Explorer.EthRPC do
end) end)
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 def eth_get_balance(address_param, block_param \\ nil) do
with {:address, {:ok, address}} <- {:address, Chain.string_to_address_hash(address_param)}, with {:address, {:ok, address}} <- {:address, Chain.string_to_address_hash(address_param)},
{:block, {:ok, block}} <- {:block, block_param(block_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(nil), do: {:ok, :latest}
defp block_param(_), do: :error 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 def methods, do: @methods
end end

Loading…
Cancel
Save