project-99aea1a5

byEast

Act as a Principal MEV (Maximal Extractable Value) R&D Engineer and a Senior Solidity Security Auditor specializing in low-latency EVM arbitrage systems on high-throughput networks. I want to build a highly competitive, production-grade cross-DEX flash loan arbitrage bot for Polygon Mainnet. The architecture must be heavily inspired by the core structural philosophy of the 'pmuens/midas' framework but radically modernized to compete in today's cutthroat MEV environment. Generate the code base according to the exact architectural specifications, structural parameters, and execution protocols outlined below. --- ### 1. SYSTEM PARAMETERS & ENVIRONMENTAL BOUNDS - Network: Polygon Mainnet (Gas asset: POL / Base trading asset: Native USDC) - Flash Loan Source: Balancer V2 Vault (Address: 0xBA12222222228d8Ba445958a75a0704d566BF2C8) for 0% fee asset borrowing. - Target DEX Liquidity Pools: QuickSwap V2 (Uniswap V2 Clone) and SushiSwap V2. - Development Framework: Foundry (Solidity ^0.8.20) for the smart contracts; TypeScript (ethers.js v6) for the off-chain orchestration engine. --- ### 2. CORE COMPONENTS REQUIRED #### COMPONENT A: The Smart Contract ('MidasPolygonEngine.sol') Write a highly gas-optimized, production-ready Solidity contract that implements the Balancer V2 Flash Loan receiver interface ('IFlashLoanRecipient'). - Gas Optimization Rules: Optimize for bytecode compactness and minimal runtime overhead. Use custom errors instead of string require statements. Use 'unchecked {}' blocks for arithmetic loops and balance changes where overflow is mathematically impossible. Avoid high-level abstractions like routers. - Direct Pool Routing: The contract must execute swaps by calling the liquidity pool pair contracts directly via '.swap()' instead of using high-level router contracts (like QuickSwapRouter). - Strict Execution Safety: Implement a bulletproof safeguard. At the end of the callback function—immediately before the Balancer Vault pulls back the loan principal—the contract must verify that the ending balance of the profit token (USDC) is greater than the starting balance plus an off-chain parameterized 'minProfitThreshold'. If this condition fails, the contract must explicitly revert the entire transaction transaction string to prevent capital loss. - Access Control: Secure the trigger mechanism so that only the owner's monitoring bot address can call the execution entry point, completely preventing external actors from frontrunning or hijacking the execution. #### COMPONENT B: The Node Engine ('monitor.ts') Write a highly optimized TypeScript orchestration script that acts as the real-time data ingestion and calculations hub. - Event-Driven Architecture: Do not use interval JSON-RPC block polling. Use a high-fidelity WebSocket provider ('wss://') to subscribe directly to the 'Sync(uint112,uint112)' or 'Swap' event logs emitted directly by the target QuickSwap and SushiSwap pair contracts. - Math Engine (Optimal Input Calculation): Implement the standard AMM optimal input amount formula for two constant-product (x * y = k) Uniswap V2 pairs to calculate the mathematically perfect input amount (A_in) that maximizes arbitrage yield given the exact reserves extracted from the latest 'Sync' logs. The equation must natively factor in the standard 0.3% fee modifier (gamma = 0.997). - MEV Guardrail & Private Relaying: Structure the transaction submission logic to bypass the public Polygon mempool entirely to prevent frontrunning and sandwich attacks. Integrate the transaction dispatch code with the FastLane Protocol API or Flashbots Polygon Builder endpoint, sending the execution payload as a private bundle. --- ### 3. OUTPUT SPECIFICATION Please provide the complete, functional code for both 'MidasPolygonEngine.sol' and 'monitor.ts'. Include explicit inline comments detailing the low-level data handoff between the off-chain TypeScript script and the contract's flash loan callback payload. Do not use placeholders or omit complex logic loops. Show less

LandingLoginDashboard
Landing

Comments (0)

No comments yet. Be the first!

System Requirements

System Requirement Document

project-99aea1a5 System Requirements Document

Introduction

This document outlines the system requirements for project-99aea1a5, a highly competitive, production-grade cross-DEX flash loan arbitrage bot designed for the Polygon Mainnet. The architecture is inspired by the 'pmuens/midas' framework but is modernized to excel in the current MEV environment.

System Overview

The project aims to develop a flash loan arbitrage bot that operates on the Polygon Mainnet, utilizing a sophisticated architecture to maximize extractable value through low-latency EVM arbitrage systems. The system will leverage the Balancer V2 Vault for flash loans and target liquidity pools on QuickSwap V2 and SushiSwap V2. Development will be conducted using Foundry for smart contracts and TypeScript for the orchestration engine.

