-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add to wallet button * refactor: useCommunity hook
- Loading branch information
Showing
5 changed files
with
239 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,116 @@ | ||
import { useMemo } from 'react' | ||
import { useMemo, useCallback, useEffect, useState } from 'react' | ||
import { abiContractsMap } from '@/lib/contracts' | ||
import { Address } from 'viem' | ||
import { useReadContracts, useAccount } from 'wagmi' | ||
import { useReadContracts, useAccount, useWaitForTransactionReceipt, useWriteContract } from 'wagmi' | ||
import { fetchIpfsUri } from '@/app/user/Balances/actions' | ||
import { NftMeta, CommunityData } from '../types' | ||
|
||
/** | ||
* useCommunity hook return properties | ||
* Hook for loading NFT metadata from IPFS | ||
*/ | ||
interface CommunityData { | ||
/** | ||
* The remaining number of tokens that can be minted | ||
*/ | ||
tokensAvailable: number | ||
/** | ||
* Number of community members who received tokens | ||
*/ | ||
membersCount: number | ||
/** | ||
* Tells whether the user is a member of the community | ||
*/ | ||
isMember: boolean | ||
/** | ||
* Serial number of the token minted for the user | ||
*/ | ||
tokenId: number | undefined | ||
/** | ||
* NFT smart contract name | ||
*/ | ||
nftName: string | undefined | ||
const useNftMeta = (nftUri?: string) => { | ||
const [nftMeta, setNftMeta] = useState<NftMeta>() | ||
|
||
useEffect(() => { | ||
if (!nftUri) return setNftMeta(undefined) | ||
fetchIpfsUri(nftUri).then(async nftMeta => { | ||
const response = await fetchIpfsUri(nftMeta.image, 'blob') | ||
const url = URL.createObjectURL(response) | ||
setNftMeta({ ...nftMeta, image: url }) | ||
}) | ||
}, [nftUri]) | ||
|
||
return nftMeta | ||
} | ||
|
||
/** | ||
* Reads different Early Adopters NFT contract functions | ||
* @param nftAddress deployed contract address | ||
* @returns community NFT view functions call results | ||
* Hook for reading NFT contract view functions | ||
*/ | ||
export const useCommunity = (nftAddress?: Address): CommunityData => { | ||
export const useContractData = (nftAddress?: Address) => { | ||
const { address } = useAccount() | ||
const contract = { | ||
// verifying `nftAddress` later in `useReadContracts` params | ||
abi: abiContractsMap[nftAddress!], | ||
address: nftAddress, | ||
} | ||
const { data } = useReadContracts( | ||
|
||
// load data from the contract view functions | ||
const { data, refetch } = useReadContracts( | ||
address && | ||
nftAddress && { | ||
contracts: [ | ||
{ | ||
...contract, | ||
functionName: 'totalSupply', | ||
}, | ||
{ | ||
...contract, | ||
functionName: 'tokensAvailable', | ||
}, | ||
{ | ||
...contract, | ||
functionName: 'balanceOf', | ||
args: [address], | ||
}, | ||
{ | ||
...contract, | ||
functionName: 'tokenIdByOwner', | ||
args: [address], | ||
}, | ||
{ | ||
...contract, | ||
functionName: 'name', | ||
}, | ||
{ ...contract, functionName: 'totalSupply' }, | ||
{ ...contract, functionName: 'tokensAvailable' }, | ||
{ ...contract, functionName: 'balanceOf', args: [address] }, | ||
{ ...contract, functionName: 'tokenIdByOwner', args: [address] }, | ||
{ ...contract, functionName: 'name' }, | ||
{ ...contract, functionName: 'symbol' }, | ||
{ ...contract, functionName: 'tokenUriByOwner', args: [address] }, | ||
], | ||
}, | ||
) | ||
|
||
return useMemo(() => { | ||
const [membersCount, tokensAvailable, balanceOf, tokenIdByOwner, nftName] = data ?? [] | ||
const communityData: CommunityData = { | ||
tokensAvailable: Number(tokensAvailable?.result ?? 0n), | ||
const [membersCount, tokensAvailable, balanceOf, tokenIdByOwner, nftName, symbol, nftUri] = data ?? [] | ||
return { | ||
refetch, | ||
membersCount: Number(membersCount?.result ?? 0n), | ||
tokensAvailable: Number(tokensAvailable?.result ?? 0n), | ||
isMember: (balanceOf?.result ?? 0n) > 0n, | ||
tokenId: typeof tokenIdByOwner?.result === 'bigint' ? Number(tokenIdByOwner.result) : undefined, | ||
nftName: nftName?.result, | ||
nftSymbol: symbol?.result, | ||
nftUri: nftUri?.result, | ||
} | ||
return communityData | ||
}, [data]) | ||
}, [data, refetch]) | ||
} | ||
|
||
/** | ||
* Hook for executing and watching NFT mint transaction | ||
*/ | ||
const useMintNFT = (nftAddress?: Address, tokensAvailable?: number) => { | ||
const { writeContractAsync: mint, isPending, data: hash } = useWriteContract() | ||
const { isLoading, isSuccess } = useWaitForTransactionReceipt({ hash }) | ||
|
||
const onMintNFT = useCallback(async () => { | ||
if (!nftAddress) throw new Error('Unknown NFT address') | ||
if (!tokensAvailable) throw new Error('No NFTs available to mint') | ||
return await mint({ | ||
abi: abiContractsMap[nftAddress], | ||
address: nftAddress || '0x0', | ||
functionName: 'mint', | ||
args: [], | ||
}) | ||
}, [mint, nftAddress, tokensAvailable]) | ||
|
||
return useMemo( | ||
() => ({ onMintNFT, isPending: isLoading || isPending, isSuccess }), | ||
[isLoading, isPending, isSuccess, onMintNFT], | ||
) | ||
} | ||
|
||
/** | ||
* Hook returning all information about Early Adopters community | ||
*/ | ||
export const useCommunity = (nftAddress?: Address): CommunityData => { | ||
const { refetch, ...data } = useContractData(nftAddress) | ||
const { onMintNFT, isPending, isSuccess } = useMintNFT(nftAddress, data.tokensAvailable) | ||
const nftMeta = useNftMeta(data.nftUri) | ||
|
||
useEffect(() => { | ||
if (isSuccess) refetch() | ||
}, [isSuccess, refetch]) | ||
|
||
return useMemo( | ||
() => | ||
({ | ||
...data, | ||
mint: { | ||
onMintNFT, | ||
isPending, | ||
}, | ||
nftMeta, | ||
}) satisfies CommunityData, | ||
[data, isPending, nftMeta, onMintNFT], | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.