惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
月光博客
月光博客
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
B
Blog
C
Check Point Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
量子位
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
BUY_LOCK_BLOCKS: How Immute Achieves Smart Contract Sandw...
Version 6 LL · 2026-04-26 · via DEV Community


yaml style: blog status: draft generated_at: 2026-01-25T12:00:00Z

Smart contract sandwich attack prevention is a core design goal of Immute, a bonding‑curve reward token live on the Sepolia testnet. By introducing a per‑address mutual‑exclusion mechanism, Immute makes it impossible for any single wallet to execute a buy‑followed‑by‑sell (or sell‑followed‑by‑buy) inside the same block, thereby neutralising the classic front‑run‑buy / back‑run‑sell pattern that plagues AMM‑style DEXes. This article dissects the attack vector, explains the logic behind the isLocked() and lockedUntil() primitives, and shows how the contract enforces the lock at the EVM level.

What Is a Sandwich Attack?

A sandwich attack is a form of maximal‑extractable value (MEV) exploitation that relies on ordering two transactions around a victim trade in a single block:

  1. Front‑run buy – the attacker sees a pending large buy and places his own buy just before it, pushing the curve price up. 2. Victim execution – the victim's buy settles at the higher price, absorbing the artificial slippage. 3. Back‑run sell – the attacker immediately sells his position in the same block, capturing the spread.

The profitability of this pattern depends entirely on the attacker’s ability to buy and sell within the same block. If a contract can enforce that a wallet cannot both buy and sell in the same block, the attack surface disappears.

Why Traditional AMM Contracts Remain Vulnerable

Most AMM contracts treat each transaction as independent. The swap function simply checks the reserves, updates them, and returns the output. There is no per‑address state that persists across the block, so a single wallet can:

  • Call swap to buy tokens. - Immediately call swap again to sell the same tokens.

Both calls are processed in the same block, so the attacker enjoys the same price impact and can pocket the spread with negligible risk. The only cost is the gas for two transactions, which is often outweighed by the extracted value.

Immute’s Per‑Address Buy‑Lock Mechanism

Immute introduces two simple but powerful state variables:

solidity mapping(address => uint256) public lockedUntil;

lockedUntil[addr] stores the timestamp (or block‑relative value) after which the address is allowed to perform the opposite action. The contract provides a view function:

solidity function isLocked(address addr) external view returns (bool) { return block.timestamp < lockedUntil[addr]; }

When an address executes a buy, the contract sets:

solidity lockedUntil[msg.sender] = block.timestamp + 1; // zero‑duration lock for the remainder of the block

When the same address attempts a sell in the same block, the first line of the sell function checks:

solidity if (isLocked(msg.sender)) revert BuyLockActive();

Because block.timestamp has not advanced, the check evaluates to true, reverting the transaction. Conversely, after a sell, the lock is set on the buy side:

solidity lockedUntil[msg.sender] = block.timestamp + 1;

Thus a user cannot immediately buy after selling, nor sell after buying, within the same block.

Why a 1‑second Lock Works

Ethereum blocks are produced roughly every 12 seconds, but block.timestamp advances only when a new block is mined. By setting lockedUntil to block.timestamp + 1, we guarantee that any subsequent transaction in the same block will see the lock still active, while a transaction in the next block will see block.timestamp equal to or greater than lockedUntil, lifting the restriction. This effectively creates a one‑block cool‑down without altering the global throughput of the contract.

Implementation Details

The IMT V8 contract (0xB575A8760c66F09a26A03bc215D612EA2486373C) implements the lock in the buy and sell entry points. Below is a simplified pseudocode representation of the guard:

```solidity function buy(address recipient, uint256 minOut) external payable { // 1. Validate curve parameters, compute output, apply 10% fee. require(!isLocked(recipient), "Buy locked"); // 2. Execute transfer and update internal accounting. _processBuy(recipient, msg.value); // 3. Set lock to prevent a sell in this block. lockedUntil[recipient] = block.timestamp + 1; }

function sell(address payable seller, uint256 amount, uint256 minOut) external { // 1. Validate allowance, compute output, apply 10% fee. require(!isLocked(seller), "Sell locked"); // 2. Execute transfer and update internal accounting. _processSell(seller, amount); // 3. Set lock to prevent a buy in this block. lockedUntil[seller] = block.timestamp + 1; } ```

The Feeder contract (`0xa87e7c25


Want to dig deeper into how Immute works on-chain?

  • Read the whitepaper — full technical spec of the bonding curve, fee distribution, and Feeder primitive.

  • Audit + V4 postmortem — every finding ever raised against the contracts and how it was resolved.

  • Live leaderboard — top holders, dividend earnings, referral payouts.

  • On-chain charts — supply curve, ETH balance, Feeder fee flow.

  • immute.io — connect a wallet and try the mechanics on Sepolia testnet (mainnet launch coming soon).