La magia de DeFi ” toma prestado cualquier cantidad, úsala, devuélvela, todo en una tx atómica
amountOut = (amountIn × 997 × reserveOut) / (reserveIn × 1000 + amountIn × 997)// 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); } }