add json_rpc method

pull/2791/head
Ayrat Badykov 5 years ago
parent d7af9034c1
commit 87b1d51553
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 31
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex

@ -1,7 +1,9 @@
defmodule EthereumJSONRPC.HTTP.IPCCLient do defmodule EthereumJSONRPC.IPC do
use GenServer use GenServer
@moduledoc false @moduledoc false
# Server
def start_link(state \\ []) do def start_link(state \\ []) do
GenServer.start_link(__MODULE__, Keyword.merge(state, socket: nil)) GenServer.start_link(__MODULE__, Keyword.merge(state, socket: nil))
end end
@ -55,4 +57,31 @@ defmodule EthereumJSONRPC.HTTP.IPCCLient do
{:reply, response, state} {:reply, response, state}
end end
# Client
def json_rpc(pid, payload, _opts) do
with {:ok, response} <- post(pid, 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")}
end
else
{:error, %Jason.DecodeError{data: ""}} -> {:error, :empty_response}
{:error, error} -> {:error, {:invalid_json, error}}
{: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 end
Loading…
Cancel
Save