Skip to content

Commit 586b2e0

Browse files
[SDK] Use insight for erc721/getNFTs and erc721/getOwnedNFTs
1 parent e29f749 commit 586b2e0

File tree

34 files changed

+1061
-200
lines changed

34 files changed

+1061
-200
lines changed

.changeset/purple-bats-march.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Use insight for erc821/getNFT, erc721/getNFTs and erc721/getOwnedNFTs
6+
7+
Standard ERC721 getNFT, getNFTs and getOwnedNFTs now use insight, our in house indexer by default. If indexer is not availbale, will fallback to RPC.
8+
9+
You can also use the indexer directly using the Insight API:
10+
11+
for an entire collection
12+
13+
```ts
14+
import { Insight } from "thirdweb";
15+
16+
const events = await Insight.getContractNFTs({
17+
client,
18+
chains: [sepolia],
19+
contractAddress: "0x1234567890123456789012345678901234567890",
20+
});
21+
```
22+
23+
or for a single NFT
24+
25+
```ts
26+
import { Insight } from "thirdweb";
27+
28+
const events = await Insight.getNFT({
29+
client,
30+
chains: [sepolia],
31+
contractAddress: "0x1234567890123456789012345678901234567890",
32+
tokenId: 1n,
33+
});
34+
```

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ env:
1616
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
1717
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
1818
TW_SECRET_KEY: ${{ secrets.TW_SECRET_KEY }}
19+
TW_CLIENT_ID: ${{ secrets.TW_CLIENT_ID }}
1920
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2021

2122
jobs:

apps/dashboard/src/@/actions/getWalletNFTs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ async function getWalletNFTsFromInsight(params: {
194194
tokenURI: nft.metadata_url,
195195
type: nft.token_type === "erc721" ? "ERC721" : "ERC1155",
196196
supply: nft.balance,
197+
tokenAddress: nft.contract.address,
198+
chainId: nft.contract.chain_id,
197199
};
198200

199201
return walletNFT;

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/(marketplace)/components/list-form.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ export const CreateListingsForm: React.FC<CreateListingsFormProps> = ({
205205
owner: nft.owner,
206206
type: "ERC721",
207207
tokenURI: nft.tokenURI,
208+
chainId: nft.chainId,
209+
tokenAddress: nft.tokenAddress,
208210
};
209211
}
210212
return {
@@ -216,6 +218,8 @@ export const CreateListingsForm: React.FC<CreateListingsFormProps> = ({
216218
owner: nft.owner,
217219
type: "ERC1155",
218220
tokenURI: nft.tokenURI,
221+
chainId: nft.chainId,
222+
tokenAddress: nft.tokenAddress,
219223
};
220224
}) as WalletNFT[];
221225
}, [ownedNFTs, form]);

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/_components/NFTCards.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const dummyMetadata: (idx: number) => NFTWithContract = (idx) => ({
2121
owner: `0x_fake_${idx}`,
2222
type: "ERC721",
2323
supply: 1n,
24+
tokenAddress: ZERO_ADDRESS,
2425
});
2526

2627
interface NFTCardsProps {

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/account/components/nfts-owned.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const NftsOwned: React.FC<NftsOwnedProps> = ({
3333
type: nft.type,
3434
contractAddress: nft.contractAddress,
3535
chainId: contract.chain.id,
36+
tokenAddress: nft.tokenAddress,
3637
}))}
3738
allNfts
3839
isPending={isWalletNFTsLoading}

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/overview/components/MarketplaceDetails.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SkeletonContainer } from "@/components/ui/skeleton";
77
import { TrackedLinkTW } from "@/components/ui/tracked-link";
88
import { ArrowRightIcon } from "lucide-react";
99
import { useMemo } from "react";
10-
import type { ThirdwebContract } from "thirdweb";
10+
import { ZERO_ADDRESS, type ThirdwebContract } from "thirdweb";
1111
import {
1212
type DirectListing,
1313
type EnglishAuction,
@@ -237,13 +237,17 @@ const dummyMetadata: (idx: number) => ListingData = (idx) => ({
237237
supply: BigInt(1),
238238
tokenURI: "",
239239
type: "ERC721",
240+
tokenAddress: ZERO_ADDRESS,
241+
chainId: 1,
240242
},
241243
currencyValuePerToken: {
242244
decimals: 18,
243245
displayValue: "0.0",
244246
name: "Ether",
245247
symbol: "ETH",
246248
value: 0n,
249+
tokenAddress: ZERO_ADDRESS,
250+
chainId: 1,
247251
},
248252
creatorAddress: "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
249253
type: "direct-listing",
@@ -253,6 +257,8 @@ const dummyMetadata: (idx: number) => ListingData = (idx) => ({
253257
value: 0n,
254258
displayValue: "0.0",
255259
decimals: 18,
260+
tokenAddress: ZERO_ADDRESS,
261+
chainId: 1,
256262
},
257263
});
258264

packages/thirdweb/src/event/actions/get-events.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type {
55
ExtractAbiEventNames,
66
} from "abitype";
77
import { type Log, formatLog } from "viem";
8-
import { getChainServices } from "../../chains/utils.js";
98
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
109
import type { ThirdwebContract } from "../../contract/contract.js";
1110
import { getContractEvents as getContractEventsInsight } from "../../insight/get-events.js";
@@ -197,7 +196,7 @@ export async function getContractEvents<
197196
),
198197
);
199198
} catch (e) {
200-
console.warn("Error fetching from insight", e);
199+
console.warn("Error fetching from insight, falling back to rpc", e);
201200
// fetch from rpc
202201
logs = await Promise.all(
203202
logsParams.map((ethLogParams) => eth_getLogs(rpcRequest, ethLogParams)),
@@ -225,17 +224,6 @@ async function getLogsFromInsight(options: {
225224
}): Promise<Log[]> {
226225
const { params, contract } = options;
227226

228-
const chainServices = await getChainServices(contract.chain);
229-
const insightEnabled = chainServices.some(
230-
(c) => c.service === "insight" && c.enabled,
231-
);
232-
233-
if (!insightEnabled) {
234-
throw new Error(
235-
`Insight is not available for chainId ${contract.chain.id}`,
236-
);
237-
}
238-
239227
const fromBlock =
240228
typeof params.fromBlock === "bigint" ? Number(params.fromBlock) : undefined;
241229

packages/thirdweb/src/extensions/erc1155/read/getNFT.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("erc1155.getNFT", () => {
1111
});
1212
expect(nft).toMatchInlineSnapshot(`
1313
{
14+
"chainId": 1,
1415
"id": 2n,
1516
"metadata": {
1617
"animation_url": "ipfs://QmYoM63qaumQznBRx38tQjkY4ewbymeFb2KWBhkfMqNHax/3.mp4",
@@ -36,6 +37,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("erc1155.getNFT", () => {
3637
},
3738
"owner": null,
3839
"supply": 2519n,
40+
"tokenAddress": "0x42d3641255C946CC451474295d29D3505173F22A",
3941
"tokenURI": "ipfs://QmbMXdbnNUAuGRoY6c6G792c6T9utfaBGqRUaMaRUf52Cb/2",
4042
"type": "ERC1155",
4143
}

packages/thirdweb/src/extensions/erc1155/read/getNFT.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export async function getNFT(
5858
type: "ERC1155",
5959
owner: null,
6060
supply,
61+
tokenAddress: options.contract.address,
62+
chainId: options.contract.chain.id,
6163
},
6264
);
6365
}

0 commit comments

Comments
 (0)