DeFi

Flash Swap

A flash swap is an AMM feature where a user receives tokens before paying for them, as long as repayment or equivalent value happens before the transaction ends.

A flash swap lets a trader borrow from a pool inside one transaction and pay it back before the transaction finishes.

Flash Swap Explained in Detail

A flash swap lets a user receive tokens from an AMM pool before paying for them. The user must repay the pool, or provide equivalent value, before the transaction ends.

The flow usually depends on a callback into the borrower's contract.

Smart contract example

pool sends tokens -> borrower callback runs -> borrower repays pool

If repayment fails, the whole transaction reverts.

Flash Swap in Auditing

Flash swaps provide same-transaction liquidity and external callback control. They can be used for arbitrage, but also for oracle manipulation, reentrancy, and price-dependent exploits.

Auditors review both protocols that offer flash swaps and protocols that can be attacked using them.

Red flags in code

  • Callback does not verify the real pool sender.

  • Protocol reads AMM spot prices after a flash swap changes reserves.

  • Repayment logic ignores fees or token behavior.

  • Callback can reenter sensitive functions.

  • Pool address or token pair assumptions are weak.

How to test or review it

  • Implement a malicious callback contract.

  • Manipulate a dependent pool or oracle during the callback.

  • Test repayment success, underpayment, and revert paths.

  • Verify callback sender and token pair.

  • Combine with flash loan attack scenarios.

Practice this in real audit scenarios

Definitions help, but auditors need reps. SCH turns concepts like Flash Swap into exploit labs, code review habits, and report-writing practice.

Start the free trial or see the full smart contract auditing course.

Sources