-
Notifications
You must be signed in to change notification settings - Fork 629
[SDK] Optimize payment methods query by removing quotes and improving balance checks #8493
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] Optimize payment methods query by removing quotes and improving balance checks #8493
Conversation
🦋 Changeset detectedLatest commit: 130de87 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 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 GitHub.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
Comment |
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 📦
|
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #8493 +/- ##
==========================================
- Coverage 54.62% 54.62% -0.01%
==========================================
Files 920 920
Lines 61135 61142 +7
Branches 4145 4143 -2
==========================================
Hits 33397 33397
- Misses 27636 27643 +7
Partials 102 102
🚀 New features to boost your workflow:
|
| const requiredUsdValue = | ||
| (destinationToken.prices?.["USD"] ?? 0) * Number(destinationAmount); | ||
|
|
||
| return finalQuotes.map((x) => { | ||
| const tokenUsdValue = | ||
| (x.originToken.prices?.["USD"] ?? 0) * | ||
| Number(toTokens(x.balance, x.originToken.decimals)); | ||
| const hasEnoughBalance = tokenUsdValue >= requiredUsdValue; |
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.
| const requiredUsdValue = | |
| (destinationToken.prices?.["USD"] ?? 0) * Number(destinationAmount); | |
| return finalQuotes.map((x) => { | |
| const tokenUsdValue = | |
| (x.originToken.prices?.["USD"] ?? 0) * | |
| Number(toTokens(x.balance, x.originToken.decimals)); | |
| const hasEnoughBalance = tokenUsdValue >= requiredUsdValue; | |
| return finalQuotes.map((x) => { | |
| let hasEnoughBalance = false; | |
| // Priority 1: Use quote's originAmount if available (most accurate) | |
| if (x.quote) { | |
| hasEnoughBalance = x.balance >= x.quote.originAmount; | |
| } else { | |
| // Priority 2: Fall back to USD comparison only if destination price is available | |
| const destinationUsdPrice = destinationToken.prices?.["USD"]; | |
| if (destinationUsdPrice && destinationUsdPrice > 0) { | |
| const requiredUsdValue = | |
| destinationUsdPrice * Number(destinationAmount); | |
| const tokenUsdPrice = x.originToken.prices?.["USD"] || 0; | |
| const tokenUsdValue = | |
| tokenUsdPrice * Number(toTokens(x.balance, x.originToken.decimals)); | |
| hasEnoughBalance = tokenUsdValue >= requiredUsdValue; | |
| } | |
| // Priority 3: If destination price is missing, default to false (fail-safe) | |
| } | |
The balance check calculation is fundamentally flawed: it compares USD values between tokens without accounting for fees, slippage, and price impact. Additionally, if the destination token's USD price is missing or zero, the check incorrectly marks all tokens as having sufficient balance.
View Details
Analysis
Balance check calculation doesn't account for fees and fails with missing destination token price
What fails: The usePaymentMethods hook's hasEnoughBalance calculation (lines 129-136) incorrectly marks tokens as having sufficient balance when destination token price is missing, and ignores fees/slippage when calculating required amount.
How to reproduce:
- Payment method with destination token that has no USD price (prices["USD"] = undefined)
- Call
usePaymentMethodswith any token with non-zero balance and price - Result:
hasEnoughBalance = trueeven though destination amount is unknown
What happens:
- When
destinationToken.prices?.["USD"]is undefined,requiredUsdValue = 0 - Any token with positive balance gets marked as sufficient (hasEnoughBalance = true)
- Button is enabled allowing user to attempt swap that will fail
Expected behavior: When destination token price is missing, assume insufficient balance (fail-safe). Additionally, when quote data is available, use quote.originAmount (which includes fees from the API's calculation) instead of naive USD comparison.
Why this matters: Users select payment methods that appear to have enough balance but fail at transaction time, resulting in poor UX. Per Bridge Quote documentation, originAmount is "the necessary originAmount to receive the desired destinationAmount" and includes all fees.
Solution implemented:
- Use
quote.originAmountwhen available (most accurate, accounts for all fees) - Fall back to USD comparison only if destination price exists and is > 0
- Default to
hasEnoughBalance = falseif destination price is missing (fail-safe)
fe7c3b3 to
130de87
Compare
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|

PR-Codex overview
This PR focuses on enhancing the handling of payment methods by introducing an optional
quoteproperty and ahasEnoughBalanceboolean. It updates types and logic to improve balance checks and ensures better integration of token pricing.Detailed summary
quoteto optional inBridge/types.ts.hasEnoughBalanceboolean inBridge/types.ts.SwapWidget.tsxto usehasEnoughBalance.hasEnoughBalanceinTokenSelection.tsx.PaymentSelection.tsxto useTokenWithPricesinstead ofToken.usePaymentMethodsto handle optionalquote.