Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ pub fn deposit_liquidity(
// Add as is if there is no liquidity
(amount_a, amount_b)
} else {
let ratio = I64F64::from_num(pool_a.amount)
.checked_mul(I64F64::from_num(pool_b.amount))
let amount_ratio = I64F64::from_num(amount_a)
.checked_div(I64F64::from_num(amount_b))
.unwrap();
if pool_a.amount > pool_b.amount {
let pool_ratio = I64F64::from_num(pool_a.amount)
.checked_div(I64F64::from_num(pool_b.amount))
.unwrap();
if pool_ratio > amount_ratio {
(
I64F64::from_num(amount_b)
.checked_mul(ratio)
.checked_mul(pool_ratio)
.unwrap()
.to_num::<u64>(),
amount_b,
Expand All @@ -52,7 +55,7 @@ pub fn deposit_liquidity(
(
amount_a,
I64F64::from_num(amount_a)
.checked_div(ratio)
.checked_div(pool_ratio)
.unwrap()
.to_num::<u64>(),
)
Expand Down
13 changes: 8 additions & 5 deletions tokens/token-swap/steel/program/src/deposit_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ pub fn process_deposit_liquidity(accounts: &[AccountInfo<'_>], data: &[u8]) -> P
// Add as is if there is no liquidity
(amount_a, amount_b)
} else {
let ratio = U256::from(pool_account_a.amount)
.checked_mul(U256::from(pool_account_b.amount))
let amount_ratio = U256::from(amount_a)
.checked_div(U256::from(amount_b))
.unwrap();
if pool_account_a.amount > pool_account_b.amount {
let pool_ratio = U256::from(pool_account_a.amount())
.checked_div(U256::from(pool_account_b.amount()))
.unwrap();
if pool_ratio > amount_ratio {
(
U256::from(amount_b).checked_mul(ratio).unwrap().as_u64(),
U256::from(amount_b).checked_mul(pool_ratio).unwrap().as_u64(),
amount_b,
)
} else {
(
amount_a,
U256::from(amount_a).checked_div(ratio).unwrap().as_u64(),
U256::from(amount_a).checked_div(pool_ratio).unwrap().as_u64(),
)
}
};
Expand Down