diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c9ab72b71..72e991bd36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ### Fixes +- [#6094](https://github.com/blockscout/blockscout/pull/6094) - Fix inconsistent behaviour of `getsourcecode` method + ### Chore ## 4.1.8-beta 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 d1e132b15e..4411c28ba2 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 @@ -7,7 +7,7 @@ defmodule BlockScoutWeb.API.RPC.ContractController do alias BlockScoutWeb.API.RPC.Helpers alias Explorer.Chain alias Explorer.Chain.Events.Publisher, as: EventsPublisher - alias Explorer.Chain.{Hash, SmartContract} + alias Explorer.Chain.{Address, Hash, SmartContract} alias Explorer.Chain.SmartContract.VerificationStatus alias Explorer.Etherscan.Contracts alias Explorer.SmartContract.Helper @@ -404,7 +404,7 @@ defmodule BlockScoutWeb.API.RPC.ContractController do address = Contracts.address_hash_to_address_with_source_code(address_hash) render(conn, :getsourcecode, %{ - contract: address + contract: address || %Address{hash: address_hash, smart_contract: nil} }) else {:address_param, :error} -> @@ -412,9 +412,6 @@ defmodule BlockScoutWeb.API.RPC.ContractController do {:format, :error} -> render(conn, :error, error: @invalid_address) - - {:contract, :not_found} -> - render(conn, :getsourcecode, %{contract: nil, address_hash: nil}) end end diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex index e0e9d43ec0..ace6af7517 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex @@ -3,6 +3,7 @@ defmodule BlockScoutWeb.API.RPC.ContractView do alias BlockScoutWeb.AddressView alias BlockScoutWeb.API.RPC.RPCView + alias Ecto.Association.NotLoaded alias Explorer.Chain alias Explorer.Chain.{Address, DecompiledSmartContract, SmartContract} @@ -34,25 +35,6 @@ defmodule BlockScoutWeb.API.RPC.ContractView do RPCView.render("show.json", data: result) end - defp prepare_source_code_contract(nil) do - %{ - "Address" => "", - "SourceCode" => "", - "ABI" => "Contract source code not verified", - "ContractName" => "", - "CompilerVersion" => "", - "DecompiledSourceCode" => "", - "DecompilerVersion" => decompiler_version(nil), - "OptimizationUsed" => "", - "OptimizationRuns" => "", - "EVMVersion" => "", - "ConstructorArguments" => "", - "ExternalLibraries" => "", - "FileName" => "", - "IsProxy" => "false" - } - end - defp prepare_source_code_contract(address) do decompiled_smart_contract = latest_decompiled_smart_contract(address.decompiled_smart_contracts) contract = address.smart_contract || %{} @@ -220,6 +202,8 @@ defmodule BlockScoutWeb.API.RPC.ContractView do } end + defp latest_decompiled_smart_contract(%NotLoaded{}), do: nil + defp latest_decompiled_smart_contract([]), do: nil defp latest_decompiled_smart_contract(contracts) do diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/contract_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/contract_controller_test.exs index 4f56efb7f0..6f0c4f6fb0 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/contract_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/contract_controller_test.exs @@ -396,20 +396,7 @@ defmodule BlockScoutWeb.API.RPC.ContractControllerTest do expected_result = [ %{ - "Address" => "", - "SourceCode" => "", - "ABI" => "Contract source code not verified", - "ContractName" => "", - "CompilerVersion" => "", - "OptimizationUsed" => "", - "DecompiledSourceCode" => "", - "DecompilerVersion" => "", - "ConstructorArguments" => "", - "EVMVersion" => "", - "ExternalLibraries" => "", - "OptimizationRuns" => "", - "FileName" => "", - "IsProxy" => "false" + "Address" => "0x8bf38d4764929064f2d4d3a56520a76ab3df415b" } ]