format result from ipc

pull/2791/head
Ayrat Badykov 5 years ago
parent 141ebda719
commit f0f3babd38
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 6
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/http.ex
  2. 29
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex

@ -132,7 +132,7 @@ defmodule EthereumJSONRPC.HTTP do
# restrict response to only those fields supported by the JSON-RPC 2.0 standard, which means that level of keys is
# validated, so we can indicate that with switch to atom keys.
defp standardize_response(%{"jsonrpc" => "2.0" = jsonrpc, "id" => id} = unstandardized) do
def standardize_response(%{"jsonrpc" => "2.0" = jsonrpc, "id" => id} = unstandardized) do
# Nethermind return string ids
id = quantity_to_integer(id)
@ -155,8 +155,8 @@ defmodule EthereumJSONRPC.HTTP do
# restrict error to only those fields supported by the JSON-RPC 2.0 standard, which means that level of keys is
# validated, so we can indicate that with switch to atom keys.
defp standardize_error(%{"code" => code, "message" => message} = unstandardized)
when is_integer(code) and is_binary(message) do
def standardize_error(%{"code" => code, "message" => message} = unstandardized)
when is_integer(code) and is_binary(message) do
standardized = %{code: code, message: message}
case Map.fetch(unstandardized, "data") do

@ -2,6 +2,8 @@ defmodule EthereumJSONRPC.IPC do
use GenServer
@moduledoc false
import EthereumJSONRPC.HTTP, only: [standardize_response: 1]
# Server
def child_spec(opts) do
@ -74,9 +76,20 @@ defmodule EthereumJSONRPC.IPC do
with {:ok, response} <- post(__MODULE__, Jason.encode!(payload)),
{:ok, decoded_body} <- Jason.decode(response) do
case decoded_body do
%{"error" => error} -> {:error, error}
result = [%{} | _] -> {:ok, format_batch(result)}
result -> {:ok, Map.get(result, "result")}
%{"error" => error} ->
{:error, error}
result = [%{} | _] ->
list =
result
|> Enum.reverse()
|> List.flatten()
|> Enum.map(&standardize_response/1)
{:ok, list}
result ->
{:ok, Map.get(result, "result")}
end
else
{:error, %Jason.DecodeError{data: ""}} -> {:error, :empty_response}
@ -84,14 +97,4 @@ defmodule EthereumJSONRPC.IPC do
{:error, error} -> {:error, error}
end
end
defp format_batch(list) do
list
|> Enum.sort(fn %{"id" => id1}, %{"id" => id2} ->
id1 <= id2
end)
|> Enum.map(fn %{"result" => result} ->
result
end)
end
end

Loading…
Cancel
Save