Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bens integration #9062

Merged
merged 9 commits into from Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Fixes

- [#9062](https://github.com/blockscout/blockscout/pull/9062) - Fix blockscout-ens integration
- [#9061](https://github.com/blockscout/blockscout/pull/9061) - Arbitrum allow tx receipt gasUsedForL1 field

### Chore
Expand Down
65 changes: 50 additions & 15 deletions apps/explorer/lib/explorer/microservice_interfaces/bens.ex
Expand Up @@ -35,7 +35,7 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
| Withdrawal.t()

@doc """
Batch request for ENS names via {{baseUrl}}/api/v1/:chainId/addresses:batch-resolve-names
Batch request for ENS names via POST {{baseUrl}}/api/v1/:chainId/addresses:batch-resolve-names
"""
@spec ens_names_batch_request([binary()]) :: {:error, :disabled | binary() | Jason.DecodeError.t()} | {:ok, any}
def ens_names_batch_request(addresses) do
Expand All @@ -49,37 +49,37 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
end

@doc """
Request for ENS name via {{baseUrl}}/api/v1/:chainId/addresses:lookup
Request for ENS name via GET {{baseUrl}}/api/v1/:chainId/addresses:lookup
"""
@spec address_lookup(binary()) :: {:error, :disabled | binary() | Jason.DecodeError.t()} | {:ok, any}
def address_lookup(address) do
with :ok <- Microservice.check_enabled(__MODULE__) do
body = %{
query_params = %{
"address" => to_string(address),
"resolvedTo" => true,
"ownedBy" => false,
"onlyActive" => true,
"resolved_to" => true,
"owned_by" => false,
"only_active" => true,
"order" => "ASC"
}

http_post_request(address_lookup_url(), body)
http_get_request(address_lookup_url(), query_params)
end
end

@doc """
Lookup for ENS domain name via {{baseUrl}}/api/v1/:chainId/domains:lookup
Lookup for ENS domain name via GET {{baseUrl}}/api/v1/:chainId/domains:lookup
"""
@spec ens_domain_lookup(binary()) :: {:error, :disabled | binary() | Jason.DecodeError.t()} | {:ok, any}
def ens_domain_lookup(domain) do
with :ok <- Microservice.check_enabled(__MODULE__) do
body = %{
query_params = %{
"name" => domain,
"onlyActive" => true,
"only_active" => true,
"sort" => "registration_date",
"order" => "DESC"
}

http_post_request(domain_lookup_url(), body)
http_get_request(domain_lookup_url(), query_params)
end
end

Expand All @@ -106,6 +106,27 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
end
end

def http_get_request(url, query_params) do
sevenzing marked this conversation as resolved.
Show resolved Hide resolved
case HTTPoison.get("#{url}?#{URI.encode_query(query_params)}") do
{:ok, %Response{body: body, status_code: 200}} ->
Jason.decode(body)

{_, error} ->
old_truncate = Application.get_env(:logger, :truncate)
Logger.configure(truncate: :infinity)

Logger.error(fn ->
[
"Error while sending request to BENS microservice url: #{url}: ",
inspect(error, limit: :infinity, printable_limit: :infinity)
]
end)

Logger.configure(truncate: old_truncate)
{:error, @request_error_msg}
end
end

@spec enabled?() :: boolean
def enabled?, do: Application.get_env(:explorer, __MODULE__)[:enabled]

Expand Down Expand Up @@ -221,7 +242,7 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
%{
"items" =>
[
%{"name" => name, "expiryDate" => expiry_date, "resolvedAddress" => %{"hash" => address_hash_string}}
%{"name" => name, "expiry_date" => expiry_date, "resolved_address" => %{"hash" => address_hash_string}}
| _other
] = items
}}
Expand Down Expand Up @@ -249,9 +270,16 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
defp item_to_address_hash_strings(%Transaction{
to_address_hash: to_address_hash,
created_contract_address_hash: nil,
from_address_hash: from_address_hash
from_address_hash: from_address_hash,
token_transfers: token_transfers
nikitosing marked this conversation as resolved.
Show resolved Hide resolved
}) do
[to_string(to_address_hash), to_string(from_address_hash)]
token_transfers_addresses =
case token_transfers do
%NotLoaded{} -> []
_ -> List.flatten(Enum.map(token_transfers, &item_to_address_hash_strings/1))
end

[to_string(to_address_hash), to_string(from_address_hash)] ++ token_transfers_addresses
end

defp item_to_address_hash_strings(%TokenTransfer{
Expand Down Expand Up @@ -304,11 +332,18 @@ defmodule Explorer.MicroserviceInterfaces.BENS do
} = tx,
names
) do
token_transfers =
case tx.token_transfers do
%NotLoaded{} -> %NotLoaded{}
token_transfers -> Enum.map(token_transfers, &put_ens_name_to_item(&1, names))
end

%Transaction{
tx
| to_address: alter_address(tx.to_address, to_address_hash, names),
created_contract_address: alter_address(tx.created_contract_address, created_contract_address_hash, names),
from_address: alter_address(tx.from_address, from_address_hash, names)
from_address: alter_address(tx.from_address, from_address_hash, names),
token_transfers: token_transfers
}
end

Expand Down