-
Notifications
You must be signed in to change notification settings - Fork 619
[SDK] Fix supportedTokens address comparison in usePaymentMethods #7824
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
[SDK] Fix supportedTokens address comparison in usePaymentMethods #7824
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
🦋 Changeset detectedLatest commit: 2bf979b The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughThis update introduces a patch changeset for the "thirdweb" package and enhances several UI widget components by documenting and exposing a new optional Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant BuyWidget/CheckoutWidget/TransactionWidget
participant usePaymentMethods
User->>BuyWidget/CheckoutWidget/TransactionWidget: Pass supportedTokens prop (optional)
BuyWidget/CheckoutWidget/TransactionWidget->>usePaymentMethods: Call with supportedTokens
usePaymentMethods->>usePaymentMethods: Filter tokens (case-insensitive address comparison)
usePaymentMethods-->>BuyWidget/CheckoutWidget/TransactionWidget: Return filtered tokens
BuyWidget/CheckoutWidget/TransactionWidget-->>User: Render UI with supported tokens
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Warning Review ran into problems🔥 ProblemsErrors were encountered while retrieving linked issues. Errors (1)
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (6)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (5)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
size-limit report 📦
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (11)
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts (1)
125-145: Pre-normalize and use a Set for O(1) membership (avoid O(n·m) and repeated toLowerCase)The current filter scans tokensToInclude for every quote and lowercases on each comparison. Build a Set of normalized keys once and check membership.
Apply this diff:
- // Filter out quotes that are not included in the supportedTokens (if provided) - const tokensToInclude = supportedTokens - ? Object.keys(supportedTokens).flatMap( - (c: string) => - supportedTokens[Number(c)]?.map((t) => ({ - chainId: Number(c), - address: t.address, - })) ?? [], - ) - : []; - const finalQuotes = supportedTokens - ? [...sufficientBalanceQuotes, ...insufficientBalanceQuotes].filter( - (q) => - tokensToInclude.find( - (t) => - t.chainId === q.originToken.chainId && - t.address.toLowerCase() === q.originToken.address.toLowerCase(), - ), - ) - : [...sufficientBalanceQuotes, ...insufficientBalanceQuotes]; + // Filter out quotes that are not included in the supportedTokens (if provided) + const allQuotes = [ + ...sufficientBalanceQuotes, + ...insufficientBalanceQuotes, + ]; + const finalQuotes = supportedTokens + ? (() => { + const tokensToIncludeSet = new Set( + Object.entries(supportedTokens).flatMap(([c, tokens]) => + (tokens ?? []).map( + (t) => `${Number(c)}:${t.address.toLowerCase()}`, + ), + ), + ); + return allQuotes.filter((q) => + tokensToIncludeSet.has( + `${q.originToken.chainId}:${q.originToken.address.toLowerCase()}`, + ), + ); + })() + : allQuotes;Pros: faster membership checks, fewer string allocations. Cons: slight increase in upfront allocation for the Set.
.changeset/violet-readers-warn.md (1)
1-6: Changeset looks good; optional: be more specific in the summaryConsider “usePaymentMethods: make supportedTokens address comparison case-insensitive” to improve traceability in changelogs.
packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx (1)
195-197: Avoid noisy production logs; add prefix and gate in devConsole logging is helpful during development. To keep production logs clean and add context, gate and prefix it.
Apply this diff:
- console.error(error); + if (process.env.NODE_ENV !== "production") { + console.error("[BridgeOrchestrator] error:", error); + }packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx (3)
36-39: Clarify supportedTokens semanticsAdd that addresses are case-insensitive and chain IDs refer to payer origin chains the user can spend from (not necessarily the destination chain).
Apply this diff to the JSDoc:
/** * Customize the supported tokens that users can pay with. + * Addresses are matched case-insensitively. + * Chain IDs map to the payer’s origin chain(s) the user can spend from. */
223-224: Grammar nit in example commentUse “i.e.” capitalization and comma.
- * amount="0.1" // in native tokens (ie. ETH) + * amount="0.1" // in native tokens (i.e., ETH)
239-260: Clarify example intent for supportedTokensTo reduce confusion when destination chain differs from listed chains, note that supportedTokens chain IDs correspond to the payer’s origin chains.
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `BuyWidget` component. ```tsx * <BuyWidget client={client} chain={ethereum} amount="0.1" - // user will only be able to pay with these tokens + // Users will only be able to pay with these tokens. + // Note: chain IDs here refer to the payer's origin chains they can spend from. supportedTokens={{ [8453]: [ { address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", name: "USDC", symbol: "USDC", }, ], }} /></blockquote></details> <details> <summary>packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx (2)</summary><blockquote> `39-44`: **Clarify supportedTokens semantics** Mention case-insensitive address matching and that chain IDs represent payer origin chains. ```diff /** * Customize the supported tokens that users can pay with. + * Addresses are matched case-insensitively. + * Chain IDs map to the payer’s origin chain(s) the user can spend from. */
237-260: Clarify example intent for supportedTokensAdd a brief note to avoid confusion when the transaction’s chain differs from the allowed payer chains.
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `TransactionWidget` component. ```tsx * <TransactionWidget client={client} transaction={prepareTransaction({ to: "0x...", chain: ethereum, client: client, })} - supportedTokens={{ + // Note: chain IDs below refer to the payer's origin chains they can spend from. + supportedTokens={{ [8453]: [ { address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", name: "USDC", symbol: "USDC", }, ], }} /></blockquote></details> <details> <summary>packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx (3)</summary><blockquote> `32-35`: **Clarify supportedTokens semantics** Add note on case-insensitive addresses and origin-chain mapping. ```diff /** * Customize the supported tokens that users can pay with. + * Addresses are matched case-insensitively. + * Chain IDs map to the payer’s origin chain(s) the user can spend from. */
221-221: Grammar nit“allows user” → “allows users”.
- * 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. + * The `CheckoutWidget` component allows users to pay a given wallet for any product or service. You can register webhooks to get notified for every purchase done via the widget.
238-259: Clarify example intent for supportedTokensAdd a brief comment noting that chain IDs refer to payer origin chains.
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `CheckoutWidget` component. ```tsx * <CheckoutWidget client={client} chain={arbitrum} amount="0.01" seller="0x123...abc" - // user will only be able to pay with these tokens + // Users will only be able to pay with these tokens. + // Note: chain IDs here refer to the payer's origin chains they can spend from. supportedTokens={{ [8453]: [ { address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", name: "USDC", symbol: "USDC", }, ], }} /></blockquote></details> </blockquote></details> <details> <summary>📜 Review details</summary> **Configuration used: CodeRabbit UI** **Review profile: CHILL** **Plan: Pro** <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between a3909e17947419728d5fecce1f83cfa50678a095 and def410b295095056032ac02767093c222e50028a. </details> <details> <summary>📒 Files selected for processing (6)</summary> * `.changeset/violet-readers-warn.md` (1 hunks) * `packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts` (1 hunks) * `packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx` (1 hunks) * `packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx` (3 hunks) * `packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx` (4 hunks) * `packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx` (2 hunks) </details> <details> <summary>🧰 Additional context used</summary> <details> <summary>📓 Path-based instructions (2)</summary> <details> <summary>**/*.{ts,tsx}</summary> **📄 CodeRabbit Inference Engine (CLAUDE.md)** > `**/*.{ts,tsx}`: Write idiomatic TypeScript with explicit function declarations and return types > Limit each file to one stateless, single-responsibility function for clarity > Re-use shared types from `@/types` or local `types.ts` barrels > Prefer type aliases over interface except for nominal shapes > Avoid `any` and `unknown` unless unavoidable; narrow generics when possible > Choose composition over inheritance; leverage utility types (`Partial`, `Pick`, etc.) > Comment only ambiguous logic; avoid restating TypeScript in prose Files: - `packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts` - `packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx` - `packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx` - `packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx` - `packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx` </details> <details> <summary>**/*.{ts,tsx,js,jsx}</summary> **📄 CodeRabbit Inference Engine (CLAUDE.md)** > Load heavy dependencies inside async paths to keep initial bundle lean (lazy loading) Files: - `packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts` - `packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx` - `packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx` - `packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx` - `packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx` </details> </details><details> <summary>🧠 Learnings (1)</summary> <details> <summary>📓 Common learnings</summary>Learnt from: CR
PR: thirdweb-dev/js#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-18T19:19:55.613Z
Learning: Applies to packages/thirdweb/src/wallets/** : EIP-1193, EIP-5792, EIP-7702 standard support in wallet modules</details> </details> </details> <details> <summary>⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)</summary> * GitHub Check: Size * GitHub Check: E2E Tests (pnpm, vite) * GitHub Check: E2E Tests (pnpm, esbuild) * GitHub Check: Lint Packages * GitHub Check: E2E Tests (pnpm, webpack) * GitHub Check: Unit Tests * GitHub Check: Build Packages * GitHub Check: Analyze (javascript) </details> <details> <summary>🔇 Additional comments (1)</summary><blockquote> <details> <summary>packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts (1)</summary> `141-141`: **Correct: case-insensitive address comparison** Lowercasing both sides fixes checksum/lowercase mismatches when filtering supported tokens. Good catch. </details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
def410b to
2bf979b
Compare

PR-Codex overview
This PR focuses on enhancing the
TransactionWidget,BuyWidget, andCheckoutWidgetcomponents by adding support for customizable payment tokens. It also improves error handling and refines sorting logic for payment methods.Detailed summary
supportedTokensprop toTransactionWidget,BuyWidget, andCheckoutWidgetcomponents.BridgeOrchestratorwithconsole.error.usePaymentMethodsfor better token handling.Summary by CodeRabbit
New Features
supportedTokensproperty.Bug Fixes
Documentation
Chores