In this video, we’ll begin working on smart contract integration using Thirdweb. We’ll cover how to interact with deployed contracts and fetch contract metadata using Thirdweb’s React hooks.
Thirdweb provides pre-built hooks to streamline blockchain interactions. One of the key hooks is useContract, which we’ll use frequently throughout the project.
During the loading phase, we can display a loading bar or a status indicator. Once the contract is loaded, we can perform various operations, starting with fetching contract metadata.
To avoid hardcoding contract addresses repeatedly, we’ll create a reusable helper function. Here’s how we approach it:
We then create a helper function, such as getMarketplaceContract, which:
We do the same for NFTs with a getNFTContract function. These helpers simplify reuse and minimize redundant code.
We set up an InfoPage component where users can view the metadata for both:
To do this:
const { data: marketplaceMetadata, isLoading: marketplaceLoading } = useContractMetadata(marketplaceContract);
const { data: nftMetadata, isLoading: nftLoading } = useContractMetadata(nftContract);
These hooks update in real time, so you don't need to manually manage loading states.
Using our custom UI components, we render the fetched metadata:
If metadata is available, we render the corresponding section. If it’s still loading, we show a message like “Loading contract info…”.
{marketplaceLoading || nftLoading ? (
<div>Loading contract info...</div>
) : (
<>
{marketplaceMetadata && (
<ContractMetadata title="NFT Marketplace" metadata={marketplaceMetadata} />
)}
{nftMetadata && (
<ContractMetadata title="NFT Collection Metadata" metadata={nftMetadata} />
)}
</>
)}
This ensures users always see a responsive UI based on real-time contract state.
You’ve now set up reusable contract hooks and built an info page to display metadata for your NFT marketplace and collection contracts.
Swap insights and ask questions about “Learn everything about Chiliz”.
Ask a question or share your thoughts about this lesson.