Skip to content

[Insight] Make clientId optional and improve error handling #7158

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

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/breezy-dodos-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/engine": patch
---

client id optional
5 changes: 5 additions & 0 deletions .changeset/five-sheep-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/insight": patch
---

client id optional
5 changes: 5 additions & 0 deletions .changeset/green-olives-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Handle large NFT colletions when updating metadata
2 changes: 1 addition & 1 deletion packages/engine/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Config } from "@hey-api/client-fetch";
import { client } from "./client/client.gen.js";

export type EngineClientOptions = {
readonly clientId: string;
readonly clientId?: string;
readonly secretKey?: string;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/insight/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Config } from "@hey-api/client-fetch";
import { client } from "./client/client.gen.js";

export type InsightClientOptions = {
readonly clientId: string;
readonly clientId?: string;
readonly secretKey?: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,47 @@ describe.runIf(process.env.TW_SECRET_KEY)("updateMetadata ERC721", () => {
"No base URI set. Please set a base URI before updating metadata",
);
});

it(
"should handle very large batch",
{
timeout: 600000,
},
async () => {
const address = await deployERC721Contract({
client,
chain,
account,
type: "DropERC721",
params: {
name: "NFT Drop",
contractURI: TEST_CONTRACT_URI,
},
});
const contract = getContract({
address,
client,
chain,
});
const lazyMintTx = lazyMint({
contract,
nfts: Array.from({ length: 1000 }, (_, i) => ({
name: `token ${i}`,
})),
});
await sendAndConfirmTransaction({ transaction: lazyMintTx, account });
const updateTx = updateMetadata({
contract,
targetTokenId: 1n,
newMetadata: { name: "token 1 - updated" },
});
await sendAndConfirmTransaction({ transaction: updateTx, account });
const nfts = await getNFTs({ contract });

expect(nfts.length).toBe(100); // first page
expect(nfts[0]?.metadata.name).toBe("token 0");
expect(nfts[1]?.metadata.name).toBe("token 1 - updated");
expect(nfts[2]?.metadata.name).toBe("token 2");
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,30 @@
(_, k) => BigInt(k) + startTokenId,
);

const currentMetadatas = await Promise.all(
range.map((id) =>
GetNFT.getNFT({ contract, tokenId: id, includeOwner: false }),
),
);

// Abort if any of the items failed to load
if (currentMetadatas.some((item) => item === undefined || !item.tokenURI)) {
throw new Error(
`Failed to load all ${range.length} items from batchIndex: ${batchIndex}`,
const BATCH_SIZE = 50;
const currentMetadatas = [];
for (let i = 0; i < range.length; i += BATCH_SIZE) {
const chunk = range.slice(i, i + BATCH_SIZE);
const chunkResults = await Promise.all(
chunk.map((id) =>
GetNFT.getNFT({
contract,
tokenId: id,
includeOwner: false,
useIndexer: false,
}),
),
);
currentMetadatas.push(...chunkResults);
if (i + BATCH_SIZE < range.length) {
await new Promise((resolve) => setTimeout(resolve, 500));
}
// Abort if any of the items failed to load
if (currentMetadatas.some((item) => item === undefined || !item.tokenURI)) {
throw new Error(
`Failed to load all ${range.length} items from batchIndex: ${batchIndex}`,
);
}

Check warning on line 74 in packages/thirdweb/src/extensions/erc721/drops/write/updateMetadata.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc721/drops/write/updateMetadata.ts#L71-L74

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

const newMetadatas: NFTInput[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export async function getUserStatus({
method: "GET",
headers: {
"Content-Type": "application/json",
"x-thirdweb-client-id": client.clientId,
Authorization: `Bearer embedded-wallet-token:${authToken}`,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
}),
});

if (!res.ok) throw new Error("Failed to generate guest account");
if (!res.ok) {
const error = await res.text();
throw new Error(
`Failed to generate guest account: ${res.status} ${res.statusText} ${error}`,
);
}

Check warning on line 53 in packages/thirdweb/src/wallets/in-app/core/authentication/guest.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/authentication/guest.ts#L49-L53

Added lines #L49 - L53 were not covered by tests

return (await res.json()) satisfies AuthStoredTokenWithCookieReturnType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { TEST_CLIENT } from "../../../../../test/src/test-clients.js";
import { baseSepolia } from "../../../../chains/chain-definitions/base-sepolia.js";
import { sendBatchTransaction } from "../../../../transaction/actions/send-batch-transaction.js";
import { prepareTransaction } from "../../../../transaction/prepare-transaction.js";
import { generateAccount } from "../../../utils/generateAccount.js";
import { inAppWallet } from "../../web/in-app.js";

const client = TEST_CLIENT;
const chain = baseSepolia;

describe.runIf(process.env.TW_SECRET_KEY)("7702 Minimal Account", () => {
it("should batch transactions", async () => {
const iaw = inAppWallet({
executionMode: {
mode: "EIP7702",
sponsorGas: true,
},
});
const account = await iaw.connect({
client,
strategy: "guest",
chain,
});
const tx1 = prepareTransaction({
client,
chain,
to: (await generateAccount({ client })).address,
value: 0n,
});
const tx2 = prepareTransaction({
client,
chain,
to: (await generateAccount({ client })).address,
value: 0n,
});
const result = await sendBatchTransaction({
account,
transactions: [tx1, tx2],
});
expect(result.transactionHash).toBeDefined();
});
});
Loading