Skip to content

swap: fetch quotes on input changes#3915

Merged
bznein merged 1 commit into
BitBoxSwiss:staging-swapfrom
bznein:staging-swap-auto-quote-near
Mar 19, 2026
Merged

swap: fetch quotes on input changes#3915
bznein merged 1 commit into
BitBoxSwiss:staging-swapfrom
bznein:staging-swap-auto-quote-near

Conversation

@bznein
Copy link
Copy Markdown
Collaborator

@bznein bznein commented Mar 4, 2026

This PR adds calls to the quote endpoint in the backiend whenever the input (buy/sell accounts/amounts) changes. @thisconnect can you please take a look at the frontend part? I tried to make the diff as limited as possible and some stuff is still missing (for example we do not show the fee here, and we can probably get rid of the route selector since we are only using one provider and we will have only one route). But when the frontends looks good to you, I can add someone from backend for review on that part. Thanks!

Before asking for reviews, here is a check list of the most common things you might need to consider:

  • updating the Changelog
  • writing unit tests
  • checking if your changes affect other coins or tokens in unintended ways
  • testing on multiple environments (Qt, Android, ...)
  • having an AI review your changes

@bznein bznein requested a review from thisconnect March 4, 2026 15:43
Copy link
Copy Markdown
Collaborator

@thisconnect thisconnect left a comment

Choose a reason for hiding this comment

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

for backend review cc @Beerosagos

Comment thread frontends/web/src/api/swap.ts
</Label>
<Button transparent className={style.maxButton}>
45678 ETH
</Button>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you leave the max-receive button so we can add functionality later and dont forget about it?

Comment thread frontends/web/src/routes/market/swap/components/swap-service-selector.tsx Outdated
@bznein bznein force-pushed the staging-swap-auto-quote-near branch from 2f5b422 to 7a23225 Compare March 9, 2026 11:57
@bznein bznein requested a review from thisconnect March 9, 2026 11:57
@bznein
Copy link
Copy Markdown
Collaborator Author

bznein commented Mar 9, 2026

@thisconnect thanks for the review, I shoulkd have addressed all the comments

Copy link
Copy Markdown
Collaborator

@thisconnect thisconnect left a comment

Choose a reason for hiding this comment

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

frontend untested LGTM with small nit

);
};

// shown in dropdown
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: keep this 2 comments here and a few lines before

@bznein bznein force-pushed the staging-swap-auto-quote-near branch from 7a23225 to 84d8e8b Compare March 9, 2026 14:54
}, QUOTE_DEBOUNCE_MS);

return () => {
isCancelled = true;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: use useMountedRef instead of isCancelled

Copy link
Copy Markdown
Collaborator Author

@bznein bznein Mar 18, 2026

Choose a reason for hiding this comment

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

I didn't forget about this one btw :) going to change it

Edit, since my frontend knowledge is limited, this is what codex said, can you help me understand if it's reasonable:

useMountedRef() only protects against updates after unmount. The local isCancelled
  flag here is doing more than that: it also cancels stale async work when the effect
  reruns because sellAmount, sellAccountCode, or buyAccountCode changed.

  That distinction matters here:

  - user types
  - effect A starts
  - user types again
  - effect B starts
  - effect A resolves late

  With isCancelled, effect A’s result is ignored after cleanup.
  With only useMountedRef(), the component is still mounted, so effect A could still
  update state and overwrite fresher quote data.

  So my view is:

  - useMountedRef() is not a drop-in replacement here
  - the current cancellation pattern is justified
  - if you want to improve it, use a request-id / sequence guard or AbortController, not    just useMountedRef()

   A concrete race looks like this:

  1. You enter amount 1.
  2. Request A starts for sellAmount=1.
  3. Before A returns, you quickly change the amount to 10.
  4. Request B starts for sellAmount=10.
  5. B returns first and updates routes for 10.
  6. A returns later

It seems like an unlikely scenario, but maybe worth considering?

@bznein bznein requested a review from Beerosagos March 11, 2026 12:01
Comment thread backend/handlers/handlers.go
Comment thread backend/handlers/handlers.go Outdated
Comment thread frontends/web/src/api/swap.ts
Comment thread backend/handlers/handlers.go Outdated
@bznein bznein force-pushed the staging-swap-auto-quote-near branch 4 times, most recently from a36569e to b50dd7f Compare March 18, 2026 10:23
@bznein bznein requested a review from Beerosagos March 18, 2026 15:59
Comment thread backend/handlers/handlers.go Outdated
Comment thread backend/handlers/handlers.go Outdated
Comment thread backend/market/swapkit/helpers.go Outdated
Comment thread backend/handlers/handlers.go Outdated
Comment thread backend/market/swapkit/helpers.go Outdated
@bznein bznein force-pushed the staging-swap-auto-quote-near branch from b50dd7f to eadf2b1 Compare March 19, 2026 12:38
@bznein bznein requested a review from Beerosagos March 19, 2026 12:39
Copy link
Copy Markdown
Collaborator

@Beerosagos Beerosagos left a comment

Choose a reason for hiding this comment

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

backend untested LGTM

@bznein bznein merged commit c9a4ab5 into BitBoxSwiss:staging-swap Mar 19, 2026
16 checks passed
@bznein bznein deleted the staging-swap-auto-quote-near branch March 19, 2026 13:40
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.

3 participants