thirdweb 5.113.0 → 5.114.1

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 (93) hide show
  1. package/dist/cjs/chains/chain-definitions/monad.js +23 -0
  2. package/dist/cjs/chains/chain-definitions/monad.js.map +1 -0
  3. package/dist/cjs/exports/chains.js +4 -2
  4. package/dist/cjs/exports/chains.js.map +1 -1
  5. package/dist/cjs/react/core/hooks/others/useInvalidateBalances.js +1 -1
  6. package/dist/cjs/react/core/hooks/others/useInvalidateBalances.js.map +1 -1
  7. package/dist/cjs/react/core/hooks/x402/useFetchWithPaymentCore.js +2 -1
  8. package/dist/cjs/react/core/hooks/x402/useFetchWithPaymentCore.js.map +1 -1
  9. package/dist/cjs/react/web/hooks/x402/useFetchWithPayment.js +13 -1
  10. package/dist/cjs/react/web/hooks/x402/useFetchWithPayment.js.map +1 -1
  11. package/dist/cjs/react/web/ui/x402/SignInRequiredModal.js +3 -3
  12. package/dist/cjs/react/web/ui/x402/SignInRequiredModal.js.map +1 -1
  13. package/dist/cjs/version.js +1 -1
  14. package/dist/cjs/x402/common.js +2 -10
  15. package/dist/cjs/x402/common.js.map +1 -1
  16. package/dist/cjs/x402/facilitator.js +1 -0
  17. package/dist/cjs/x402/facilitator.js.map +1 -1
  18. package/dist/cjs/x402/fetchWithPayment.js +5 -8
  19. package/dist/cjs/x402/fetchWithPayment.js.map +1 -1
  20. package/dist/cjs/x402/schemas.js +4 -1
  21. package/dist/cjs/x402/schemas.js.map +1 -1
  22. package/dist/cjs/x402/settle-payment.js +12 -1
  23. package/dist/cjs/x402/settle-payment.js.map +1 -1
  24. package/dist/cjs/x402/types.js +6 -1
  25. package/dist/cjs/x402/types.js.map +1 -1
  26. package/dist/cjs/x402/verify-payment.js +11 -1
  27. package/dist/cjs/x402/verify-payment.js.map +1 -1
  28. package/dist/esm/chains/chain-definitions/monad.js +20 -0
  29. package/dist/esm/chains/chain-definitions/monad.js.map +1 -0
  30. package/dist/esm/exports/chains.js +1 -0
  31. package/dist/esm/exports/chains.js.map +1 -1
  32. package/dist/esm/react/core/hooks/others/useInvalidateBalances.js +1 -1
  33. package/dist/esm/react/core/hooks/others/useInvalidateBalances.js.map +1 -1
  34. package/dist/esm/react/core/hooks/x402/useFetchWithPaymentCore.js +2 -1
  35. package/dist/esm/react/core/hooks/x402/useFetchWithPaymentCore.js.map +1 -1
  36. package/dist/esm/react/web/hooks/x402/useFetchWithPayment.js +13 -1
  37. package/dist/esm/react/web/hooks/x402/useFetchWithPayment.js.map +1 -1
  38. package/dist/esm/react/web/ui/x402/SignInRequiredModal.js +3 -3
  39. package/dist/esm/react/web/ui/x402/SignInRequiredModal.js.map +1 -1
  40. package/dist/esm/version.js +1 -1
  41. package/dist/esm/x402/common.js +2 -10
  42. package/dist/esm/x402/common.js.map +1 -1
  43. package/dist/esm/x402/facilitator.js +1 -0
  44. package/dist/esm/x402/facilitator.js.map +1 -1
  45. package/dist/esm/x402/fetchWithPayment.js +5 -8
  46. package/dist/esm/x402/fetchWithPayment.js.map +1 -1
  47. package/dist/esm/x402/schemas.js +4 -1
  48. package/dist/esm/x402/schemas.js.map +1 -1
  49. package/dist/esm/x402/settle-payment.js +12 -1
  50. package/dist/esm/x402/settle-payment.js.map +1 -1
  51. package/dist/esm/x402/types.js +5 -0
  52. package/dist/esm/x402/types.js.map +1 -1
  53. package/dist/esm/x402/verify-payment.js +11 -1
  54. package/dist/esm/x402/verify-payment.js.map +1 -1
  55. package/dist/scripts/bridge-widget.js +2 -2
  56. package/dist/types/chains/chain-definitions/monad.d.ts +7 -0
  57. package/dist/types/chains/chain-definitions/monad.d.ts.map +1 -0
  58. package/dist/types/exports/chains.d.ts +1 -0
  59. package/dist/types/exports/chains.d.ts.map +1 -1
  60. package/dist/types/react/core/hooks/others/useInvalidateBalances.d.ts +1 -1
  61. package/dist/types/react/core/hooks/others/useInvalidateBalances.d.ts.map +1 -1
  62. package/dist/types/react/core/hooks/x402/useFetchWithPaymentCore.d.ts.map +1 -1
  63. package/dist/types/react/web/hooks/x402/useFetchWithPayment.d.ts +23 -0
  64. package/dist/types/react/web/hooks/x402/useFetchWithPayment.d.ts.map +1 -1
  65. package/dist/types/react/web/ui/x402/SignInRequiredModal.d.ts +3 -0
  66. package/dist/types/react/web/ui/x402/SignInRequiredModal.d.ts.map +1 -1
  67. package/dist/types/version.d.ts +1 -1
  68. package/dist/types/x402/common.d.ts.map +1 -1
  69. package/dist/types/x402/facilitator.d.ts.map +1 -1
  70. package/dist/types/x402/fetchWithPayment.d.ts.map +1 -1
  71. package/dist/types/x402/schemas.d.ts +15 -15
  72. package/dist/types/x402/schemas.d.ts.map +1 -1
  73. package/dist/types/x402/settle-payment.d.ts +12 -1
  74. package/dist/types/x402/settle-payment.d.ts.map +1 -1
  75. package/dist/types/x402/types.d.ts +13 -1
  76. package/dist/types/x402/types.d.ts.map +1 -1
  77. package/dist/types/x402/verify-payment.d.ts +11 -1
  78. package/dist/types/x402/verify-payment.d.ts.map +1 -1
  79. package/package.json +3 -3
  80. package/src/chains/chain-definitions/monad.ts +20 -0
  81. package/src/exports/chains.ts +1 -0
  82. package/src/react/core/hooks/others/useInvalidateBalances.ts +1 -1
  83. package/src/react/core/hooks/x402/useFetchWithPaymentCore.ts +2 -1
  84. package/src/react/web/hooks/x402/useFetchWithPayment.tsx +26 -0
  85. package/src/react/web/ui/x402/SignInRequiredModal.tsx +15 -5
  86. package/src/version.ts +1 -1
  87. package/src/x402/common.ts +2 -20
  88. package/src/x402/facilitator.ts +1 -0
  89. package/src/x402/fetchWithPayment.ts +5 -11
  90. package/src/x402/schemas.ts +4 -1
  91. package/src/x402/settle-payment.ts +12 -1
  92. package/src/x402/types.ts +16 -1
  93. package/src/x402/verify-payment.ts +11 -1
