FV-SOL-8-C2 Front-Running

TLDR

Exploiting the price calculation mechanism to cause an unusually high slippage rate

Game

This contract allows users to execute token swaps on a DEX. This transactions are visible in the mempool. Umm.. what else?

// SPDX-License-Identifier: MIT
// Open me in VSCode and really think before opening the hints!
// Add @audit tags wherever suspicious
// Go to the solidity docs to complete missing knowledge of what's happening here
// Solve by drafting a fix!
pragma solidity ^0.8.0;

interface IDEX {
    function swap(address tokenIn, address tokenOut, uint256 amountIn) external returns (uint256);
    function getPrice(address tokenIn, address tokenOut) external view returns (uint256);
}

contract FrontRunningGame {
    IDEX public dex;
    address public tokenOut;

    constructor(address _dex, address _tokenOut) {
        dex = IDEX(_dex);
        tokenOut = _tokenOut;
    }

    // Function to perform a swap without protection against front-running
    function executeSwap(address tokenIn, uint256 amountIn, uint256 minAmountOut) public {
        uint256 amountOut = dex.swap(tokenIn, tokenOut, amountIn);
        require(amountOut >= minAmountOut, "Slippage too high");
    }
}

Last updated