Core Components: - uniswap_manager.py: V3 concentrated liquidity position manager - clp_hedger.py: Hyperliquid perpetuals hedging bot - requirements.txt: Python dependencies - .gitignore: Security exclusions for sensitive data - doc/: Project documentation - tools/: Utility scripts and Git agent Features: - Automated liquidity provision on Uniswap V3 (WETH/USDC) - Delta-neutral hedging using Hyperliquid perpetuals - Position lifecycle management (open/close/rebalance) - Automated backup and version control system Security: - Private keys and tokens excluded from version control - Environment variables properly handled - Automated security validation for backups Git Agent: - Hourly automated backups to separate branches - Keep last 100 backups (~4 days coverage) - Detailed change tracking and parameter monitoring - Push to Gitea server automatically - Manual main branch control preserved - No performance tracking for privacy - No notifications for simplicity Files Added: - git_agent.py: Main automation script - agent_config.json: Configuration with Gitea settings - git_utils.py: Git operations wrapper - backup_manager.py: Backup branch management - change_detector.py: File change analysis - cleanup_manager.py: 100-backup rotation - commit_formatter.py: Detailed commit messages - README_GIT_AGENT.md: Complete usage documentation
4.0 KiB
4.0 KiB
Low Latency Optimization Plan: Memory Sharing Integration
Overview
Currently, the system consists of two separate processes (uniswap_manager_refactored.py and clp_hedger.py) communicating via a file (hedge_status.json). This introduces inevitable latency due to:
- Polling Intervals: The hedger must "sleep" and "wake up" to check the file.
- File I/O: Reading/writing to disk is thousands of times slower than memory operations.
- Synchronization: Potential race conditions if both try to access the file simultaneously.
Goal
Eliminate file reliance to achieve sub-millisecond reaction times between "Uniswap Position Out of Range" detection and "Hedge Close" execution.
Proposed Architecture: Unified Multi-Threaded Bot
Instead of two independent scripts, we will merge them into a single Python application running two concurrent threads that share a common data object in memory.
Key Components
-
SharedStateClass (The Brain)- A thread-safe data structure (using
threading.Lock) that holds the current position status, price, and range. - Events: Uses
threading.Event(e.g.,close_signal) to allow the Manager to instantly wake up the Hedger without waiting for a sleep cycle to finish.
- A thread-safe data structure (using
-
UniswapManagerThread- Role: Monitors on-chain data (RPC).
- Action: When it detects "Out of Range", it updates
SharedStateand setsclose_signal.set().
-
ClpHedgerThread- Role: Manages the Hyperliquid hedge.
- Action: Instead of
time.sleep(1), it waits onclose_signal.wait(timeout=1). - Reaction: If
close_signalis triggered, it executes the close logic immediately (0 latency).
-
main_bot.py(The Entry Point)- Initializes
SharedState. - Starts
UniswapManagerandClpHedgeras threads. - Handles centralized logging and clean shutdown.
- Initializes
Implementation Steps
Step 1: Create SharedState
Define a class that replaces the JSON file structure.
class SharedState:
def __init__(self):
self.lock = threading.Lock()
self.close_event = threading.Event()
self.position_data = {} # Stores the dict formerly in JSON
def update_position(self, data):
with self.lock:
self.position_data.update(data)
def get_position(self):
with self.lock:
return self.position_data.copy()
def trigger_emergency_close(self):
self.close_event.set()
Step 2: Refactor uniswap_manager_refactored.py
- Convert the script into a class
UniswapManager. - Replace all
load_status_data()andsave_status_data()calls withself.shared_state.update_position(...). - When "Out of Range" is detected:
# Old update_position_status(token_id, "CLOSING") # New self.shared_state.update_position({'status': 'CLOSING'}) self.shared_state.trigger_emergency_close() # Wakes up Hedger instantly
Step 3: Refactor clp_hedger.py
- Convert the script into a class
ClpHedger. - Replace file reading logic with
self.shared_state.get_position(). - Update the main loop to handle the event:
# Old time.sleep(CHECK_INTERVAL) # New # Wait for 1 second OR immediate signal if self.shared_state.close_event.wait(timeout=1.0): self.close_all_positions() self.shared_state.close_event.clear()
Step 4: Create main_bot.py
if __name__ == "__main__":
state = SharedState()
manager = UniswapManager(state)
hedger = ClpHedger(state)
t1 = threading.Thread(target=manager.run)
t2 = threading.Thread(target=hedger.run)
t1.start()
t2.start()
t1.join()
t2.join()
Benefits
- Zero Latency: The moment the Manager sets the event, the Hedger reacts. No polling delay.
- Reliability: No file corruption risks (like the JSON error experienced earlier).
- Efficiency: Reduces disk I/O, extending SD card/drive life and reducing CPU usage.