thirdweb 5.48.3 → 5.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/cjs/exports/react.js +3 -1
  2. package/dist/cjs/exports/react.js.map +1 -1
  3. package/dist/cjs/gas/fee-data.js +2 -0
  4. package/dist/cjs/gas/fee-data.js.map +1 -1
  5. package/dist/cjs/react/core/hooks/wallets/useSendToken.js +1 -1
  6. package/dist/cjs/react/core/hooks/wallets/useSendToken.js.map +1 -1
  7. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js +11 -2
  8. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js.map +1 -1
  9. package/dist/cjs/react/web/ui/components/Modal.js +12 -2
  10. package/dist/cjs/react/web/ui/components/Modal.js.map +1 -1
  11. package/dist/cjs/react/web/ui/prebuilt/NFT/NFT.js +217 -0
  12. package/dist/cjs/react/web/ui/prebuilt/NFT/NFT.js.map +1 -0
  13. package/dist/cjs/utils/abi/encodeAbiParameters.js +2 -1
  14. package/dist/cjs/utils/abi/encodeAbiParameters.js.map +1 -1
  15. package/dist/cjs/utils/nft/parseNft.js.map +1 -1
  16. package/dist/cjs/version.js +1 -1
  17. package/dist/cjs/wallets/in-app/native/helpers/api/fetchers.js +13 -2
  18. package/dist/cjs/wallets/in-app/native/helpers/api/fetchers.js.map +1 -1
  19. package/dist/cjs/wallets/in-app/web/lib/in-app-account.js.map +1 -1
  20. package/dist/esm/exports/react.js +1 -0
  21. package/dist/esm/exports/react.js.map +1 -1
  22. package/dist/esm/gas/fee-data.js +2 -0
  23. package/dist/esm/gas/fee-data.js.map +1 -1
  24. package/dist/esm/react/core/hooks/wallets/useSendToken.js +1 -1
  25. package/dist/esm/react/core/hooks/wallets/useSendToken.js.map +1 -1
  26. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js +11 -2
  27. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js.map +1 -1
  28. package/dist/esm/react/web/ui/components/Modal.js +12 -2
  29. package/dist/esm/react/web/ui/components/Modal.js.map +1 -1
  30. package/dist/esm/react/web/ui/prebuilt/NFT/NFT.js +212 -0
  31. package/dist/esm/react/web/ui/prebuilt/NFT/NFT.js.map +1 -0
  32. package/dist/esm/utils/abi/encodeAbiParameters.js +2 -1
  33. package/dist/esm/utils/abi/encodeAbiParameters.js.map +1 -1
  34. package/dist/esm/utils/nft/parseNft.js.map +1 -1
  35. package/dist/esm/version.js +1 -1
  36. package/dist/esm/wallets/in-app/native/helpers/api/fetchers.js +13 -2
  37. package/dist/esm/wallets/in-app/native/helpers/api/fetchers.js.map +1 -1
  38. package/dist/esm/wallets/in-app/web/lib/in-app-account.js.map +1 -1
  39. package/dist/types/exports/react.d.ts +1 -0
  40. package/dist/types/exports/react.d.ts.map +1 -1
  41. package/dist/types/gas/fee-data.d.ts.map +1 -1
  42. package/dist/types/react/web/ui/components/Modal.d.ts.map +1 -1
  43. package/dist/types/react/web/ui/prebuilt/NFT/NFT.d.ts +75 -0
  44. package/dist/types/react/web/ui/prebuilt/NFT/NFT.d.ts.map +1 -0
  45. package/dist/types/utils/abi/encodeAbiParameters.d.ts.map +1 -1
  46. package/dist/types/utils/nft/parseNft.d.ts +1 -0
  47. package/dist/types/utils/nft/parseNft.d.ts.map +1 -1
  48. package/dist/types/version.d.ts +1 -1
  49. package/dist/types/wallets/in-app/native/helpers/api/fetchers.d.ts +1 -1
  50. package/dist/types/wallets/in-app/native/helpers/api/fetchers.d.ts.map +1 -1
  51. package/dist/types/wallets/in-app/web/lib/in-app-account.d.ts.map +1 -1
  52. package/package.json +1 -1
  53. package/src/exports/react.ts +5 -0
  54. package/src/extensions/erc1155/drops/read/getActiveClaimCondition.test.ts +87 -0
  55. package/src/gas/fee-data.ts +2 -0
  56. package/src/react/core/hooks/wallets/useSendToken.ts +1 -1
  57. package/src/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.tsx +12 -2
  58. package/src/react/web/ui/components/Modal.tsx +17 -1
  59. package/src/react/web/ui/prebuilt/NFT/NFT.test.tsx +111 -0
  60. package/src/react/web/ui/prebuilt/NFT/NFT.tsx +272 -0
  61. package/src/utils/abi/encodeAbiParameters.ts +2 -1
  62. package/src/utils/nft/parseNft.ts +1 -0
  63. package/src/version.ts +1 -1
  64. package/src/wallets/in-app/native/helpers/api/fetchers.ts +17 -1
  65. package/src/wallets/in-app/web/lib/in-app-account.ts +1 -0
