fix(token-swap): correct ratio calculation and liquidity assertion in deposit_liquidity#567
Open
HkSolDev wants to merge 3 commits intosolana-developers:mainfrom
Open
Conversation
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the
deposit_liquidityinstruction which had incorrect ratiocalculation logic and test assertions that didn't catch the bug.
Root Cause
The ratio was computed with
checked_mul(pool_a × pool_b) instead ofchecked_div(pool_a / pool_b), so deposit proportionality was wrong.Tests were also passing the wrong token amount for the B slot, masking
the bug entirely.
Changes
programs/token-swap/src/instructions/deposit_liquidity.rspool_a.checked_mul(pool_b)→pool_a.checked_div(pool_b)so proportionality is calculated correctly
.unwrap()with.ok_or(DepositTooSmall)?onchecked_divto avoid panic when pool_b is zero
tests/deposit-liquidity.tsdepositAmountB(1M) instead ofdepositAmountA(4M) for token B slotso unequal deposits are actually tested
sqrt(A × B) - minLiquidityformulainstead of
A - minLiquiditywhich only worked when A == BdepositAmountBnotdepositAmountATesting
```bash
cargo build-sbf # ✅ passes (warnings only)
anchor test # ✅ 7/7 tests pass
```
Closes #512