Fix error on non-existent method id + regression test

pull/6642/head
Никита Поздняков 2 years ago
parent 75ad5b449d
commit 98685a8d1f
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 44
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/smart_contract_controller_test.exs
  2. 12
      apps/explorer/lib/explorer/smart_contract/reader.ex

@ -292,6 +292,50 @@ defmodule BlockScoutWeb.API.V2.SmartContractControllerTest do
} == response } == response
end end
test "query-read-method with nonexistent method_id", %{conn: conn} do
abi = [
%{
"type" => "function",
"stateMutability" => "view",
"outputs" => [%{"type" => "address", "name" => "", "internalType" => "address"}],
"name" => "getCaller",
"inputs" => []
},
%{
"type" => "function",
"stateMutability" => "view",
"outputs" => [%{"type" => "bool", "name" => "", "internalType" => "bool"}],
"name" => "isWhitelist",
"inputs" => [%{"type" => "address", "name" => "_address", "internalType" => "address"}]
},
%{
"type" => "function",
"stateMutability" => "nonpayable",
"outputs" => [],
"name" => "disableWhitelist",
"inputs" => [%{"type" => "bool", "name" => "disable", "internalType" => "bool"}]
}
]
blockchain_get_code_mock()
target_contract = insert(:smart_contract, abi: abi)
request =
post(conn, "/api/v2/smart-contracts/#{target_contract.address_hash}/query-read-method", %{
"contract_type" => "regular",
"args" => ["0xfffffffffffffffffffffffffffffffffffffffe"],
"method_id" => "00000000"
})
assert response = json_response(request, 200)
assert %{
"is_error" => true,
"result" => %{"error" => "method_id does not exist"}
} == response
end
test "query-read-method returns error 1", %{conn: conn} do test "query-read-method returns error 1", %{conn: conn} do
abi = [ abi = [
%{ %{

@ -436,9 +436,12 @@ defmodule Explorer.SmartContract.Reader do
abi abi
|> ABI.parse_specification() |> ABI.parse_specification()
%{outputs: outputs, method_id: method_id} = proccess_abi(parsed_final_abi, method_id) with %{outputs: outputs, method_id: method_id} <- proccess_abi(parsed_final_abi, method_id) do
query_contract_and_link_outputs(contract_address_hash, args, from, abi, outputs, method_id, leave_error_as_map) query_contract_and_link_outputs(contract_address_hash, args, from, abi, outputs, method_id, leave_error_as_map)
else
{:error, message} ->
{:error, message}
end
end end
@spec query_function_with_custom_abi( @spec query_function_with_custom_abi(
@ -515,10 +518,15 @@ defmodule Explorer.SmartContract.Reader do
defp proccess_abi(abi, method_id) do defp proccess_abi(abi, method_id) do
function_object = find_function_by_method(abi, method_id) function_object = find_function_by_method(abi, method_id)
if function_object do
%ABI.FunctionSelector{returns: returns, method_id: method_id} = function_object %ABI.FunctionSelector{returns: returns, method_id: method_id} = function_object
outputs = extract_outputs(returns) outputs = extract_outputs(returns)
%{outputs: outputs, method_id: method_id} %{outputs: outputs, method_id: method_id}
else
{:error, "method_id does not exist"}
end
end end
defp query_contract_and_link_outputs(contract_address_hash, args, from, abi, outputs, method_id, leave_error_as_map) do defp query_contract_and_link_outputs(contract_address_hash, args, from, abi, outputs, method_id, leave_error_as_map) do

Loading…
Cancel
Save