Skip to content

Commit

Permalink
Merge pull request #113 from storyprotocol/soyoung/ip-metadata-from-ipfs
Browse files Browse the repository at this point in the history
fix: merge `ipMetadata` with ipfs data
  • Loading branch information
SoYoung210 authored Jan 19, 2025
2 parents e993b51 + 5ca50af commit f533a3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/storykit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@storyprotocol/storykit",
"author": "storyprotocol engineering <[email protected]>",
"version": "0.2.7",
"version": "0.2.8",
"type": "module",
"exports": {
".": {
Expand Down
6 changes: 6 additions & 0 deletions packages/storykit/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ export async function listResource<T>(
console.error(error)
}
}

export async function getMetadataFromIpfs(ipfsUrl: string) {
const metadata = await fetch(ipfsUrl).then((res) => res.json())
console.log("@@ metadata", metadata)
return metadata
}
18 changes: 15 additions & 3 deletions packages/storykit/src/providers/IpProvider/IpProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useInfiniteQuery, useQuery } from "@tanstack/react-query"
import React from "react"
import { Address, Hash } from "viem"

import { getResource, listResource } from "../../lib/api"
import { getMetadataFromIpfs, getResource, listResource } from "../../lib/api"
import { getNFTByTokenId } from "../../lib/simplehash"
import { RESOURCE_TYPE } from "../../types/api"
import {
Expand Down Expand Up @@ -111,12 +111,24 @@ export const IpProvider = ({
})

// Fetch IP metadata
const { isLoading: isIpaMetadataLoading, data: ipaMetadata } = useQuery({
const { isLoading: isIpaMetadataLoading, data: ipaMetadataRaw } = useQuery({
queryKey: [RESOURCE_TYPE.ASSET, `${ipId}/metadata`],
queryFn: () => getResource(RESOURCE_TYPE.ASSET, `${ipId}/metadata`, chain.name as STORYKIT_SUPPORTED_CHAIN),
enabled: queryOptions.ipaMetadata,
})

// Fetch IP Metadata from IPFS
const { isLoading: isLoadingFromIpfs, data: metadaFromIpfs } = useQuery({
queryKey: ["getMetadataFromIpfs", ipId, ipaMetadataRaw?.metadataUri ?? ""],
queryFn: () => getMetadataFromIpfs(ipaMetadataRaw?.metadataUri ?? ""),
enabled: queryOptions.ipaMetadata && ipaMetadataRaw != null,
})

const ipaMetadata = {
...ipaMetadataRaw,
metadataJson: metadaFromIpfs,
}

const fetchParentEdgeOptions = {
pagination: {
limit: 500,
Expand Down Expand Up @@ -346,7 +358,7 @@ export const IpProvider = ({
loadMoreAssetChildren,
isAssetChildrenDataLoading,
ipaMetadata,
isIpaMetadataLoading,
isIpaMetadataLoading: isIpaMetadataLoading || isLoadingFromIpfs,
ipLicenseData: ipLicenseData?.data,
isipLicenseDataLoading,
licenseTermsData,
Expand Down

0 comments on commit f533a3f

Please sign in to comment.