What is front-running in smart contracts?

Front-running is getting your transaction executed before one you have already seen waiting to be included, so you profit from it or take what it was about to claim. Pending transactions on a public chain can be seen before they are final (SWC-114), so any function whose outcome depends on order can be front-run by whoever is watching.

Last updated . 2 minute read. By the QuillAudits Team.

What forms does front-running take?

Kind What the attacker does Example
Displacement Runs first, so the pending transaction fails or does nothing Copying a puzzle answer or a claim
Insertion Runs first, so the pending transaction executes on changed state Buying before a large swap
Suppression Delays the pending transaction, for example by filling blocks Holding off a bid until an auction ends

The three names come from the Consensys Diligence guide to front-running (Consensys). Insertion before and after a trade is a sandwich, one of the kinds of MEV.

SWC-114 gives a token example as well: when an owner changes an ERC-20 allowance, a spender who sees the change pending can spend the old allowance first and the new one afterwards (SWC-114).

How do you fix a front-runnable function?

Remove what going first gains. Bind claims to msg.sender, take a minimum output and a deadline on trades, and when an answer must stay hidden until it is judged, split it into a commit and a reveal (SWC-114).

Commit-reveal with separate phases (illustrative, not audited code) CommitRevealPrize.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract CommitRevealPrize {
    bytes32 public immutable answerHash;
    uint256 public immutable revealStart; // no commits from this time on
    mapping(address => bytes32) public commitments;
    address public winner;

    constructor(bytes32 answerHash_, uint256 revealStart_) payable {
        answerHash = answerHash_;
        revealStart = revealStart_;
    }

    function commit(bytes32 commitment) external {
        require(block.timestamp < revealStart, "commit phase over");
        commitments[msg.sender] = commitment;
    }

    // A copied reveal fails: the commitment includes the original sender.
    function reveal(string calldata answer, bytes32 salt) external {
        require(block.timestamp >= revealStart && winner == address(0), "closed");
        require(commitments[msg.sender] == keccak256(abi.encode(msg.sender, answer, salt)), "bad reveal");
        require(keccak256(bytes(answer)) == answerHash, "wrong answer");
        winner = msg.sender;
        payable(msg.sender).transfer(address(this).balance);
    }
}

The separate phases matter, because if commits stayed open, someone who saw a reveal could commit and reveal the same answer ahead of it.

FAQ

Can an initializer be front-run?

Yes, when a proxy is deployed in one transaction and initialized in another, because anyone who sees the deployment can call initialize first and become the owner. Deploy and initialize in the same transaction.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts