Fetch data from IPFS URI
  • In smart contracts, some methods return a URI pointing to IPFS.
  • To fetch data from this URI we need first to register an oracle:
oracle(ipfs, r, 'hyperdapp.mypinata.cloud/ipfs/').
  • Then, we need to extract the CID from the IPFS URI. The URI root is what comes before the CID. For example, https://ipfs.io/ipfs/
// SYNTAX
extract_cid([URI, CID]) :-
atom_chars(URI, S0),
append(URI_ROOT, S1, S0),
append(S2, "", S1),
atom_chars(CID, S2).
// EXAMPLE
extract_cid([URI, CID]) :-
atom_chars(URI, S0),
append(https://ipfs.io/ipfs/, S1, S0),
append(S2, "", S1),
atom_chars(CID, S2).
  • Finally, we can prompt the data returned from IPFS:
prompt :-
call_fn(hyperdapp, tokenURI('55'), [URI]),
extract_cid([URI, CID]),
get_http(ipfs, CID, {
name: Name,
description: Description,
image: Image
}),
show [
text('Name: ', Name),
text('Description: ', Description),
image(Image)
].
  • We call the tokenURI method from the smart contract.
  • We extract the CID from the URI.
  • We use the get_http() predicate on the ipfs oracle. The output can be a big object, but we extract only what we need to display. For example, the name, description and image.
The complete dApp treated in this example can be found here - RINKEBY
Copy link