Skip to content

Conversation

@a-hariti
Copy link
Contributor

In the Token Swap program example,

The current deposit_liquidity instruction checks the existing liquidity to establish a tokenA/tokenB ratio, but it has a bug.

let ratio = I64F64::from_num(pool_a.amount)
.checked_mul(I64F64::from_num(pool_b.amount))
.unwrap();
if pool_a.amount > pool_b.amount {
(
I64F64::from_num(amount_b)
.checked_mul(ratio)
.unwrap()
.to_num::<u64>(),
amount_b,
)
} else {
(
amount_a,
I64F64::from_num(amount_a)
.checked_div(ratio)
.unwrap()
.to_num::<u64>(),
)
}
};

Specifically here (notice the multiplication instead of division)

 let ratio = I64F64::from_num(pool_a.amount) 
         .checked_mul(I64F64::from_num(pool_b.amount)) 
         .unwrap(); 

I fixed that and added a tests to verify the desired behavior:

  1. deposit the same ratio as the pool
  2. handle deposits of a different ratio

@a-hariti a-hariti changed the title fix deposit ratio bug fix(tokens/token-swap): deposit ratio bug Jan 10, 2026
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.

1 participant