|
|
@ -7,7 +7,8 @@ defmodule BlockScoutWeb.API.V1.DecompiledSmartContractController do |
|
|
|
def create(conn, params) do |
|
|
|
def create(conn, params) do |
|
|
|
if auth_token(conn) == actual_token() do |
|
|
|
if auth_token(conn) == actual_token() do |
|
|
|
with {:ok, hash} <- validate_address_hash(params["address_hash"]), |
|
|
|
with {:ok, hash} <- validate_address_hash(params["address_hash"]), |
|
|
|
:ok <- smart_contract_exists?(hash) do |
|
|
|
:ok <- smart_contract_exists?(hash), |
|
|
|
|
|
|
|
:ok <- decompiled_contract_exists?(params["address_hash"], params["decompiler_version"]) do |
|
|
|
case Chain.create_decompiled_smart_contract(params) do |
|
|
|
case Chain.create_decompiled_smart_contract(params) do |
|
|
|
{:ok, decompiled_smart_contract} -> |
|
|
|
{:ok, decompiled_smart_contract} -> |
|
|
|
send_resp(conn, :created, Jason.encode!(decompiled_smart_contract)) |
|
|
|
send_resp(conn, :created, Jason.encode!(decompiled_smart_contract)) |
|
|
@ -22,8 +23,18 @@ defmodule BlockScoutWeb.API.V1.DecompiledSmartContractController do |
|
|
|
send_resp(conn, :unprocessable_entity, encode(errors)) |
|
|
|
send_resp(conn, :unprocessable_entity, encode(errors)) |
|
|
|
end |
|
|
|
end |
|
|
|
else |
|
|
|
else |
|
|
|
:invalid_address -> send_resp(conn, :unprocessable_entity, encode(%{error: "address_hash is invalid"})) |
|
|
|
:invalid_address -> |
|
|
|
:not_found -> send_resp(conn, :unprocessable_entity, encode(%{error: "address is not found"})) |
|
|
|
send_resp(conn, :unprocessable_entity, encode(%{error: "address_hash is invalid"})) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:not_found -> |
|
|
|
|
|
|
|
send_resp(conn, :unprocessable_entity, encode(%{error: "address is not found"})) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:contract_exists -> |
|
|
|
|
|
|
|
send_resp( |
|
|
|
|
|
|
|
conn, |
|
|
|
|
|
|
|
:unprocessable_entity, |
|
|
|
|
|
|
|
encode(%{error: "decompiled code already exists for the decompiler version"}) |
|
|
|
|
|
|
|
) |
|
|
|
end |
|
|
|
end |
|
|
|
else |
|
|
|
else |
|
|
|
send_resp(conn, :forbidden, "") |
|
|
|
send_resp(conn, :forbidden, "") |
|
|
@ -44,6 +55,13 @@ defmodule BlockScoutWeb.API.V1.DecompiledSmartContractController do |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp decompiled_contract_exists?(address_hash, decompiler_version) do |
|
|
|
|
|
|
|
case Chain.decompiled_code(address_hash, decompiler_version) do |
|
|
|
|
|
|
|
{:ok, _} -> :contract_exists |
|
|
|
|
|
|
|
_ -> :ok |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp auth_token(conn) do |
|
|
|
defp auth_token(conn) do |
|
|
|
case get_req_header(conn, "auth_token") do |
|
|
|
case get_req_header(conn, "auth_token") do |
|
|
|
[token] -> token |
|
|
|
[token] -> token |
|
|
|