|
|
|
@ -3616,6 +3616,115 @@ defmodule Explorer.Chain do |
|
|
|
|
Repo.exists?(query) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Checks if a `t:Explorer.Chain.Token.t/0` with the given `hash` exists. |
|
|
|
|
|
|
|
|
|
Returns `:ok` if found |
|
|
|
|
|
|
|
|
|
iex> address = insert(:address) |
|
|
|
|
iex> insert(:token, contract_address: address) |
|
|
|
|
iex> Explorer.Chain.check_token_exists(address.hash) |
|
|
|
|
:ok |
|
|
|
|
|
|
|
|
|
Returns `:not_found` if not found |
|
|
|
|
|
|
|
|
|
iex> {:ok, hash} = Explorer.Chain.string_to_address_hash("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") |
|
|
|
|
iex> Explorer.Chain.check_token_exists(hash) |
|
|
|
|
:not_found |
|
|
|
|
""" |
|
|
|
|
@spec check_token_exists(Hash.Address.t()) :: :ok | :not_found |
|
|
|
|
def check_token_exists(hash) do |
|
|
|
|
hash |
|
|
|
|
|> token_exists?() |
|
|
|
|
|> boolean_to_check_result() |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Checks if a `t:Explorer.Chain.Token.t/0` with the given `hash` exists. |
|
|
|
|
|
|
|
|
|
Returns `true` if found |
|
|
|
|
|
|
|
|
|
iex> address = insert(:address) |
|
|
|
|
iex> insert(:token, contract_address: address) |
|
|
|
|
iex> Explorer.Chain.token_exists?(address.hash) |
|
|
|
|
true |
|
|
|
|
|
|
|
|
|
Returns `false` if not found |
|
|
|
|
|
|
|
|
|
iex> {:ok, hash} = Explorer.Chain.string_to_address_hash("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") |
|
|
|
|
iex> Explorer.Chain.token_exists?(hash) |
|
|
|
|
false |
|
|
|
|
""" |
|
|
|
|
@spec token_exists?(Hash.Address.t()) :: boolean() |
|
|
|
|
def token_exists?(hash) do |
|
|
|
|
query = |
|
|
|
|
from( |
|
|
|
|
token in Token, |
|
|
|
|
where: token.contract_address_hash == ^hash |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
Repo.exists?(query) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Checks if a `t:Explorer.Chain.TokenTransfer.t/0` with the given `hash` and `token_id` exists. |
|
|
|
|
|
|
|
|
|
Returns `:ok` if found |
|
|
|
|
|
|
|
|
|
iex> contract_address = insert(:address) |
|
|
|
|
iex> token_id = 10 |
|
|
|
|
iex> insert(:token_transfer, |
|
|
|
|
...> from_address: contract_address, |
|
|
|
|
...> token_contract_address: contract_address, |
|
|
|
|
...> token_id: token_id |
|
|
|
|
...> ) |
|
|
|
|
iex> Explorer.Chain.check_erc721_token_instance_exists(token_id, contract_address.hash) |
|
|
|
|
:ok |
|
|
|
|
|
|
|
|
|
Returns `:not_found` if not found |
|
|
|
|
|
|
|
|
|
iex> {:ok, hash} = Explorer.Chain.string_to_address_hash("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") |
|
|
|
|
iex> Explorer.Chain.check_erc721_token_instance_exists(10, hash) |
|
|
|
|
:not_found |
|
|
|
|
""" |
|
|
|
|
@spec check_erc721_token_instance_exists(binary() | non_neg_integer(), Hash.Address.t()) :: :ok | :not_found |
|
|
|
|
def check_erc721_token_instance_exists(token_id, hash) do |
|
|
|
|
token_id |
|
|
|
|
|> erc721_token_instance_exist?(hash) |
|
|
|
|
|> boolean_to_check_result() |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Checks if a `t:Explorer.Chain.TokenTransfer.t/0` with the given `hash` and `token_id` exists. |
|
|
|
|
|
|
|
|
|
Returns `true` if found |
|
|
|
|
|
|
|
|
|
iex> contract_address = insert(:address) |
|
|
|
|
iex> token_id = 10 |
|
|
|
|
iex> insert(:token_transfer, |
|
|
|
|
...> from_address: contract_address, |
|
|
|
|
...> token_contract_address: contract_address, |
|
|
|
|
...> token_id: token_id |
|
|
|
|
...> ) |
|
|
|
|
iex> Explorer.Chain.erc721_token_instance_exist?(token_id, contract_address.hash) |
|
|
|
|
true |
|
|
|
|
|
|
|
|
|
Returns `false` if not found |
|
|
|
|
|
|
|
|
|
iex> {:ok, hash} = Explorer.Chain.string_to_address_hash("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") |
|
|
|
|
iex> Explorer.Chain.erc721_token_instance_exist?(10, hash) |
|
|
|
|
false |
|
|
|
|
""" |
|
|
|
|
@spec erc721_token_instance_exist?(binary() | non_neg_integer(), Hash.Address.t()) :: boolean() |
|
|
|
|
def erc721_token_instance_exist?(token_id, hash) do |
|
|
|
|
query = |
|
|
|
|
from(tt in TokenTransfer, |
|
|
|
|
where: tt.token_contract_address_hash == ^hash and tt.token_id == ^token_id |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
Repo.exists?(query) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp boolean_to_check_result(true), do: :ok |
|
|
|
|
|
|
|
|
|
defp boolean_to_check_result(false), do: :not_found |
|
|
|
|