Get nft tokens metadata from IPFS support

pull/3278/head
Victor Baranov 4 years ago
parent 3685e95cc0
commit 3755bbb983
  1. 1
      CHANGELOG.md
  2. 23
      apps/explorer/lib/explorer/token/instance_metadata_retriever.ex

@ -1,6 +1,7 @@
## Current ## Current
### Features ### Features
- [#3278](https://github.com/poanetwork/blockscout/pull/3278) - Support of fetching of NFT tokens metadata from IPFS
- [#3273](https://github.com/poanetwork/blockscout/pull/3273) - Update token metadata at burn/mint events - [#3273](https://github.com/poanetwork/blockscout/pull/3273) - Update token metadata at burn/mint events
- [#3268](https://github.com/poanetwork/blockscout/pull/3268) - Token total supply on-demand fetcher - [#3268](https://github.com/poanetwork/blockscout/pull/3268) - Token total supply on-demand fetcher
- [#3261](https://github.com/poanetwork/blockscout/pull/3261) - Bridged tokens table - [#3261](https://github.com/poanetwork/blockscout/pull/3261) - Bridged tokens table

@ -8,6 +8,8 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
alias Explorer.SmartContract.Reader alias Explorer.SmartContract.Reader
alias HTTPoison.{Error, Response} alias HTTPoison.{Error, Response}
@token_uri "c87b56dd"
@abi [ @abi [
%{ %{
"type" => "function", "type" => "function",
@ -38,7 +40,7 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
def fetch_metadata(contract_address_hash, token_id) do def fetch_metadata(contract_address_hash, token_id) do
# c87b56dd = keccak256(tokenURI(uint256)) # c87b56dd = keccak256(tokenURI(uint256))
contract_functions = %{"c87b56dd" => [token_id]} contract_functions = %{@token_uri => [token_id]}
contract_address_hash contract_address_hash
|> query_contract(contract_functions) |> query_contract(contract_functions)
@ -49,26 +51,26 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
Reader.query_contract(contract_address_hash, @abi, contract_functions) Reader.query_contract(contract_address_hash, @abi, contract_functions)
end end
def fetch_json(%{"c87b56dd" => {:ok, [""]}}) do def fetch_json(%{@token_uri => {:ok, [""]}}) do
{:ok, %{error: @no_uri_error}} {:ok, %{error: @no_uri_error}}
end end
def fetch_json(%{"c87b56dd" => {:error, "(-32015) VM execution error."}}) do def fetch_json(%{@token_uri => {:error, "(-32015) VM execution error."}}) do
{:ok, %{error: @no_uri_error}} {:ok, %{error: @no_uri_error}}
end end
def fetch_json(%{"c87b56dd" => {:ok, ["http://" <> _ = token_uri]}}) do def fetch_json(%{@token_uri => {:ok, ["http://" <> _ = token_uri]}}) do
fetch_metadata(token_uri) fetch_metadata(token_uri)
end end
def fetch_json(%{"c87b56dd" => {:ok, ["https://" <> _ = token_uri]}}) do def fetch_json(%{@token_uri => {:ok, ["https://" <> _ = token_uri]}}) do
fetch_metadata(token_uri) fetch_metadata(token_uri)
end end
def fetch_json(%{"c87b56dd" => {:ok, ["data:application/json," <> json]}}) do def fetch_json(%{@token_uri => {:ok, ["data:application/json," <> json]}}) do
decoded_json = URI.decode(json) decoded_json = URI.decode(json)
fetch_json(%{"c87b56dd" => {:ok, [decoded_json]}}) fetch_json(%{@token_uri => {:ok, [decoded_json]}})
rescue rescue
e -> e ->
Logger.debug(["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"], Logger.debug(["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"],
@ -78,7 +80,12 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
{:error, json} {:error, json}
end end
def fetch_json(%{"c87b56dd" => {:ok, [json]}}) do def fetch_json(%{@token_uri => {:ok, ["ipfs://ipfs/" <> ipfs_uid]}}) do
ipfs_url = "https://ipfs.io/ipfs/" <> ipfs_uid
fetch_metadata(ipfs_url)
end
def fetch_json(%{@token_uri => {:ok, [json]}}) do
{:ok, json} = decode_json(json) {:ok, json} = decode_json(json)
check_type(json) check_type(json)

Loading…
Cancel
Save