Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/huge-berries-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Use insight for 1155 getNFTs, getOwnedNFTs and getNFT
11 changes: 11 additions & 0 deletions packages/thirdweb/src/extensions/erc1155/read/getNFT.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ import { DROP1155_CONTRACT } from "~test/test-contracts.js";
import { getNFT } from "./getNFT.js";

describe.runIf(process.env.TW_SECRET_KEY)("erc1155.getNFT", () => {
it("without owner with indexer", async () => {
const nft = await getNFT({
contract: DROP1155_CONTRACT,
tokenId: 2n,
});
expect(nft.metadata.name).toBe("Aura Platinum");
// biome-ignore lint/suspicious/noExplicitAny: todo type this better
expect((nft as any).supply).toBe(2519n);
});

it("without owner", async () => {
const nft = await getNFT({
contract: DROP1155_CONTRACT,
tokenId: 2n,
useIndexer: false,
});
expect(nft).toMatchInlineSnapshot(`
{
Expand Down
52 changes: 51 additions & 1 deletion packages/thirdweb/src/extensions/erc1155/read/getNFT.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getNFT as getNFTInsight } from "../../../insight/get-nfts.js";
import type { BaseTransactionOptions } from "../../../transaction/types.js";
import { fetchTokenMetadata } from "../../../utils/nft/fetchTokenMetadata.js";
import { type NFT, parseNFT } from "../../../utils/nft/parseNft.js";
import { totalSupply } from "../__generated__/IERC1155/read/totalSupply.js";
import { uri } from "../__generated__/IERC1155/read/uri.js";

export { isUriSupported as isGetNFTSupported } from "../__generated__/IERC1155/read/uri.js";

/**
Expand All @@ -12,6 +12,11 @@ export { isUriSupported as isGetNFTSupported } from "../__generated__/IERC1155/r
*/
export type GetNFTParams = {
tokenId: bigint;
/**
* Whether to use the insight API to fetch the NFT.
* @default true
*/
useIndexer?: boolean;
};

/**
Expand All @@ -30,6 +35,51 @@ export type GetNFTParams = {
*/
export async function getNFT(
options: BaseTransactionOptions<GetNFTParams>,
): Promise<NFT> {
const { useIndexer = true } = options;
if (useIndexer) {
try {
return await getNFTFromInsight(options);
} catch {
return await getNFTFromRPC(options);
}
}
return await getNFTFromRPC(options);
}

async function getNFTFromInsight(
options: BaseTransactionOptions<GetNFTParams>,
): Promise<NFT> {
const tokenId = options.tokenId;
const nft = await getNFTInsight({
client: options.contract.client,
chain: options.contract.chain,
contractAddress: options.contract.address,
tokenId: options.tokenId,
});
if (!nft) {
return parseNFT(
{
id: tokenId,
type: "ERC1155",
uri: "",
},
{
tokenId: options.tokenId,
tokenUri: "",
type: "ERC1155",
owner: null,
supply: 0n,
tokenAddress: options.contract.address,
chainId: options.contract.chain.id,
},
);
}
return nft;
}

async function getNFTFromRPC(
options: BaseTransactionOptions<GetNFTParams>,
): Promise<NFT> {
const [tokenUri, supply] = await Promise.all([
uri({
Expand Down
38 changes: 38 additions & 0 deletions packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { maxUint256 } from "ox/Solidity";
import { getContractNFTs } from "../../../insight/get-nfts.js";
import type { BaseTransactionOptions } from "../../../transaction/types.js";
import { min } from "../../../utils/bigint.js";
import type { NFT } from "../../../utils/nft/parseNft.js";
Expand All @@ -23,6 +24,11 @@
* The number of NFTs to retrieve.
*/
count?: number;
/**
* Whether to use the insight API to fetch the NFTs.
* @default true
*/
useIndexer?: boolean;
};

/**
Expand All @@ -42,6 +48,38 @@
*/
export async function getNFTs(
options: BaseTransactionOptions<GetNFTsParams>,
): Promise<NFT[]> {
const { useIndexer = true } = options;
if (useIndexer) {
try {
return await getNFTsFromInsight(options);
} catch {
return await getNFTsFromRPC(options);
}
}
return await getNFTsFromRPC(options);
}

Check warning on line 61 in packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts#L60-L61

Added lines #L60 - L61 were not covered by tests

async function getNFTsFromInsight(
options: BaseTransactionOptions<GetNFTsParams>,
): Promise<NFT[]> {
const { contract, start, count = Number(DEFAULT_QUERY_ALL_COUNT) } = options;

const result = await getContractNFTs({
client: contract.client,
chains: [contract.chain],
contractAddress: contract.address,
queryOptions: {
limit: count,
page: start ? Math.floor(start / count) : undefined,
},
});

return result;
}

Check warning on line 79 in packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts#L78-L79

Added lines #L78 - L79 were not covered by tests

async function getNFTsFromRPC(
options: BaseTransactionOptions<GetNFTsParams>,
): Promise<NFT[]> {
const start = BigInt(options.start || 0);
const count = BigInt(options.count || DEFAULT_QUERY_ALL_COUNT);
Expand Down
67 changes: 65 additions & 2 deletions packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { getOwnedNFTs as getInsightNFTs } from "../../../insight/get-nfts.js";
import type { BaseTransactionOptions } from "../../../transaction/types.js";
import type { NFT } from "../../../utils/nft/parseNft.js";
import { getNFT } from "./getNFT.js";
import {
type GetOwnedTokenIdsParams,
getOwnedTokenIds,
} from "./getOwnedTokenIds.js";

/**
* Parameters for retrieving NFTs.
* @extension ERC1155
*/
export type GetOwnedNFTsParams = GetOwnedTokenIdsParams;
export type GetOwnedNFTsParams = GetOwnedTokenIdsParams & {
/**
* Whether to use the insight API to fetch the NFTs.
* @default true
*/
useIndexer?: boolean;
};

/**
* Retrieves the owned ERC1155 NFTs for a given wallet address.
Expand All @@ -30,6 +36,63 @@
*/
export async function getOwnedNFTs(
options: BaseTransactionOptions<GetOwnedNFTsParams>,
): Promise<(NFT & { quantityOwned: bigint })[]> {
const { useIndexer = true } = options;
if (useIndexer) {
try {
return await getOwnedNFTsFromInsight(options);
} catch {
return await getOwnedNFTsFromRPC(options);
}
}
return await getOwnedNFTsFromRPC(options);
}

Check warning on line 49 in packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts#L48-L49

Added lines #L48 - L49 were not covered by tests

async function getOwnedNFTsFromInsight(
options: BaseTransactionOptions<GetOwnedNFTsParams>,
): Promise<(NFT & { quantityOwned: bigint })[]> {
const limit = 50;
const nfts: (NFT & { quantityOwned: bigint })[] = [];
let page = 0;
let hasMore = true;

// TODO (insight): add support for contract address filters
while (hasMore) {
const pageResults = await getInsightNFTs({
client: options.contract.client,
chains: [options.contract.chain],
ownerAddress: options.address,
queryOptions: {
limit,
page,
},
});

nfts.push(...pageResults);

Check warning on line 71 in packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts#L71

Added line #L71 was not covered by tests

// If we got fewer results than the limit, we've reached the end
if (pageResults.length < limit) {
hasMore = false;
} else {
page++;
}

Check warning on line 78 in packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts#L74-L78

Added lines #L74 - L78 were not covered by tests
}

const results = nfts;

Check warning on line 81 in packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts#L81

Added line #L81 was not covered by tests

return results
.filter(
(n) =>
n.tokenAddress.toLowerCase() === options.contract.address.toLowerCase(),
)
.map((result) => ({
...result,
owner: options.address,
}));
}

Check warning on line 92 in packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts#L83-L92

Added lines #L83 - L92 were not covered by tests

async function getOwnedNFTsFromRPC(
options: BaseTransactionOptions<GetOwnedNFTsParams>,
): Promise<(NFT & { quantityOwned: bigint })[]> {
const ownedBalances = await getOwnedTokenIds(options);

Expand Down
Loading
Loading