@@ -0,0 +1,111 @@
1
+ import { useContext } from "react";
2
+ import { describe, expect, it } from "vitest";
3
+ import { render, screen, waitFor } from "~test/react-render.js";
4
+ import { DOODLES_CONTRACT } from "~test/test-contracts.js";
5
+ import { NFT, NFTProviderContext, getNFTInfo } from "./NFT.js";
6
+
7
+ describe.runIf(process.env.TW_SECRET_KEY)("NFT prebuilt component", () => {
8
+ it("should fetch the NFT metadata", async () => {
9
+ const nft = await getNFTInfo({
10
+ contract: DOODLES_CONTRACT,
11
+ tokenId: 1n,
12
+ });
13
+ expect(nft.metadata).toStrictEqual({
14
+ attributes: [
15
+ {
16
+ trait_type: "face",
17
+ value: "holographic beard",
18
+ },
19
+ {
20
+ trait_type: "hair",
21
+ value: "white bucket cap",
22
+ },
23
+ {
24
+ trait_type: "body",
25
+ value: "purple sweater with satchel",
26
+ },
27
+ {
28
+ trait_type: "background",
29
+ value: "grey",
30
+ },
31
+ {
32
+ trait_type: "head",
33
+ value: "gradient 2",
34
+ },
35
+ ],
36
+ description:
37
+ "A community-driven collectibles project featuring art by Burnt Toast. Doodles come in a joyful range of colors, traits and sizes with a collection size of 10,000. Each Doodle allows its owner to vote for experiences and activations paid for by the Doodles Community Treasury. Burnt Toast is the working alias for Scott Martin, a Canadian–based illustrator, designer, animator and muralist.",
38
+ image: "ipfs://QmTDxnzcvj2p3xBrKcGv1wxoyhAn2yzCQnZZ9LmFjReuH9",
39
+ name: "Doodle #1",
40
+ });
41
+ });
42
+
43
+ it("should render children correctly", () => {
44
+ render(
45
+ <NFT contract={DOODLES_CONTRACT} tokenId={0n}>
46
+ <div>Child Component</div>
47
+ </NFT>,
48
+ );
49
+
50
+ expect(screen.getByText("Child Component")).toBeInTheDocument();
51
+ });
52
+
53
+ it("should provide context values to children", () => {
54
+ function NFTConsumer() {
55
+ const context = useContext(NFTProviderContext);
56
+ if (!context) {
57
+ return <div>No context</div>;
58
+ }
59
+ return (
60
+ <div>
61
+ Contract: {String(context.contract)}, Token ID:{" "}
62
+ {context.tokenId.toString()}
63
+ </div>
64
+ );
65
+ }
66
+ render(
67
+ <NFT contract={DOODLES_CONTRACT} tokenId={0n}>
68
+ <NFTConsumer />
69
+ </NFT>,
70
+ );
71
+
72
+ expect(screen.getByText(/Contract:/)).toBeInTheDocument();
73
+ expect(screen.getByText(/Token ID: 0/)).toBeInTheDocument();
74
+ });
75
+
76
+ it("should render the NFT image", () => {
77
+ render(
78
+ <NFT contract={DOODLES_CONTRACT} tokenId={0n}>
79
+ <NFT.Media />
80
+ </NFT>,
81
+ );
82
+
83
+ waitFor(() => expect(screen.getByRole("img")).toBeInTheDocument());
84
+ });
85
+
86
+ it("should render the NFT name", () => {
87
+ render(
88
+ <NFT contract={DOODLES_CONTRACT} tokenId={1n}>
89
+ <NFT.Name />
90
+ </NFT>,
91
+ );
92
+
93
+ waitFor(() => expect(screen.getByText("Doodle #1")).toBeInTheDocument());
94
+ });
95
+
96
+ it("should render the NFT description", () => {
97
+ render(
98
+ <NFT contract={DOODLES_CONTRACT} tokenId={1n}>
99
+ <NFT.Name />
100
+ </NFT>,
101
+ );
102
+
103
+ waitFor(() =>
104
+ expect(
105
+ screen.getByText(
106
+ "A community-driven collectibles project featuring art by Burnt Toast",
107
+ ),
108
+ ).toBeInTheDocument(),
109
+ );
110
+ });
111
+ });
@@ -0,0 +1,272 @@
1
+ "use client";
2
+
3
+ import { useSuspenseQuery } from "@tanstack/react-query";
4
+ import { createContext, useContext } from "react";
5
+ import type { ThirdwebContract } from "../../../../../contract/contract.js";
6
+ import { getNFT as getNFT721 } from "../../../../../extensions/erc721/read/getNFT.js";
7
+ import { getNFT as getNFT1155 } from "../../../../../extensions/erc1155/read/getNFT.js";
8
+ import type { NFT as NFTType } from "../../../../../utils/nft/parseNft.js";
9
+ import { MediaRenderer } from "../../MediaRenderer/MediaRenderer.js";
10
+ import type { MediaRendererProps } from "../../MediaRenderer/types.js";
11
+
12
+ /**
13
+ * Props for the <NFT> component
14
+ * @component
15
+ */
16
+ export type NFTProviderProps = {
17
+ /**
18
+ * The NFT contract address. Accepts both ERC721 and ERC1155 contracts
19
+ */
20
+ contract: ThirdwebContract;
21
+ /**
22
+ * The tokenId whose info you want to display
23
+ */
24
+ tokenId: bigint;
25
+ };
26
+
27
+ /**
28
+ * @internal
29
+ */
30
+ export const NFTProviderContext = /* @__PURE__ */ createContext<
31
+ NFTProviderProps | undefined
32
+ >(undefined);
33
+
34
+ /**
35
+ * A React context provider component that supplies NFT-related data to its child components.
36
+ *
37
+ * This component serves as a wrapper around the `NFTProviderContext.Provider` and passes
38
+ * the provided NFT data down to all of its child components through the context API.
39
+ *
40
+ *
41
+ * @component
42
+ * @param {React.PropsWithChildren<NFTProviderProps>} props - The props for the NFT provider
43
+ *
44
+ * @example
45
+ * ```tsx
46
+ * import { getContract } from "thirdweb";
47
+ * import { NFT } from "thirdweb/react";
48
+ *
49
+ * const contract = getContract({
50
+ * address: "0x...",
51
+ * chain: ethereum,
52
+ * client: yourThirdwebClient,
53
+ * });
54
+ *
55
+ * <NFT contract={contract} tokenId={0n}>
56
+ * <Suspense fallback={"Loading media..."}>
57
+ * <NFT.Media />
58
+ * <NFT.Description />
59
+ * </Suspense>
60
+ * </NFT>
61
+ * ```
62
+ */
63
+ export function NFT(props: React.PropsWithChildren<NFTProviderProps>) {
64
+ return (
65
+ <NFTProviderContext.Provider value={props}>
66
+ {props.children}
67
+ </NFTProviderContext.Provider>
68
+ );
69
+ }
70
+
71
+ /**
72
+ * @internal
73
+ */
74
+ function useNFTContext() {
75
+ const ctx = useContext(NFTProviderContext);
76
+ if (!ctx) {
77
+ throw new Error(
78
+ "NFTProviderContext not found. Make sure you are using NFT.Media, NFT.Description, etc. inside a <NFT /> component",
79
+ );
80
+ }
81
+ return ctx;
82
+ }
83
+
84
+ /**
85
+ * @component
86
+ * The props for the <NFT.Media /> component
87
+ * It is similar to the [`MediaRendererProps`](https://portal.thirdweb.com/references/typescript/v5/MediaRendererProps)
88
+ * (excluding `src`, `poster` and `client`) that you can
89
+ * use to style the NFT.Media
90
+ */
91
+ export type NFTMediaProps = Omit<
92
+ MediaRendererProps,
93
+ "src" | "poster" | "client"
94
+ >;
95
+
96
+ /**
97
+ * This component fetches and displays an NFT's media. It uses thirdweb [`MediaRenderer`](https://portal.thirdweb.com/react/v5/components/MediaRenderer) under the hood
98
+ * so you can style it just like how you would style a MediaRenderer.
99
+ * @returns A MediaRenderer component
100
+ *
101
+ * Since this component has an internal loading state (for when the NFT media is being fetched),
102
+ * you must wrap it with React.Suspense to make it work.
103
+ *
104
+ * @component
105
+ * @example
106
+ * ### Basic usage
107
+ * ```tsx
108
+ * import { getContract } from "thirdweb";
109
+ * import { NFT } from "thirdweb/react";
110
+ *
111
+ * const nftContract = getContract({
112
+ * address: "0x...",
113
+ * chain: ethereum,
114
+ * client: yourThirdwebClient,
115
+ * });
116
+ *
117
+ * <NFT contract={nftContract} tokenId={0n}>
118
+ * This will show the media for tokenId #0 from the `nftContract` collection
119
+ * <Suspense fallback={"Loading media..."}>
120
+ * <NFT.Media />
121
+ * </Suspense>
122
+ * </NFT>
123
+ * ```
124
+ *
125
+ * ### Basic stylings
126
+ * You can style NFT.Media with the `style` and `className` props.
127
+ * ```tsx
128
+ * <NFT.Media style={{ borderRadius: "8px" }} className="mx-auto" />
129
+ * ```
130
+ */
131
+ NFT.Media = (props: NFTMediaProps) => {
132
+ const { contract, tokenId } = useNFTContext();
133
+ const nftQuery = useSuspenseQuery({
134
+ queryKey: [
135
+ "__nft_info_internal__",
136
+ contract.chain.id,
137
+ contract.address,
138
+ tokenId.toString(),
139
+ ],
140
+ queryFn: () => getNFTInfo({ contract, tokenId }),
141
+ });
142
+ const animation_url = nftQuery.data?.metadata.animation_url;
143
+ const image =
144
+ nftQuery.data?.metadata.image || nftQuery.data?.metadata.image_url;
145
+
146
+ return (
147
+ <MediaRenderer
148
+ client={contract.client}
149
+ src={animation_url || image}
150
+ poster={image}
151
+ {...props}
152
+ />
153
+ );
154
+ };
155
+
156
+ /**
157
+ * This component fetches and displays an NFT's name. It takes in a `className` and `style` props
158
+ * so you can style it just like how you would style a <span> element.
159
+ * @returns A <span> element containing the name of the NFT
160
+ *
161
+ * Since this component has an internal loading state (for when the NFT media is being fetched),
162
+ * you must wrap it with React.Suspense to make it work.
163
+ *
164
+ * @component
165
+ * @example
166
+ * ### Basic usage
167
+ * ```tsx
168
+ * import { getContract } from "thirdweb";
169
+ * import { NFT } from "thirdweb/react";
170
+ *
171
+ * const nftContract = getContract({
172
+ * address: "0x...",
173
+ * chain: ethereum,
174
+ * client: yourThirdwebClient,
175
+ * });
176
+ *
177
+ * <NFT contract={nftContract} tokenId={0n}>
178
+ * This will show the name for tokenId #0 from the `nftContract` collection
179
+ * <Suspense fallback={"Loading nft name..."}>
180
+ * <NFT.Name className="mx-auto" style={{ color: "red" }} />
181
+ * </Suspense>
182
+ * </NFT>
183
+ * ```
184
+ */
185
+ NFT.Name = (props: { className?: string; style?: React.CSSProperties }) => {
186
+ const { contract, tokenId } = useNFTContext();
187
+ const nftQuery = useSuspenseQuery({
188
+ queryKey: [
189
+ "__nft_info_internal__",
190
+ contract.chain.id,
191
+ contract.address,
192
+ tokenId.toString(),
193
+ ],
194
+ queryFn: () => getNFTInfo({ contract, tokenId }),
195
+ });
196
+ const name = nftQuery.data?.metadata.name || "";
197
+ return <span {...props}>{name}</span>;
198
+ };
199
+
200
+ /**
201
+ * This component fetches and displays an NFT's description. It takes in a `className` and `style` props
202
+ * so you can style it just like how you would style a <span> element.
203
+ * @returns A <span> element containing the description of the NFT
204
+ *
205
+ * Since this component has an internal loading state (for when the NFT media is being fetched),
206
+ * you must wrap it with React.Suspense to make it work.
207
+ *
208
+ * @component
209
+ * @example
210
+ * ### Basic usage
211
+ * ```tsx
212
+ * import { getContract } from "thirdweb";
213
+ * import { NFT } from "thirdweb/react";
214
+ *
215
+ * const nftContract = getContract({
216
+ * address: "0x...",
217
+ * chain: ethereum,
218
+ * client: yourThirdwebClient,
219
+ * });
220
+ *
221
+ * <NFT contract={nftContract} tokenId={0n}>
222
+ * This will show the description for tokenId #0 from the `nftContract` collection
223
+ * <Suspense fallback={"Loading description..."}>
224
+ * <NFT.Description className="mx-auto" style={{ color: "red" }} />
225
+ * </Suspense>
226
+ * </NFT>
227
+ * ```
228
+ */
229
+ NFT.Description = (props: {
230
+ className: string;
231
+ style: React.CSSProperties;
232
+ }) => {
233
+ const { contract, tokenId } = useNFTContext();
234
+ const nftQuery = useSuspenseQuery({
235
+ queryKey: [
236
+ "__nft_info_internal__",
237
+ contract.chain.id,
238
+ contract.address,
239
+ tokenId.toString(),
240
+ ],
241
+ queryFn: () => getNFTInfo({ contract, tokenId }),
242
+ });
243
+ const description = nftQuery.data?.metadata.description || "";
244
+ return <span {...props}>{description}</span>;
245
+ };
246
+
247
+ /**
248
+ * @internal
249
+ */
250
+ export async function getNFTInfo(options: NFTProviderProps): Promise<NFTType> {
251
+ const nft = await Promise.allSettled([
252
+ getNFT721(options),
253
+ getNFT1155(options),
254
+ ]).then(([possibleNFT721, possibleNFT1155]) => {
255
+ // getNFT extension always return an NFT object
256
+ // so we need to check if the tokenURI exists
257
+ if (
258
+ possibleNFT721.status === "fulfilled" &&
259
+ possibleNFT721.value.tokenURI
260
+ ) {
261
+ return possibleNFT721.value;
262
+ }
263
+ if (
264
+ possibleNFT1155.status === "fulfilled" &&
265
+ possibleNFT1155.value.tokenURI
266
+ ) {
267
+ return possibleNFT1155.value;
268
+ }
269
+ throw new Error("Failed to load NFT metadata");
270
+ });
271
+ return nft;
272
+ }
@@ -158,7 +158,8 @@ function encodeParams(preparedParams: PreparedParam[]): Hex {
158
158
  * @internal Export for unit test
159
159
  */
160
160
  export function encodeAddress(value: Hex): PreparedParam {
161
- if (!isAddress(value)) {
161
+ // We allow empty strings for deployment transactions where there is no to address
162
+ if ((value as string) !== "" && value !== undefined && !isAddress(value)) {
162
163
  throw new Error("Invalid address.");
163
164
  }
164
165
  return { dynamic: false, encoded: padHex(value.toLowerCase() as Hex) };
@@ -27,6 +27,7 @@ export type NFTMetadata = {
27
27
  background_color?: string;
28
28
  properties?: Record<string, unknown>;
29
29
  attributes?: Record<string, unknown>;
30
+ image_url?: string;
30
31
  } & Record<string, unknown>;
31
32
 
32
33
  export type NFT =
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.48.3";
1
+ export const version = "5.49.0";
@@ -59,6 +59,7 @@ export const authFetchEmbeddedWalletUser = async (
59
59
  client: ThirdwebClient,
60
60
  url: string,
61
61
  props: Parameters<typeof fetch>[1],
62
+ retries = 3,
62
63
  ): Promise<Response> => {
63
64
  const authTokenClient = await getAuthTokenClient(client.clientId);
64
65
  const params = { ...props };
@@ -78,7 +79,22 @@ export const authFetchEmbeddedWalletUser = async (
78
79
  [PAPER_CLIENT_ID_HEADER]: client.clientId,
79
80
  ...getSessionHeaders(),
80
81
  };
81
- return getClientFetch(client)(url, params);
82
+
83
+ try {
84
+ return await getClientFetch(client)(url, params);
85
+ } catch (e) {
86
+ if (retries > 0) {
87
+ await new Promise((resolve) => setTimeout(resolve, 500));
88
+ return await authFetchEmbeddedWalletUser(
89
+ client,
90
+ url,
91
+ params,
92
+ retries - 1,
93
+ );
94
+ } else {
95
+ throw e;
96
+ }
97
+ }
82
98
  };
83
99
 
84
100
  export async function fetchUserDetails(args: {
@@ -223,6 +223,7 @@ export class IFrameWallet {
223
223
  nonce: tx.nonce,
224
224
  chainId: tx.chainId,
225
225
  };
226
+
226
227
  if (tx.maxFeePerGas) {
227
228
  // ethers (in the iframe) rejects any type 0 trasaction with unknown keys
228
229
  // TODO remove this once iframe is upgraded to v5