Home

GraphProtocol: Read IPFS Data in Subgraph

Things to note

Add the following in the top of subgraph manifest subgraph.yaml

# ...
features:
  - ipfsOnEthereumContracts
# ...

Use the following function to get the data stored in IPFS as a string

import { Bytes, ByteArray, ipfs } from "@graphprotocol/graph-ts";

export function getDataFromIpfsHash(hash: Bytes): string {
  const hashStr = hash.toHexString();
  const hashHex = "1220" + hashStr.slice(2);
  const ipfsHashBase58 = ByteArray.fromHexString(hashHex).toBase58();

  const ipfsHash = ipfsHashBase58; // readable IPFS hash

  let result = ipfs.cat(ipfsHashBase58);

  if (!result) {
    return "";
  }

  const ipfsBytes = result;
  const ipfsData = ByteArray.fromHexString(result.toHexString()).toString();

  return ipfsData
}


Last Updated on

Next Post: Rules vs Guidelines →

Comments