Skip to content

Commit

Permalink
Add withMetadata typings and metadata response types for getNFTs() (#100
Browse files Browse the repository at this point in the history
)
  • Loading branch information
thebrianchen authored Feb 25, 2022
1 parent 5d3985f commit 8cec29a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,44 @@ An object with the following fields:
- `owner`: The address that you want to fetch NFTs for.
- `pageKey`: (Optional) A key to fetch the next page of results.
- `contractAddresses`: (Optional) An array of contract addresses to filter the owner's results to.
- `withMetadata`: (Optional) If `false`, the returned NFTs will omit metadata. Defaults to `true`.

**Returns:**

An object with the following fields:
When metadata is included, the returned object has the following fields:

- `ownedNfts`: An array of NFT objects that the address owns. Each NFT object has the following structure.
- `contract`:
- `address`: The address of the contract or collection that the NFT belongs to.
- `id`:
- `tokenId`: Raw token id.
- `tokenMetadata`:
- `tokenType`: The type of token being sent as part of the request (Can be one of ["erc721" | "erc1155"]).
- `title`: The title of the NFT, or an empty string if no title is available.
- `description`: The descriptions of the NFT, or an empty string if no description is available.
- `tokenUri`: (Optional)
- `raw`: Uri representing the location of the NFT's original metadata blob. This is a backup for you to parse
when the `metadata` field is not automatically populated.
- `gateway`: Public gateway uri for the raw uri.
- `media`: (Optional) An array of objects with the following structure.
- `uri`: A `tokenUri` as described above.
- `metadata`: (Optional)
- `image`: (Optional) A uri string that should be usable in an <image> tag.
- `attributes`: (Optional) An array of attributes from the NFT metadata. Each attribute is a dictionary with
unknown keys and values, as they depend directly on the contract.
- `timeLastUpdated`: ISO timestamp of the last cache refresh for the information returned in the metadata field.
- `pageKey`: (Optional) A key to fetch the next page of results, if applicable.
- `totalCount`: The total number of NFTs in the result set.

If metadata is omitted, an object with the following fields is returned:

- `ownedNfts`: An array of NFT objects that the address owns. Each NFT object has the following structure.
- `contract`:
- `address`: The address of the contract or collection that the NFT belongs to.
- `id`:
- `tokenId`: Raw token id.
- `tokenMetadata`:
- `tokenType`: The type of token being sent as part of the request (Can be one of ["erc721" | "erc1155"]).
- `contract`:
- `address`: The address of the contract or collection that the NFT belongs to.
- `id`:
- `tokenId`: Raw token id.
- `tokenMetadata`:
- `tokenType`: The type of token being sent as part of the request (Can be one of ["erc721" | "erc1155"]).
- `pageKey`: (Optional) A key to fetch the next page of results, if applicable.
- `totalCount`: The total number of NFTs in the result set.

Expand Down
34 changes: 25 additions & 9 deletions src/alchemy-apis/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export interface AssetTransfersResult {
rawContract: RawContract;
}

export interface NftMetadata {
export interface NftMetadata extends Record<string, any> {
name?: string;
description?: string;
image?: string;
attributes?: Array<Record<string, any>>;
}
Expand Down Expand Up @@ -112,9 +114,9 @@ export interface GetNftMetadataParams {
tokenType?: "erc721" | "erc1155";
}

export interface GetNftMetadataResponse {
contract: NftContract;
id: NftId;
export type GetNftMetadataResponse = NftMetadata;

export interface Nft extends BaseNft {
title: string;
description: string;
tokenUri?: TokenUri;
Expand All @@ -123,10 +125,23 @@ export interface GetNftMetadataResponse {
timeLastUpdated: string;
}

export interface BaseNft {
contract: NftContract;
id: NftId;
}

export interface GetNftsParams {
owner: string;
pageKey?: string;
contractAddresses?: string[];
withMetadata?: boolean;
}

export interface GetNftsParamsWithoutMetadata {
owner: string;
pageKey?: string;
contractAddresses?: string[];
withMetadata: false;
}

export interface GetNftsResponse {
Expand All @@ -135,6 +150,12 @@ export interface GetNftsResponse {
totalCount: number;
}

export interface GetNftsResponseWithoutMetadata {
ownedNfts: BaseNft[];
pageKey?: string;
totalCount: number;
}

export interface TransactionReceiptsBlockNumber {
blockNumber: string;
}
Expand Down Expand Up @@ -179,11 +200,6 @@ export interface Log {
transactionIndex: string;
}

export interface Nft {
contract: NftContract;
id: NftId;
}

export interface ERC1155Metadata {
tokenId: string;
value: string;
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
GetNftMetadataParams,
GetNftMetadataResponse,
GetNftsParams,
GetNftsParamsWithoutMetadata,
GetNftsResponse,
GetNftsResponseWithoutMetadata,
TokenAllowanceParams,
TokenAllowanceResponse,
TokenBalancesResponse,
Expand Down Expand Up @@ -70,9 +72,13 @@ export interface AlchemyMethods {
callback?: Web3Callback<GetNftMetadataResponse>,
): Promise<GetNftMetadataResponse>;
getNfts(
params: GetNftsParams,
callback?: Web3Callback<GetNftsResponse>,
): Promise<GetNftsResponse>;
params: GetNftsParams | GetNftsParamsWithoutMetadata,
callback?: Web3Callback<GetNftsResponse | GetNftsResponseWithoutMetadata>,
): Promise<GetNftsResponse | GetNftsResponseWithoutMetadata>;
getNfts(
params: GetNftsParamsWithoutMetadata,
callback?: Web3Callback<GetNftsResponseWithoutMetadata>,
): Promise<GetNftsResponseWithoutMetadata>;
getTransactionReceipts(
params: TransactionReceiptsParams,
callback?: Web3Callback<TransactionReceiptsResponse>,
Expand Down Expand Up @@ -194,7 +200,7 @@ export function createAlchemyWeb3(
params,
path: "/v1/getNFTMetadata/",
}),
getNfts: (params: GetNftsParams, callback) =>
getNfts: (params: GetNftsParams | GetNftsParamsWithoutMetadata, callback) =>
callAlchemyRestEndpoint({
restSender,
callback,
Expand Down

0 comments on commit 8cec29a

Please sign in to comment.