Repository: alchemyplatform/alchemy-sdk-js
Pull request #287 (2023-03-22 17:28:19)

add new token types


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6d089a6c..a04325f1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@
 
 ### Minor Changes
 
+- Added additional descriptive tokenTypes in NFT- and contract-level metadata, for contracts which don't support any NFT standard.
+
 ## 2.6.1
 
 ### Major Changes
diff --git a/src/internal/nft-api.ts b/src/internal/nft-api.ts
index 8f1f66a1..57ee9bee 100644
--- a/src/internal/nft-api.ts
+++ b/src/internal/nft-api.ts
@@ -48,6 +48,7 @@ import {
   TransfersNftResponse
 } from '../types/types';
 import { AlchemyApiType, ETH_NULL_ADDRESS } from '../util/const';
+import { sanitizeTokenType } from '../util/inputSanitization';
 import {
   getBaseNftFromRaw,
   getContractsForOwnerFromRaw,
@@ -101,10 +102,7 @@ export async function getNftMetadata(
     {
       contractAddress,
       tokenId: BigNumber.from(tokenId!).toString(),
-      tokenType:
-        options?.tokenType !== NftTokenType.UNKNOWN
-          ? options?.tokenType
-          : undefined,
+      tokenType: sanitizeTokenType(options?.tokenType),
       tokenUriTimeoutInMs: options?.tokenUriTimeoutInMs,
       refreshCache: options?.refreshCache
     }
@@ -903,6 +901,16 @@ interface GetNftsAlchemyParams {
   orderBy?: string;
 }
 
+/**
+ * NftTokenTypes that are allowed as request inputs.
+ *
+ * @internal
+ */
+export type InputNftTokenType =
+  | NftTokenType.ERC1155
+  | NftTokenType.ERC721
+  | undefined;
+
 /**
  * Interface for the `getNftMetadata` endpoint.
  *
@@ -911,7 +919,7 @@ interface GetNftsAlchemyParams {
 interface GetNftMetadataParams {
   contractAddress: string;
   tokenId: string;
-  tokenType?: NftTokenType;
+  tokenType?: InputNftTokenType;
   refreshCache?: boolean;
   tokenUriTimeoutInMs?: number;
 }
diff --git a/src/types/types.ts b/src/types/types.ts
index a4260450..810c1779 100644
--- a/src/types/types.ts
+++ b/src/types/types.ts
@@ -396,6 +396,8 @@ export enum AssetTransfersOrder {
 export enum NftTokenType {
   ERC721 = 'ERC721',
   ERC1155 = 'ERC1155',
+  NO_SUPPORTED_NFT_STANDARD = 'NO_SUPPORTED_NFT_STANDARD',
+  NOT_A_CONTRACT = 'NOT_A_CONTRACT',
   UNKNOWN = 'UNKNOWN'
 }
 
diff --git a/src/util/inputSanitization.ts b/src/util/inputSanitization.ts
new file mode 100644
index 00000000..8307b126
--- /dev/null
+++ b/src/util/inputSanitization.ts
@@ -0,0 +1,9 @@
+import { InputNftTokenType } from '../internal/nft-api';
+import { NftTokenType } from '../types/types';
+
+export function sanitizeTokenType(tokenType?: NftTokenType): InputNftTokenType {
+  if (tokenType === NftTokenType.ERC1155 || tokenType === NftTokenType.ERC721) {
+    return tokenType;
+  }
+  return undefined;
+}
diff --git a/src/util/util.ts b/src/util/util.ts
index f64c9762..cd83324a 100644
--- a/src/util/util.ts
+++ b/src/util/util.ts
@@ -212,6 +212,12 @@ function parseNftTokenType(tokenType: string | undefined): NftTokenType {
     case 'erc1155':
     case 'ERC1155':
       return NftTokenType.ERC1155;
+    case 'no_supported_nft_standard':
+    case 'NO_SUPPORTED_NFT_STANDARD':
+      return NftTokenType.NO_SUPPORTED_NFT_STANDARD;
+    case 'not_a_contract':
+    case 'NOT_A_CONTRACT':
+      return NftTokenType.NOT_A_CONTRACT;
     default:
       return NftTokenType.UNKNOWN;
   }


Test patch:
diff --git a/test/unit/nft-api.test.ts b/test/unit/nft-api.test.ts
index c1196c56..f5102642 100644
--- a/test/unit/nft-api.test.ts
+++ b/test/unit/nft-api.test.ts
@@ -231,6 +231,19 @@ describe('NFT module', () => {
       );
     });
 
+    it('sets tokenType to undefined if tokenType is NOT_A_CONTRACT', async () => {
+      verifyNftMetadata(
+        await alchemy.nft.getNftMetadata(
+          contractAddress,
+          tokenId,
+          NftTokenType.NOT_A_CONTRACT
+        ),
+        expectedNft,
+        contractAddress,
+        tokenId
+      );
+    });
+
     it('surfaces errors', async () => {
       mock.reset();
       mock.onGet().reply(500, 'Internal Server Error');