Validate a same-token repayment in a Uniswap v2 flash swap

The v2 pair transfers output before checking the final invariant. A callback test must return enough of the borrowed token and must establish which pair requested the callback.

Reproduce the same-token case

Use a reference v2 pair with reserves well above the illustrative output and no other balance changes. With a nonempty data argument the pair calls uniswapV2Call on the receiver after the outgoing transfer. For a same-token repayment, account for the fee on the returned input.

borrow token0 = 997 raw units
same-token repayment = ceil(997 × 1000 / 997) = 1000

Borrowing 997 units and returning 1,000 satisfies the same-token boundary under the reference fee. Returning 999 does not. In the callback, verify msg.sender against the intended factory’s pair for these token addresses before moving funds.

Do not reuse this calculation for a cross-token return

Repaying in the other asset depends on both reserves and the pair invariant. A fee-on-transfer token can also deliver less than the nominal transfer. Inspect the actual final balances and test the pair implementation used by the application. github.com UniswapV2Pair.sol