From 970983ee749e9c3accb7399742ab2771701d2919 Mon Sep 17 00:00:00 2001 From: Evan Tan Date: Mon, 14 Mar 2022 12:53:45 +0800 Subject: [PATCH 1/2] feat: add additional hook --- src/hooks/web3ApiWrappers/index.ts | 1 + .../web3ApiWrappers/useNFTTokenIds/index.ts | 1 + .../useNFTTokenIds/useNFTTokenIds.ts | 68 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/hooks/web3ApiWrappers/useNFTTokenIds/index.ts create mode 100644 src/hooks/web3ApiWrappers/useNFTTokenIds/useNFTTokenIds.ts diff --git a/src/hooks/web3ApiWrappers/index.ts b/src/hooks/web3ApiWrappers/index.ts index 7e820e1..4489846 100644 --- a/src/hooks/web3ApiWrappers/index.ts +++ b/src/hooks/web3ApiWrappers/index.ts @@ -2,6 +2,7 @@ export * from "./useApiContract"; export * from "./useERC20Balances"; export * from "./useERC20Transfers"; export * from "./useNFTBalances"; +export * from "./useNFTTokenIds"; export * from "./useNFTTransfers"; export * from "./useNativeBalance"; export * from "./useNativeTransactions"; diff --git a/src/hooks/web3ApiWrappers/useNFTTokenIds/index.ts b/src/hooks/web3ApiWrappers/useNFTTokenIds/index.ts new file mode 100644 index 0000000..8ed2533 --- /dev/null +++ b/src/hooks/web3ApiWrappers/useNFTTokenIds/index.ts @@ -0,0 +1 @@ +export * from "./useNFTTokenIds"; diff --git a/src/hooks/web3ApiWrappers/useNFTTokenIds/useNFTTokenIds.ts b/src/hooks/web3ApiWrappers/useNFTTokenIds/useNFTTokenIds.ts new file mode 100644 index 0000000..6cbc669 --- /dev/null +++ b/src/hooks/web3ApiWrappers/useNFTTokenIds/useNFTTokenIds.ts @@ -0,0 +1,68 @@ +import { useMemo } from "react"; +import MoralisType from "moralis"; +import { useMoralis } from "../../core/useMoralis"; +import { + useMoralisWeb3Api, + useMoralisWeb3ApiCall, + UseMoralisWeb3ApiCallOptions, +} from "../../core/useMoralisWeb3Api"; +import { resolveIPFS } from "../../../functions/resolveIPFS"; +import { DEFAULT_API_CHAIN } from "../../../config"; +import { isValidApiChain } from "../../../utils/isValidApiChain"; + +export interface UseNFTTokenIdsParams + extends Omit< + Parameters[0], + "address" | "limit" + > { + address: string; + limit?: number; +} + +export const useNFTTokenIds = ( + params?: UseNFTTokenIdsParams, + options?: UseMoralisWeb3ApiCallOptions, +) => { + const { token } = useMoralisWeb3Api(); + const { chainId } = useMoralis(); + + const { + fetch: getNFTTokenIds, + data, + error, + isLoading, + isFetching, + } = useMoralisWeb3ApiCall( + token.getAllTokenIds, + { + chain: params?.chain ?? isValidApiChain(chainId) ?? DEFAULT_API_CHAIN, + address: params?.address ?? "", + limit: params?.limit ?? 5, + ...params, + }, + { autoFetch: options?.autoFetch ?? !!token, ...options }, + ); + + const NFTTokenIds = useMemo(() => { + if (!data?.result || !data?.result.length) { + return data; + } + + const formattedResult = data.result.map((nft) => { + try { + if (nft.metadata) { + const metadata = JSON.parse(nft.metadata); + const image = resolveIPFS(metadata?.image); + return { ...nft, image, metadata }; + } + } catch (error) { + return nft; + } + return nft; + }); + + return { ...data, result: formattedResult }; + }, [data]); + + return { getNFTTokenIds, data: NFTTokenIds, error, isLoading, isFetching }; +}; From 1d381ca9917667539adac0da63f51feb46987976 Mon Sep 17 00:00:00 2001 From: Evan Tan Date: Mon, 14 Mar 2022 12:54:51 +0800 Subject: [PATCH 2/2] docs: update readme --- README.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/README.md b/README.md index 9b364f5..e1d5187 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ function App() { - [`useNativeBalance()`](#usenativebalance) - [`useNativeTransactions()`](#usenativetransactions) - [`useNFTBalances()`](#usenftbalances) + - [`useNFTTokenIds()`](#usenfttokenids) - [`useNFTTransfers()`](#usenfttransfers) - [`useTokenPrice()`](#usetokenprice) - [`useWeb3Contract()`](#useweb3contract) @@ -1309,6 +1310,87 @@ const NFTBalances = () => { } ``` +### `useNFTTokenIds()` + +Gets NFTs from the contract address. Supports both ERC721 and ERC1155. Returns an object with the total number of tokens and the array of NFT objects. + +**Options**: +- `address`: A contract address (i.e. 0x1a2b3x...). +- `chain` (optional): The blockchain to get data from. Valid values are listed on the [Web3 API Intro page](https://docs.moralis.io/moralis-server/web3-sdk/intro#supported-chains). Default value: current chain (if the chain is not supported it will use the Eth chain). +- `limit` (optional): Number of NFTs to fetch. Default value: 5 + +**Example** +```jsx +import { useNFTTokenIds } from "react-moralis"; + +const { getNFTTokenIds, data, error, isLoading, isFetching } = useNFTTokenIds(); + +const NFTTokenIds = () => { + return ( +
+ {error && <>{JSON.stringify(error)}} + +
{JSON.stringify(data, null, 2)}
+
+ ); +}; +``` + +**Example return** (Object) +```json +{ + "total": "10000", + "page": 0, + "page_size": 1, + "cursor": "...", + "result": [ + { + "token_address": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d", + "token_id": "9989", + "amount": "1", + "contract_type": "ERC721", + "name": "BoredApeYachtClub", + "symbol": "BAYC", + "token_uri": "https://gateway.moralisipfs.com/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/9989", + "metadata": { + "image": "ipfs://QmRixhzz7vuF7Lq1h9XrH8rCyL7kGBZRf3i79ArqDmk7eN", + "attributes": [ + { + "trait_type": "Clothes", + "value": "Vietnam Jacket" + }, + { + "trait_type": "Hat", + "value": "Faux Hawk" + }, + { + "trait_type": "Mouth", + "value": "Bored Pizza" + }, + { + "trait_type": "Fur", + "value": "Red" + }, + { + "trait_type": "Eyes", + "value": "Bored" + }, + { + "trait_type": "Background", + "value": "New Punk Blue" + } + ] + }, + "synced_at": "2022-03-10T17:18:56.558Z", + "image": "ipfs://QmRixhzz7vuF7Lq1h9XrH8rCyL7kGBZRf3i79ArqDmk7eN" + } + ] +} +``` + ### `useNFTTransfers()` Gets the NFT transfers. Returns an object with the number of NFT transfers and the array of NFT transfers.