In this video, we complete the core logic for our NFT Marketplace application by implementing the functionality for listing NFTs for sale and canceling active listings using Thirdweb’s tools.
Previously, we set up the UI components:
In this video, we implement the logic behind both features.
To list an NFT:
We retrieve the Marketplace and NFT contract using:
getMarketplaceContract()
getNFTContract()
We use Thirdweb’s useGrantRole hook:
const { mutate: grantRole, error: roleError } = useGrantRole(nftContract);
This allows us to assign the admin role to the marketplace address.
We then use:
const { mutate: createDirectListing, isLoading: listingLoading, error: listError } = useCreateDirectListing(marketplace);
The listing details are structured using a utility function createListingFromPriceId(price, tokenId) that encapsulates all required listing parameters (except the user-set price and token ID).
Once structured, we execute:
createDirectListing(listing);
We manage UI feedback using conditional checks:
{listingLoading && <div className="text-center mt-4">Listing in progress...</div>}
Canceling a listing is simpler since we’re directly interacting with the marketplace contract.
const { mutate: cancelListing, isLoading, error } = useCancelDirectListing(marketplace);
When the user clicks the cancel button, we execute:
cancelListing({ listingId });
This function is triggered inside a try-catch block to handle any runtime errors.
Just like with listing, we handle:
{isLoading && <div className="text-center mt-4">Canceling listing...</div>}
With this video, we’ve completed:
The NFT Marketplace application is now fully functional, supporting wallet authentication, viewing NFT metadata, dynamic listing controls, and real-time marketplace management.
Swap insights and ask questions about “Learn everything about Chiliz”.
Ask a question or share your thoughts about this lesson.