Repository: alchemyplatform/alchemy-sdk-js
Pull request #261 (2023-02-03 21:44:01)

Add blockHash field to getNftsForOwner() response


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f7a72c49..7dc5ec6d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
 
 ### Minor Changes
 - Added the `wyvern` and `cryptopunks` marketplaces in the `NftSaleMarketplace` enum to the `NftNamespace.getNftSales()` method.
+- Added the `blockHash` field to the `OwnedNftsResponse` returned by `NftNamespace.getNftsForOwner()` to track the block hash that the request was based on.
 
 ## 2.4.1
 
diff --git a/src/internal/nft-api.ts b/src/internal/nft-api.ts
index 28a5900d..ed780220 100644
--- a/src/internal/nft-api.ts
+++ b/src/internal/nft-api.ts
@@ -212,7 +212,8 @@ export async function getNftsForOwner(
       balance: parseInt(res.balance)
     })),
     pageKey: response.pageKey,
-    totalCount: response.totalCount
+    totalCount: response.totalCount,
+    blockHash: response.blockHash
   };
 }
 
diff --git a/src/internal/raw-interfaces.ts b/src/internal/raw-interfaces.ts
index 88ead506..9c879e15 100644
--- a/src/internal/raw-interfaces.ts
+++ b/src/internal/raw-interfaces.ts
@@ -128,6 +128,7 @@ export interface RawGetBaseNftsResponse {
   ownedNfts: RawOwnedBaseNft[];
   pageKey?: string;
   totalCount: number;
+  blockHash: string;
 }
 
 /**
@@ -139,6 +140,7 @@ export interface RawGetNftsResponse {
   ownedNfts: RawOwnedNft[];
   pageKey?: string;
   totalCount: number;
+  blockHash: string;
 }
 
 /**
diff --git a/src/types/types.ts b/src/types/types.ts
index c25753f9..34b4443e 100644
--- a/src/types/types.ts
+++ b/src/types/types.ts
@@ -696,6 +696,9 @@ export interface OwnedNftsResponse {
 
   /** The total count of NFTs owned by the provided address. */
   readonly totalCount: number;
+
+  /** The canonical head block hash of when your request was received. */
+  blockHash: string;
 }
 
 /**
@@ -718,6 +721,9 @@ export interface OwnedBaseNftsResponse {
 
   /** The total count of NFTs owned by the provided address. */
   readonly totalCount: number;
+
+  /** The canonical head block hash of when your request was received. */
+  blockHash: string;
 }
 
 /**


Test patch:
diff --git a/test/integration/nft.test.ts b/test/integration/nft.test.ts
index e981005a..93649387 100644
--- a/test/integration/nft.test.ts
+++ b/test/integration/nft.test.ts
@@ -124,6 +124,7 @@ describe('E2E integration tests', () => {
         .length
     ).toBeGreaterThan(0);
     expect(response.ownedNfts.length).toEqual(51);
+    expect(typeof response.blockHash).toEqual('string');
   });
 
   it('getOwnersForNft() from NFT', async () => {
diff --git a/test/unit/nft-api.test.ts b/test/unit/nft-api.test.ts
index 4d7647bf..71cc4d80 100644
--- a/test/unit/nft-api.test.ts
+++ b/test/unit/nft-api.test.ts
@@ -309,7 +309,8 @@ describe('NFT module', () => {
         createRawOwnedBaseNft('0xCA2', '0x2', '2', NftTokenType.ERC721)
       ],
       pageKey: 'page-key1',
-      totalCount: 3
+      totalCount: 3,
+      blockHash: '0x123abc'
     };
 
     const nftResponse: RawGetNftsResponse = {
@@ -318,7 +319,8 @@ describe('NFT module', () => {
         createRawOwnedNft('b', '0xCA2', '0x2', '2', NftTokenType.ERC1155)
       ],
       pageKey: 'page-key1',
-      totalCount: 3
+      totalCount: 3,
+      blockHash: '0x123abc'
     };
 
     const paramCases = [
@@ -370,7 +372,8 @@ describe('NFT module', () => {
         createOwnedBaseNft('0xCA2', '0x2', 2, NftTokenType.ERC721)
       ],
       pageKey: 'page-key1',
-      totalCount: 3
+      totalCount: 3,
+      blockHash: '0x123abc'
     };
     const nftExpected: OwnedNftsResponse = {
       ownedNfts: [
@@ -378,7 +381,8 @@ describe('NFT module', () => {
         createOwnedNft('b', '0xCA2', '0x2', 2, NftTokenType.ERC1155)
       ],
       pageKey: 'page-key1',
-      totalCount: 3
+      totalCount: 3,
+      blockHash: '0x123abc'
     };
     const responseCases: Array<
       [
@@ -432,13 +436,15 @@ describe('NFT module', () => {
           createRawOwnedBaseNft('0xCA2', '0x2', '2', NftTokenType.ERC721)
         ],
         pageKey: 'page-key1',
-        totalCount: 3
+        totalCount: 3,
+        blockHash: '0x123abc'
       },
       {
         ownedNfts: [
           createRawOwnedBaseNft('0xCA2', '0x3', '1', NftTokenType.ERC721)
         ],
-        totalCount: 3
+        totalCount: 3,
+        blockHash: '0x123abc'
       }
     ];
     const nftResponses: RawGetNftsResponse[] = [
@@ -448,13 +454,15 @@ describe('NFT module', () => {
           createRawOwnedNft('b', '0xCA2', '0x2', '2', NftTokenType.ERC1155)
         ],
         pageKey: 'page-key1',
-        totalCount: 3
+        totalCount: 3,
+        blockHash: '0x123abc'
       },
       {
         ownedNfts: [
           createRawOwnedNft('c', '0xCA2', '0x3', '1', NftTokenType.ERC1155)
         ],
-        totalCount: 3
+        totalCount: 3,
+        blockHash: '0x123abc'
       }
     ];
 
@@ -646,7 +654,8 @@ describe('NFT module', () => {
             createRawOwnedNft('b', '0xCA2', '0x2', '2', NftTokenType.ERC1155)
           ],
           pageKey: 'page-key1',
-          totalCount: 2
+          totalCount: 2,
+          blockHash: '0x123abc'
         }
       ];
       setupMock(mockResponse);
@@ -1250,20 +1259,23 @@ describe('NFT module', () => {
     const addresses = ['0xCA1', '0xCA2'];
     const emptyResponse: RawGetNftsResponse = {
       ownedNfts: [],
-      totalCount: 0
+      totalCount: 0,
+      blockHash: '0x123abc'
     };
     const partialResponse: RawGetNftsResponse = {
       ownedNfts: [
         createRawOwnedNft('b', '0xCA2', '0x2', '2', NftTokenType.ERC1155)
       ],
-      totalCount: 1
+      totalCount: 1,
+      blockHash: '0x123abc'
     };
     const nftResponse: RawGetNftsResponse = {
       ownedNfts: [
         createRawOwnedNft('a', '0xCA1', '0x1', '1'),
         createRawOwnedNft('b', '0xCA2', '0x2', '2', NftTokenType.ERC1155)
       ],
-      totalCount: 2
+      totalCount: 2,
+      blockHash: '0x123abc'
     };
 
     it('calls with the correct parameters', async () => {