@ -48,33 +48,50 @@ 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
defp fetch_json ( %{ " tokenURI " = > { :ok , [ " " ] } } ) do
def fetch_json ( %{ " tokenURI " = > { :ok , [ " " ] } } ) do
{ :ok , %{ error : @no_uri_error } }
{ :ok , %{ error : @no_uri_error } }
end
end
defp fetch_json ( %{ " tokenURI " = > { :error , " (-32015) VM execution error. " } } ) do
def fetch_json ( %{ " tokenURI " = > { :error , " (-32015) VM execution error. " } } ) do
{ :ok , %{ error : @no_uri_error } }
{ :ok , %{ error : @no_uri_error } }
end
end
defp fetch_json ( %{ " tokenURI " = > { :ok , [ " http:// " <> _ = token_uri ] } } ) do
def fetch_json ( %{ " tokenURI " = > { :ok , [ " http:// " <> _ = token_uri ] } } ) do
fetch_metadata ( token_uri )
fetch_metadata ( token_uri )
end
end
defp fetch_json ( %{ " tokenURI " = > { :ok , [ " https:// " <> _ = token_uri ] } } ) do
def fetch_json ( %{ " tokenURI " = > { :ok , [ " https:// " <> _ = token_uri ] } } ) do
fetch_metadata ( token_uri )
fetch_metadata ( token_uri )
end
end
defp fetch_json ( %{ " tokenURI " = > { :ok , [ json ] } } ) do
def fetch_json ( %{ " tokenURI " = > { :ok , [ " data:application/json, " <> json ] } } ) do
Jason . decode ( json )
decoded_json = URI . decode ( json )
fetch_json ( %{ " tokenURI " = > { :ok , [ decoded_json ] } } )
rescue
e ->
Logger . debug ( [ " Unknown metadata format #{ inspect ( json ) } . error #{ inspect ( e ) } " ] ,
fetcher : :token_instances
)
{ :error , json }
end
def fetch_json ( %{ " tokenURI " = > { :ok , [ json ] } } ) do
{ :ok , json } = decode_json ( json )
{ :ok , %{ metadata : json } }
rescue
rescue
e ->
e ->
Logger . error ( fn -> [ " Unknown metadata format #{ inspect ( json ) } . error #{ inspect ( e ) } " ] end )
Logger . debug ( [ " Unknown metadata format #{ inspect ( json ) } . error #{ inspect ( e ) } " ] ,
fetcher : :token_instances
)
{ :error , json }
{ :error , json }
end
end
defp fetch_json ( result ) do
def fetch_json ( result ) do
Logger . error ( fn -> [ " Unknown metadata format #{ inspect ( result ) } . " ] end )
Logger . debug ( [ " Unknown metadata format #{ inspect ( result ) } . " ] , fetcher : :token_instances )
{ :error , result }
{ :error , result }
end
end
@ -82,7 +99,7 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
defp fetch_metadata ( token_uri ) do
defp fetch_metadata ( token_uri ) do
case HTTPoison . get ( token_uri ) do
case HTTPoison . get ( token_uri ) do
{ :ok , % Response { body : body , status_code : 200 } } ->
{ :ok , % Response { body : body , status_code : 200 } } ->
{ :ok , json } = Jason . decode ( body )
{ :ok , json } = decode_json ( body )
{ :ok , %{ metadata : json } }
{ :ok , %{ metadata : json } }
@ -94,8 +111,20 @@ defmodule Explorer.Token.InstanceMetadataRetriever do
end
end
rescue
rescue
e ->
e ->
Logger . error ( fn -> [ " Could not send request to token uri #{ inspect ( token_uri ) } . error #{ inspect ( e ) } " ] end )
Logger . debug ( [ " Could not send request to token uri #{ inspect ( token_uri ) } . error #{ inspect ( e ) } " ] ,
fetcher : :token_instances
)
{ :error , :request_error }
{ :error , :request_error }
end
end
defp decode_json ( body ) do
if String . valid? ( body ) do
Jason . decode ( body )
else
body
|> :unicode . characters_to_binary ( :latin1 )
|> Jason . decode ( )
end
end
end
end