Skip to content

Commit 2a0b52c

Browse files
authored
add getNFTsByAccount (#1148)
* add getNFTsByAccount * improve chain param * add chain doc
1 parent eb30f08 commit 2a0b52c

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/api/api.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
getNFTPath,
4040
getRefreshMetadataPath,
4141
getCollectionOffersPath,
42+
getListNFTsByAccountPath,
4243
} from "../orders/utils";
4344
import {
4445
Chain,
@@ -416,6 +417,40 @@ export class OpenSeaAPI {
416417
return response;
417418
}
418419

420+
/**
421+
* Fetch NFTs owned by an account.
422+
* @param address The address of the account
423+
* @param limit The number of NFTs to retrieve. Must be greater than 0 and less than 51.
424+
* @param next Cursor to retrieve the next page of NFTs
425+
* @param retries Number of times to retry if the service is unavailable for any reason.
426+
* @param chain The chain to query. Defaults to the chain set in the constructor.
427+
* @returns The {@link ListNFTsResponse} returned by the API.
428+
*/
429+
public async getNFTsByAccount(
430+
address: string,
431+
limit: number | undefined = undefined,
432+
next: string | undefined = undefined,
433+
retries = 1,
434+
chain = this.chain,
435+
): Promise<ListNFTsResponse> {
436+
let response;
437+
try {
438+
response = await this.get<ListNFTsResponse>(
439+
getListNFTsByAccountPath(chain, address),
440+
{
441+
limit,
442+
next,
443+
},
444+
);
445+
} catch (error) {
446+
_throwOrContinue(error, retries);
447+
await delay(1000);
448+
return this.getNFTsByAccount(address, limit, next, retries - 1, chain);
449+
}
450+
451+
return response;
452+
}
453+
419454
/**
420455
* Fetch metadata, traits, ownership information, and rarity for a single NFT.
421456
* @param chain The NFT's chain.

src/orders/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export const getListNFTsByContractPath = (chain: Chain, address: string) => {
4747
return `/v2/chain/${chain}/contract/${address}/nfts`;
4848
};
4949

50+
export const getListNFTsByAccountPath = (chain: Chain, address: string) => {
51+
return `/v2/chain/${chain}/account/${address}/nfts`;
52+
};
53+
5054
export const getNFTPath = (
5155
chain: Chain,
5256
address: string,

test/integration/getNFTs.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ suite("SDK: NFTs", () => {
3838
assert(response.next, "Response should have a next cursor");
3939
});
4040

41+
test("Get NFTs By Account", async () => {
42+
const address = "0xfBa662e1a8e91a350702cF3b87D0C2d2Fb4BA57F";
43+
const response = await sdk.api.getNFTsByAccount(address);
44+
assert(response, "Response should exist.");
45+
assert.equal(response.nfts.length, 50, "Response should include 50 NFTs");
46+
assert(response.next, "Response should have a next cursor");
47+
});
48+
4149
test("Get NFT", async () => {
4250
const tokenAddress = "0x4768cbf202f365fbf704b9b9d397551a0443909b"; // Roo Troop
4351
const identifier = "2";

0 commit comments

Comments
 (0)