mev-sandwich-detector
v1.0.2
Published
Detect MEV sandwich attacks on Ethereum transactions with Lightning micropayments
Maintainers
Readme
MEV Sandwich Detector
Detect MEV sandwich attacks on Ethereum transactions with automatic Lightning Network micropayments.
Features
- 🔍 Real-time sandwich attack detection
- ⚡ Automatic Lightning micropayments via L402
- 🎯 Monitor specific addresses for MEV threats
- 📊 MEV statistics and analytics
- 🔐 Secure payment caching (5-minute TTL)
Installation
npm install mev-sandwich-detectorQuick Start
const MEVDetector = require('mev-sandwich-detector');
const detector = new MEVDetector({
nwcUri: 'nostr+walletconnect://...' // From your Lightning wallet
});
// Check if a transaction is being sandwiched
const result = await detector.checkTransaction('0x1234...');
console.log(result);
// {
// isSandwiched: true,
// frontrunTx: '0xabc...',
// backrunTx: '0xdef...',
// estimatedLoss: '0.05 ETH',
// confidence: 0.95
// }API Reference
checkTransaction(txHash)
Check if a specific transaction is being sandwich attacked.
Parameters:
txHash(string): Ethereum transaction hash
Returns: Promise
isSandwiched(boolean): Whether sandwich attack detectedfrontrunTx(string): Frontrunning transaction hashbackrunTx(string): Backrunning transaction hashestimatedLoss(string): Estimated loss amountconfidence(number): Detection confidence (0-1)
Cost: 50 sats (~$0.05)
monitorAddress(options)
Monitor an address for real-time sandwich attacks.
Parameters:
options.address(string): Ethereum address to monitoroptions.minValue(number): Minimum transaction value in ETH
Returns: Promise
subscriptionId(string): Monitoring subscription IDwebsocketUrl(string): WebSocket connection URL
Cost: 100 sats (~$0.10) per alert
getStats(timeframe)
Get MEV sandwich attack statistics.
Parameters:
timeframe(string): '1h', '24h', or '7d'
Returns: Promise
totalAttacks(number): Total sandwich attacks detectedtotalValue(string): Total value extracted by MEV botsavgLoss(string): Average loss per victimtopVictims(Array): Most affected addresses
Cost: 20 sats (~$0.02)
analyzeRisk(tx)
Analyze pending transaction for sandwich risk before sending.
Parameters:
tx(Object): Transaction object with to, from, value, gasPrice
Returns: Promise
riskScore(number): Risk score (0-100)recommendation(string): 'safe', 'caution', or 'danger'suggestedGasPrice(string): Recommended gas price to avoid sandwich
Cost: 30 sats (~$0.03)
Pricing
| Method | Cost | Description |
|--------|------|-------------|
| checkTransaction() | 50 sats | Check single transaction |
| monitorAddress() | 100 sats/alert | Real-time monitoring |
| getStats() | 20 sats | MEV statistics |
| analyzeRisk() | 30 sats | Pre-transaction risk analysis |
Configuration
Set your Lightning wallet connection:
# .env file
NWC_URI=nostr+walletconnect://your-wallet-uri-hereGet your NWC URI from:
- Alby: Settings → Wallet Connect
- Mutiny: Settings → Nostr Wallet Connect
- Phoenix: Settings → Developer → NWC
How It Works
- Request Data → Package makes API call with your query
- Payment Required → Server responds with 402 + Lightning invoice
- Auto-Pay → Package pays automatically via your Lightning wallet
- Receive Data → MEV analysis returned instantly
- Cached → Payment cached for 5 minutes (no repayment needed)
Examples
Monitor Your Trading Bot
const detector = new MEVDetector({ nwcUri: process.env.NWC_URI });
// Monitor your bot's address
const subscription = await detector.monitorAddress({
address: '0xYourBotAddress',
minValue: 0.1 // Only alert for txs > 0.1 ETH
});
console.log('Monitoring started:', subscription.subscriptionId);Check Before Swapping
const tx = {
to: '0xUniswapRouter',
from: '0xYourAddress',
value: '1000000000000000000', // 1 ETH
gasPrice: '50000000000' // 50 gwei
};
const risk = await detector.analyzeRisk(tx);
if (risk.recommendation === 'danger') {
console.log('⚠️ High sandwich risk detected!');
console.log('Suggested gas price:', risk.suggestedGasPrice);
}License
MIT
Links
- npm: https://www.npmjs.com/package/mev-sandwich-detector
- GitHub: https://github.com/RodMillz/mev-sandwich-detector
Support
Questions? Open an issue on GitHub or reach out via Lightning: [email protected]