Source Content Inventory

  • Network: Polygon Mainnet
  • Gas Asset: POL
  • Base Trading Asset: Native USDC
  • Flash Loan Source: Balancer V2 Vault (Address: 0xBA12222222228d8Ba445958a75a0704d566BF2C8)
  • Target DEX Liquidity Pools: QuickSwap V2, SushiSwap V2
  • Development Framework: Foundry (Solidity ^0.8.20), TypeScript (ethers.js v6)
Page 1 of 5

Functional Requirements as Story Points

  • As a Developer, I should be able to write a gas-optimized Solidity contract implementing the Balancer V2 Flash Loan receiver interface.
  • As a Developer, I should ensure the contract executes swaps by directly calling liquidity pool pair contracts.
  • As a Developer, I should implement a safeguard to verify the ending balance of the profit token is greater than the starting balance plus 'minProfitThreshold'.
  • As a Developer, I should secure the contract's execution entry point to allow only the owner's monitoring bot address to trigger it.
  • As a Developer, I should write a TypeScript script using a WebSocket provider to subscribe to 'Sync' or 'Swap' event logs.
  • As a Developer, I should implement a math engine to calculate the optimal input amount for arbitrage yield.
  • As a Developer, I should structure transaction submission logic to bypass the public Polygon mempool.

User Personas

  • Developer: Responsible for writing and optimizing the smart contract and orchestration script.
  • Bot Owner: The individual or entity that owns and operates the arbitrage bot, ensuring its secure and efficient execution.
Page 2 of 5

Core User Flows

  • Developer writes and deploys the smart contract on Polygon Mainnet.
  • Developer configures the TypeScript orchestration script to monitor liquidity pools.
  • Bot Owner triggers the execution entry point via the monitoring bot address.
  • The smart contract executes flash loans and swaps, verifying profit conditions.
  • The orchestration script calculates optimal input amounts and submits transactions privately.

Visuals Colors and Theme

  • primary: #1E3A8A (Deep Blue)
  • primary_light: #3B82F6 (Light Blue)
  • secondary: #F97316 (Orange)
  • accent: #10B981 (Green)
  • highlight: #F59E0B (Amber)
  • bg: #F3F4F6 (Light Gray)
  • surface: rgba(255, 255, 255, 0.8)
  • text: #111827 (Dark Gray)
  • text_muted: #6B7280 (Muted Gray)
  • border: rgba(209, 213, 219, 0.2)
Page 3 of 5

Signature Design Concept

The landing page for project-99aea1a5 will feature an interactive galaxy map where each star represents a feature or section of the bot. Users can click on stars to open detailed information cards, drag to rotate the galaxy, and hover to highlight connections between features. This concept will be implemented using @react-three/fiber and @react-three/drei for 3D interactions, creating an engaging and memorable user experience.

Interaction Model & Motion Direction

The landing page will utilize a "parallax" interaction model, providing a layered depth effect as users scroll. Decorative elements will move at different speeds to create a dynamic visual experience, while core content remains in the natural flow. This approach is ideal for storytelling and creating a visually rich first impression.

Non-Functional Requirements

  • The system must ensure high availability and low latency to maximize arbitrage opportunities.
  • Security measures must be in place to prevent unauthorized access and ensure safe execution of transactions.
  • The system should be scalable to handle increased transaction volumes as needed.
Page 4 of 5

Tech Stack

  • Frontend: React for web interfaces
  • Backend: Solidity for smart contracts, TypeScript for orchestration
  • Database: Not specified, assumed not required for this project
  • AI Models: Not applicable
  • AI Tools: Not applicable
  • Local Orchestration: Docker, docker-compose
  • Server-side Orchestration: Kubernetes

Assumptions and Constraints

  • The system will operate exclusively on the Polygon Mainnet.
  • Flash loans will be sourced from the Balancer V2 Vault.
  • The bot will target liquidity pools on QuickSwap V2 and SushiSwap V2.
  • The development framework will be Foundry and TypeScript.

Glossary

  • MEV: Maximal Extractable Value
  • DEX: Decentralized Exchange
  • EVM: Ethereum Virtual Machine
  • AMM: Automated Market Maker
  • POL: Polygon's native gas asset
  • USDC: USD Coin, a stablecoin
  • Flash Loan: A type of uncollateralized loan in DeFi that must be borrowed and repaid within a single transaction block.
Page 5 of 5
Landing design preview
Landing: View Galaxy Map
Login: Sign In
Dashboard: View Bot Status
Dashboard: Trigger Execution
Dashboard: View Profit Reports
Settings: Configure Profit Threshold
Settings: Manage Access Control
Dashboard: View Transaction History
Dashboard: Monitor Gas Costs
Settings: Update Bot Address
Landing design preview
Landing: View Galaxy Map
Login: Sign In
Dashboard: View Bot Status
Dashboard: Trigger Execution
Dashboard: View Profit Reports
Settings: Configure Profit Threshold
Settings: Manage Access Control
Dashboard: View Transaction History
Dashboard: Monitor Gas Costs
Settings: Update Bot Address