Repository: alchemyplatform/alchemy-sdk-js
Pull request #46 (2022-07-01 22:41:30)

Standardize all tokenType enums to uppercase, regardless of response type


Patch:
diff --git a/src/api/nft-api.ts b/src/api/nft-api.ts
index 33497e6a..fd8dea79 100644
--- a/src/api/nft-api.ts
+++ b/src/api/nft-api.ts
@@ -115,7 +115,6 @@ export async function getNftMetadata(
  *
  * @param alchemy - The Alchemy SDK instance.
  * @param contractAddress - The contract address of the NFT.
- * @param tokenType - Optionally specify the type of token to speed up the query.
  * @public
  */
 export function getNftContractMetadata(
@@ -127,7 +126,7 @@ export function getNftContractMetadata(
  * Get the NFT metadata associated with the provided Base NFT.
  *
  * @param alchemy - The Alchemy SDK instance.
- * @param baseNft - The base NFT object to be used for the request.
+ * @param baseNftContract - The base NFT contract object to be used for the request.
  * @public
  */
 export function getNftContractMetadata(
diff --git a/src/types/types.ts b/src/types/types.ts
index efb71065..2a25d187 100644
--- a/src/types/types.ts
+++ b/src/types/types.ts
@@ -112,9 +112,9 @@ export enum AssetTransfersOrder {
 
 /** @public */
 export enum NftTokenType {
-  ERC721 = 'erc721',
-  ERC1155 = 'erc1155',
-  UNKNOWN = 'unknown'
+  ERC721 = 'ERC721',
+  ERC1155 = 'ERC1155',
+  UNKNOWN = 'UNKNOWN'
 }
 
 /** @public */
diff --git a/src/util/util.ts b/src/util/util.ts
index 6b07ee28..bbcd7cbf 100644
--- a/src/util/util.ts
+++ b/src/util/util.ts
@@ -2,10 +2,10 @@ import { BigNumber } from 'ethers';
 import { BaseNft, BaseNftContract, Nft, NftContract } from '../api/nft';
 import { toHex } from '../api/util';
 import {
-  RawBaseNftContract,
-  RawNftContract,
   RawBaseNft,
-  RawNft
+  RawBaseNftContract,
+  RawNft,
+  RawNftContract
 } from '../internal/raw-interfaces';
 import { NftTokenType, TokenUri } from '../types/types';
 
@@ -32,7 +32,7 @@ export function getNftContractFromRaw(
     name: rawNftContract.contractMetadata.name,
     symbol: rawNftContract.contractMetadata.symbol,
     totalSupply: rawNftContract.contractMetadata.totalSupply,
-    tokenType: rawNftContract.contractMetadata.tokenType
+    tokenType: parseNftTokenType(rawNftContract.contractMetadata.tokenType)
   };
 }
 
@@ -43,7 +43,7 @@ export function getBaseNftFromRaw(
   return {
     contract: { address: contractAddress },
     tokenId: BigNumber.from(rawBaseNft.id.tokenId).toString(),
-    tokenType: rawBaseNft.id.tokenMetadata?.tokenType ?? NftTokenType.UNKNOWN
+    tokenType: parseNftTokenType(rawBaseNft.id.tokenMetadata?.tokenType)
   };
 }
 
@@ -68,8 +68,17 @@ function parseNftTokenId(tokenId: string): string {
   return BigNumber.from(tokenId).toString();
 }
 
-function parseNftTokenType(tokenType: NftTokenType | undefined): NftTokenType {
-  return tokenType ?? NftTokenType.UNKNOWN;
+function parseNftTokenType(tokenType: string | undefined): NftTokenType {
+  switch (tokenType) {
+    case 'erc721':
+    case 'ERC721':
+      return NftTokenType.ERC721;
+    case 'erc1155':
+    case 'ERC1155':
+      return NftTokenType.ERC1155;
+    default:
+      return NftTokenType.UNKNOWN;
+  }
 }
 
 function parseNftDescription(description: string | string[]): string {


Test patch:
diff --git a/test/unit/nft.test.ts b/test/unit/nft.test.ts
index 8de62dec..c9d5f5fa 100644
--- a/test/unit/nft.test.ts
+++ b/test/unit/nft.test.ts
@@ -6,11 +6,23 @@ describe('BaseNft class', () => {
   const contractAddress = '0xCA1';
   const tokenId = '0x01';
   const tokenIdString = '1';
-  it('fromResponse() defaults to UNKNOWN token type', () => {
-    const nft = getBaseNftFromRaw(createRawBaseNft(tokenId), contractAddress);
+  it('fromResponse() defaults to UNKNOWN token type and normalizes capitalization', () => {
+    let nft = getBaseNftFromRaw(createRawBaseNft(tokenId), contractAddress);
     expect(nft.tokenType).toEqual(NftTokenType.UNKNOWN);
     expect(nft.tokenId).toEqual(tokenIdString);
     expect(nft.contract.address).toEqual(contractAddress);
+
+    nft = getBaseNftFromRaw(
+      createRawBaseNft(tokenId, 'erc721' as NftTokenType),
+      contractAddress
+    );
+    expect(nft.tokenType).toEqual(NftTokenType.ERC721);
+
+    nft = getBaseNftFromRaw(
+      createRawBaseNft(tokenId, 'ERC721' as NftTokenType),
+      contractAddress
+    );
+    expect(nft.tokenType).toEqual(NftTokenType.ERC721);
   });
 
   it('fromResponse() normalizes tokenId fields', () => {
@@ -32,13 +44,25 @@ describe('Nft class', () => {
   const tokenId = 1;
   const tokenIdString = '1';
   it('fromResponse() defaults to UNKNOWN token type', () => {
-    const nft = getNftFromRaw(
+    let nft = getNftFromRaw(
       createRawNft('title', toHex(tokenId)),
       contractAddress
     );
     expect(nft.tokenType).toEqual(NftTokenType.UNKNOWN);
     expect(nft.tokenId).toEqual(tokenIdString);
     expect(nft.contract.address).toEqual(contractAddress);
+
+    nft = getNftFromRaw(
+      createRawNft('title', toHex(tokenId), 'erc721' as NftTokenType),
+      contractAddress
+    );
+    expect(nft.tokenType).toEqual(NftTokenType.ERC721);
+
+    nft = getNftFromRaw(
+      createRawNft('title', toHex(tokenId), 'ERC721' as NftTokenType),
+      contractAddress
+    );
+    expect(nft.tokenType).toEqual(NftTokenType.ERC721);
   });
 
   it('fromResponse() normalizes tokenId fields', () => {