From c6acfa0668b4720018857ca3c59e2c03d4e77f5e Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 15:49:47 +0700 Subject: [PATCH 1/9] Fix bens variables to snake_case and change from POST to GET --- .../explorer/microservice_interfaces/bens.ex | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index 186f599b12..afa77e7eec 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -54,32 +54,32 @@ defmodule Explorer.MicroserviceInterfaces.BENS do @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 @@ -106,6 +106,27 @@ defmodule Explorer.MicroserviceInterfaces.BENS do end end + def http_get_request(url, query_params) do + 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] @@ -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 }} From 31e48a4c710adb1f542a1b5ffcdfae35c18444a3 Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 16:38:33 +0700 Subject: [PATCH 2/9] Fix token_transfers bug --- .../lib/explorer/microservice_interfaces/bens.ex | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index afa77e7eec..454af2a3ec 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -270,9 +270,17 @@ 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, }) do - [to_string(to_address_hash), to_string(from_address_hash)] + token_transfers_addresses = List.flatten(Enum.map(token_transfers, &item_to_address_hash_strings/1)) + Logger.info(fn -> + [ + "aboba", + inspect(token_transfers_addresses) + ] + end) + [to_string(to_address_hash), to_string(from_address_hash)] ++ token_transfers_addresses end defp item_to_address_hash_strings(%TokenTransfer{ @@ -329,7 +337,8 @@ defmodule Explorer.MicroserviceInterfaces.BENS do 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: Enum.map(tx.token_transfers, &put_ens_name_to_item(&1, names)) } end From d1ee2e78e6f561933a5125845e0083170edaa3da Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 16:56:14 +0700 Subject: [PATCH 3/9] Add #9062 changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 480901a0b5..f4f63d9919 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 684c4a0c15899146175a32317a60555d9fb675f9 Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 16:56:35 +0700 Subject: [PATCH 4/9] mix format --- apps/explorer/lib/explorer/microservice_interfaces/bens.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index 454af2a3ec..fd30124e46 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -271,15 +271,17 @@ defmodule Explorer.MicroserviceInterfaces.BENS do to_address_hash: to_address_hash, created_contract_address_hash: nil, from_address_hash: from_address_hash, - token_transfers: token_transfers, + token_transfers: token_transfers }) do token_transfers_addresses = List.flatten(Enum.map(token_transfers, &item_to_address_hash_strings/1)) + Logger.info(fn -> [ "aboba", inspect(token_transfers_addresses) ] end) + [to_string(to_address_hash), to_string(from_address_hash)] ++ token_transfers_addresses end From 98385a5c8d2c78ed192c2bc9ee64a5c48537b28c Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 16:58:17 +0700 Subject: [PATCH 5/9] Remove print debug --- .../explorer/lib/explorer/microservice_interfaces/bens.ex | 8 -------- 1 file changed, 8 deletions(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index fd30124e46..9f6e450787 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -274,14 +274,6 @@ defmodule Explorer.MicroserviceInterfaces.BENS do token_transfers: token_transfers }) do token_transfers_addresses = List.flatten(Enum.map(token_transfers, &item_to_address_hash_strings/1)) - - Logger.info(fn -> - [ - "aboba", - inspect(token_transfers_addresses) - ] - end) - [to_string(to_address_hash), to_string(from_address_hash)] ++ token_transfers_addresses end From 0db3a3173f0e2365c2c86e4fbbdc17d3f8588a72 Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 17:01:42 +0700 Subject: [PATCH 6/9] Fix comment --- apps/explorer/lib/explorer/microservice_interfaces/bens.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index 9f6e450787..02e42b24cf 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -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 @@ -49,7 +49,7 @@ 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 From 1ba76320a3f64a8c25dcc7568f1e8d379def84f8 Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 18:03:31 +0700 Subject: [PATCH 7/9] Handle NotLoaded --- .../lib/explorer/microservice_interfaces/bens.ex | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index 02e42b24cf..e70c2606e7 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -273,7 +273,12 @@ defmodule Explorer.MicroserviceInterfaces.BENS do from_address_hash: from_address_hash, token_transfers: token_transfers }) do - token_transfers_addresses = List.flatten(Enum.map(token_transfers, &item_to_address_hash_strings/1)) + 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 @@ -327,12 +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), - token_transfers: Enum.map(tx.token_transfers, &put_ens_name_to_item(&1, names)) + token_transfers: token_transfers } end From 7f8a3d2d0f7593739d5f16158ab0aa44f8d46bfe Mon Sep 17 00:00:00 2001 From: Lymarenko Lev Date: Tue, 26 Dec 2023 20:15:00 +0700 Subject: [PATCH 8/9] Make func private --- apps/explorer/lib/explorer/microservice_interfaces/bens.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index e70c2606e7..b0b4106670 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -106,7 +106,7 @@ defmodule Explorer.MicroserviceInterfaces.BENS do end end - def http_get_request(url, query_params) do + defp http_get_request(url, query_params) do case HTTPoison.get("#{url}?#{URI.encode_query(query_params)}") do {:ok, %Response{body: body, status_code: 200}} -> Jason.decode(body) From e879849d23dbf2d5a3c6b7d10cde4f8cd86a2710 Mon Sep 17 00:00:00 2001 From: Nikita Pozdniakov Date: Tue, 26 Dec 2023 21:12:51 +0300 Subject: [PATCH 9/9] Refactoring --- .../lib/explorer/microservice_interfaces/bens.ex | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex index b0b4106670..59eb1c8240 100644 --- a/apps/explorer/lib/explorer/microservice_interfaces/bens.ex +++ b/apps/explorer/lib/explorer/microservice_interfaces/bens.ex @@ -107,7 +107,7 @@ defmodule Explorer.MicroserviceInterfaces.BENS do end defp http_get_request(url, query_params) do - case HTTPoison.get("#{url}?#{URI.encode_query(query_params)}") do + case HTTPoison.get(url, [], params: query_params) do {:ok, %Response{body: body, status_code: 200}} -> Jason.decode(body) @@ -275,8 +275,11 @@ defmodule Explorer.MicroserviceInterfaces.BENS do }) do token_transfers_addresses = case token_transfers do - %NotLoaded{} -> [] - _ -> List.flatten(Enum.map(token_transfers, &item_to_address_hash_strings/1)) + token_transfers_list when is_list(token_transfers_list) -> + List.flatten(Enum.map(token_transfers_list, &item_to_address_hash_strings/1)) + + _ -> + [] end [to_string(to_address_hash), to_string(from_address_hash)] ++ token_transfers_addresses @@ -334,8 +337,11 @@ defmodule Explorer.MicroserviceInterfaces.BENS do ) do token_transfers = case tx.token_transfers do - %NotLoaded{} -> %NotLoaded{} - token_transfers -> Enum.map(token_transfers, &put_ens_name_to_item(&1, names)) + token_transfers_list when is_list(token_transfers_list) -> + Enum.map(token_transfers_list, &put_ens_name_to_item(&1, names)) + + other -> + other end %Transaction{