import { useEffect , useState } from 'react' ; import { ethers } from 'ethers' ; import { AlchemyProvider , FallbackProvider , InfuraProvider , } from '@ethersproject/providers' ; import { DEVELOPER_DAO_CONTRACT , DEVELOPER_DAO_CONTRACT_NETWORK_PROVIDER , } from '../utils/DeveloperDaoConstants' ; import { Ddao__factory } from '../../types/ethers-contracts/factories/Ddao__factory' ; const MAX_SUPPLY = 8000 ; const PUBLIC_MAX_SUPPLY = 7777 ; export function useDevNFTSupply ( ) { const [ totalSupply , setTotalSupply ] = useState < number > ( - 1 ) ; const [ lockedSupply , setLockedSupply ] = useState < number > ( - 1 ) ; const [ uniqueTokenHolderCount , setUniqueTokenHolderCount ] = useState < number > ( - 1 ) ; useEffect ( ( ) => { const fetch = async ( ) => { const contract = Ddao__factory . connect ( DEVELOPER_DAO_CONTRACT , new FallbackProvider ( [ { provider : new InfuraProvider ( DEVELOPER_DAO_CONTRACT_NETWORK_PROVIDER ) , } , { provider : new AlchemyProvider ( DEVELOPER_DAO_CONTRACT_NETWORK_PROVIDER ) , } , ] ) ) ; const transferredTokens = await contract . queryFilter ( contract . filters . Transfer ( ) , 0 , 'latest' ) ; const claimedTokens = { } ; transferredTokens . forEach ( ( tx ) => { const tokenId = tx . args . tokenId . toNumber ( ) ; claimedTokens [ tokenId ] = tx . args . to ; } ) ; const totalSupply = Object . keys ( claimedTokens ) . length ; const uniqueAddresses = new Set < string > ( Object . values ( claimedTokens ) ) ; let countLockedAndMinted = 0 ; for ( let lockedTokenId = PUBLIC_MAX_SUPPLY + 1 ; lockedTokenId <= MAX_SUPPLY ; lockedTokenId ++ ) { if ( claimedTokens [ lockedTokenId ] && claimedTokens [ lockedTokenId ] !== ethers . constants . AddressZero ) countLockedAndMinted ++ ; } setLockedSupply ( countLockedAndMinted ) ; setTotalSupply ( totalSupply ) ; setUniqueTokenHolderCount ( uniqueAddresses . size ) ; } ; fetch ( ) ; } , [ ] ) ; return { isLoading : totalSupply === - 1 || lockedSupply === - 1 || uniqueTokenHolderCount === - 1 , totalSupply , uniqueTokenHolders : uniqueTokenHolderCount , lockedSupply , publicSupply : totalSupply - lockedSupply , maxSupply : MAX_SUPPLY , remainingTotalSupply : MAX_SUPPLY - totalSupply , remainingPublicSupply : PUBLIC_MAX_SUPPLY - ( totalSupply - lockedSupply ) , remainingLockedSupply : MAX_SUPPLY - PUBLIC_MAX_SUPPLY - lockedSupply , } ; }