¡ FLASH SWAPS: Préstamos sin Colateral en 1 Bloque

La magia de DeFi ” toma prestado cualquier cantidad, úsala, devuélvela, todo en una tx atómica

Semanas 5-6: DEX Avanzado y Flash Swaps
Flash SwapsAMM x*y=kArbitraje atómicoUniswap V2
¡ Piénsalo así: Imagina que puedes sacar $1M del banco sin colateral, usarlos en 10 negocios simultáneos y devolverlos, todo en 1 segundo. Si algo falla, es como si nada hubiera pasado ” la blockchain hace un revert. Eso es un flash swap. No existe en finanzas tradicionales. En DeFi lo usan arbitrajistas que explotan diferencias de precio entre exchanges sin necesitar capital propio. La fórmula AMM x × y = k (invariante de producto constante) garantiza siempre hay liquidez.
📚 Refs: Uniswap V2 Whitepaper (Hayden Adams, 2020) · "Flash Loans" ” Aave Documentation
AMM Simulator
Flash Swap
Código

Liquidity Pool ETH/USDC ” x × y = k

Reserva ETH (x)
1,000 ETH
Reserva USDC (y)
2,000,000 USDC
Invariante k = x × y
2,000,000,000
Precio ETH
$2,000
10 ETH
Recibirás (USDC)
~19,608 USDC
Precio después del swap
$1,923
Price Impact
3.85%
Fee (0.3%)
0.030 ETH
Fórmula: amountOut = (amountIn × 997 × reserveOut) / (reserveIn × 1000 + amountIn × 997)
El 0.3% de fee va a los liquidity providers.

Simulador de Flash Swap Arbitraje

$2000
$2060
100
1
Tomar prestado X ETH de Uniswap (sin colateral)
2
Vender ETH en SushiSwap al precio más alto
3
Devolver ETH + 0.3% fee a Uniswap
4
Guardar la diferencia ” profit libre de riesgo
Profit estimado
$0
Ajusta los precios para ver oportunidades

Flash Swap ” Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";

contract FlashArbBot {
  IUniswapV2Router02 immutable uniRouter;
  IUniswapV2Router02 immutable sushiRouter;
  address immutable owner;

  constructor(address _uni, address _sushi) {
    uniRouter = IUniswapV2Router02(_uni);
    sushiRouter = IUniswapV2Router02(_sushi);
    owner = msg.sender;
  }

  // Paso 1: Iniciar el flash swap
  function initiateFlashSwap(
    address pair,
    uint256 wethAmount
  ) external {
    // amount0Out=0, amount1Out=wethAmount (WETH es token1)
    IUniswapV2Pair(pair).swap(
      0, wethAmount, address(this),
      abi.encode(wethAmount) // data != "" activa flash swap
    );
  }

  // Paso 2: Callback ” Uniswap llama este función
  function uniswapV2Call(
    address,    // sender
    uint256,    // amount0
    uint256 amount1, // WETH recibido
    bytes calldata data
  ) external {
    // Vender WETH en SushiSwap (precio más alto)
    address[] memory path = new address[](2);
    path[0] = WETH; path[1] = USDC;
    uint256[] memory amounts = sushiRouter.swapExactTokensForTokens(
      amount1, 0, path, address(this), block.timestamp
    );
    // Devolver WETH + 0.3% fee a Uniswap
    uint256 repay = amount1 * 1000 / 997 + 1;
    IWETH(WETH).transfer(msg.sender, repay);
    // Profit = USDC recibido - costo del repay
    uint256 profit = amounts[1] - (repay * uniPrice / 1e18);
    IERC20(USDC).transfer(owner, profit);
  }
}
¡ Simula un swap en el AMM y observa el price impact. Luego configura una oportunidad de arbitraje con flash swap.