> For the complete documentation index, see [llms.txt](https://docs.hipersnipe.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hipersnipe.com/architecture/bot-and-engines.md).

# Bot and background engines

## main.py — Telegram bot

Entry point for the aiogram dispatcher. Responsibilities:

* Register all handler routers from `handlers/`
* `/start`, `/wallet`, `/assets`, `/limits`, `/help` commands
* Main menu and inline keyboard callbacks
* Start background asyncio tasks on startup
* Graceful shutdown via `utils/system_manager.py`

### Handler routers (included from main)

| Router module                     | Domain                             |
| --------------------------------- | ---------------------------------- |
| `handlers/user_setup.py`          | Registration, wallet, assets, help |
| `handlers/trading.py`             | Aggregates trading sub-routers     |
| `handlers/trading_ca_handler.py`  | Universal CA detection             |
| `handlers/trading_token_ui.py`    | Token dashboard UI                 |
| `handlers/trading_buy_sell.py`    | Buy/sell execution                 |
| `handlers/trading_limits.py`      | Limit orders dashboard             |
| `handlers/trading_auto_sell.py`   | Trading Shield config UI           |
| `handlers/settings.py`            | Pro Settings                       |
| `handlers/admin.py`               | Admin panel                        |
| `handlers/analytics_ui.py`        | PnL analytics                      |
| `handlers/dca_ui.py`              | DCA management                     |
| `handlers/copytrade_ui.py`        | Copy trading UI                    |
| `handlers/snipe_ui.py`            | Raydium LP snipe                   |
| `handlers/channel_interceptor.py` | Partner channel deep links         |

## limit\_engine.py — Automation engine

Long-running process that:

* Subscribes to AMM pool accounts via Helius WSS
* Evaluates pending limit orders against live prices
* Executes Trading Shield TP/SL rules
* Manages presigned sell cache for low-latency exits
* Rebuilds shield registry on config changes

Key log prefixes: `[ORACLE]`, `[REBUILD]`, `[METRICS]`, `[PERF-STATS]`.

## dca\_engine.py

Executes due DCA orders from the database on a schedule.

## Listeners and watchers

| Module                   | Trigger                     | Action                                              |
| ------------------------ | --------------------------- | --------------------------------------------------- |
| `deposit_listener.py`    | Incoming SOL to user wallet | Notify user                                         |
| `copytrade_listener.py`  | Target wallet buy detected  | Mirror buy                                          |
| `pump_watcher.py`        | Pump.fun events             | Migration Sell, Dev Sell, Snipe Migration, Auto Buy |
| `snipe_watcher.py`       | Raydium LP creation         | Auto-buy snipe targets                              |
| `project_buy_watcher.py` | Project tracking buys       | Group buy alerts                                    |
| `tg_scraper.py`          | Telethon group messages     | TG CA Sniper auto-buy                               |

## TG scraper

`tg_scraper.py` runs as a Telethon client when `TG_SESSION_STRING` is configured. Monitors admin-configured groups for contract addresses and triggers snipe buys.

## Auxiliary tasks (utils/)

| Module                | Schedule                     | Purpose                                  |
| --------------------- | ---------------------------- | ---------------------------------------- |
| `daily_tasks.py`      | Daily 12:00 Europe/Ljubljana | Community War Room leaderboard broadcast |
| `gamification.py`     | On demand                    | In-bot tournament leaderboard            |
| `leaderboard_sync.py` | Background                   | Sync leaderboard data                    |
| `broadcaster.py`      | Event-driven                 | PnL win cards to community               |

## Process launcher

`start_bot.py` manages subprocess lifecycle:

* Writes `bot.pid` for `restart_bot.py`
* Prefixes logs: `[BOT UI]` and `[LIMIT ENGINE]`
* Forwards SIGINT/SIGTERM to children


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hipersnipe.com/architecture/bot-and-engines.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
