feat: Add missing filecoin robust addresses (#10935)
* feat: Add missing filecoin robust addresses * Narrow down spec * Refactoringpull/10985/head
parent
1b2232a88c
commit
f8b4cb794a
@ -1,35 +1,105 @@ |
|||||||
defmodule BlockScoutWeb.API.V2.FilecoinView do |
if Application.compile_env(:explorer, :chain_type) == :filecoin do |
||||||
@moduledoc """ |
defmodule BlockScoutWeb.API.V2.FilecoinView do |
||||||
View functions for rendering Filecoin-related data in JSON format. |
@moduledoc """ |
||||||
""" |
View functions for rendering Filecoin-related data in JSON format. |
||||||
|
""" |
||||||
alias Explorer.Chain.Address |
|
||||||
|
alias Explorer.Chain |
||||||
@doc """ |
alias Explorer.Chain.Address |
||||||
Extends the json output with a sub-map containing information related to |
|
||||||
Filecoin native addressing. |
@api_true [api?: true] |
||||||
""" |
|
||||||
@spec extend_address_json_response(map(), Address.t()) :: map() |
@doc """ |
||||||
def extend_address_json_response(result, %Address{} = address) do |
Extends the json output with a sub-map containing information related to |
||||||
filecoin_id = Map.get(address, :filecoin_id) |
Filecoin native addressing. |
||||||
filecoin_robust = Map.get(address, :filecoin_robust) |
""" |
||||||
filecoin_actor_type = Map.get(address, :filecoin_actor_type) |
@spec extend_address_json_response(map(), Address.t()) :: map() |
||||||
|
def extend_address_json_response( |
||||||
is_fetched = |
result, |
||||||
Enum.all?( |
%Address{filecoin_id: filecoin_id, filecoin_robust: filecoin_robust, filecoin_actor_type: filecoin_actor_type} |
||||||
[ |
) do |
||||||
filecoin_id, |
Map.put(result, :filecoin, %{ |
||||||
filecoin_robust, |
id: filecoin_id, |
||||||
filecoin_actor_type |
robust: filecoin_robust, |
||||||
], |
actor_type: filecoin_actor_type |
||||||
&(not is_nil(&1)) |
}) |
||||||
) |
end |
||||||
|
|
||||||
Map.put(result, :filecoin, %{ |
@spec preload_and_put_filecoin_robust_address(map(), %{ |
||||||
is_fetched: is_fetched, |
address_hash: String.t() | nil, |
||||||
id: filecoin_id, |
field_prefix: String.t() | nil |
||||||
robust: filecoin_robust, |
}) :: |
||||||
actor_type: filecoin_actor_type |
map() |
||||||
}) |
def preload_and_put_filecoin_robust_address(result, %{address_hash: address_hash} = params) do |
||||||
|
address = address_hash && Address.get(address_hash, @api_true) |
||||||
|
|
||||||
|
put_filecoin_robust_address(result, Map.put(params, :address, address)) |
||||||
|
end |
||||||
|
|
||||||
|
@doc """ |
||||||
|
Adds a Filecoin robust address to the given result. |
||||||
|
|
||||||
|
## Parameters |
||||||
|
|
||||||
|
- result: The initial result to which the Filecoin robust address will be added. |
||||||
|
- opts: A map containing the following keys: |
||||||
|
- `:address` - A struct containing the `filecoin_robust` address. |
||||||
|
- `:field_prefix` - A prefix to be used for the field name in the result. |
||||||
|
|
||||||
|
## Returns |
||||||
|
|
||||||
|
The updated result with the Filecoin robust address added. |
||||||
|
""" |
||||||
|
@spec put_filecoin_robust_address(map(), %{ |
||||||
|
required(:address) => Address.t(), |
||||||
|
required(:field_prefix) => String.t() | nil, |
||||||
|
optional(any) => any |
||||||
|
}) :: map() |
||||||
|
def put_filecoin_robust_address(result, %{ |
||||||
|
address: %Address{filecoin_robust: filecoin_robust}, |
||||||
|
field_prefix: field_prefix |
||||||
|
}) do |
||||||
|
put_filecoin_robust_address_internal(result, filecoin_robust, field_prefix) |
||||||
|
end |
||||||
|
|
||||||
|
def put_filecoin_robust_address(result, %{field_prefix: field_prefix}) do |
||||||
|
put_filecoin_robust_address_internal(result, nil, field_prefix) |
||||||
|
end |
||||||
|
|
||||||
|
defp put_filecoin_robust_address_internal(result, filecoin_robust, field_prefix) do |
||||||
|
field_name = (field_prefix && "#{field_prefix}_filecoin_robust_address") || "filecoin_robust_address" |
||||||
|
Map.put(result, field_name, filecoin_robust) |
||||||
|
end |
||||||
|
|
||||||
|
@doc """ |
||||||
|
Preloads and inserts Filecoin robust addresses into the search results. |
||||||
|
|
||||||
|
## Parameters |
||||||
|
|
||||||
|
- search_results: The search results that need to be enriched with Filecoin robust addresses. |
||||||
|
|
||||||
|
## Returns |
||||||
|
|
||||||
|
- The search results with preloaded Filecoin robust addresses. |
||||||
|
""" |
||||||
|
@spec preload_and_put_filecoin_robust_address_to_search_results(list()) :: list() |
||||||
|
def preload_and_put_filecoin_robust_address_to_search_results(search_results) do |
||||||
|
addresses_map = |
||||||
|
search_results |
||||||
|
|> Enum.map(& &1["address"]) |
||||||
|
|> Enum.reject(&is_nil/1) |
||||||
|
|> Chain.hashes_to_addresses(@api_true) |
||||||
|
|> Enum.group_by(&to_string(&1.hash)) |
||||||
|
|
||||||
|
search_results |
||||||
|
|> Enum.map(fn |
||||||
|
%{"address" => address_hash} = result when not is_nil(address_hash) -> |
||||||
|
address = addresses_map[String.downcase(address_hash)] |> List.first() |
||||||
|
put_filecoin_robust_address(result, %{address: address, field_prefix: nil}) |
||||||
|
|
||||||
|
other -> |
||||||
|
other |
||||||
|
end) |
||||||
|
end |
||||||
end |
end |
||||||
end |
end |
||||||
|
Loading…
Reference in new issue