From 2bf979b8f0db7b3d57a8a8d5ee6b0a091f6d6730 Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Mon, 11 Aug 2025 10:32:49 +1200 Subject: [PATCH] Fix supportedTokens address comparison in usePaymentMethods --- .changeset/violet-readers-warn.md | 5 +++ .../src/react/core/hooks/usePaymentMethods.ts | 44 ++++++------------- .../web/ui/Bridge/BridgeOrchestrator.tsx | 1 + .../src/react/web/ui/Bridge/BuyWidget.tsx | 30 ++++++++++++- .../react/web/ui/Bridge/CheckoutWidget.tsx | 43 +++++++++++++++++- .../react/web/ui/Bridge/TransactionWidget.tsx | 27 ++++++++++++ 6 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 .changeset/violet-readers-warn.md diff --git a/.changeset/violet-readers-warn.md b/.changeset/violet-readers-warn.md new file mode 100644 index 00000000000..efc532bacef --- /dev/null +++ b/.changeset/violet-readers-warn.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Fix supportedTokens address comparison diff --git a/packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts b/packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts index 7f6ac866495..bb67e4a7c22 100644 --- a/packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts +++ b/packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts @@ -93,32 +93,15 @@ export function usePaymentMethods(options: { type: "wallet" as const, quote: s.quote, })); - const insufficientBalanceQuotes = validTokenQuotes - .filter((s) => s.balance < s.quote.originAmount) - .sort((a, b) => { - return ( - Number.parseFloat( - toTokens(a.quote.originAmount, a.originToken.decimals), - ) * - (a.originToken.prices.USD || 1) - - Number.parseFloat( - toTokens(b.quote.originAmount, b.originToken.decimals), - ) * - (b.originToken.prices.USD || 1) - ); - }); + const sufficientBalanceQuotes = validTokenQuotes - .filter((s) => s.balance >= s.quote.originAmount) + .filter((s) => !!s.originToken.prices.USD) .sort((a, b) => { return ( - Number.parseFloat( - toTokens(a.quote.originAmount, a.originToken.decimals), - ) * - (a.originToken.prices.USD || 1) - - Number.parseFloat( - toTokens(b.quote.originAmount, b.originToken.decimals), - ) * - (b.originToken.prices.USD || 1) + Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) * + (b.originToken.prices.USD || 1) - + Number.parseFloat(toTokens(a.balance, a.originToken.decimals)) * + (a.originToken.prices.USD || 1) ); }); @@ -133,15 +116,14 @@ export function usePaymentMethods(options: { ) : []; const finalQuotes = supportedTokens - ? [...sufficientBalanceQuotes, ...insufficientBalanceQuotes].filter( - (q) => - tokensToInclude.find( - (t) => - t.chainId === q.originToken.chainId && - t.address === q.originToken.address, - ), + ? sufficientBalanceQuotes.filter((q) => + tokensToInclude.find( + (t) => + t.chainId === q.originToken.chainId && + t.address.toLowerCase() === q.originToken.address.toLowerCase(), + ), ) - : [...sufficientBalanceQuotes, ...insufficientBalanceQuotes]; + : sufficientBalanceQuotes; return finalQuotes; }, queryKey: [ diff --git a/packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx b/packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx index 7caf3ebc3c6..9346ab31bad 100644 --- a/packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx +++ b/packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx @@ -192,6 +192,7 @@ export function BridgeOrchestrator({ // Handle errors const handleError = useCallback( (error: Error) => { + console.error(error); onError?.(error); send({ error, type: "ERROR_OCCURRED" }); }, diff --git a/packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx b/packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx index 85f3e4266b7..b185c038a8f 100644 --- a/packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx +++ b/packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx @@ -33,6 +33,9 @@ import { BridgeOrchestrator, type UIOptions } from "./BridgeOrchestrator.js"; import { UnsupportedTokenScreen } from "./UnsupportedTokenScreen.js"; export type BuyWidgetProps = { + /** + * Customize the supported tokens that users can pay with. + */ supportedTokens?: SupportedTokens; /** * A client is the entry point to the thirdweb SDK. @@ -217,7 +220,7 @@ type UIOptionsResult = * * ``` * @@ -229,11 +232,34 @@ type UIOptionsResult = * * ``` * + * ### Customize the supported tokens + * + * You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `BuyWidget` component. + * + * ```tsx + * + * ``` + * + * * ### Customize the UI * * You can customize the UI of the `BuyWidget` component by passing a custom theme object to the `theme` prop. diff --git a/packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx b/packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx index 78a3d40cf1a..b7346bac010 100644 --- a/packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx +++ b/packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx @@ -29,6 +29,9 @@ import { BridgeOrchestrator, type UIOptions } from "./BridgeOrchestrator.js"; import { UnsupportedTokenScreen } from "./UnsupportedTokenScreen.js"; export type CheckoutWidgetProps = { + /** + * Customize the supported tokens that users can pay with. + */ supportedTokens?: SupportedTokens; /** * A client is the entry point to the thirdweb SDK. @@ -215,11 +218,43 @@ type UIOptionsResult = * @example * ### Default configuration * - * By default, the `CheckoutWidget` component will allows users to fund their wallets with crypto or fiat on any of the supported chains.. + * The `CheckoutWidget` component allows user to pay a given wallet for any product or service. You can register webhooks to get notified for every purchase done via the widget. + * + * ```tsx + * { + * alert("Purchase successful!"); + * }} + * /> + * ``` + * + * ### Customize the supported tokens + * + * You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `CheckoutWidget` component. * * ```tsx * * ``` * @@ -230,6 +265,9 @@ type UIOptionsResult = * ```tsx * * ``` * + * ### Customize the supported tokens + * + * You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `TransactionWidget` component. + * + * ```tsx + * + * ``` + * * ### Customize the UI * * You can customize the UI of the `TransactionWidget` component by passing a custom theme object to the `theme` prop.