rustic-bot

byEast

Act as a Principal MEV Engineer, Quant Researcher, and Elite Smart Contract Security Auditor specializing in High-Frequency Trading (HFT) on EVM chains. Architect a hyper-optimized, low-latency, front-run-protected flash loan arbitrage framework engineered exclusively for the Polygon (MATIC/POL) Mainnet. Provide a complete, production-ready, bulletproof implementation across the following three files. Do not use placeholders, truncated code, shorthand syntax, or "// TODO" comments. --- ### 1. FlashLoanArbitrage.sol Write a highly optimized, gas-minimized Solidity smart contract (compiler `0.8.24` or higher with EVM version `shanghai` or `cancun`) that enforces the following requirements: * **Core Architecture:** Inherit from Aave V3’s `FlashLoanSimpleReceiverBase` using `IPoolAddressesProvider`. Utilize OpenZeppelin's `Ownable2Step` (for secure ownership transfer) and a custom, gas-efficient reentrancy guard. * **Low-Gas Execution Routing:** Implement an ultra-streamlined `executeOperation` that decodes execution payloads dynamically. It must support arbitrary multi-hop routing combining UniswapV2 and UniswapV3 pools (QuickSwap and SushiSwap variants). Use low-level assembly (`p_copy`, `calldatacopy`) where appropriate to minimize gas overhead during token swaps. * **Reorg & Expiry Protection:** Accept a `deadline` or `targetBlockNumber` parameter passed from the bot. If the transaction is mined after the `targetBlockNumber` (indicating a Polygon block reorg or mempool delay), the contract must immediately revert to prevent executing on stale, toxic prices. * **Strict Validation & Event Accounting:** * Validate that `msg.sender == address(POOL)`. * Ensure the final balance covers `amount + premium` + `minProfit`. Revert explicitly on failure to save gas via state rollback. * Emit a structured event: `event ArbitrageSuccess(address indexed token, uint256 profit, uint256 gasUsed)`. * **Capital Recovery:** Include an emergency `rescueFunds` function restricted to the owner via `onlyOwner`. ### 2. bot.js (Ethers.js v6) Write a high-performance Node.js execution engine built for speed and reliability: * **Atomic State Fetching (Multicall3):** Do not query pools individually. The bot must utilize the `Multicall3` contract (`0xcA11bde05977b3631167028862bE2a173976CA11`) to batch all `getAmountsOut` or slot0/liquidity calls into a single RPC query per block. * **Concurrency & Block-Tag Locking:** Listen to new blocks via WebSockets (`provider.on('block')`). For every new block, lock queries to that exact block number to prevent race conditions or reading desynchronized node states. * **Pre-Execution Simulation (`eth_call`):** Prior to broadcasting, execute a local simulation using `provider.call()` or `provider.estimateGas()`. Evaluate the exact return data. If the simulation fails or the projected net profit fails to clear current gas costs, drop the transaction instantly. * **MEV Relay Integration (FastLane / Flashbots):** Build payload submission logic that targets Polygon private relays (e.g., FastLane Framework or Flashbots/Polygon Builders) utilizing custom headers and signature verification to guarantee front-running and sandwich protection. ### 3. hardhat.config.js Provide an enterprise-ready Hardhat configuration file: * **Network Tuning:** Define Polygon Mainnet with optimized `timeout`, `count`, and `pollingInterval` configs matching a high-performance private node. * **Aggressive EIP-1559 Pricing:** Implement a dynamic gas pricing algorithm config framework. Ensure `maxPriorityFeePerGas` is explicitly calculated to outbid competitive public searchers during network congestion spikes, using a safe multiplier over the base fee. * **Compiler Optimization:** Enable the Solidity compiler optimizer with `runs: 999999` and enable `viaIR` (Internal Representation) optimization to ensure minimal runtime bytecode gas costs. Format the complete output cleanly using individual Markdown code blocks for each file, accompanied by a checklist of exact production dependencies and network addresses.

No preview

Comments (0)

No comments yet. Be the first!

System Requirements

System Requirement Document
📝

Requirements Not Generated Yet

The SRD will be automatically generated as you discuss your project with the Tech Lead Agent.

No completed page designs yet.

Completed design pages will appear here when they are ready to preview.

No user flows yet.

The User Flow Agent will generate per-persona navigation diagrams after SRD updates.

No completed page designs yet.

Completed design pages will appear here when they are ready to preview.

No user flows yet.

The User Flow Agent will generate per-persona navigation diagrams after SRD updates.