@advanced_security Best Practices

Overview

The @advanced_security attribute provides intelligent MEV detection that:


How It Works

Smart Detection

The system distinguishes between:

  1. Monitoring Code ✅ Allowed

  2. Protected Execution ✅ Allowed

  3. Unprotected Execution ⚠️ Blocked (if @advanced_security enabled)


Usage Examples

✅ Example 1: Monitoring Code (Always Allowed)

@advanced_security
service AnalyticsService {
    fn find_price_differences() -> list<map<string, any>> {
        // ✅ Allowed - monitoring function
        // System detects "find_" prefix = monitoring code
        return self.analyze_prices();
    }
    
    fn detect_arbitrage_opportunities() -> list<map<string, any>> {
        // ✅ Allowed - monitoring function
        // System detects "detect_" prefix = monitoring code
        return [];
    }
    
    fn monitor_liquidity_health() -> map<string, any> {
        // ✅ Allowed - monitoring function
        return {};
    }
}

✅ Example 2: Protected Execution (Allowed)

@advanced_security
service ProtectedDeFiService {
    fn execute_protected_swap(...) {
        // ✅ Allowed - protection patterns detected:
        // - commit-reveal pattern
        // - slippage protection
        // - oracle price validation
        
        let commitment_hash = crypto::hash(...);  // Protection detected
        let min_amount_out = ...;  // Slippage protection detected
        let oracle_price = self.get_oracle_price(...);  // Oracle detected
    }
}

⚠️ Example 3: Unprotected Execution (Blocked)

@advanced_security
service VulnerableService {
    fn execute_swap(token_in: string, token_out: string, amount: float) {
        // ❌ Blocked: "MEV pattern detected. Consider adding protection patterns"
        // No protection patterns found
        
        // Unprotected swap code
    }
}

Fix: Add protection patterns (see manual MEV protection guide)


Best Practices

1. Use Descriptive Function Names

✅ Good (Monitoring):

fn find_price_differences()  // ✅ Detected as monitoring
fn detect_opportunities()   // ✅ Detected as monitoring
fn analyze_market()         // ✅ Detected as monitoring

⚠️ Avoid (Ambiguous):

fn arbitrage()              // ❌ Looks like execution
fn trade()                  // ❌ Looks like execution

2. Add Protection Patterns

For execution code, always add protection:

@advanced_security
service DeFiService {
    fn execute_swap(...) {
        // ✅ Protection patterns:
        // 1. Commit-reveal (for large swaps)
        // 2. Slippage protection
        // 3. Oracle price validation
        
        let commitment_hash = crypto::hash(...);
        let min_amount_out = self.calculate_min_output(...);
        let oracle_price = self.get_oracle_price(...);
    }
}

3. Separate Monitoring from Execution

✅ Good Pattern:

@advanced_security
service Service {
    // Monitoring (allowed)
    fn find_opportunities() -> list<any> {
        return [];
    }
    
    // Execution (needs protection)
    fn execute_protected_swap(...) {
        // Has protection patterns
    }
}

When to Use @advanced_security

Use When:

Don't Use When:


Protection Pattern Recognition

The system automatically detects these protection patterns:

Commit-Reveal Patterns

Slippage Protection

Oracle Validation

Fair Batching

Time Delays


Configuration

Default Behavior

@advanced_security  // Monitor mode (warn only, allow monitoring)

Behavior:

Disable for Specific Services

// No @advanced_security = no MEV detection
service AnalyticsService {
    fn find_opportunities() {
        // No MEV detection runs
    }
}

Migration Guide

From Old (Blocking) to New (Smart)

Before (Blocked everything):

@advanced_security
service Service {
    fn find_arbitrage() {  // ❌ Blocked
    }
}

After (Smart detection):

@advanced_security
service Service {
    fn find_price_differences() {  // ✅ Allowed (monitoring)
    }
    
    fn execute_protected_swap() {  // ✅ Allowed (has protection)
    }
    
    fn execute_unprotected_swap() {  // ⚠️ Blocked (needs protection)
    }
}

Summary

Best Design: Monitor, Warn, Guide - Don't Block Legitimate Code

  1. Monitoring code → Always allowed
  2. Protected execution → Allowed (protection detected)
  3. ⚠️ Unprotected execution → Blocked with helpful suggestions
  4. 💡 Guidance → Suggests protection patterns

Result: Developers can write monitoring/analytics code freely, while still getting protection for actual execution code.


Last Updated: February 2026 Status: Implemented