fetch ERC721 token instance by token_id

pull/2642/head
Ayrat Badykov 5 years ago
parent 81d2b10959
commit efdd82e8d8
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 16
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance_controller.ex
  2. 7
      apps/block_scout_web/lib/block_scout_web/web_router.ex
  3. 15
      apps/explorer/lib/explorer/chain.ex
  4. 19
      apps/explorer/test/explorer/chain_test.exs

@ -0,0 +1,16 @@
defmodule BlockScoutWeb.Tokens.InstanceController do
use BlockScoutWeb, :controller
alias Explorer.Chain
def show(conn, %{"id" => token_id, "token_address_hash" => token_address_hash}) do
with {:ok, token_address} <- Chain.hash_to_address(token_address_hash, []),
{:ok, token_transfer} <-
Chain.erc721_token_instance_from_token_id_and_token_address(token_id, token_address.hash) do
json(conn, token_transfer)
else
_ ->
not_found(conn)
end
end
end

@ -178,6 +178,13 @@ defmodule BlockScoutWeb.WebRouter do
only: [:index],
as: :inventory
)
resources(
"/token_instance",
Tokens.InstanceController,
only: [:show],
as: :instance
)
end
resources(

@ -2959,6 +2959,21 @@ defmodule Explorer.Chain do
|> Repo.all()
end
@spec erc721_token_instance_from_token_id_and_token_address(binary(), Hash.Address.t()) :: TokenTransfer.t() | nil
def erc721_token_instance_from_token_id_and_token_address(token_id, token_contract_address) do
query =
from(tt in TokenTransfer,
where: tt.token_contract_address_hash == ^token_contract_address and tt.token_id == ^token_id,
order_by: [desc: tt.block_number],
limit: 1
)
case Repo.one(query) do
nil -> {:error, :not_found}
token_instance -> {:ok, token_instance}
end
end
@spec address_to_coin_balances(Hash.Address.t(), [paging_options]) :: []
def address_to_coin_balances(address_hash, options) do
paging_options = Keyword.get(options, :paging_options, @default_paging_options)

@ -82,6 +82,25 @@ defmodule Explorer.ChainTest do
end
end
describe "ERC721_token_instance_from_token_id_and_token_address/2" do
test "return ERC721 token instance" do
contract_address = insert(:address)
token_id = 10
insert(:token_transfer,
from_address: contract_address,
token_contract_address: contract_address,
token_id: token_id
)
assert {:ok, result} =
Chain.erc721_token_instance_from_token_id_and_token_address(token_id, contract_address.hash)
assert result.token_id == Decimal.new(token_id)
end
end
describe "address_to_logs/2" do
test "fetches logs" do
%Address{hash: address_hash} = address = insert(:address)

Loading…
Cancel
Save