add endpoint to create decompiled smart contracts

pull/1596/head
Ayrat Badykov 6 years ago
parent 4a8ff4b511
commit bac51ee183
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 2
      apps/block_scout_web/lib/block_scout_web/router.ex
  2. 12
      apps/explorer/lib/explorer/chain.ex
  3. 2
      apps/explorer/lib/explorer/chain/decompiled_smart_contract.ex
  4. 26
      apps/explorer/test/explorer/chain_test.exs

@ -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

@ -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.

@ -14,6 +14,8 @@ defmodule Explorer.Chain.DecompiledSmartContract do
references: :hash,
type: Hash.Address
)
timestamps()
end
def changeset(%__MODULE__{} = smart_contract, attrs) do

@ -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 =

Loading…
Cancel
Save