@@ -15,13 +15,23 @@ type SignInRequiredModalProps = {
15
15
  theme: Theme | "light" | "dark";
16
16
  onSignIn: () => void;
17
17
  onCancel: () => void;
18
+ title?: string;
19
+ description?: string;
20
+ buttonLabel?: string;
18
21
  };
19
22
 
20
23
  /**
21
24
  * @internal
22
25
  */
23
26
  export function SignInRequiredModal(props: SignInRequiredModalProps) {
24
- const { theme, onSignIn, onCancel } = props;
27
+ const {
28
+ theme,
29
+ onSignIn,
30
+ onCancel,
31
+ title = "Sign in required",
32
+ description = "Account required to complete payment, please sign in to continue.",
33
+ buttonLabel = "Sign in",
34
+ } = props;
25
35
 
26
36
  return (
27
37
  <CustomThemeProvider theme={theme}>
@@ -35,10 +45,10 @@ export function SignInRequiredModal(props: SignInRequiredModalProps) {
35
45
  }
36
46
  }}
37
47
  size="compact"
38
- title="Sign in required"
48
+ title={title}
39
49
  >
40
50
  <Container p="lg">
41
- <ModalHeader title="Sign in required" />
51
+ <ModalHeader title={title} />
42
52
 
43
53
  <Container
44
54
  flex="column"
@@ -55,7 +65,7 @@ export function SignInRequiredModal(props: SignInRequiredModalProps) {
55
65
  lineHeight: 1.5,
56
66
  }}
57
67
  >
58
- Account required to complete payment, please sign in to continue.
68
+ {description}
59
69
  </Text>
60
70
  </Container>
61
71
  </Container>
