Protect smart contracts from front running for beginners
Ways to protect a smart contract from front running: use commitments, trade limits, batch pricing, safer allowances and private submission to curb ordering gains.
Web3 News Editorial3 min read

Solidity developers can reduce front-running risk by limiting trade losses, hiding sensitive inputs until ordering is fixed, and removing gains from transaction position. Start with minimum-out and deadline checks, then choose commitments, batch settlement, pricing safeguards, safer allowance changes or private submission to match the risk; test that reordered or delayed calls revert or settle at the intended price.
Pending calls in the public mempool expose their inputs, and builders choose transaction order. Flashbots is a research and infrastructure organization focused on how maximal extractable value is handled in Ethereum. Its flashbots relay covers private transaction submission and how it relates to front-running risk.
1. Set minimum-out and deadline limits
Minimum-out and deadline parameters bound how badly a reordered or delayed trade can fill. A swap should revert if it would return less than the user’s stated minimum, or if it arrives after the user’s deadline.
This suits most trades with a known acceptable price and time window. The caller supplies both values; the contract checks them at execution, when the builder’s chosen ordering may have changed the market.
2. Use commit-reveal for hidden choices
Commit-reveal hides an action until ordering is fixed: the user first submits a hash of the value and a secret salt, then reveals both after a set number of blocks. It suits bids or choices where seeing the value early would let another trader copy or outbid it.
The salt matters. A commitment that hashes only a guessable bid amount can be brute-forced by bots, revealing the bid before the reveal phase. Use a random, private salt, bind the commitment to the user and action, and reject reveals outside the allowed block window.
3. Settle orders in a batch auction
A batch auction settles orders together at one clearing price, so position within the batch earns no price advantage. It suits protocols that can collect orders for a short interval instead of executing each one immediately.
The contract needs clear rules for the batch window, eligible orders and clearing price. Users should still set acceptable limits so an unfavorable batch cannot force a trade.
4. Price against a TWAP oracle
A time-weighted average price (TWAP) oracle makes a single spot-price move less influential on contract pricing. It suits lending, collateral checks or trades that need a reference price beyond the current block.
Choose the averaging window to fit the asset’s liquidity and how quickly the protocol must react. A longer window smooths brief moves but can lag a real price change.
5. Change ERC-20 allowances safely
Safe allowance changes address the known race when an ERC-20 user changes a non-zero approval. A spender may use the old allowance before the change and then use the new one after it is mined.
This suits any contract that asks users to approve tokens. Have the user set the allowance to zero before setting a new amount, or use token-specific increase/decrease allowance functions where available.
6. Submit sensitive calls privately
Private submission sends a transaction without exposing it in the public mempool first. It suits time-sensitive trades whose calldata would give a bot a clear opportunity to copy or move ahead.
Flashbots Protect is one example of a private submission service. Keep contract-side limits in place: private routing changes who sees a pending call, while minimum-out and deadline checks bound the result if execution is delayed.
Match the defence to the ordering risk: bound each trade, hide inputs that must stay secret, and batch actions when position should not matter.