|
|
|
@ -25,6 +25,8 @@ defmodule EthereumJSONRPC do |
|
|
|
|
documentation for `EthereumJSONRPC.RequestCoordinator`. |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
require Logger |
|
|
|
|
|
|
|
|
|
alias EthereumJSONRPC.{ |
|
|
|
|
Block, |
|
|
|
|
Blocks, |
|
|
|
@ -381,6 +383,29 @@ defmodule EthereumJSONRPC do |
|
|
|
|
|> Enum.into(%{}, fn {params, id} -> {id, params} end) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Assigns not matched ids between requests and responses to responses with incorrect ids |
|
|
|
|
""" |
|
|
|
|
def sanitize_responses(responses, id_to_params) do |
|
|
|
|
responses |
|
|
|
|
|> Enum.reduce( |
|
|
|
|
{[], Map.keys(id_to_params) -- Enum.map(responses, & &1.id)}, |
|
|
|
|
fn |
|
|
|
|
%{id: nil} = res, {result_res, [id | rest]} -> |
|
|
|
|
Logger.error( |
|
|
|
|
"Empty id in response: #{inspect(res)}, stacktrace: #{inspect(Process.info(self(), :current_stacktrace))}" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
{[%{res | id: id} | result_res], rest} |
|
|
|
|
|
|
|
|
|
res, {result_res, non_matched} -> |
|
|
|
|
{[res | result_res], non_matched} |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
|> elem(0) |
|
|
|
|
|> Enum.reverse() |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
1. POSTs JSON `payload` to `url` |
|
|
|
|
2. Decodes the response |
|
|
|
@ -422,7 +447,7 @@ defmodule EthereumJSONRPC do |
|
|
|
|
@doc """ |
|
|
|
|
Converts `t:quantity/0` to `t:non_neg_integer/0`. |
|
|
|
|
""" |
|
|
|
|
@spec quantity_to_integer(quantity) :: non_neg_integer() | :error |
|
|
|
|
@spec quantity_to_integer(quantity) :: non_neg_integer() | nil |
|
|
|
|
def quantity_to_integer("0x" <> hexadecimal_digits) do |
|
|
|
|
String.to_integer(hexadecimal_digits, 16) |
|
|
|
|
end |
|
|
|
@ -432,10 +457,12 @@ defmodule EthereumJSONRPC do |
|
|
|
|
def quantity_to_integer(string) when is_binary(string) do |
|
|
|
|
case Integer.parse(string) do |
|
|
|
|
{integer, ""} -> integer |
|
|
|
|
_ -> :error |
|
|
|
|
_ -> nil |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def quantity_to_integer(_), do: nil |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Converts `t:non_neg_integer/0` to `t:quantity/0` |
|
|
|
|
""" |
|
|
|
@ -503,7 +530,7 @@ defmodule EthereumJSONRPC do |
|
|
|
|
""" |
|
|
|
|
def timestamp_to_datetime(timestamp) do |
|
|
|
|
case quantity_to_integer(timestamp) do |
|
|
|
|
:error -> |
|
|
|
|
nil -> |
|
|
|
|
nil |
|
|
|
|
|
|
|
|
|
quantity -> |
|
|
|
|