Skip to content

Commit

Permalink
06 22 include count (#343)
Browse files Browse the repository at this point in the history
* add refreshCache

* add changelog

* add jsdoc

* includeCount

* test

* changelog

* move changelog, update comment
  • Loading branch information
ayang-rkjs committed Jun 22, 2023
1 parent d9f6619 commit 70d2fb5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Minor Changes

- Add the `refreshCache` parameter to NFT rarity endpoints to allow users to update stale rarity values.
- Add the `includeCount` parameter to getOwnersForContract.

## 2.9.0

### Major Changes
Expand Down
12 changes: 8 additions & 4 deletions src/api/nft-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,14 @@ export class NftNamespace {
*
* @param contractAddress - Contract address for the NFT collection.
* @param tokenId - Token id of the NFT.
* @param refreshCache - If true, bypass cache and recompute rarity snapshot.
*/
computeRarity(
contractAddress: string,
tokenId: BigNumberish
tokenId: BigNumberish,
refreshCache?: boolean
): Promise<NftAttributeRarity[]> {
return computeRarity(this.config, contractAddress, tokenId);
return computeRarity(this.config, contractAddress, tokenId, refreshCache);
}

/**
Expand All @@ -541,11 +543,13 @@ export class NftNamespace {
* Get a summary of attribute prevalence for an NFT collection.
*
* @param contractAddress - Contract address for the NFT collection.
* @param refreshCache - If true, bypass cache and recompute rarity snapshot.
*/
summarizeNftAttributes(
contractAddress: string
contractAddress: string,
refreshCache?: boolean
): Promise<NftAttributesResponse> {
return summarizeNftAttributes(this.config, contractAddress);
return summarizeNftAttributes(this.config, contractAddress, refreshCache);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/internal/nft-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export async function getOwnersForContract(

return {
owners: response.ownerAddresses,
totalCount: response.totalCount,

// Only include the pageKey in the final response if it's defined
...(response.pageKey !== undefined && { pageKey: response.pageKey })
Expand Down Expand Up @@ -623,14 +624,16 @@ export async function computeRarity(
config: AlchemyConfig,
contractAddress: string,
tokenId: BigNumberish,
refreshCache?: boolean,
srcMethod = 'computeRarity'
): Promise<NftAttributeRarity[]> {
const response = await requestHttpWithBackoff<
ComputeRarityParams,
RawNftAttributeRarity[]
>(config, AlchemyApiType.NFT, 'computeRarity', srcMethod, {
contractAddress,
tokenId: BigNumber.from(tokenId).toString()
tokenId: BigNumber.from(tokenId).toString(),
refreshCache
});

return getNftRarityFromRaw(response);
Expand All @@ -654,13 +657,15 @@ export async function searchContractMetadata(
export async function summarizeNftAttributes(
config: AlchemyConfig,
contractAddress: string,
refreshCache?: boolean,
srcMethod = 'summarizeNftAttributes'
): Promise<NftAttributesResponse> {
return requestHttpWithBackoff<
SummarizeNftAttributesParams,
NftAttributesResponse
>(config, AlchemyApiType.NFT, 'summarizeNftAttributes', srcMethod, {
contractAddress
contractAddress,
refreshCache
});
}

Expand Down Expand Up @@ -1034,6 +1039,7 @@ interface GetNftSalesParams {
interface ComputeRarityParams {
contractAddress: string;
tokenId: string;
refreshCache?: boolean;
}

/**
Expand All @@ -1052,6 +1058,7 @@ interface SearchContractMetadataParams {
*/
interface SummarizeNftAttributesParams {
contractAddress: string;
refreshCache?: boolean;
}

interface ReingestContractParams {
Expand Down
1 change: 1 addition & 0 deletions src/internal/raw-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export interface RawContractBaseNft {
*/
export interface RawGetOwnersForContractResponse {
ownerAddresses: string[];
totalCount?: number;
}

export interface RawGetOwnersForContractWithTokenBalancesResponse {
Expand Down
8 changes: 8 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,11 @@ export interface GetOwnersForNftResponse {
export interface GetOwnersForContractResponse {
/** An array of owner addresses for the provided contract address */
owners: string[];

/**
* Total count of unique owners. Only present if
* {@link GetOwnersForContractOptions.includeCount} is true. */
totalCount?: number;
}

/**
Expand Down Expand Up @@ -1610,6 +1615,9 @@ export interface GetOwnersForContractOptions {

/** Optional page key to paginate the next page for large requests. */
pageKey?: string;

/** If true, include total count of owners in the response. */
includeCount?: boolean;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/integration/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ describe('E2E integration tests', () => {
expect(response.owners.length).toBeGreaterThan(0);
});

it('getOwnersForContract() with includeCount', async () => {
const response = await alchemy.nft.getOwnersForContract(contractAddress, {
includeCount: true
});
expect(response.totalCount).not.toBeUndefined();
});

it('getOwnersForContract()', async () => {
const address = '0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85';
const response = await alchemy.nft.getOwnersForContract(address, {
Expand Down

0 comments on commit 70d2fb5

Please sign in to comment.