|
1 | 1 | import { |
| 2 | + ContractEvent, |
2 | 3 | getBlockNumber, |
3 | 4 | useAddress, |
4 | 5 | useContract, |
@@ -49,21 +50,20 @@ export const useGetTokenURI = (id: number | null) => { |
49 | 50 |
|
50 | 51 | export const useGetOwnedNftIdsByAddress = (address?: string) => { |
51 | 52 | const { mintNFTContract, isLoading } = useMintNFTContract(); |
52 | | - const [ids, setIds] = useState<number[]>([]); |
| 53 | + const [ids, setIds] = useState<number[]>(); |
53 | 54 |
|
54 | 55 | useEffect(() => { |
55 | 56 | const fetch = async () => { |
56 | 57 | if (!address || isLoading) return; |
57 | | - const _ids: number[] = []; |
58 | 58 | const balance = await mintNFTContract?.call("balanceOf", [address]); |
59 | | - for (let index = 0; index < balance.toNumber(); index++) { |
60 | | - const tokenId = await mintNFTContract?.call("tokenOfOwnerByIndex", [ |
61 | | - address, |
62 | | - index, |
63 | | - ]); |
64 | | - _ids.push(tokenId.toNumber()); |
65 | | - } |
66 | | - setIds(_ids); |
| 59 | + const tokenIdsPromise = Array(balance.toNumber()) |
| 60 | + .fill("") |
| 61 | + .map((_, index) => { |
| 62 | + return mintNFTContract?.call("tokenOfOwnerByIndex", [address, index]); |
| 63 | + }); |
| 64 | + const tokenIds = await Promise.all(tokenIdsPromise); |
| 65 | + |
| 66 | + setIds(tokenIds.map((id) => id.toNumber())); |
67 | 67 | }; |
68 | 68 | fetch(); |
69 | 69 | }, [address, isLoading]); |
@@ -140,23 +140,34 @@ export const useGetOwnedNFTByAddress = (address?: string) => { |
140 | 140 | const [isLoading, setIsLoading] = useState(true); |
141 | 141 |
|
142 | 142 | useEffect(() => { |
143 | | - setIsLoading(true); |
144 | 143 | setNfts([]); |
145 | 144 | if (!ids) return; |
146 | 145 |
|
147 | 146 | const fetch = async () => { |
148 | | - const _nfts: any[] = []; |
149 | | - for (const id of ids) { |
150 | | - try { |
| 147 | + setIsLoading(true); |
| 148 | + |
| 149 | + const tokenURIPromises = ids.map((id) => { |
| 150 | + const getTokenURI = async (id: number) => { |
151 | 151 | const tokenURI = await mintNFTContract?.call("tokenURI", [id]); |
152 | | - const { data: metaData } = await axios.get(ipfs2http(tokenURI)); |
153 | | - _nfts.push({ ...metaData, tokenId: id }); |
154 | | - } catch (error) { |
155 | | - console.log(error); |
156 | | - continue; |
157 | | - } |
158 | | - } |
159 | | - setNfts(_nfts); |
| 152 | + return { tokenURI, tokenId: id }; |
| 153 | + }; |
| 154 | + return getTokenURI(id); |
| 155 | + }); |
| 156 | + const tokenURIs = await Promise.all(tokenURIPromises); |
| 157 | + |
| 158 | + const metaDataPromises = tokenURIs.map(({ tokenURI, tokenId }) => { |
| 159 | + const getMetaData = async (tokenURI: string, tokenId: number) => { |
| 160 | + try { |
| 161 | + const { data: metaData } = await axios.get(ipfs2http(tokenURI)); |
| 162 | + return { ...metaData, tokenId }; |
| 163 | + } catch (error) { |
| 164 | + console.log(error); |
| 165 | + } |
| 166 | + }; |
| 167 | + return getMetaData(tokenURI, tokenId); |
| 168 | + }); |
| 169 | + const _nfts = await Promise.all(metaDataPromises); |
| 170 | + setNfts(_nfts.filter((nft) => nft)); |
160 | 171 | setIsLoading(false); |
161 | 172 | }; |
162 | 173 |
|
@@ -259,11 +270,20 @@ export const useMintParticipateNFT = ( |
259 | 270 | }, [mintedTokenURI]); |
260 | 271 |
|
261 | 272 | useEffect(() => { |
262 | | - if (status !== "success" || !data || data.length < 1) return; |
263 | | - console.log(data); |
264 | | - const tokenId = data[data.length - 1].data?.tokenId.toNumber(); |
| 273 | + const includesNewEvent = (data: ContractEvent<Record<string, any>>[]) => { |
| 274 | + if (!fromBlock) return false; |
| 275 | + return data.some((event) => { |
| 276 | + return event.transaction.blockNumber > fromBlock; |
| 277 | + }); |
| 278 | + }; |
| 279 | + if (status !== "success" || !data || !includesNewEvent(data)) return; |
| 280 | + const tokenId = data |
| 281 | + .sort((a, b) => { |
| 282 | + return b.transaction.blockNumber - a.transaction.blockNumber; |
| 283 | + })[0] |
| 284 | + .data?.tokenId.toNumber(); |
265 | 285 | setMintedNFTId(tokenId); |
266 | | - }, [data, status]); |
| 286 | + }, [data, status, fromBlock]); |
267 | 287 |
|
268 | 288 | const checkCanMint = useCallback( |
269 | 289 | async (eventId: number, secretPhrase: string) => { |
|
0 commit comments