Skip to content

test: add AMM slippage error and liquidity withdrawal test cases#634

Open
nitinraj71 wants to merge 2 commits into
solana-foundation:mainfrom
nitinraj71:test/token-swap-edge-cases
Open

test: add AMM slippage error and liquidity withdrawal test cases#634
nitinraj71 wants to merge 2 commits into
solana-foundation:mainfrom
nitinraj71:test/token-swap-edge-cases

Conversation

@nitinraj71

Copy link
Copy Markdown

Description

This PR expands the integration test coverage for the swap_example program within the token-swap example directory. Currently, the test suite only validates the primary "Swap from A to B" happy path.

Changes Introduced

  • Added Withdraw Liquidity successfully Test: Validates the end-to-end execution of partial pool share burning, explicit account resolution for the amm state context, and subsequent token balance distribution updates.
  • Added Slippage Bounds Enforcement Test: Employs an intentional mismatch scenario within swap_exact_tokens_for_tokens to mathematically guarantee the program correctly returns an execution error when min_output_amount boundaries are breached.

Verification

  • All integration suites run successfully on localnet (11 passing verified via anchor test natively within a clean WSL/Linux sub-system configuration).

@nitinraj71 nitinraj71 requested a review from dev-jodee as a code owner July 13, 2026 11:20
@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds two new integration test cases to the token-swap Anchor test suite: a Withdraw Liquidity successfully test that validates LP token burning and corresponding token balance increases, and a slippage enforcement test that verifies the program rejects swaps when min_output_amount is set impossibly high.

  • Withdraw test captures initial balances, burns half the LP position via withdrawLiquidity, then asserts the LP balance decreased by exactly withdrawAmount and that both holderAccountA and holderAccountB balances increased.
  • Slippage test now correctly uses a targetError sentinel variable outside the try block, fixing the previously reported expect.fail() swallowing bug and adding an error-type assertion beyond a bare existence check.

Confidence Score: 5/5

Safe to merge; the two new tests are structurally correct and the previously reported bugs are all addressed.

Both new tests follow sound patterns: the withdraw test captures pre/post balances and asserts LP burn and token receipts; the slippage test correctly moves expect.fail outside the catch block and adds an error-type assertion. No logic errors or incorrect on-chain interactions were found.

No files require special attention.

Important Files Changed

Filename Overview
tokens/token-swap/anchor/tests/swap.ts Adds two new tests: a withdraw-liquidity happy path and a slippage-rejection test. Previously reported bugs (swallowed expect.fail, bare error existence check, missing token-receipt assertions) are all addressed. Minor remaining concern: the slippage test matches the error type via a string substring of toString() rather than structured error-code access.

Reviews (2): Last reviewed commit: "test: change error log matching signatur..." | Re-trigger Greptile

Comment thread tokens/token-swap/anchor/tests/swap.ts Outdated
Comment on lines +148 to +151
expect.fail("The transaction should have failed due to high slippage, but it succeeded.");
} catch (err: any) {
expect(err).to.exist;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 expect.fail() is swallowed by the outer catch, making this test permanently pass

expect.fail(...) throws a Chai AssertionError. That exception is immediately caught by the surrounding catch (err: any) block on line 149, and then expect(err).to.exist evaluates to true — because err is the AssertionError object. This means the test will pass unconditionally: if the swap succeeds (slippage not enforced), expect.fail throws, the catch silently swallows it, and the assertion on line 150 trivially passes. The test provides no real coverage for the slippage guard.

Comment on lines +149 to +151
} catch (err: any) {
expect(err).to.exist;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Catch block validates error existence only, not error type

expect(err).to.exist is always satisfied by any thrown value, including network errors, account-not-found errors, or the AssertionError from expect.fail. The test won't distinguish a real slippage rejection from an unrelated setup failure. Consider asserting the specific program error code (e.g., err.error?.errorCode?.code === "SlippageExceeded") to make sure the right error is being raised.

Comment on lines +120 to +123
const postLiquidity = await connection.getTokenAccountBalance(values.liquidityAccount);
expect(new BN(postLiquidity.value.amount).toString()).to.equal(
new BN(initialLiquidity.value.amount).sub(withdrawAmount).toString()
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Withdraw test asserts LP burn but not underlying token receipt

The test only checks that the LP token balance drops by withdrawAmount. It never checks that holderAccountA or holderAccountB balances increased. A regression where the program burns LP tokens but fails to transfer the underlying tokens back would pass this test without notice.

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