Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Add option to NOT load NFT metadata #1823

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/sdk/src/core/schema/QueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export const QueryAllParamsSchema = /* @__PURE__ */ (() =>
.object({
start: z.number().default(0),
count: z.number().default(DEFAULT_QUERY_ALL_COUNT),
loadMetadata: z.boolean().default(true),
})
.default({
start: 0,
count: DEFAULT_QUERY_ALL_COUNT,
loadMetadata: true,
}))();

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk/src/core/schema/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ export type NFT = {
supply: string;
quantityOwned?: string;
};
/**
* @public
*/
export type NFTWithoutMetadata = {
metadata: Pick<NFTMetadata, "id">;
owner: string;
type: "ERC1155" | "ERC721" | "metaplex";
supply: string;
quantityOwned?: string;
};
47 changes: 34 additions & 13 deletions packages/sdk/src/evm/core/classes/erc-721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
NFT,
NFTMetadata,
NFTMetadataOrUri,
NFTWithoutMetadata,
} from "../../../core/schema/nft";
import { resolveAddress } from "../../common/ens/resolveAddress";
import {
Expand Down Expand Up @@ -167,16 +168,34 @@ export class Erc721<
* @returns The NFT metadata
* @twfeature ERC721
*/
public async get(tokenId: BigNumberish): Promise<NFT> {
const [owner, metadata] = await Promise.all([
this.ownerOf(tokenId).catch(() => constants.AddressZero),
this.getTokenMetadata(tokenId).catch(() => ({
id: tokenId.toString(),
uri: "",
...FALLBACK_METADATA,
})),
]);
return { owner, metadata, type: "ERC721", supply: "1" };
public async get<T extends boolean | undefined = undefined>(
tokenId: BigNumberish,
loadMetadata?: T,
): Promise<T extends true | undefined ? NFT : NFTWithoutMetadata> {
if (loadMetadata === false) {
const owner = await this.ownerOf(tokenId).catch(
() => constants.AddressZero,
);
const nft: NFTWithoutMetadata = {
owner,
metadata: {
id: tokenId.toString(),
},
type: "ERC721",
supply: "1",
};
return nft as T extends true | undefined ? NFT : NFTWithoutMetadata;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to solve this without using type assertion.
Googling leads to this open issue: microsoft/TypeScript#33912

} else {
const [owner, metadata] = await Promise.all([
this.ownerOf(tokenId).catch(() => constants.AddressZero),
this.getTokenMetadata(tokenId).catch(() => ({
id: tokenId.toString(),
uri: "",
...FALLBACK_METADATA,
})),
]);
return { owner, metadata, type: "ERC721", supply: "1" };
}
}

/**
Expand Down Expand Up @@ -425,11 +444,10 @@ export class Erc721<
public async getOwned(
walletAddress?: AddressOrEns,
queryParams?: QueryAllParams,
) {
): Promise<NFT[] | NFTWithoutMetadata[]> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't wrap my head around how to deal with the typings here

if (walletAddress) {
walletAddress = await resolveAddress(walletAddress);
}

if (this.query?.owned) {
return this.query.owned.all(walletAddress, queryParams);
} else {
Expand All @@ -445,8 +463,11 @@ export class Erc721<
const count = queryParams?.count || DEFAULT_QUERY_ALL_COUNT;
ownedTokens = ownedTokens.slice(start, start + count);
}
const loadMetadata = queryParams
? queryParams.loadMetadata !== false
: true;
return await Promise.all(
ownedTokens.map(async (i) => this.get(i.tokenId)),
ownedTokens.map(async (i) => this.get(i.tokenId, loadMetadata)),
);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {
NFTMetadata,
NFTMetadataOrUri,
BasicNFTInput,
NFTWithoutMetadata,
} from "./core/schema/nft";

export type { CurrencyValue, TokenMetadata } from "./core/schema/token";
Expand Down
Loading