parent
6a9e4a6895
commit
3efa2d9996
@ -0,0 +1,62 @@ |
||||
defmodule EthereumJSONRPC.FetchedBalance do |
||||
@moduledoc """ |
||||
A single balance fetched from `eth_getBalance`. |
||||
""" |
||||
|
||||
import EthereumJSONRPC, only: [quantity_to_integer: 1] |
||||
|
||||
@type params :: %{address_hash: EthereumJSONRPC.hash(), block_number: non_neg_integer(), value: non_neg_integer()} |
||||
@type error :: %{code: integer(), message: String.t(), data: %{block_quantity: String.t(), hash: String.t()}} |
||||
|
||||
@doc """ |
||||
Converts `response` to balance params or annotated error. |
||||
""" |
||||
|
||||
@spec from_response(%{id: id, result: String.t()}, %{id => %{block_quantity: block_quantity, hash_data: hash_data}}) :: |
||||
{:ok, params()} |
||||
when id: non_neg_integer(), block_quantity: String.t(), hash_data: String.t() |
||||
def from_response(%{id: id, result: fetched_balance_quantity}, id_to_params) when is_map(id_to_params) do |
||||
%{block_quantity: block_quantity, hash_data: hash_data} = Map.fetch!(id_to_params, id) |
||||
|
||||
{:ok, |
||||
%{ |
||||
address_hash: hash_data, |
||||
block_number: quantity_to_integer(block_quantity), |
||||
value: quantity_to_integer(fetched_balance_quantity) |
||||
}} |
||||
end |
||||
|
||||
@spec from_response( |
||||
%{ |
||||
id: id, |
||||
error: %{code: code, message: message} |
||||
}, |
||||
%{id => %{block_quantity: block_quantity, hash_data: hash_data}} |
||||
) :: {:error, %{code: code, message: message, data: %{block_quantity: block_quantity, hash: hash_data}}} |
||||
when id: non_neg_integer(), |
||||
code: integer(), |
||||
message: String.t(), |
||||
block_quantity: String.t(), |
||||
hash_data: String.t() |
||||
def from_response(%{id: id, error: %{code: code, message: message} = error}, id_to_params) |
||||
when is_integer(code) and is_binary(message) and is_map(id_to_params) do |
||||
%{block_quantity: block_quantity, hash_data: hash_data} = Map.fetch!(id_to_params, id) |
||||
|
||||
annotated_error = Map.put(error, :data, %{block_quantity: block_quantity, hash_data: hash_data}) |
||||
|
||||
{:error, annotated_error} |
||||
end |
||||
|
||||
@spec request(%{id: id, block_quantity: block_quantity, hash_data: hash_data}) :: %{ |
||||
jsonrpc: String.t(), |
||||
id: id, |
||||
method: String.t(), |
||||
params: [hash_data | block_quantity] |
||||
} |
||||
when id: EthereumJSONRPC.request_id(), |
||||
block_quantity: EthereumJSONRPC.quantity(), |
||||
hash_data: EthereumJSONRPC.hash() |
||||
def request(%{id: id, block_quantity: block_quantity, hash_data: hash_data}) do |
||||
EthereumJSONRPC.request(%{id: id, method: "eth_getBalance", params: [hash_data, block_quantity]}) |
||||
end |
||||
end |
@ -0,0 +1,49 @@ |
||||
defmodule EthereumJSONRPC.FetchedBalances do |
||||
@moduledoc """ |
||||
Balance params and errors from a batch request from `eth_getBalance`. |
||||
""" |
||||
|
||||
alias EthereumJSONRPC.FetchedBalance |
||||
|
||||
defstruct errors: [], |
||||
params_list: [] |
||||
|
||||
@typedoc """ |
||||
* `params_list` - all the balance params from requests that succeeded in the batch. |
||||
* `errors` - all the error from requests that failed in the batch. |
||||
""" |
||||
@type t :: %__MODULE__{params_list: [FetchedBalance.params()], errors: [FetchedBalance.error()]} |
||||
|
||||
@doc """ |
||||
Converts `responses` to `t/0`. |
||||
""" |
||||
def from_responses(responses, id_to_params) do |
||||
responses |
||||
|> Enum.map(&FetchedBalance.from_response(&1, id_to_params)) |
||||
|> Enum.reduce( |
||||
%__MODULE__{}, |
||||
fn |
||||
{:ok, params}, %__MODULE__{params_list: params_list} = acc -> |
||||
%__MODULE__{acc | params_list: [params | params_list]} |
||||
|
||||
{:error, reason}, %__MODULE__{errors: errors} = acc -> |
||||
%__MODULE__{acc | errors: [reason | errors]} |
||||
end |
||||
) |
||||
end |
||||
|
||||
@doc """ |
||||
`eth_getBalance` requests for `id_to_params`. |
||||
""" |
||||
@spec requests(%{id => %{block_quantity: block_quantity, hash_data: hash_data}}) :: [ |
||||
%{jsonrpc: String.t(), id: id, method: String.t(), params: [hash_data | block_quantity]} |
||||
] |
||||
when id: EthereumJSONRPC.request_id(), |
||||
block_quantity: EthereumJSONRPC.quantity(), |
||||
hash_data: EthereumJSONRPC.hash() |
||||
def requests(id_to_params) when is_map(id_to_params) do |
||||
Enum.map(id_to_params, fn {id, %{block_quantity: block_quantity, hash_data: hash_data}} -> |
||||
FetchedBalance.request(%{id: id, block_quantity: block_quantity, hash_data: hash_data}) |
||||
end) |
||||
end |
||||
end |
Loading…
Reference in new issue