Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
fix: return null from non existant contractURI query, reorg queries, …
Browse files Browse the repository at this point in the history
…add customIpfs prop throughout queries
  • Loading branch information
dblodorn committed Nov 7, 2022
1 parent 81b144e commit da85424
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 38 deletions.
13 changes: 12 additions & 1 deletion examples/nextjs-dapp/src/pages/drops-metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ function Page() {
return (
<section className="flex flex-col gap-4">
<h1>Render Drops Style Contract Metadata</h1>
<DropsContractProvider collectionAddress="0xb603d89f9f87fda06cd80c4a04ec6dc0440b8f42" networkId="5">
<DropsContractProvider collectionAddress="0xb603d89f9f87fda06cd80c4a04ec6dc0440b8f42" networkId="5" ipfsGateway="zora-prod.mypinata.cloud">
<DropsComponents.Thumbnail style={{maxWidth: 600}} />
<DropsComponents.Metadata />
<div className="flex flex-row gap-2">
<DropsComponents.MintButton />
<DropsComponents.MintQuantity />
</div>
<DropsComponents.TxStatus />
<DropsData />
</DropsContractProvider>
<h1>Render Editions Style Contract Metadata</h1>
<DropsContractProvider collectionAddress="0x213b55e62323f918b3fb45ac02882a885f9b1da3" networkId="5" ipfsGateway="zora-prod.mypinata.cloud">
<DropsComponents.Thumbnail style={{maxWidth: 600}} />
<DropsComponents.Metadata />
<div className="flex flex-row gap-2">
Expand Down
2 changes: 1 addition & 1 deletion packages/zora-drops-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@public-assembly/zora-drops-utils",
"version": "0.1.61",
"version": "0.1.62",
"description": "Example package with some web3 peer dependencies",
"main": "dist/public-assembly-zora-drops-utils.cjs.js",
"module": "dist/public-assembly-zora-drops-utils.esm.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function DropsContractProvider({
contractAddress: collectionAddress,
networkId: networkId,
refreshInterval: refreshInterval,
ipfsGateway: ipfsGateway,
})
const [error, setError] = React.useState<any | undefined>(undefined)
const [purchaseLoading, setPurchaseLoading] = React.useState(false)
Expand Down
8 changes: 5 additions & 3 deletions packages/zora-drops-utils/src/data/dropsArrayFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { dropsFetcher } from './dropsFetcher'
import { EDITION_QUERY } from './dropsQuery'
import { DropsArrayRequestProps } from '../typings/requestTypes'

