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