🎯 Initial commit: Uniswap Auto CLP trading system

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
This commit is contained in:
2025-12-19 20:30:48 +01:00
commit 5ca16ec33f
18 changed files with 4207 additions and 0 deletions

View File

@ -0,0 +1,108 @@
# 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:
1. **Polling Intervals:** The hedger must "sleep" and "wake up" to check the file.
2. **File I/O:** Reading/writing to disk is thousands of times slower than memory operations.
3. **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
1. **`SharedState` Class (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.
2. **`UniswapManager` Thread**
* **Role:** Monitors on-chain data (RPC).
* **Action:** When it detects "Out of Range", it updates `SharedState` and sets `close_signal.set()`.
3. **`ClpHedger` Thread**
* **Role:** Manages the Hyperliquid hedge.
* **Action:** Instead of `time.sleep(1)`, it waits on `close_signal.wait(timeout=1)`.
* **Reaction:** If `close_signal` is triggered, it executes the close logic **immediately** (0 latency).
4. **`main_bot.py` (The Entry Point)**
* Initializes `SharedState`.
* Starts `UniswapManager` and `ClpHedger` as threads.
* Handles centralized logging and clean shutdown.
## Implementation Steps
### Step 1: Create `SharedState`
Define a class that replaces the JSON file structure.
```python
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()` and `save_status_data()` calls with `self.shared_state.update_position(...)`.
* When "Out of Range" is detected:
```python
# 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:
```python
# 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`
```python
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
1. **Zero Latency:** The moment the Manager sets the event, the Hedger reacts. No polling delay.
2. **Reliability:** No file corruption risks (like the JSON error experienced earlier).
3. **Efficiency:** Reduces disk I/O, extending SD card/drive life and reducing CPU usage.