|
|
@ -554,9 +554,19 @@ defmodule Explorer.Chain do |
|
|
|
|
|
|
|
|
|
|
|
@spec create_decompiled_smart_contract(map()) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()} |
|
|
|
@spec create_decompiled_smart_contract(map()) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()} |
|
|
|
def create_decompiled_smart_contract(attrs) do |
|
|
|
def create_decompiled_smart_contract(attrs) do |
|
|
|
%DecompiledSmartContract{} |
|
|
|
changeset = DecompiledSmartContract.changeset(%DecompiledSmartContract{}, attrs) |
|
|
|
|> DecompiledSmartContract.changeset(attrs) |
|
|
|
|
|
|
|
|> Repo.insert(on_conflict: :replace_all, conflict_target: [:decompiler_version, :address_hash]) |
|
|
|
Multi.new() |
|
|
|
|
|
|
|
|> Multi.insert(:decompiled_smart_contract, changeset, |
|
|
|
|
|
|
|
on_conflict: :replace_all, |
|
|
|
|
|
|
|
conflict_target: [:decompiler_version, :address_hash] |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|> Multi.run(:set_address_decompiled, &set_address_decompiled/2) |
|
|
|
|
|
|
|
|> Repo.transaction() |
|
|
|
|
|
|
|
|> case do |
|
|
|
|
|
|
|
{:ok, %{decompiled_smart_contract: decompiled_smart_contract}} -> {:ok, decompiled_smart_contract} |
|
|
|
|
|
|
|
{:error, _, error_value, _} -> {:error, error_value} |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
@ -2235,6 +2245,7 @@ defmodule Explorer.Chain do |
|
|
|
|> Multi.insert(:smart_contract, smart_contract_changeset) |
|
|
|
|> Multi.insert(:smart_contract, smart_contract_changeset) |
|
|
|
|> Multi.run(:clear_primary_address_names, &clear_primary_address_names/2) |
|
|
|
|> Multi.run(:clear_primary_address_names, &clear_primary_address_names/2) |
|
|
|
|> Multi.run(:insert_address_name, &create_address_name/2) |
|
|
|
|> Multi.run(:insert_address_name, &create_address_name/2) |
|
|
|
|
|
|
|
|> Multi.run(:set_address_verified, &set_address_verified/2) |
|
|
|
|> Repo.transaction() |
|
|
|
|> Repo.transaction() |
|
|
|
|
|
|
|
|
|
|
|
with {:ok, %{smart_contract: smart_contract}} <- insert_result do |
|
|
|
with {:ok, %{smart_contract: smart_contract}} <- insert_result do |
|
|
@ -2242,6 +2253,35 @@ defmodule Explorer.Chain do |
|
|
|
else |
|
|
|
else |
|
|
|
{:error, :smart_contract, changeset, _} -> |
|
|
|
{:error, :smart_contract, changeset, _} -> |
|
|
|
{:error, changeset} |
|
|
|
{:error, changeset} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:error, :set_address_verified, message, _} -> |
|
|
|
|
|
|
|
{:error, message} |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp set_address_verified(repo, %{smart_contract: %SmartContract{address_hash: address_hash}}) do |
|
|
|
|
|
|
|
query = |
|
|
|
|
|
|
|
from( |
|
|
|
|
|
|
|
address in Address, |
|
|
|
|
|
|
|
where: address.hash == ^address_hash |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case repo.update_all(query, set: [verified: true]) do |
|
|
|
|
|
|
|
{1, _} -> {:ok, []} |
|
|
|
|
|
|
|
_ -> {:error, "There was an error annotating that the address has been verified."} |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp set_address_decompiled(repo, %{decompiled_smart_contract: %DecompiledSmartContract{address_hash: address_hash}}) do |
|
|
|
|
|
|
|
query = |
|
|
|
|
|
|
|
from( |
|
|
|
|
|
|
|
address in Address, |
|
|
|
|
|
|
|
where: address.hash == ^address_hash |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case repo.update_all(query, set: [decompiled: true]) do |
|
|
|
|
|
|
|
{1, _} -> {:ok, []} |
|
|
|
|
|
|
|
_ -> {:error, "There was an error annotating that the address has been verified."} |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
@ -2754,50 +2794,46 @@ defmodule Explorer.Chain do |
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
address in Address, |
|
|
|
address in Address, |
|
|
|
where: |
|
|
|
where: address.contract_code != <<>>, |
|
|
|
fragment( |
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
"EXISTS (SELECT 1 FROM decompiled_smart_contracts WHERE decompiled_smart_contracts.address_hash = ?)", |
|
|
|
where: address.decompiled == true, |
|
|
|
address.hash |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
preload: [:decompiled_smart_contracts, :smart_contract], |
|
|
|
|
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
|
|
|
|
limit: ^limit, |
|
|
|
limit: ^limit, |
|
|
|
offset: ^offset |
|
|
|
offset: ^offset, |
|
|
|
|
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
|
|
|
|
preload: [:smart_contract] |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
query |
|
|
|
query |
|
|
|
|> filter_decompiled_with_version(not_decompiled_with_version) |
|
|
|
|> reject_decompiled_with_version(not_decompiled_with_version) |
|
|
|
|> Repo.all() |
|
|
|
|> Repo.all() |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp filter_decompiled_with_version(query, nil) do |
|
|
|
defp reject_decompiled_with_version(query, nil), do: query |
|
|
|
query |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp filter_decompiled_with_version(query, not_decompiled_with_version) do |
|
|
|
defp reject_decompiled_with_version(query, reject_version) do |
|
|
|
from(address in query, |
|
|
|
from( |
|
|
|
left_join: decompiled_smart_contract in DecompiledSmartContract, |
|
|
|
address in query, |
|
|
|
on: decompiled_smart_contract.decompiler_version == ^not_decompiled_with_version, |
|
|
|
left_join: decompiled_smart_contract in assoc(address, :decompiled_smart_contracts), |
|
|
|
on: decompiled_smart_contract.address_hash == address.hash, |
|
|
|
on: decompiled_smart_contract.decompiler_version == ^reject_version, |
|
|
|
where: is_nil(decompiled_smart_contract.id), |
|
|
|
where: is_nil(decompiled_smart_contract.address_hash) |
|
|
|
distinct: [address.hash] |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def list_verified_contracts(limit, offset) do |
|
|
|
def list_verified_contracts(limit, offset) do |
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
address in Address, |
|
|
|
smart_contract in SmartContract, |
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
order_by: [asc: smart_contract.inserted_at], |
|
|
|
join: smart_contract in SmartContract, |
|
|
|
|
|
|
|
on: smart_contract.address_hash == address.hash, |
|
|
|
|
|
|
|
preload: [{:smart_contract, smart_contract}, :decompiled_smart_contracts], |
|
|
|
|
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
|
|
|
|
limit: ^limit, |
|
|
|
limit: ^limit, |
|
|
|
offset: ^offset |
|
|
|
offset: ^offset, |
|
|
|
|
|
|
|
preload: [:address] |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
Repo.all(query) |
|
|
|
query |
|
|
|
|
|
|
|
|> Repo.all() |
|
|
|
|
|
|
|
|> Enum.map(fn smart_contract -> |
|
|
|
|
|
|
|
Map.put(smart_contract.address, :smart_contract, smart_contract) |
|
|
|
|
|
|
|
end) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def list_contracts(limit, offset) do |
|
|
|
def list_contracts(limit, offset) do |
|
|
@ -2805,7 +2841,7 @@ defmodule Explorer.Chain do |
|
|
|
from( |
|
|
|
from( |
|
|
|
address in Address, |
|
|
|
address in Address, |
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
preload: [:smart_contract, :decompiled_smart_contracts], |
|
|
|
preload: [:smart_contract], |
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
limit: ^limit, |
|
|
|
limit: ^limit, |
|
|
|
offset: ^offset |
|
|
|
offset: ^offset |
|
|
@ -2814,22 +2850,22 @@ defmodule Explorer.Chain do |
|
|
|
Repo.all(query) |
|
|
|
Repo.all(query) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def list_unverified_contracts(limit, offset) do |
|
|
|
def list_unordered_unverified_contracts(limit, offset) do |
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
address in Address, |
|
|
|
address in Address, |
|
|
|
left_join: smart_contract in SmartContract, |
|
|
|
|
|
|
|
on: smart_contract.address_hash == address.hash, |
|
|
|
|
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
|
|
|
|
where: is_nil(smart_contract.address_hash), |
|
|
|
|
|
|
|
where: address.contract_code != <<>>, |
|
|
|
where: address.contract_code != <<>>, |
|
|
|
preload: [{:smart_contract, smart_contract}, :decompiled_smart_contracts], |
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
where: fragment("? IS NOT TRUE", address.verified), |
|
|
|
limit: ^limit, |
|
|
|
limit: ^limit, |
|
|
|
offset: ^offset |
|
|
|
offset: ^offset |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
Repo.all(query) |
|
|
|
query |
|
|
|
|
|
|
|
|> Repo.all() |
|
|
|
|
|
|
|
|> Enum.map(fn address -> |
|
|
|
|
|
|
|
%{address | smart_contract: nil} |
|
|
|
|
|
|
|
end) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def list_empty_contracts(limit, offset) do |
|
|
|
def list_empty_contracts(limit, offset) do |
|
|
@ -2845,30 +2881,23 @@ defmodule Explorer.Chain do |
|
|
|
Repo.all(query) |
|
|
|
Repo.all(query) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def list_not_decompiled_contracts(limit, offset) do |
|
|
|
def list_unordered_not_decompiled_contracts(limit, offset) do |
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
address in Address, |
|
|
|
address in Address, |
|
|
|
where: |
|
|
|
where: fragment("? IS NOT TRUE", address.verified), |
|
|
|
fragment( |
|
|
|
where: fragment("? IS NOT TRUE", address.decompiled), |
|
|
|
"NOT EXISTS (SELECT 1 FROM decompiled_smart_contracts WHERE decompiled_smart_contracts.address_hash = ?)", |
|
|
|
|
|
|
|
address.hash |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
where: address.contract_code != <<>>, |
|
|
|
where: address.contract_code != <<>>, |
|
|
|
left_join: smart_contract in SmartContract, |
|
|
|
|
|
|
|
on: smart_contract.address_hash == address.hash, |
|
|
|
|
|
|
|
left_join: decompiled_smart_contract in DecompiledSmartContract, |
|
|
|
|
|
|
|
on: decompiled_smart_contract.address_hash == address.hash, |
|
|
|
|
|
|
|
preload: [:smart_contract, :decompiled_smart_contracts], |
|
|
|
|
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
where: not is_nil(address.contract_code), |
|
|
|
where: is_nil(smart_contract.address_hash), |
|
|
|
|
|
|
|
where: is_nil(decompiled_smart_contract.address_hash), |
|
|
|
|
|
|
|
order_by: [asc: address.inserted_at], |
|
|
|
|
|
|
|
limit: ^limit, |
|
|
|
limit: ^limit, |
|
|
|
offset: ^offset |
|
|
|
offset: ^offset |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
Repo.all(query) |
|
|
|
query |
|
|
|
|
|
|
|
|> Repo.all() |
|
|
|
|
|
|
|
|> Enum.map(fn address -> |
|
|
|
|
|
|
|
%{address | smart_contract: nil} |
|
|
|
|
|
|
|
end) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
|