-
Notifications
You must be signed in to change notification settings - Fork 619
[SDK] Fix: Sort payment quotes in ascending order #7819
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: Sort payment quotes in ascending order #7819
Conversation
🦋 Changeset detectedLatest commit: de9b4f0 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 |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughA changeset file was added to document a patch update for the "thirdweb" package, specifying that payment quotes are now sorted in ascending order. The sorting logic within Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Warning Review ran into problems🔥 ProblemsErrors were encountered while retrieving linked issues. Errors (1)
✨ 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. |
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: 1
🔭 Outside diff range comments (1)
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts (1)
100-108: Apply the same?? 1fix to the insufficient-balance sorterFor parity and correctness, avoid treating 0 USD as 1 here as well.
- ) * - (a.originToken.prices.USD || 1) - + ) * + (a.originToken.prices.USD ?? 1) - Number.parseFloat( toTokens(b.quote.originAmount, b.originToken.decimals), ) * - (b.originToken.prices.USD || 1) + (b.originToken.prices.USD ?? 1)
🧹 Nitpick comments (1)
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts (1)
96-124: Optional: precompute USD values to avoid repeated conversions during sortThis reduces repeated
toTokens+parseFloatwork inside the comparator and keeps logic DRY.// compute once per item and sort by the precomputed key (Schwartzian transform) const insufficientBalanceQuotes = validTokenQuotes .filter((s) => s.balance < s.quote.originAmount) .map((q) => ({ q, usd: Number.parseFloat(toTokens(q.quote.originAmount, q.originToken.decimals)) * (q.originToken.prices.USD ?? 1), })) .sort((a, b) => a.usd - b.usd) .map((x) => x.q); const sufficientBalanceQuotes = validTokenQuotes .filter((s) => s.balance >= s.quote.originAmount) .map((q) => ({ q, usd: Number.parseFloat(toTokens(q.quote.originAmount, q.originToken.decimals)) * (q.originToken.prices.USD ?? 1), })) .sort((a, b) => a.usd - b.usd) .map((x) => x.q);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
.changeset/dirty-roses-carry.md(1 hunks)packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 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@/typesor localtypes.tsbarrels
Prefer type aliases over interface except for nominal shapes
Avoidanyandunknownunless 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
**/*.{ts,tsx,js,jsx}
📄 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
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
PR: thirdweb-dev/js#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-18T19:19:55.613Z
Learning: Surface breaking changes prominently in PR descriptions
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
Learnt from: gregfromstl
PR: thirdweb-dev/js#7450
File: packages/thirdweb/src/bridge/Webhook.ts:57-81
Timestamp: 2025-06-26T19:46:04.024Z
Learning: In the onramp webhook schema (`packages/thirdweb/src/bridge/Webhook.ts`), the `currencyAmount` field is intentionally typed as `z.number()` while other amount fields use `z.string()` because `currencyAmount` represents fiat currency amounts in decimals (like $10.50), whereas other amount fields represent token amounts in wei (very large integers that benefit from bigint representation). The different naming convention (`currencyAmount` vs `amount`) reflects this intentional distinction.
📚 Learning: 2025-07-18T19:19:55.613Z
Learnt from: CR
PR: thirdweb-dev/js#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-18T19:19:55.613Z
Learning: Surface breaking changes prominently in PR descriptions
Applied to files:
.changeset/dirty-roses-carry.md
📚 Learning: 2025-05-27T19:55:25.056Z
Learnt from: MananTank
PR: thirdweb-dev/js#7177
File: apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/public-pages/erc20/_hooks/useTokenPriceData.ts:49-49
Timestamp: 2025-05-27T19:55:25.056Z
Learning: In the ERC20 public pages token price data hook (`useTokenPriceData.ts`), direct array access on `json.data[0]` without optional chaining is intentionally correct and should not be changed to use safety checks.
Applied to files:
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
📚 Learning: 2025-06-26T19:46:04.024Z
Learnt from: gregfromstl
PR: thirdweb-dev/js#7450
File: packages/thirdweb/src/bridge/Webhook.ts:57-81
Timestamp: 2025-06-26T19:46:04.024Z
Learning: In the onramp webhook schema (`packages/thirdweb/src/bridge/Webhook.ts`), the `currencyAmount` field is intentionally typed as `z.number()` while other amount fields use `z.string()` because `currencyAmount` represents fiat currency amounts in decimals (like $10.50), whereas other amount fields represent token amounts in wei (very large integers that benefit from bigint representation). The different naming convention (`currencyAmount` vs `amount`) reflects this intentional distinction.
Applied to files:
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
📚 Learning: 2025-07-18T19:20:32.530Z
Learnt from: CR
PR: thirdweb-dev/js#0
File: .cursor/rules/dashboard.mdc:0-0
Timestamp: 2025-07-18T19:20:32.530Z
Learning: Applies to dashboard/**/*client.tsx : Interactive UI that relies on hooks (`useState`, `useEffect`, React Query, wallet hooks).
Applied to files:
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
📚 Learning: 2025-07-18T19:19:55.613Z
Learnt from: CR
PR: thirdweb-dev/js#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-18T19:19:55.613Z
Learning: Applies to test/src/test-wallets.ts : Predefined test accounts are in `test/src/test-wallets.ts`
Applied to files:
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
📚 Learning: 2025-07-18T19:19:55.613Z
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
Applied to files:
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
⏰ 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)
- GitHub Check: Size
- GitHub Check: E2E Tests (pnpm, esbuild)
- GitHub Check: E2E Tests (pnpm, vite)
- GitHub Check: E2E Tests (pnpm, webpack)
- GitHub Check: Build Packages
- GitHub Check: Lint Packages
- GitHub Check: Unit Tests
- GitHub Check: Analyze (javascript)
🔇 Additional comments (2)
packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts (1)
115-121: Ascending comparator fix is correctThe comparator now returns value(a) - value(b), which sorts ascending as intended. Matches the PR objective.
.changeset/dirty-roses-carry.md (1)
1-5: Changeset looks goodProper patch entry for "thirdweb" with a concise message that matches the code change.
| toTokens(a.quote.originAmount, a.originToken.decimals), | ||
| ) * | ||
| (b.originToken.prices.USD || 1) - | ||
| (a.originToken.prices.USD || 1) - |
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.
🛠️ Refactor suggestion
Use nullish coalescing instead of logical OR for USD price
|| 1 treats a legitimate 0 USD price as 1. Use ?? 1 to only default when price is null/undefined.
- (a.originToken.prices.USD || 1) -
+ (a.originToken.prices.USD ?? 1) -
...
- (b.originToken.prices.USD || 1)
+ (b.originToken.prices.USD ?? 1)Also applies to: 121-121
🤖 Prompt for AI Agents
In packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts at lines 117 and
121, replace the logical OR operator (||) used for defaulting USD prices with
the nullish coalescing operator (??). This change ensures that a valid 0 USD
price is not incorrectly replaced by 1, and the default of 1 is only applied
when the price is null or undefined.
size-limit report 📦
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7819 +/- ##
=======================================
Coverage 56.33% 56.33%
=======================================
Files 905 905
Lines 58821 58821
Branches 4144 4146 +2
=======================================
Hits 33137 33137
Misses 25578 25578
Partials 106 106
🚀 New features to boost your workflow:
|
PR-Codex overview
This PR focuses on sorting payment quotes in ascending order within the
usePaymentMethods.tsfile by adjusting the calculation logic for comparing quotes.Detailed summary
usePaymentMethods.tsto sort payment quotes based onoriginAmountandprices.USD.aandbin the calculations to ensure correct ascending order sorting.Summary by CodeRabbit
Bug Fixes
Chores