MEV Protection

Advanced mechanisms to ensure fair trading

PercolatePerps implements advanced MEV protection mechanisms to ensure fair trading:

// Anti-Sandwich Tracking
pub struct ToxicityTracker {
    pub recent_trades: VecDeque<Trade>,
    pub toxicity_scores: HashMap<Pubkey, f64>,
}

impl ToxicityTracker {
    pub fn detect_sandwich(&mut self, trade: &Trade) -> bool {
        let window = self.recent_trades
            .iter()
            .filter(|t| t.timestamp > trade.timestamp - 5);
        
        // Check for suspicious patterns
        let same_user_trades: Vec<_> = window
            .filter(|t| t.user == trade.user)
            .collect();
        
        if same_user_trades.len() >= 2 {
            let price_impact = self.calculate_price_impact(
                &same_user_trades
            );
            
            if price_impact > SANDWICH_THRESHOLD {
                self.toxicity_scores
                    .entry(trade.user)
                    .and_modify(|s| *s += 1.0)
                    .or_insert(1.0);
                return true;
            }
        }
        
        false
    }
}

Two-Phase Execution

Reserve-commit pattern ensures atomic operations and prevents sandwich attacks by requiring explicit commitment of reserved resources.

// Two-Phase Reserve-Commit
pub fn reserve_order(
    ctx: Context<ReserveOrder>,
    params: OrderParams,
) -> Result<Reservation> {
    let reservation = Reservation {
        id: generate_id(),
        user: ctx.accounts.user.key(),
        params,
        reserved_at: Clock::get()?.unix_timestamp,
    };
    
    ctx.accounts.slab.reservations.insert(
        reservation.id,
        reservation.clone()
    );
    
    Ok(reservation)
}

pub fn commit_order(
    ctx: Context<CommitOrder>,
    reservation_id: u64,
) -> Result<()> {
    let res = ctx.accounts.slab.reservations
        .remove(&reservation_id)
        .ok_or(ErrorCode::InvalidReservation)?;
    
    // Atomic execution
    ctx.accounts.slab.execute_order(res.params)?;
    Ok(())
}

Anti-Sandwich Tracking

Built-in toxicity controls monitor and prevent front-running attempts, protecting traders from MEV extraction.

Capability-Based Security

Time-limited authorization tokens (max 2 minutes) ensure that trading permissions cannot be exploited over extended periods.

Ready to Trade?

Experience MEV-protected trading on the most advanced perpetual platform