export async function dropsArrayFetcher({
contractAddresses,
networkId = '1',
}: DropsArrayRequestProps) {
ipfsGateway,
}: DropsArrayRequestProps & {
ipfsGateway?: string
}) {
try {
const editions = await Promise.all(
contractAddresses.map(async (contractAddress) => {
const metadata = await dropsFetcher(
networkId,
contractAddress.toLowerCase(),
EDITION_QUERY
ipfsGateway
)
.then((res) => {
return res
Expand Down
33 changes: 10 additions & 23 deletions packages/zora-drops-utils/src/data/dropsFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
import { GraphQLClient } from 'graphql-request'
import { returnDropEndpoint } from '../constants'
import { Networks } from '../typings'
import { addIPFSGateway } from '../lib'

export async function decodeContractUri(contractURI?: string) {
const url = contractURI ? addIPFSGateway(contractURI) : undefined
try {
const contractURIData = await fetch(url)
.then((res) => res.text())
.then((t) => {
try {
return JSON.parse(t.replace(/\\n/g, ' '))
} catch (e) {
return undefined
}
})
return contractURIData
} catch (err) {
console.error(err)
}
}
import { DROPS_QUERY } from './dropsQuery'
import { decodeContractUri } from '../lib/decodeContractURI'

export async function dropsFetcher(
chainId: Networks,
collectionAddress: string,
query: any
ipfsGateway?: string
) {
const variables = {
collectionAddress: collectionAddress.toLowerCase(),
Expand All @@ -33,11 +16,15 @@ export async function dropsFetcher(
const endpoint = returnDropEndpoint(chainId)
const client = new GraphQLClient(endpoint, { headers: {} })

const data = await client.request(query, variables).then((data) => data?.erc721Drop)
const contractURI = await decodeContractUri(data?.contractURI)
const data = await client
.request(DROPS_QUERY, variables)
.then((data) => data?.erc721Drop)
const contractURI = data?.contractURI
? await decodeContractUri(data?.contractURI, ipfsGateway)
: null

return {
...data,
contractURI: contractURI,
contractURI: contractURI ? contractURI : null,
}
}
6 changes: 3 additions & 3 deletions packages/zora-drops-utils/src/data/dropsQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from 'graphql-request'

export const EDITIONS_STYLE_CONTRACT_METADATA = gql`
export const ERC721_DROPS_CONTRACT_METADATA = gql`
fragment ERC721Fields on ERC721Drop {
name
owner
Expand Down Expand Up @@ -28,11 +28,11 @@ export const EDITIONS_STYLE_CONTRACT_METADATA = gql`
}
`

export const EDITION_QUERY = gql`
export const DROPS_QUERY = gql`
query editionsStyleDropMetadataerc721Drops($collectionAddress: String!) {
erc721Drop(id: $collectionAddress) {
...ERC721Fields
}
}
${EDITIONS_STYLE_CONTRACT_METADATA}
${ERC721_DROPS_CONTRACT_METADATA}
`
12 changes: 9 additions & 3 deletions packages/zora-drops-utils/src/hooks/useDrop.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React from 'react'
import { EDITION_QUERY, dropsFetcher } from '../data'
import { dropsFetcher } from '../data'
import { DropsRequestProps } from '../typings'
import { useValidAddress } from './useValidAddress'

export function useDrop({ contractAddress, networkId = '1' }: DropsRequestProps) {
export function useDrop({
contractAddress,
networkId = '1',
ipfsGateway,
}: DropsRequestProps & {
ipfsGateway?: string
}) {
const [data, setData] = React.useState<any>(undefined)
const [error, setError] = React.useState<any>(undefined)
const [isLoading, setIsLoading] = React.useState(true)
Expand All @@ -15,7 +21,7 @@ export function useDrop({ contractAddress, networkId = '1' }: DropsRequestProps)
setIsLoading(true)

async function getDropsData() {
await dropsFetcher(networkId, contractAddress, EDITION_QUERY)
await dropsFetcher(networkId, contractAddress, ipfsGateway)
.then((res) => {
setData(res)
setIsLoading(false)
Expand Down
6 changes: 4 additions & 2 deletions packages/zora-drops-utils/src/hooks/useSWRDrop.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import useSWR from 'swr'
import { EDITION_QUERY, dropsFetcher } from '../data'
import { dropsFetcher } from '../data'
import { DropsRequestProps } from '../typings'
import { useValidAddress } from './useValidAddress'

export function useSWRDrop({
contractAddress,
networkId = '1',
refreshInterval = 2000,
ipfsGateway,
}: {
refreshInterval?: number
ipfsGateway?: string
} & DropsRequestProps) {
const isValidAddress = useValidAddress(contractAddress)

const { data, error, isValidating } = useSWR(
[networkId, contractAddress, EDITION_QUERY],
[networkId, contractAddress, ipfsGateway],
dropsFetcher,
{
isPaused: () => !isValidAddress,
Expand Down
4 changes: 2 additions & 2 deletions packages/zora-drops-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export {
EDITIONS_STYLE_CONTRACT_METADATA,
EDITION_QUERY,
ERC721_DROPS_CONTRACT_METADATA,
DROPS_QUERY,
dropsFetcher,
dropsArrayFetcher,
} from './data'
Expand Down
25 changes: 25 additions & 0 deletions packages/zora-drops-utils/src/lib/decodeContractURI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { addIPFSGateway } from './addIPFSGateway'

export async function decodeContractUri(contractURI?: string, ipfsGateway?: string) {
const url = !contractURI.startsWith('data:application')
? addIPFSGateway(contractURI, ipfsGateway)
: undefined

if (!url) return null

try {
const contractURIData = await fetch(url)
.then((res) => res.text())
.then((t) => {
try {
return JSON.parse(t.replace(/\\n/g, ' '))
} catch (e) {
return null
}
})
return contractURIData
} catch (err) {
console.error(err)
return null
}
}

0 comments on commit da85424

Please sign in to comment.