A flash swap lets a contract use pool tokens before repaying in one transaction. The pool sends tokens first, then checks that it got enough back before the transaction ends. This helps when another step must happen before payment.
How does a flash swap work?
A flash swap is a pool trade with repayment delayed until the same transaction. A pool is a smart contract holding two tokens for people to trade. A smart contract is code that runs on the blockchain.
The contract asks a token pair for tokens and names a callback, a function the pair calls after sending them. That function runs the planned steps, such as selling the borrowed tokens elsewhere. Before it ends, the contract repays the pair.
For a BaseSwap trader, this matters when comparing a regular wallet swap with a contract strategy: BaseSwap fees and failed swaps affect the cost of the trade, while a flash swap adds a same-transaction repayment check. A regular swap does not usually ask you to write a callback.
- A contract requests tokens from a pair.
- The pair sends them and calls the contract.
- The contract uses the tokens and sends repayment to the pair.
- The pair checks its balances; if repayment is short, the whole transaction reverts, meaning its changes are undone.
This all-or-nothing rule is called atomicity: either every step succeeds, or none of the token transfers take effect. The caller still pays gas, the network fee for processing a transaction, even if it reverts.
How much must the contract repay?
Repayment depends on which token goes back to the pair. Many V2-style pools charge a 0.30% swap fee, but the exact fee depends on the pool’s rules. Check the pair’s terms before calculating.
If the contract returns the same token it borrowed, it must return slightly more than it received. For example, borrowing 10,000 tokens at a 0.30% pool fee takes about 10,030.09 of that token to repay. The effective charge is about 0.301% of the borrowed amount.
If it pays with the pair’s other token, repayment follows the pool’s normal swap price and fee. The amount depends on the pair’s reserves, meaning the tokens it holds, and how much was borrowed. A small withdrawal from a deep pool may need less repayment than a large one from a shallow pool.
These are two different cases: returning the borrowed token has a simple fee calculation; paying with the other token requires enough value at the current pool price. A contract should calculate the required amount from the pair’s current reserves, rather than assume that 0.30% of the borrowed amount will cover it.
When is a flash swap useful, and what can go wrong?
It can help when a contract needs temporary tokens to complete a profitable trade or another on-chain action. For example, it might borrow one token, sell it at a better price elsewhere, then repay the pair in the other token. The trade only works if the proceeds cover repayment and gas.
Compare that with borrowing the same token and returning it after use: the contract needs the original amount plus the same-token fee. If the other step returns too little, the pair rejects repayment and the transaction reverts. A bad price, thin liquidity, or another trade changing the price can turn an expected profit into a failed transaction that still costs gas.
On BaseSwap, I would treat a flash swap as a contract-building feature, not a shortcut in the normal swap screen. Before running one, I’d verify the pair’s fee, calculate repayment from current reserves, and simulate the full transaction, including gas.