You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`payAmount` — Pay this much collateral. Alternative: use `sizeAmount` to specify position size
181
184
-`fromAmount` / `toAmount` — For swaps, specify input or desired output amount
182
185
186
+
### Step 3: Close a position
187
+
188
+
There is no convenience `close()` method. Closing requires computing decrease amounts via `getDecreasePositionAmounts()` from `@gmx-io/sdk/utils/trade`, then calling `createDecreaseOrder()`.
189
+
190
+
> **Important:** Always re-fetch `marketsInfoData` and `tokensData` right before closing. These contain oracle prices that go stale within seconds — using old data produces an `acceptablePrice` the keeper will reject.
**Low-level methods** — require you to pre-compute amounts, provide full market/token objects, and handle execution fees. Required for closing positions (no convenience `close()` method exists):
276
+
277
+
| Method | Purpose | Required Setup |
278
+
|--------|---------|---------------|
279
+
|`sdk.orders.createIncreaseOrder()`| Open position (full control) |`IncreasePositionAmounts`, `marketInfo`, `tokensData`|
280
+
|`sdk.orders.createDecreaseOrder()`| Close/reduce position |`DecreasePositionAmounts` via `getDecreasePositionAmounts()`|
> **Key gap:** There is no `sdk.orders.close()`. To close a position, use `getDecreasePositionAmounts()` from `@gmx-io/sdk/utils/trade` + `createDecreaseOrder()`. See [Step 3: Close a position](#step-3-close-a-position) for the full pattern.
220
284
221
285
## Order Types
222
286
@@ -283,24 +347,7 @@ Base URL: `https://{network}-api.gmxinfra.io`
283
347
|`/markets`| GET | Market configuration (index/long/short tokens) |
284
348
|`/markets/info`| GET | Extended market info with pool sizes and utilization |
285
349
286
-
### OpenAPI Endpoints
287
-
288
-
Base URL: `https://gmx-api-{network}.gmx.io/api/v1`
289
-
290
-
| Endpoint | Method | Description |
291
-
|----------|--------|-------------|
292
-
|`/markets`| GET | Market metadata |
293
-
|`/tickers`| GET | Current prices per market |
294
-
|`/tokens`| GET | Token information |
295
-
|`/positions?account={addr}`| GET | Open positions for an account |
296
-
|`/orders?account={addr}`| GET | Pending orders for an account |
297
-
|`/rates`| GET | Funding and borrowing rates |
298
-
|`/apy`| GET | Pool APY data |
299
-
|`/performance?account={addr}`| GET | Account trading performance |
300
-
301
-
Swagger spec: `{base_url}/swagger.json`
302
-
303
-
Network slugs: `arbitrum`, `avalanche`, `botanix`
350
+
All endpoints are served from the Oracle base URL above. The legacy `gmx-api-{network}.gmx.io` domain is no longer available.
304
351
305
352
### GraphQL (Subsquid)
306
353
@@ -312,14 +359,15 @@ Example — fetch recent trade actions:
312
359
query {
313
360
tradeActions(
314
361
where: { account_eq: "0x..." }
315
-
orderBy:transaction_timestamp_DESC
362
+
orderBy:timestamp_DESC
316
363
limit: 10
317
364
) {
318
365
id
319
366
eventName
320
367
orderType
321
368
sizeDeltaUsd
322
-
transaction { timestamp, hash }
369
+
timestamp
370
+
transactionHash
323
371
}
324
372
}
325
373
```
@@ -340,12 +388,114 @@ query {
340
388
341
389
**BigInt amounts:** All amounts use BigInt. Prices are scaled to 30 decimals (`1 USD = 10^30`). Token amounts use their native decimals (e.g., USDC = 6, ETH = 18).
342
390
391
+
**Stale data:**`marketsInfoData` and `tokensData` contain oracle prices at fetch time. These go stale within seconds. Always re-fetch fresh data before operations that depend on current prices — especially `createDecreaseOrder()`, which computes `acceptablePrice` from the data you provide. Using stale prices causes keeper rejection.
392
+
343
393
**Multicall batching:** The SDK batches RPC calls automatically. Production chains use `batchSize: 1024 * 1024` bytes per multicall with no waiting. This is configured per-chain in `BATCH_CONFIGS`.
344
394
345
395
**GMX Account:** Cross-chain trading from Ethereum, Base, or BNB Chain via LayerZero/Stargate bridge. Users can trade on Arbitrum/Avalanche without bridging manually.
346
396
347
397
**Subaccounts:** Delegate trading to a subaccount address for one-click trading. The subaccount can execute orders without requiring the main wallet signature each time.
348
398
399
+
## Full Example: Open → Monitor → Close
400
+
401
+
End-to-end flow that opens a long position, monitors it, and closes it.
0 commit comments