|
|
@ -273,12 +273,12 @@ defmodule Explorer.Chain do |
|
|
|
inner_join: transaction in assoc(log, :transaction), |
|
|
|
inner_join: transaction in assoc(log, :transaction), |
|
|
|
order_by: [desc: transaction.block_number, desc: transaction.index], |
|
|
|
order_by: [desc: transaction.block_number, desc: transaction.index], |
|
|
|
preload: [:transaction], |
|
|
|
preload: [:transaction], |
|
|
|
where: |
|
|
|
where: transaction.block_number < ^block_number, |
|
|
|
log.address_hash == ^address_hash and |
|
|
|
or_where: transaction.block_number == ^block_number and transaction.index > ^transaction_index, |
|
|
|
(transaction.block_number < ^block_number or |
|
|
|
or_where: |
|
|
|
(transaction.block_number == ^block_number and transaction.index > ^transaction_index) or |
|
|
|
transaction.block_number == ^block_number and transaction.index == ^transaction_index and |
|
|
|
(transaction.block_number == ^block_number and transaction.index == ^transaction_index and |
|
|
|
log.index > ^log_index, |
|
|
|
log.index > ^log_index)), |
|
|
|
where: log.address_hash == ^address_hash, |
|
|
|
limit: ^paging_options.page_size, |
|
|
|
limit: ^paging_options.page_size, |
|
|
|
select: log |
|
|
|
select: log |
|
|
|
) |
|
|
|
) |
|
|
@ -676,31 +676,40 @@ defmodule Explorer.Chain do |
|
|
|
iex> Explorer.Chain.hash_to_address(hash) |
|
|
|
iex> Explorer.Chain.hash_to_address(hash) |
|
|
|
{:error, :not_found} |
|
|
|
{:error, :not_found} |
|
|
|
|
|
|
|
|
|
|
|
Optionally accepts: |
|
|
|
## Options |
|
|
|
- a list of bindings to preload, just like `Ecto.Query.preload/3` |
|
|
|
|
|
|
|
- a boolean to also fetch the `has_decompiled_code?` virtual field or not |
|
|
|
* `:necessity_by_association` - use to load `t:association/0` as `:required` or `:optional`. If an association is |
|
|
|
|
|
|
|
`:required`, and the `t:Explorer.Chain.Address.t/0` has no associated record for that association, |
|
|
|
|
|
|
|
then the `t:Explorer.Chain.Address.t/0` will not be included in the list. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Optionally it also accepts a boolean to fetch the `has_decompiled_code?` virtual field or not |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
@spec hash_to_address(Hash.Address.t(), [Macro.t()], boolean()) :: {:ok, Address.t()} | {:error, :not_found} |
|
|
|
@spec hash_to_address(Hash.Address.t(), [necessity_by_association_option], boolean()) :: |
|
|
|
|
|
|
|
{:ok, Address.t()} | {:error, :not_found} |
|
|
|
def hash_to_address( |
|
|
|
def hash_to_address( |
|
|
|
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash, |
|
|
|
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash, |
|
|
|
preloads \\ [ |
|
|
|
options \\ [ |
|
|
|
:contracts_creation_internal_transaction, |
|
|
|
necessity_by_association: %{ |
|
|
|
:names, |
|
|
|
:contracts_creation_internal_transaction => :optional, |
|
|
|
:smart_contract, |
|
|
|
:names => :optional, |
|
|
|
:token, |
|
|
|
:smart_contract => :optional, |
|
|
|
:contracts_creation_transaction |
|
|
|
:token => :optional, |
|
|
|
|
|
|
|
:contracts_creation_transaction => :optional |
|
|
|
|
|
|
|
} |
|
|
|
], |
|
|
|
], |
|
|
|
query_decompiled_code_flag \\ true |
|
|
|
query_decompiled_code_flag \\ true |
|
|
|
) do |
|
|
|
) do |
|
|
|
|
|
|
|
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) |
|
|
|
|
|
|
|
|
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
address in Address, |
|
|
|
address in Address, |
|
|
|
preload: ^preloads, |
|
|
|
|
|
|
|
where: address.hash == ^hash |
|
|
|
where: address.hash == ^hash |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
query |
|
|
|
query |
|
|
|
|
|
|
|
|> join_associations(necessity_by_association) |
|
|
|
|> with_decompiled_code_flag(hash, query_decompiled_code_flag) |
|
|
|
|> with_decompiled_code_flag(hash, query_decompiled_code_flag) |
|
|
|
|> Repo.one() |
|
|
|
|> Repo.one() |
|
|
|
|> case do |
|
|
|
|> case do |
|
|
@ -772,16 +781,39 @@ defmodule Explorer.Chain do |
|
|
|
iex> {:ok, %Explorer.Chain.Address{hash: found_hash}} = Explorer.Chain.hash_to_address(hash) |
|
|
|
iex> {:ok, %Explorer.Chain.Address{hash: found_hash}} = Explorer.Chain.hash_to_address(hash) |
|
|
|
iex> found_hash == hash |
|
|
|
iex> found_hash == hash |
|
|
|
true |
|
|
|
true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Options |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `:necessity_by_association` - use to load `t:association/0` as `:required` or `:optional`. If an association is |
|
|
|
|
|
|
|
`:required`, and the `t:Explorer.Chain.Address.t/0` has no associated record for that association, |
|
|
|
|
|
|
|
then the `t:Explorer.Chain.Address.t/0` will not be included in the list. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Optionally it also accepts a boolean to fetch the `has_decompiled_code?` virtual field or not |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
@spec find_or_insert_address_from_hash(Hash.Address.t()) :: {:ok, Address.t()} |
|
|
|
@spec find_or_insert_address_from_hash(Hash.Address.t(), [necessity_by_association_option], boolean()) :: |
|
|
|
def find_or_insert_address_from_hash(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do |
|
|
|
{:ok, Address.t()} |
|
|
|
case hash_to_address(hash) do |
|
|
|
def find_or_insert_address_from_hash( |
|
|
|
|
|
|
|
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash, |
|
|
|
|
|
|
|
options \\ [ |
|
|
|
|
|
|
|
necessity_by_association: %{ |
|
|
|
|
|
|
|
:contracts_creation_internal_transaction => :optional, |
|
|
|
|
|
|
|
:names => :optional, |
|
|
|
|
|
|
|
:smart_contract => :optional, |
|
|
|
|
|
|
|
:token => :optional, |
|
|
|
|
|
|
|
:contracts_creation_transaction => :optional |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
query_decompiled_code_flag \\ true |
|
|
|
|
|
|
|
) do |
|
|
|
|
|
|
|
case hash_to_address(hash, options, query_decompiled_code_flag) do |
|
|
|
{:ok, address} -> |
|
|
|
{:ok, address} -> |
|
|
|
{:ok, address} |
|
|
|
{:ok, address} |
|
|
|
|
|
|
|
|
|
|
|
{:error, :not_found} -> |
|
|
|
{:error, :not_found} -> |
|
|
|
create_address(%{hash: to_string(hash)}) |
|
|
|
create_address(%{hash: to_string(hash)}) |
|
|
|
hash_to_address(hash) |
|
|
|
hash_to_address(hash, options, query_decompiled_code_flag) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
@ -2715,21 +2747,30 @@ defmodule Explorer.Chain do |
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
|
Fetches a `t:Token.t/0` by an address hash. |
|
|
|
Fetches a `t:Token.t/0` by an address hash. |
|
|
|
|
|
|
|
|
|
|
|
Optionally accepts a list of bindings to preload, just like `Ecto.Query.preload/3` |
|
|
|
## Options |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `:necessity_by_association` - use to load `t:association/0` as `:required` or `:optional`. If an association is |
|
|
|
|
|
|
|
`:required`, and the `t:Token.t/0` has no associated record for that association, |
|
|
|
|
|
|
|
then the `t:Token.t/0` will not be included in the list. |
|
|
|
""" |
|
|
|
""" |
|
|
|
@spec token_from_address_hash(Hash.Address.t(), [Macro.t()]) :: {:ok, Token.t()} | {:error, :not_found} |
|
|
|
@spec token_from_address_hash(Hash.Address.t(), [necessity_by_association_option]) :: |
|
|
|
|
|
|
|
{:ok, Token.t()} | {:error, :not_found} |
|
|
|
def token_from_address_hash( |
|
|
|
def token_from_address_hash( |
|
|
|
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash, |
|
|
|
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash, |
|
|
|
preloads \\ [] |
|
|
|
options \\ [] |
|
|
|
) do |
|
|
|
) do |
|
|
|
|
|
|
|
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) |
|
|
|
|
|
|
|
|
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
token in Token, |
|
|
|
token in Token, |
|
|
|
where: token.contract_address_hash == ^hash, |
|
|
|
where: token.contract_address_hash == ^hash |
|
|
|
preload: ^preloads |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
case Repo.one(query) do |
|
|
|
query |
|
|
|
|
|
|
|
|> join_associations(necessity_by_association) |
|
|
|
|
|
|
|
|> Repo.one() |
|
|
|
|
|
|
|
|> case do |
|
|
|
nil -> |
|
|
|
nil -> |
|
|
|
{:error, :not_found} |
|
|
|
{:error, :not_found} |
|
|
|
|
|
|
|
|
|
|
|