@@ -63,7 +73,7 @@ export function SignInRequiredModal(props: SignInRequiredModalProps) {
63
73
  {/* Action Buttons */}
64
74
  <ScreenBottomContainer>
65
75
  <Button fullWidth gap="xs" onClick={onSignIn} variant="accent">
66
- Sign in
76
+ {buttonLabel}
67
77
  </Button>
68
78
  <Button fullWidth gap="xs" onClick={onCancel} variant="secondary">
69
79
  Cancel
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.113.0";
1
+ export const version = "5.114.1";
@@ -35,28 +35,10 @@ type GetPaymentRequirementsResult = {
35
35
  export async function decodePaymentRequest(
36
36
  args: PaymentArgs,
37
37
  ): Promise<GetPaymentRequirementsResult | PaymentRequiredResult> {
38
- const {
39
- price,
40
- network,
41
- facilitator,
42
- payTo,
43
- resourceUrl,
44
- routeConfig = {},
45
- method,
46
- paymentData,
47
- extraMetadata,
48
- } = args;
38
+ const { facilitator, routeConfig = {}, paymentData } = args;
49
39
  const { errorMessages } = routeConfig;
50
40
 
51
- const paymentRequirementsResult = await facilitator.accepts({
52
- resourceUrl,
53
- method,
54
- network,
55
- price,
56
- routeConfig,
57
- payTo,
58
- extraMetadata,
59
- });
41
+ const paymentRequirementsResult = await facilitator.accepts(args);
60
42
 
61
43
  // Check for payment header, if none, return the payment requirements
62
44
  if (!paymentData) {
@@ -278,6 +278,7 @@ export function facilitator(
278
278
  method: args.method,
279
279
  network: caip2ChainId,
280
280
  price: args.price,
281
+ scheme: args.scheme,
281
282
  routeConfig: args.routeConfig,
282
283
  serverWalletAddress: facilitator.address,
283
284
  recipientAddress: args.payTo,
@@ -71,9 +71,9 @@ export function wrapFetchWithPayment(
71
71
  accepts: unknown[];
72
72
  error?: string;
73
73
  };
74
- const parsedPaymentRequirements = accepts
75
- .map((x) => RequestedPaymentRequirementsSchema.parse(x))
76
- .filter((x) => x.scheme === "exact"); // TODO (402): accept other schemes
74
+ const parsedPaymentRequirements = accepts.map((x) =>
75
+ RequestedPaymentRequirementsSchema.parse(x),
76
+ );
77
77
 
78
78
  const account = wallet.getAccount();
79
79
  let chain = wallet.getChain();
@@ -88,7 +88,6 @@ export function wrapFetchWithPayment(
88
88
  : defaultPaymentRequirementsSelector(
89
89
  parsedPaymentRequirements,
90
90
  chain.id,
91
- "exact",
92
91
  error,
93
92
  );
94
93
 
@@ -158,7 +157,6 @@ export function wrapFetchWithPayment(
158
157
  function defaultPaymentRequirementsSelector(
159
158
  paymentRequirements: RequestedPaymentRequirements[],
160
159
  chainId: number,
161
- scheme: "exact",
162
160
  error?: string,
163
161
  ) {
164
162
  if (!paymentRequirements.length) {
@@ -168,9 +166,7 @@ function defaultPaymentRequirementsSelector(
168
166
  }
169
167
  // find the payment requirements matching the connected wallet chain
170
168
  const matchingPaymentRequirements = paymentRequirements.find(
171
- (x) =>
172
- extractEvmChainId(networkToCaip2ChainId(x.network)) === chainId &&
173
- x.scheme === scheme,
169
+ (x) => extractEvmChainId(networkToCaip2ChainId(x.network)) === chainId,
174
170
  );
175
171
 
176
172
  if (matchingPaymentRequirements) {
@@ -178,9 +174,7 @@ function defaultPaymentRequirementsSelector(
178
174
  } else {
179
175
  // if no matching payment requirements, use the first payment requirement
180
176
  // and switch the wallet to that chain
181
- const firstPaymentRequirement = paymentRequirements.find(
182
- (x) => x.scheme === scheme,
183
- );
177
+ const firstPaymentRequirement = paymentRequirements[0];
184
178
  return firstPaymentRequirement;
185
179
  }
186
180
  }
@@ -10,6 +10,7 @@ import {
10
10
  } from "x402/types";
11
11
  import { z } from "zod";
12
12
  import type { Chain } from "../chains/types.js";
13
+ import { PaymentSchemeSchema } from "./types.js";
13
14
 
14
15
  const FacilitatorNetworkSchema = z.string();
15
16
 
@@ -17,6 +18,7 @@ export type FacilitatorNetwork = z.infer<typeof FacilitatorNetworkSchema>;
17
18
 
18
19
  const RequestedPaymentPayloadSchema = PaymentPayloadSchema.extend({
19
20
  network: FacilitatorNetworkSchema,
21
+ scheme: PaymentSchemeSchema,
20
22
  });
21
23
 
22
24
  export type RequestedPaymentPayload = z.infer<
@@ -32,6 +34,7 @@ export type UnsignedPaymentPayload = Omit<
32
34
  export const RequestedPaymentRequirementsSchema =
33
35
  PaymentRequirementsSchema.extend({
34
36
  network: FacilitatorNetworkSchema,
37
+ scheme: PaymentSchemeSchema,
35
38
  });
36
39
 
37
40
  export type RequestedPaymentRequirements = z.infer<
@@ -76,7 +79,7 @@ const FacilitatorSupportedResponseSchema =
76
79
  kinds: z.array(
77
80
  z.object({
78
81
  x402Version: z.literal(1),
79
- scheme: z.literal("exact"),
82
+ scheme: PaymentSchemeSchema,
80
83
  network: FacilitatorNetworkSchema,
81
84
  extra: z
82
85
  .object({
@@ -51,7 +51,6 @@ import {
51
51
  * routeConfig: {
52
52
  * description: "Access to premium API content",
53
53
  * mimeType: "application/json",
54
- * maxTimeoutSeconds: 300,
55
54
  * },
56
55
  * });
57
56
  *
@@ -68,6 +67,18 @@ import {
68
67
  * }
69
68
  * ```
70
69
  *
70
+ * ### Upto Payment Scheme
71
+ *
72
+ * You can also use the `upto` payment scheme to settle the payment dynamically based on the usage.
73
+ *
74
+ * ```ts
75
+ * const result = await settlePayment({
76
+ * ...paymentArgs,
77
+ * scheme: "upto",
78
+ * price: "$0.10", // max payable amount
79
+ * });
80
+ * ```
81
+ *
71
82
  * ### Express middleware example
72
83
  *
73
84
  * ```ts
package/src/x402/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Money, PaymentMiddlewareConfig } from "x402/types";
2
- import type z from "zod";
2
+ import z from "zod";
3
3
  import type { Chain } from "../chains/types.js";
4
4
  import type { Prettify } from "../utils/type-utils.js";
5
5
  import type { ThirdwebX402Facilitator, WaitUntil } from "./facilitator.js";
@@ -31,6 +31,8 @@ export type PaymentArgs = {
31
31
  price: Money | ERC20TokenAmount;
32
32
  /** The payment facilitator instance used to verify and settle payments */
33
33
  facilitator: ThirdwebX402Facilitator;
34
+ /** The scheme of the payment, either "exact" or "upto", defaults to "exact" */
35
+ scheme?: PaymentScheme;
34
36
  /** Optional configuration for the payment middleware route */
35
37
  routeConfig?: PaymentMiddlewareConfig;
36
38
  /** Optional recipient address to receive the payment if different from your facilitator address */
@@ -91,7 +93,9 @@ export type VerifyPaymentResult = Prettify<
91
93
  | {
92
94
  /** HTTP 200 - Payment was successfully verified */
93
95
  status: 200;
96
+ /** The decoded payment payload */
94
97
  decodedPayment: RequestedPaymentPayload;
98
+ /** The selected payment requirements */
95
99
  selectedPaymentRequirements: RequestedPaymentRequirements;
96
100
  }
97
101
  | PaymentRequiredResult
@@ -101,8 +105,19 @@ export type SupportedSignatureType = z.infer<
101
105
  typeof SupportedSignatureTypeSchema
102
106
  >;
103
107
 
108
+ export const PaymentSchemeSchema = z.union([
109
+ z.literal("exact"),
110
+ z.literal("upto"),
111
+ ]);
112
+ type PaymentScheme = z.infer<typeof PaymentSchemeSchema>;
113
+
114
+ /**
115
+ * The asset, scheme and amount for the payment in base units
116
+ */
104
117
  export type ERC20TokenAmount = {
118
+ /** The amount of the payment in base units */
105
119
  amount: string;
120
+ /** The asset of the payment, decimals and eip712 data are optional and will be inferred from the address if not provided */
106
121
  asset: {
107
122
  address: `0x${string}`;
108
123
  decimals?: number;
@@ -42,7 +42,6 @@ import {
42
42
  * routeConfig: {
43
43
  * description: "Access to premium API content",
44
44
  * mimeType: "application/json",
45
- * maxTimeoutSeconds: 300,
46
45
  * },
47
46
  * };
48
47
  *
@@ -68,6 +67,17 @@ import {
68
67
  * }
69
68
  * ```
70
69
  *
70
+ * ### Upto Payment Scheme
71
+ *
72
+ * You can also use the `upto` payment scheme to verify a payment where the final price is dynamically calculated based on the usage.
73
+ *
74
+ * ```ts
75
+ * const result = await verifyPayment({
76
+ * ...paymentArgs,
77
+ * scheme: "upto",
78
+ * price: "$0.10", // max payable amount
79
+ * });
80
+ * ```
71
81
  * @public
72
82
  * @beta
73
83
  * @x402