From e1541db6d6dd009e9743af3f84905582ddd8bd67 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Tue, 12 Apr 2022 15:18:37 +0300 Subject: [PATCH] Logging error in publishing of smart-contract --- CHANGELOG.md | 1 + .../api/rpc/contract_controller.ex | 47 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bd408bf21..c4de622554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ - [#5239](https://github.com/blockscout/blockscout/pull/5239) - Add accounting for block rewards in `getblockreward` api method ### Chore +- [#5450](https://github.com/blockscout/blockscout/pull/5450) - Logging error in publishing of smart-contract - [#5433](https://github.com/blockscout/blockscout/pull/5433) - Caching modules refactoring - [#5419](https://github.com/blockscout/blockscout/pull/5419) - Add check if address exists for some api methods - [#5408](https://github.com/blockscout/blockscout/pull/5408) - Update websocket_client hex package diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/contract_controller.ex index 83676fa555..ae289a38a2 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/contract_controller.ex @@ -1,6 +1,8 @@ defmodule BlockScoutWeb.API.RPC.ContractController do use BlockScoutWeb, :controller + require Logger + alias BlockScoutWeb.AddressContractVerificationController, as: VerificationController alias BlockScoutWeb.API.RPC.Helpers alias Explorer.Chain @@ -13,6 +15,12 @@ defmodule BlockScoutWeb.API.RPC.ContractController do alias Explorer.SmartContract.Vyper.Publisher, as: VyperPublisher alias Explorer.ThirdPartyIntegrations.Sourcify + @smth_went_wrong "Something went wrong while publishing the contract." + @verified "Smart-contract already verified." + @invalid_address "Invalid address hash" + @invalid_args "Invalid args format" + @address_required "Query parameter address is required" + def verify(conn, %{"addressHash" => address_hash} = params) do with {:params, {:ok, fetched_params}} <- {:params, fetch_verify_params(params)}, {:format, {:ok, casted_address_hash}} <- to_address_hash(address_hash), @@ -36,13 +44,20 @@ defmodule BlockScoutWeb.API.RPC.ContractController do ]} ] }}} -> - render(conn, :error, error: "Smart-contract already verified.") + render(conn, :error, error: @verified) - {:publish, _} -> - render(conn, :error, error: "Something went wrong while publishing the contract.") + {:publish, error} -> + Logger.error(fn -> + [ + @smth_went_wrong, + inspect(error) + ] + end) + + render(conn, :error, error: @smth_went_wrong) {:format, :error} -> - render(conn, :error, error: "Invalid address hash") + render(conn, :error, error: @invalid_address) {:params, {:error, error}} -> render(conn, :error, error: error) @@ -58,7 +73,7 @@ defmodule BlockScoutWeb.API.RPC.ContractController do end if Chain.smart_contract_fully_verified?(address_hash) do - render(conn, :error, error: "Smart-contract already verified.") + render(conn, :error, error: @verified) else case Sourcify.check_by_address(address_hash) do {:ok, _verified_status} -> @@ -97,10 +112,10 @@ defmodule BlockScoutWeb.API.RPC.ContractController do render(conn, :show, %{result: uid}) else {:check_verified_status, true} -> - render(conn, :error, error: "Smart-contract already verified.", data: "Smart-contract already verified") + render(conn, :error, error: @verified, data: @verified) {:format, :error} -> - render(conn, :error, error: "Invalid address hash", data: "Invalid address hash") + render(conn, :error, error: @invalid_address, data: @invalid_address) {:params, {:error, error}} -> render(conn, :error, error: error, data: error) @@ -128,7 +143,7 @@ defmodule BlockScoutWeb.API.RPC.ContractController do end defp prepare_params(files) when is_struct(files) do - {:error, "Invalid args format"} + {:error, @invalid_args} end defp prepare_params(files) when is_map(files) do @@ -140,7 +155,7 @@ defmodule BlockScoutWeb.API.RPC.ContractController do end defp prepare_params(_arg) do - {:error, "Invalid args format"} + {:error, @invalid_args} end defp validate_files(files) do @@ -269,13 +284,13 @@ defmodule BlockScoutWeb.API.RPC.ContractController do ]} ] }}} -> - render(conn, :error, error: "Smart-contract already verified.") + render(conn, :error, error: @verified) {:publish, _} -> - render(conn, :error, error: "Something went wrong while publishing the contract.") + render(conn, :error, error: @smth_went_wrong) {:format, :error} -> - render(conn, :error, error: "Invalid address hash") + render(conn, :error, error: @invalid_address) {:params, {:error, error}} -> render(conn, :error, error: error) @@ -357,10 +372,10 @@ defmodule BlockScoutWeb.API.RPC.ContractController do render(conn, :getabi, %{abi: contract.abi}) else {:address_param, :error} -> - render(conn, :error, error: "Query parameter address is required") + render(conn, :error, error: @address_required) {:format, :error} -> - render(conn, :error, error: "Invalid address hash") + render(conn, :error, error: @invalid_address) {:contract, :not_found} -> render(conn, :error, error: "Contract source code not verified") @@ -378,10 +393,10 @@ defmodule BlockScoutWeb.API.RPC.ContractController do }) else {:address_param, :error} -> - render(conn, :error, error: "Query parameter address is required") + render(conn, :error, error: @address_required) {:format, :error} -> - render(conn, :error, error: "Invalid address hash") + render(conn, :error, error: @invalid_address) {:contract, :not_found} -> render(conn, :getsourcecode, %{contract: nil, address_hash: nil})