From c978721d032861d25a6f7a66bb24c34c8de2bd2b Mon Sep 17 00:00:00 2001 From: Diego Date: Sat, 10 Sep 2022 11:46:18 -0500 Subject: [PATCH 1/3] feat: alchemy sdk get nft metadata in nft component --- .../pages/web3-components/index.tsx | 5 +- packages/components/package.json | 1 + .../components/src/components/NFT/NFT.tsx | 56 +++++++++++-------- .../Web3uiProvider/Web3uiProvider.tsx | 28 ++++++---- yarn.lock | 2 +- 5 files changed, 56 insertions(+), 36 deletions(-) diff --git a/apps/playground/pages/web3-components/index.tsx b/apps/playground/pages/web3-components/index.tsx index a09ef7d..6d7a0ef 100644 --- a/apps/playground/pages/web3-components/index.tsx +++ b/apps/playground/pages/web3-components/index.tsx @@ -152,7 +152,10 @@ const Web3ComponentsPage: NextPage = () => { > NFT - + diff --git a/packages/components/package.json b/packages/components/package.json index 0fb467b..a1dba34 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -62,6 +62,7 @@ "jest": "^28.1.2", "jest-environment-jsdom": "^28.1.2", "react": "18.1.0", + "react-query": "^3.39.2", "ts-jest": "^28.0.5", "tsconfig": "*", "tsup": "^6.2.0", diff --git a/packages/components/src/components/NFT/NFT.tsx b/packages/components/src/components/NFT/NFT.tsx index fcaf0cb..08cc635 100644 --- a/packages/components/src/components/NFT/NFT.tsx +++ b/packages/components/src/components/NFT/NFT.tsx @@ -1,7 +1,11 @@ // import React, { useCallback, useEffect, useRef } from 'react'; import React, { useEffect } from 'react'; // import { useNFTsByOwner } from '@web3-ui/hooks'; -import { useNFTMetadata } from 'ankr-react'; +import { Alchemy } from 'alchemy-sdk'; +import { useQuery } from 'react-query'; + +// Using default settings - pass in a settings object to specify your API key and network +const alchemy = new Alchemy(); export type NFTProps = { /** @@ -27,30 +31,38 @@ export interface NFTData { animationUrl?: string; } +type getNFTMetadataProps = { + contractAddress?: string; + tokenId?: string; +}; + /** * Component to fetch and display NFT data */ -export const NFT = ({ - // contractAddress, - // tokenId, - size = 'xs', -}: NFTProps) => { - // const { data, error, isLoading } = useNFTMetadata({ - // blockchain: 'eth', - // contractAddress: '0xEdB61f74B0d09B2558F1eeb79B247c1F363Ae452', - // tokenId: '2276' - // }); - - // console.log('data', data); - - // working ankr api hook for nfts by owner - // const { data, error, isLoading } = useNFTsByOwner({ - // walletAddress: '0xb8c2C29ee19D8307cb7255e1Cd9CbDE883A267d5', - // blockchain: ['eth', 'polygon'] - // }); - // console.log('loading', isLoading); - // console.log('error', error); - // console.log(data); +export const NFT = ({ contractAddress, tokenId, size = 'xs' }: NFTProps) => { + const { data, isLoading } = useQuery( + ['getNFTMetadata', contractAddress, tokenId], + () => { + return fetchNft({ + contractAddress, + tokenId, + }); + }, + { + enabled: !!contractAddress && !!tokenId, + } + ); + + const fetchNft = async ({ + contractAddress, + tokenId, + }: getNFTMetadataProps) => { + if (!contractAddress || !tokenId) return null; + const data = await alchemy.nft.getNftMetadata(contractAddress!, tokenId!); + return data; + }; + + console.log(data); return
nft here
; }; diff --git a/packages/components/src/provider/Web3uiProvider/Web3uiProvider.tsx b/packages/components/src/provider/Web3uiProvider/Web3uiProvider.tsx index 73b8d0e..31deb24 100644 --- a/packages/components/src/provider/Web3uiProvider/Web3uiProvider.tsx +++ b/packages/components/src/provider/Web3uiProvider/Web3uiProvider.tsx @@ -4,7 +4,7 @@ import { Provider as AnkrProvider } from 'ankr-react'; // import { HooksProvider } from '@web3-ui/hooks'; // import { AlchemyProvider } from '@web3-ui/hooks'; import { CSS, getCssText, createTheme } from '../../theme/stitches.config'; - +import { QueryClient, QueryClientProvider } from 'react-query'; export interface IWeb3uiProviderProps { children: React.ReactNode; theme?: CSS | null; @@ -24,6 +24,8 @@ const createThemeRootSelector = (id: string | undefined) => { return id ? `[${attr}="${id}-theme"]` : `[${attr}]`; }; +const queryClient = new QueryClient(); + export const Web3uiProvider = ({ children, theme, @@ -55,16 +57,18 @@ export const Web3uiProvider = ({ // TODO dangerous set innerHTML is set for ssr. need to test this. https://stitches.dev/docs/server-side-rendering return ( - -
-