> For the complete documentation index, see [llms.txt](https://docs.godex.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.godex.ai/getting-started/copy-trading-on-dex-exchange/how-to-work.md).

# HOW TO WORK

## 🧠 Objective:

Develop a BOT that monitors a trader’s wallet on GMX.io, detects when they open or close Long or Short positions, and automatically replicates those trades to the user’s wallet (copy trading).

### ✅ Step 1: Understand the Trading Mechanism on GMX

GMX is a decentralized perpetual exchange (perpetual DEX) that operates without an order book, relying instead on smart contracts.

* GMX uses the PositionRouter contract to open positions.
* All trader transactions can be tracked on Arbiscan or via RPC/WS nodes.
* Wallets typically interact with the PositionRouter contract to open and close positions.

### ✅ Step 2: Select the Trader to Monitor

* Identify a notable trader’s wallet (via communities, on-chain analysis, or tools like DeBank, Arkham, Lookonchain, etc.).
* Record the wallet address to be monitored.

### ✅ Step 3: Monitor Position Opening Transactions

#### Option 1: Use Arbiscan API *(simpler but delayed)*

* Query getInternalTransactionsByAddress or getLogs to check for interactions with the PositionRouter contract.
* Filter by the following function signatures:
  * createIncreasePosition
  * createDecreasePosition
  * executeIncreaseOrder
  * cancelIncreaseOrder

#### Option 2: Use Web3 WebSocket *(real-time)*

* Use Web3.py or ethers.js to connect to an Arbitrum WebSocket (WS) node.
* Subscribe to events emitted by the PositionRouter contract (such as order creation, cancellation, or execution).

### ✅ Step 4: Extract Trade Order Details

Once a position opening event (IncreasePosition) is detected, extract the following data:

* tokenIn: Token used for margin (e.g., USDC)
* indexToken: The asset being traded (e.g., ETH, BTC)
* isLong: True/False (indicating Long or Short)
* sizeDelta: Trade size in USD
* acceptablePrice: Target execution price
* executionFee: Gas fee
* leverage: Calculated from sizeDelta and collateralDelta

### ✅ Step 5: Copy the Trade to the User’s Wallet

* Send a similar order using the PositionRouter contract by calling createIncreasePosition(...) from the user’s wallet.
* Use Web3.py or ethers.js to interact with the contract, passing in the same parameters as the trader (optionally adjusted based on your own capital ratio).

### ✅ Step 6: Add Advanced Features

1. Dashboard:
   * Manage the list of trader wallets being monitored.
   * Display stats such as number of copied trades, total PnL, and remaining capital.
2. Telegram/Discord Notifications:
   * Alert when a new trade is opened.
   * Notify when the bot successfully copies a trade.
3. Auto Capital Scaling:
   * Example: If the trader uses 10,000 USDC to open a Long position, the bot can replicate it with 1,000 USDC (10%).

### ✅ Sample System Architecture

```
+-------------------+
|     User Wallet   |
+-------------------+
        ↑
        |
        | Copy
        ↓
+-------------------+     Monitor      +----------------------+
|     BOT Engine    | <----------------|   Trader Wallet(s)   |
|  (Python/Node.js) |                  |     on GMX.io        |
+--------+----------+                  +----------------------+
         |
         | Trigger
         ↓
+--------------------------+
|     PositionRouter SC    |
| (createIncreasePosition) |
+--------------------------+
```

### ✅ Resources Used

* GMX Contracts: <https://github.com/gmx-io/gmx-contracts>
* ABI of PositionRouter: Can be obtained from Arbiscan
* GMX Documentation: <https://gmxio.gitbook.io/gmx/>
