Skip to content

Commit 4882570

Browse files
fix(token-swap): use pool_b amount (not pool_a squared) in invariant check
The post-swap invariant check in `handle_swap_exact_tokens_for_tokens` was comparing against `pool_account_a.amount * pool_account_a.amount` instead of `pool_account_a.amount * pool_account_b.amount`. The constant-product invariant is `a * b = k`, not `a * a`. With the bug, the check was effectively meaningless for any real swap because the squared single-side amount bears no relation to the swap's mathematical correctness — the check would spuriously pass or fail depending on pool sizes rather than catching actual invariant violations. Split out from 25399b25 ("Speed up CI") — that commit bundled unrelated CI workflow changes with this math fix; separating so each can be reviewed in isolation.
1 parent b6133a7 commit 4882570

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

tokens/token-swap/anchor/programs/token-swap/src/instructions/swap_exact_tokens_for_tokens.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub fn handle_swap_exact_tokens_for_tokens(
134134
// We tolerate if the new invariant is higher because it means a rounding error for LPs
135135
context.accounts.pool_account_a.reload()?;
136136
context.accounts.pool_account_b.reload()?;
137-
if invariant > context.accounts.pool_account_a.amount * context.accounts.pool_account_a.amount {
137+
if invariant > context.accounts.pool_account_a.amount * context.accounts.pool_account_b.amount {
138138
return err!(TutorialError::InvariantViolated);
139139
}
140140

0 commit comments

Comments
 (0)