Repository navigation
fix(limit-twap): disable confirm when balance is insufficient - #7804
Conversation
The Limit and TWAP confirmation modals froze their sell amount but never re-checked the wallet balance, so if a previous order filled while the modal was open the Confirm button stayed enabled with no balance change shown. Mirror the swap confirm flow: re-validate the frozen amount against live combined balances via a shared useHasEnoughBalanceForAmount hook, disable Confirm and show an 'Insufficient balance' label when it no longer fits. Closes cowprotocol#5645
|
@tenderdeve is attempting to deploy a commit to the cow-dev Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughAdds live balance validation hooks and wires them into the Limit Orders and TWAP confirm modals so confirmation is blocked and relabeled when balances are insufficient. ChangesInsufficient Balance Validation
Estimated code review effort: 2 (Simple) | ~12 minutes Suggested labels: Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant User
participant ConfirmModal
participant useHasEnoughBalanceForAmount
participant useIsZeroBalance
participant useTokensBalancesCombined
User->>ConfirmModal: Open confirm modal
ConfirmModal->>useHasEnoughBalanceForAmount: check frozen sell amount
ConfirmModal->>useIsZeroBalance: check current token balance
useHasEnoughBalanceForAmount->>useTokensBalancesCombined: read combined balances
useIsZeroBalance->>useTokensBalancesCombined: read combined balances
useTokensBalancesCombined-->>useHasEnoughBalanceForAmount: balances
useTokensBalancesCombined-->>useIsZeroBalance: balances
useHasEnoughBalanceForAmount-->>ConfirmModal: insufficient? boolean
useIsZeroBalance-->>ConfirmModal: zero balance? boolean
alt insufficient balance
ConfirmModal-->>User: disable confirm, show insufficient balance text
else sufficient balance
ConfirmModal-->>User: keep confirm enabled
end
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/cowswap-frontend/src/modules/combinedBalances/hooks/useHasEnoughBalanceForAmount.ts (1)
1-33: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winNo test coverage for new shared validation hook.
This is a critical-path hook (gates order confirmation across two flows) with no accompanying unit test in the diff.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/cowswap-frontend/src/modules/combinedBalances/hooks/useHasEnoughBalanceForAmount.ts` around lines 1 - 33, The new shared validation logic in useHasEnoughBalanceForAmount has no test coverage, so add unit tests for the hook. Cover the key behaviors by mocking useTokensBalancesCombined and verifying it returns true when inputAmount is nullish, true when the balance is equal or higher, and false when the balance is lower. Place the tests alongside the hook so the critical confirmation gating behavior stays protected.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In
`@apps/cowswap-frontend/src/modules/combinedBalances/hooks/useHasEnoughBalanceForAmount.ts`:
- Around line 1-33: The new shared validation logic in
useHasEnoughBalanceForAmount has no test coverage, so add unit tests for the
hook. Cover the key behaviors by mocking useTokensBalancesCombined and verifying
it returns true when inputAmount is nullish, true when the balance is equal or
higher, and false when the balance is lower. Place the tests alongside the hook
so the critical confirmation gating behavior stays protected.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 6d056b1d-9ff4-4b35-b3d9-0ab192d3cccc
📒 Files selected for processing (4)
apps/cowswap-frontend/src/modules/combinedBalances/hooks/useHasEnoughBalanceForAmount.tsapps/cowswap-frontend/src/modules/combinedBalances/index.tsapps/cowswap-frontend/src/modules/limitOrders/containers/LimitOrdersConfirmModal/index.tsxapps/cowswap-frontend/src/modules/twap/containers/TwapConfirmModal/index.tsx
elena-zh
left a comment
There was a problem hiding this comment.
Hey @tenderdeve , it works perfectly fine with TWAP, but you've missed an important note from the original issue for Limit orders: the button on the modal should show this error when a sell token's balance is 0. Only 0. Because Limit orders are allowed to be placed with amount > balance
In your current implementation the limit orders form allows to proceed
but the button on the 'confirm' screen is disabled:
This needs to be fixed
Limit orders may be placed with an amount greater than the wallet balance, so the confirm modal must not disable on amount > balance. Per issue cowprotocol#5645, only block (and show "Insufficient <token> balance") when the sell token balance drops to exactly 0 while the modal is open. Add a dedicated useIsZeroBalance hook and switch LimitOrdersConfirmModal to it; TWAP keeps the amount-vs-balance check (useHasEnoughBalanceForAmount) unchanged. Add unit tests for both hooks.
|
@elena-zh good catch — fixed in d0276c8. Per the issue's note, Limit orders can be placed with amount > balance, so the confirm modal now only disables (and shows Insufficient … balance) when the sell token balance is exactly 0, not whenever amount > balance. So a limit order with amount > balance (balance > 0) now proceeds through Confirm as expected. TWAP is unchanged — still blocks when balance < order amount, as the issue specifies. Implementation: added a dedicated |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@apps/cowswap-frontend/src/modules/combinedBalances/hooks/useIsZeroBalance.ts`:
- Around line 19-30: The useIsZeroBalance hook can incorrectly return true while
useTokensBalancesCombined is still loading, causing a false
zero-balance/“Insufficient balance” state before real balances arrive. Update
useIsZeroBalance to check the loading state (or hasFirstLoad) from
useTokensBalancesCombined before computing CurrencyAmount.fromRawAmount and
returning balance.equalTo(0), and treat the loading phase as non-zero until
balances are available.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 0900d4ac-d70a-41f2-8291-d661c98d74cd
📒 Files selected for processing (5)
apps/cowswap-frontend/src/modules/combinedBalances/hooks/useHasEnoughBalanceForAmount.test.tsapps/cowswap-frontend/src/modules/combinedBalances/hooks/useIsZeroBalance.test.tsapps/cowswap-frontend/src/modules/combinedBalances/hooks/useIsZeroBalance.tsapps/cowswap-frontend/src/modules/combinedBalances/index.tsapps/cowswap-frontend/src/modules/limitOrders/containers/LimitOrdersConfirmModal/index.tsx
✅ Files skipped from review due to trivial changes (2)
- apps/cowswap-frontend/src/modules/combinedBalances/hooks/useIsZeroBalance.test.ts
- apps/cowswap-frontend/src/modules/combinedBalances/index.ts
useIsZeroBalance read from balancesCombinedAtom whose default `values` is empty, so it briefly returned true (and showed 'Insufficient balance') before balances arrived. Gate on `hasFirstLoad` so the confirm button isn't blocked by a transient zero. Adds a test for the pre-load case.
|
@coderabbitai good catch — addressed in 16245ea. |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
cd /repo 2>/dev/null || true
git log --all --oneline | grep -i "16245ea1a" || true
git show 16245ea1a --stat 2>/dev/null | head -50
git show 16245ea1a -- apps/cowswap-frontend/src/modules/combinedBalances/hooks/useIsZeroBalance.ts 2>/dev/nullLength of output: 3177
ᕙ(⇀‸↼‶)ᕗ |
Summary
Fixes #5645.
The Swap confirm modal already re-checks the wallet balance against its frozen sell amount (#5614), but the Limit and TWAP confirm modals did not. So if you open the confirm modal with a max-balance order and a previous order fills while it's open, the balance is gone but the Confirm button stays enabled and shows no change — letting you submit an order you can't cover.
Changes
useHasEnoughBalanceForAmount(amount)(modules/combinedBalances) that re-validates a (frozen) amount against live combined balances — the same logic the swap modal uses, extracted so all flows share it.LimitOrdersConfirmModalandTwapConfirmModalnow fold insufficient balance intoisConfirmDisabledand switch the button label toInsufficient <token> balance.Returns "enough" when there's no amount yet, so the button isn't blocked during loading.
Testing steps
Summary by CodeRabbit
New Features
Bug Fixes
Tests