File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ 'use client' ;
3+ import { useEffect , useState } from "react" ;
4+ import { useScaffoldReadContract , useScaffoldWriteContract } from "~~/hooks/scaffold-eth" ;
5+ import { useAccount } from "wagmi" ;
6+
7+ // Get Max Supply for the NFT Collection
8+ // The first step involves using the useScaffoldReadContract hook to read data from your smart contract.
9+ // The code snippet retrieves the maximum supply of NFTs allowed in the collection
10+ const MintNFT = ( ) => {
11+ const { data : supply } = useScaffoldReadContract ( {
12+ contractName : "NFT" ,
13+ functionName : "maxSupply" ,
14+ } ) ;
15+
16+ // Get Current Token ID
17+ const { data : tokenID } = useScaffoldReadContract ( {
18+ contractName : "NFT" ,
19+ functionName : "_nextTokenId" ,
20+ } ) ;
21+
22+ const [ remaining , setRemaining ] = useState < number | null > ( null ) ;
23+ useEffect ( ( ) => {
24+ if ( supply !== undefined ) {
25+ setRemaining ( Number ( supply ) - ( tokenID ? Number ( tokenID ) : 0 ) ) ;
26+ }
27+ } , [ tokenID , supply ] ) ;
28+
29+ const { writeContractAsync : writeScaffoldContractAsync } = useScaffoldWriteContract ( "NFT" ) ;
30+ const { address : connectedAddress } = useAccount ( ) ;
31+
32+ return (
33+ < div >
34+ < button
35+ className = "btn btn-primary"
36+ onClick = { async ( ) => {
37+ try {
38+ await writeScaffoldContractAsync ( {
39+ functionName : "safeMint" ,
40+ args : [ connectedAddress ] ,
41+ } ) ;
42+ } catch ( e ) {
43+ console . error ( "Error minting NFT:" , e ) ;
44+ }
45+ } }
46+ >
47+ Mint NFT
48+ </ button >
49+ </ div >
50+ ) ;
51+ } ;
52+
53+ export default MintNFT ;
You can’t perform that action at this time.
0 commit comments