Skip to content

Fix/2326 swap calculation UI#2329

Merged
KoalaSat merged 1 commit intoRoboSats:mainfrom
FedericoLuque:fix/2326-swap-calculation-ui
Dec 26, 2025
Merged

Fix/2326 swap calculation UI#2329
KoalaSat merged 1 commit intoRoboSats:mainfrom
FedericoLuque:fix/2326-swap-calculation-ui

Conversation

@FedericoLuque
Copy link
Contributor

@FedericoLuque FedericoLuque commented Dec 22, 2025

What does this PR do?

Fixes #2326

  1. Cause of the Bug:

The OrderDetails component used a helper function (amountToString) that, by default, limited the precision to 4 digits.

For an amount of 100,155 Sats, this caused rounding to 1.002 x 10^5, displaying 100,200 in the interface ("You send via...").

However, the confirmation button ("Confirm Sent") used a different calculation (toFixed(8)), displaying the exact amount converted to BTC (0.00100155), which caused the discrepancy.

1.2 Fix:

I have forced a precision of 9 digits when the currency is Bitcoin (currentOrder.currency === 1000). This ensures that the amount displayed in the order details is accurate and matches the confirmation button.

  1. Cause of the Bug:

This error occurred exclusively in range orders (e.g., from 10k to 1M sats) when a taker decided to take an amount less than the maximum (e.g., 100k sats).

The interface incorrectly calculated the exchange rate using the Maximum Order Amount (1M) instead of the Actual Amount agreed upon (100k).

Since the backend reported the satoshis corresponding to the actual amount (100k), the division Maximum / Actual Sats generated an artificially inflated rate (10 times higher in this example).

When applying this inflated rate to calculate "You receive...", the result was divided by 10, displaying a much lower figure than the actual amount (e.g., 10,000 instead of 100,000).

2.2 Fix:

I reversed the priority logic in the calculation. Now the system correctly uses the Actual Amount (order.amount) if it is defined (the deal has already been closed), and only uses the maximum (order.max_amount) if a final amount has not yet been defined. This ensures that the rate and the displayed satoshis are always consistent.

Checklist before merging

  • Install pre-commit and initialize it: pip install pre-commit, then pre-commit install. Pre-commit installs git hooks that automatically check the codebase. If pre-commit fails when you commit your changes, please fix the problems it points out.

@FedericoLuque FedericoLuque force-pushed the fix/2326-swap-calculation-ui branch from daccd54 to e5f5334 Compare December 22, 2025 14:48
const defaultRoutingBudget = 0.001;
const btc_now = order.satoshis_now / 100000000;
const rate = Number(order.max_amount ?? order.amount) / btc_now;
const rate = Number(order.amount ?? order.max_amount) / btc_now;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized in other places we first check has_range, but not here, that's why this was the only place failing. Can you do that instead? https://github.com/RoboSats/robosats/blob/main/frontend/src/components/Charts/DepthChart/index.tsx#L80

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

@FedericoLuque FedericoLuque force-pushed the fix/2326-swap-calculation-ui branch 2 times, most recently from 388cbd0 to 0b2d4f7 Compare December 25, 2025 14:29
@FedericoLuque FedericoLuque force-pushed the fix/2326-swap-calculation-ui branch from 0b2d4f7 to 053f13a Compare December 25, 2025 14:38
Copy link
Member

@KoalaSat KoalaSat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @FedericoLuque and good catch!

Please accept a little tip for your work, paste here a LN invoice with long expiration date for 40,000 sats

@KoalaSat KoalaSat merged commit b2c7f40 into RoboSats:main Dec 26, 2025
6 checks passed
@FedericoLuque
Copy link
Contributor Author

lnbc400u1p55aphgpp5zxqpyf64ecqqjmdfpjyd7qtnuu6u2xjqnmhesans8g2ms6qt8ylqdq9xgc56cqzzsxq9phtuqsp5eddl7s3wshrtj5sfk74rgg8ger86yg2d04n4sw8gdy680uklqm5q9qxpqysgq0rg7clrvrqpemrqxzja9k5amy52hct3wqwqr8h477v6hk7hyucf5nj9e5p0j64zee63wrnfjjrgz7xpv7j8dlv0h4kftse8htcfmr6cqks82hn

@FedericoLuque FedericoLuque deleted the fix/2326-swap-calculation-ui branch December 27, 2025 20:15
@RobosatsDevFund
Copy link

5e6707e2eb53daefc9598b02fdd308671d0345c9f25358aaa22f0a907105d2b8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UI Data Mismatch: "You receive" calculation error and discrepancy in "Confirm Sent" amount (On-Chain Swap)

3 participants