Skip to content

Commit

Permalink
include token metadata call
Browse files Browse the repository at this point in the history
  • Loading branch information
salieflewis committed Jun 28, 2023
1 parent 2062e1a commit 5b694d6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/builder-utils/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export * from './useInterval'
export * from './useMinBidAmount'
export * from './useSettle'
export * from './useTokenExplorer'
export * from './useTokenMetadata'
export * from './useVote'
35 changes: 35 additions & 0 deletions packages/builder-utils/src/hooks/useTokenMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import parseBase64String from '../lib/parseBase64String'
import { tokenAbi } from '../abi'
import { useTokenContext } from '../context'
import { useEffect, useState } from 'react'
import { useContractRead } from 'wagmi'

type TokenMetadata = {
description: string
image: string
name: string
properties?: string
}

export function useTokenMetadata({ tokenId }: { tokenId: number }) {
const [json, setJson] = useState<TokenMetadata>()

const { tokenAddress } = useTokenContext()

const { data: tokenUri } = useContractRead({
address: tokenAddress,
abi: tokenAbi,
functionName: 'tokenURI',
args: [BigInt(tokenId)],
})

useEffect(() => {
if (tokenUri) {
setJson(parseBase64String(tokenUri))
}
}, [tokenId])

return {
json: json as TokenMetadata,
}
}
1 change: 1 addition & 0 deletions packages/builder-utils/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './etherscanLink'
export * from './formatFromUnix'
export * from './parseBase64String'
export * from './shortenAddress'
7 changes: 7 additions & 0 deletions packages/builder-utils/src/lib/parseBase64String.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const parseBase64String = (val: string) => {
const clean: string = val?.substring(29)
const json = Buffer.from(clean, 'base64').toString()
return JSON.parse(json)
}

export default parseBase64String

0 comments on commit 5b694d6

Please sign in to comment.