diff --git a/apps/block_scout_web/lib/block_scout_web/router.ex b/apps/block_scout_web/lib/block_scout_web/router.ex index cf40492b28..d13ff1a85e 100644 --- a/apps/block_scout_web/lib/block_scout_web/router.ex +++ b/apps/block_scout_web/lib/block_scout_web/router.ex @@ -22,6 +22,8 @@ defmodule BlockScoutWeb.Router do pipe_through(:api) get("/supply", SupplyController, :supply) + + resources("/decompiled_smart_contract", DecompiledSmartContractController, only: [:create]) end scope "/api", BlockScoutWeb.API.RPC do diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index a28b6aac96..fbce4407d6 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -31,6 +31,7 @@ defmodule Explorer.Chain do Block, BlockNumberCache, Data, + DecompiledSmartContract, Hash, Import, InternalTransaction, @@ -512,6 +513,17 @@ defmodule Explorer.Chain do |> Repo.insert() end + @doc """ + Creates a decompiled smart contract. + """ + + @spec create_decompiled_smart_contract(map()) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()} + def create_decompiled_smart_contract(attrs) do + %DecompiledSmartContract{} + |> DecompiledSmartContract.changeset(attrs) + |> Repo.insert() + end + @doc """ Converts the `Explorer.Chain.Data.t:t/0` to `iodata` representation that can be written to users efficiently. diff --git a/apps/explorer/lib/explorer/chain/decompiled_smart_contract.ex b/apps/explorer/lib/explorer/chain/decompiled_smart_contract.ex index 99cea2e3cc..4b509b7ec0 100644 --- a/apps/explorer/lib/explorer/chain/decompiled_smart_contract.ex +++ b/apps/explorer/lib/explorer/chain/decompiled_smart_contract.ex @@ -14,6 +14,8 @@ defmodule Explorer.Chain.DecompiledSmartContract do references: :hash, type: Hash.Address ) + + timestamps() end def changeset(%__MODULE__{} = smart_contract, attrs) do diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 259e20e05e..899e187714 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -2586,6 +2586,32 @@ defmodule Explorer.ChainTest do end end + describe "create_decompiled_smart_contract/1" do + test "with valid params creates decompiled smart contract" do + address_hash = to_string(insert(:address).hash) + decompiler_version = "test_decompiler" + decompiled_source_code = "hello world" + + params = %{ + address_hash: address_hash, + decompiler_version: decompiler_version, + decompiled_source_code: decompiled_source_code + } + + {:ok, decompiled_smart_contract} = Chain.create_decompiled_smart_contract(params) + + assert decompiled_smart_contract.decompiler_version == decompiler_version + assert decompiled_smart_contract.decompiled_source_code == decompiled_source_code + assert address_hash == to_string(decompiled_smart_contract.address_hash) + end + + test "with invalid params can't create decompiled smart contract" do + params = %{code: "cat"} + + {:error, _changeset} = Chain.create_decompiled_smart_contract(params) + end + end + describe "create_smart_contract/1" do setup do smart_contract_bytecode =