@catalyst-team/poly-sdk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +538 -0
  3. package/README.md +357 -78
  4. package/docs/00-design.md +1 -1
  5. package/docs/02-API.md +21 -21
  6. package/docs/arb/test-plan.md +387 -0
  7. package/docs/arb/test-results.md +336 -0
  8. package/docs/arbitrage.md +754 -0
  9. package/docs/reports/smart-money-analysis-2025-12-23-cn.md +840 -0
  10. package/examples/13-arbitrage-service.ts +211 -0
  11. package/examples/README.md +1 -1
  12. package/package.json +19 -19
  13. package/scripts/arb/faze-bo3-arb.ts +385 -0
  14. package/scripts/arb/settle-position.ts +190 -0
  15. package/scripts/arb/token-rebalancer.ts +420 -0
  16. package/scripts/arb-tests/01-unit-tests.ts +495 -0
  17. package/scripts/arb-tests/02-integration-tests.ts +412 -0
  18. package/scripts/arb-tests/03-e2e-tests.ts +503 -0
  19. package/scripts/arb-tests/README.md +109 -0
  20. package/scripts/verify/verify-all-apis.ts +1 -1
  21. package/src/clients/clob-api.ts +1 -1
  22. package/src/clients/gamma-api.ts +1 -1
  23. package/src/core/cache-adapter-bridge.ts +1 -1
  24. package/src/core/types.ts +3 -3
  25. package/src/core/unified-cache.ts +1 -1
  26. package/src/index.ts +25 -19
  27. package/src/services/arbitrage-service.ts +1807 -0
  28. package/.env +0 -0
  29. package/dist/__tests__/clob-api.test.d.ts +0 -5
  30. package/dist/__tests__/clob-api.test.d.ts.map +0 -1
  31. package/dist/__tests__/clob-api.test.js +0 -240
  32. package/dist/__tests__/clob-api.test.js.map +0 -1
  33. package/dist/__tests__/integration/bridge-client.integration.test.d.ts +0 -11
  34. package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +0 -1
  35. package/dist/__tests__/integration/bridge-client.integration.test.js +0 -260
  36. package/dist/__tests__/integration/bridge-client.integration.test.js.map +0 -1
  37. package/dist/__tests__/integration/clob-api.integration.test.d.ts +0 -13
  38. package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +0 -1
  39. package/dist/__tests__/integration/clob-api.integration.test.js +0 -170
  40. package/dist/__tests__/integration/clob-api.integration.test.js.map +0 -1
  41. package/dist/__tests__/integration/ctf-client.integration.test.d.ts +0 -17
  42. package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +0 -1
  43. package/dist/__tests__/integration/ctf-client.integration.test.js +0 -234
  44. package/dist/__tests__/integration/ctf-client.integration.test.js.map +0 -1
  45. package/dist/__tests__/integration/data-api.integration.test.d.ts +0 -9
  46. package/dist/__tests__/integration/data-api.integration.test.d.ts.map +0 -1
  47. package/dist/__tests__/integration/data-api.integration.test.js +0 -161
  48. package/dist/__tests__/integration/data-api.integration.test.js.map +0 -1
  49. package/dist/__tests__/integration/gamma-api.integration.test.d.ts +0 -9
  50. package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +0 -1
  51. package/dist/__tests__/integration/gamma-api.integration.test.js +0 -170
  52. package/dist/__tests__/integration/gamma-api.integration.test.js.map +0 -1
  53. package/dist/__tests__/test-utils.d.ts +0 -92
  54. package/dist/__tests__/test-utils.d.ts.map +0 -1
  55. package/dist/__tests__/test-utils.js +0 -143
  56. package/dist/__tests__/test-utils.js.map +0 -1
  57. package/dist/clients/bridge-client.d.ts +0 -388
  58. package/dist/clients/bridge-client.d.ts.map +0 -1
  59. package/dist/clients/bridge-client.js +0 -587
  60. package/dist/clients/bridge-client.js.map +0 -1
  61. package/dist/clients/clob-api.d.ts +0 -318
  62. package/dist/clients/clob-api.d.ts.map +0 -1
  63. package/dist/clients/clob-api.js +0 -388
  64. package/dist/clients/clob-api.js.map +0 -1
  65. package/dist/clients/ctf-client.d.ts +0 -473
  66. package/dist/clients/ctf-client.d.ts.map +0 -1
  67. package/dist/clients/ctf-client.js +0 -915
  68. package/dist/clients/ctf-client.js.map +0 -1
  69. package/dist/clients/data-api.d.ts +0 -134
  70. package/dist/clients/data-api.d.ts.map +0 -1
  71. package/dist/clients/data-api.js +0 -265
  72. package/dist/clients/data-api.js.map +0 -1
  73. package/dist/clients/gamma-api.d.ts +0 -401
  74. package/dist/clients/gamma-api.d.ts.map +0 -1
  75. package/dist/clients/gamma-api.js +0 -352
  76. package/dist/clients/gamma-api.js.map +0 -1
  77. package/dist/clients/trading-client.d.ts +0 -252
  78. package/dist/clients/trading-client.d.ts.map +0 -1
  79. package/dist/clients/trading-client.js +0 -543
  80. package/dist/clients/trading-client.js.map +0 -1
  81. package/dist/clients/websocket-manager.d.ts +0 -100
  82. package/dist/clients/websocket-manager.d.ts.map +0 -1
  83. package/dist/clients/websocket-manager.js +0 -193
  84. package/dist/clients/websocket-manager.js.map +0 -1
  85. package/dist/core/cache-adapter-bridge.d.ts +0 -36
  86. package/dist/core/cache-adapter-bridge.d.ts.map +0 -1
  87. package/dist/core/cache-adapter-bridge.js +0 -81
  88. package/dist/core/cache-adapter-bridge.js.map +0 -1
  89. package/dist/core/cache.d.ts +0 -40
  90. package/dist/core/cache.d.ts.map +0 -1
  91. package/dist/core/cache.js +0 -71
  92. package/dist/core/cache.js.map +0 -1
  93. package/dist/core/errors.d.ts +0 -38
  94. package/dist/core/errors.d.ts.map +0 -1
  95. package/dist/core/errors.js +0 -84
  96. package/dist/core/errors.js.map +0 -1
  97. package/dist/core/rate-limiter.d.ts +0 -31
  98. package/dist/core/rate-limiter.d.ts.map +0 -1
  99. package/dist/core/rate-limiter.js +0 -70
  100. package/dist/core/rate-limiter.js.map +0 -1
  101. package/dist/core/types.d.ts +0 -314
  102. package/dist/core/types.d.ts.map +0 -1
  103. package/dist/core/types.js +0 -19
  104. package/dist/core/types.js.map +0 -1
  105. package/dist/core/unified-cache.d.ts +0 -63
  106. package/dist/core/unified-cache.d.ts.map +0 -1
  107. package/dist/core/unified-cache.js +0 -114
  108. package/dist/core/unified-cache.js.map +0 -1
  109. package/dist/index.d.ts +0 -94
  110. package/dist/index.d.ts.map +0 -1
  111. package/dist/index.js +0 -258
  112. package/dist/index.js.map +0 -1
  113. package/dist/mcp/errors.d.ts +0 -33
  114. package/dist/mcp/errors.d.ts.map +0 -1
  115. package/dist/mcp/errors.js +0 -86
  116. package/dist/mcp/errors.js.map +0 -1
  117. package/dist/mcp/index.d.ts +0 -62
  118. package/dist/mcp/index.d.ts.map +0 -1
  119. package/dist/mcp/index.js +0 -173
  120. package/dist/mcp/index.js.map +0 -1
  121. package/dist/mcp/server.d.ts +0 -17
  122. package/dist/mcp/server.d.ts.map +0 -1
  123. package/dist/mcp/server.js +0 -155
  124. package/dist/mcp/server.js.map +0 -1
  125. package/dist/mcp/tools/guide.d.ts +0 -12
  126. package/dist/mcp/tools/guide.d.ts.map +0 -1
  127. package/dist/mcp/tools/guide.js +0 -801
  128. package/dist/mcp/tools/guide.js.map +0 -1
  129. package/dist/mcp/tools/index.d.ts +0 -11
  130. package/dist/mcp/tools/index.d.ts.map +0 -1
  131. package/dist/mcp/tools/index.js +0 -27
  132. package/dist/mcp/tools/index.js.map +0 -1
  133. package/dist/mcp/tools/market.d.ts +0 -11
  134. package/dist/mcp/tools/market.d.ts.map +0 -1
  135. package/dist/mcp/tools/market.js +0 -314
  136. package/dist/mcp/tools/market.js.map +0 -1
  137. package/dist/mcp/tools/order.d.ts +0 -10
  138. package/dist/mcp/tools/order.d.ts.map +0 -1
  139. package/dist/mcp/tools/order.js +0 -258
  140. package/dist/mcp/tools/order.js.map +0 -1
  141. package/dist/mcp/tools/trade.d.ts +0 -38
  142. package/dist/mcp/tools/trade.d.ts.map +0 -1
  143. package/dist/mcp/tools/trade.js +0 -314
  144. package/dist/mcp/tools/trade.js.map +0 -1
  145. package/dist/mcp/tools/trader.d.ts +0 -11
  146. package/dist/mcp/tools/trader.d.ts.map +0 -1
  147. package/dist/mcp/tools/trader.js +0 -277
  148. package/dist/mcp/tools/trader.js.map +0 -1
  149. package/dist/mcp/tools/wallet.d.ts +0 -274
  150. package/dist/mcp/tools/wallet.d.ts.map +0 -1
  151. package/dist/mcp/tools/wallet.js +0 -579
  152. package/dist/mcp/tools/wallet.js.map +0 -1
  153. package/dist/mcp/types.d.ts +0 -413
  154. package/dist/mcp/types.d.ts.map +0 -1
  155. package/dist/mcp/types.js +0 -5
  156. package/dist/mcp/types.js.map +0 -1
  157. package/dist/services/authorization-service.d.ts +0 -97
  158. package/dist/services/authorization-service.d.ts.map +0 -1
  159. package/dist/services/authorization-service.js +0 -279
  160. package/dist/services/authorization-service.js.map +0 -1
  161. package/dist/services/market-service.d.ts +0 -108
  162. package/dist/services/market-service.d.ts.map +0 -1
  163. package/dist/services/market-service.js +0 -458
  164. package/dist/services/market-service.js.map +0 -1
  165. package/dist/services/realtime-service.d.ts +0 -82
  166. package/dist/services/realtime-service.d.ts.map +0 -1
  167. package/dist/services/realtime-service.js +0 -150
  168. package/dist/services/realtime-service.js.map +0 -1
  169. package/dist/services/swap-service.d.ts +0 -217
  170. package/dist/services/swap-service.d.ts.map +0 -1
  171. package/dist/services/swap-service.js +0 -695
  172. package/dist/services/swap-service.js.map +0 -1
  173. package/dist/services/wallet-service.d.ts +0 -94
  174. package/dist/services/wallet-service.d.ts.map +0 -1
  175. package/dist/services/wallet-service.js +0 -173
  176. package/dist/services/wallet-service.js.map +0 -1
  177. package/dist/utils/price-utils.d.ts +0 -153
  178. package/dist/utils/price-utils.d.ts.map +0 -1
  179. package/dist/utils/price-utils.js +0 -236
  180. package/dist/utils/price-utils.js.map +0 -1
  181. package/docs/01-mcp.md +0 -2041
  182. package/docs/e2e/01-trader-tools.md +0 -159
  183. package/docs/e2e/02-market-tools.md +0 -180
  184. package/docs/e2e/03-order-tools.md +0 -166
  185. package/docs/e2e/04-wallet-tools.md +0 -224
  186. package/docs/e2e/05-trading-tools.md +0 -327
  187. package/docs/e2e/06-integration-scenarios.md +0 -481
  188. package/docs/e2e/coordinator.md +0 -376
  189. package/scripts/truth.md +0 -440
  190. package/src/mcp/README.md +0 -380
  191. package/src/mcp/errors.ts +0 -124
  192. package/src/mcp/index.ts +0 -309
  193. package/src/mcp/server.ts +0 -183
  194. package/src/mcp/tools/guide.ts +0 -821
  195. package/src/mcp/tools/index.ts +0 -73
  196. package/src/mcp/tools/market.ts +0 -363
  197. package/src/mcp/tools/order.ts +0 -326
  198. package/src/mcp/tools/trade.ts +0 -417
  199. package/src/mcp/tools/trader.ts +0 -322
  200. package/src/mcp/tools/wallet.ts +0 -683
  201. package/src/mcp/types.ts +0 -472
package/src/mcp/README.md DELETED
@@ -1,380 +0,0 @@
1
- # Polymarket MCP Server
2
-
3
- A Model Context Protocol (MCP) server that exposes Polymarket functionality to AI agents.
4
-
5
- ## Quick Start
6
-
7
- ### Run with npx (after build)
8
-
9
- ```bash
10
- # Build the package
11
- pnpm -F @prediction-router/poly-sdk build
12
-
13
- # Run the MCP server
14
- pnpm -F @prediction-router/poly-sdk mcp
15
- ```
16
-
17
- ### Development mode
18
-
19
- ```bash
20
- pnpm -F @prediction-router/poly-sdk mcp:dev
21
- ```
22
-
23
- ## Claude Desktop Configuration
24
-
25
- Add to your Claude Desktop config (`~/Library/Application Support/Claude/claude_desktop_config.json`):
26
-
27
- ### Read-only mode (market data, orderbook, trader analysis)
28
-
29
- ```json
30
- {
31
- "mcpServers": {
32
- "polymarket": {
33
- "command": "node",
34
- "args": ["/path/to/PerdictionRouter/packages/poly-sdk/dist/mcp/server.js"]
35
- }
36
- }
37
- }
38
- ```
39
-
40
- ### Trading mode (with private key)
41
-
42
- ```json
43
- {
44
- "mcpServers": {
45
- "polymarket": {
46
- "command": "node",
47
- "args": ["/path/to/PerdictionRouter/packages/poly-sdk/dist/mcp/server.js"],
48
- "env": {
49
- "POLY_PRIVATE_KEY": "your-wallet-private-key"
50
- }
51
- }
52
- }
53
- }
54
- ```
55
-
56
- ### Development mode
57
-
58
- ```json
59
- {
60
- "mcpServers": {
61
- "polymarket": {
62
- "command": "npx",
63
- "args": ["tsx", "/path/to/PerdictionRouter/packages/poly-sdk/src/mcp/server.ts"],
64
- "env": {
65
- "POLY_PRIVATE_KEY": "your-wallet-private-key"
66
- }
67
- }
68
- }
69
- }
70
- ```
71
-
72
- ## Environment Variables
73
-
74
- | Variable | Description | Required |
75
- |----------|-------------|----------|
76
- | `POLY_PRIVATE_KEY` | Wallet private key (with or without 0x prefix) | For trading |
77
- | `POLY_CHAIN_ID` | Chain ID (137=mainnet, 80002=testnet) | Optional (default: 137) |
78
-
79
- **Note**: API credentials are automatically derived from the private key using Polymarket's `createOrDeriveApiKey()`. You only need to provide the wallet private key to enable trading.
80
-
81
- ## Available Tools
82
-
83
- ### Trader Tools
84
-
85
- | Tool | Description |
86
- |------|-------------|
87
- | `get_trader_positions` | Get current positions for a wallet address |
88
- | `get_trader_trades` | Get trade history for a wallet |
89
- | `get_trader_profile` | Get trader profile with PnL and stats |
90
- | `get_leaderboard` | Get top traders from leaderboard |
91
-
92
- ### Market Tools
93
-
94
- | Tool | Description |
95
- |------|-------------|
96
- | `get_market` | Get market details by slug or conditionId |
97
- | `search_markets` | Search markets by keyword |
98
- | `get_trending_markets` | Get trending markets by volume/liquidity |
99
- | `get_market_trades` | Get recent trades for a market |
100
-
101
- ### Order Tools
102
-
103
- | Tool | Description |
104
- |------|-------------|
105
- | `get_orderbook` | Get orderbook depth for a market outcome |
106
- | `get_best_prices` | Get best bid/ask prices for both outcomes |
107
- | `estimate_execution` | Estimate execution price and slippage |
108
-
109
- ### Trade Tools (requires API credentials)
110
-
111
- | Tool | Description |
112
- |------|-------------|
113
- | `place_limit_order` | Place a limit order |
114
- | `place_market_order` | Place a market order |
115
- | `cancel_order` | Cancel an existing order |
116
- | `get_my_orders` | Get your open orders |
117
-
118
- ### Wallet Tools (requires private key)
119
-
120
- | Tool | Description |
121
- |------|-------------|
122
- | `get_supported_deposit_assets` | Get all supported assets and chains for deposits |
123
- | `get_deposit_addresses` | Get deposit addresses (EVM, Solana, Bitcoin) |
124
- | `check_allowances` | Check ERC20 and ERC1155 allowances for trading |
125
- | `approve_trading` | Set up all required approvals for trading |
126
-
127
- ## Tool Examples
128
-
129
- ### Get Market
130
-
131
- ```json
132
- {
133
- "name": "get_market",
134
- "arguments": {
135
- "identifier": "will-bitcoin-hit-100k-in-2024"
136
- }
137
- }
138
- ```
139
-
140
- Response:
141
- ```json
142
- {
143
- "market": {
144
- "conditionId": "0x...",
145
- "question": "Will Bitcoin hit $100k in 2024?",
146
- "slug": "will-bitcoin-hit-100k-in-2024"
147
- },
148
- "prices": {
149
- "yes": 0.65,
150
- "no": 0.35
151
- },
152
- "stats": {
153
- "volume": 1500000,
154
- "liquidity": 250000
155
- }
156
- }
157
- ```
158
-
159
- ### Get Trader Positions
160
-
161
- ```json
162
- {
163
- "name": "get_trader_positions",
164
- "arguments": {
165
- "address": "0x1234..."
166
- }
167
- }
168
- ```
169
-
170
- Response:
171
- ```json
172
- {
173
- "trader": {
174
- "address": "0x1234...",
175
- "name": "TopTrader"
176
- },
177
- "positions": [
178
- {
179
- "market": "Will Bitcoin hit $100k?",
180
- "outcome": "Yes",
181
- "size": 1000,
182
- "avgPrice": 0.55,
183
- "currentPrice": 0.65,
184
- "pnl": 100,
185
- "pnlPercent": 18.18
186
- }
187
- ],
188
- "summary": {
189
- "totalPositions": 5,
190
- "totalValue": 5000,
191
- "unrealizedPnl": 500
192
- }
193
- }
194
- ```
195
-
196
- ### Get Orderbook
197
-
198
- ```json
199
- {
200
- "name": "get_orderbook",
201
- "arguments": {
202
- "conditionId": "0x...",
203
- "outcome": "Yes",
204
- "depth": 10
205
- }
206
- }
207
- ```
208
-
209
- Response:
210
- ```json
211
- {
212
- "market": {
213
- "conditionId": "0x...",
214
- "title": "Will Bitcoin hit $100k?"
215
- },
216
- "outcome": "Yes",
217
- "orderbook": {
218
- "bids": [
219
- { "price": 0.64, "size": 1000, "total": 640 },
220
- { "price": 0.63, "size": 2000, "total": 1900 }
221
- ],
222
- "asks": [
223
- { "price": 0.66, "size": 500, "total": 330 },
224
- { "price": 0.67, "size": 1500, "total": 1335 }
225
- ]
226
- },
227
- "summary": {
228
- "bestBid": 0.64,
229
- "bestAsk": 0.66,
230
- "spread": 0.02,
231
- "spreadPercent": 3.12
232
- }
233
- }
234
- ```
235
-
236
- ### Estimate Execution
237
-
238
- ```json
239
- {
240
- "name": "estimate_execution",
241
- "arguments": {
242
- "conditionId": "0x...",
243
- "outcome": "Yes",
244
- "side": "BUY",
245
- "amount": 1000
246
- }
247
- }
248
- ```
249
-
250
- Response:
251
- ```json
252
- {
253
- "market": "Will Bitcoin hit $100k?",
254
- "order": {
255
- "side": "BUY",
256
- "outcome": "Yes",
257
- "usdcAmount": 1000
258
- },
259
- "estimate": {
260
- "avgPrice": 0.658,
261
- "sharesReceived": 1519.76,
262
- "priceImpact": 0.012,
263
- "worstPrice": 0.67
264
- },
265
- "warning": null
266
- }
267
- ```
268
-
269
- ### Check Allowances
270
-
271
- ```json
272
- {
273
- "name": "check_allowances",
274
- "arguments": {}
275
- }
276
- ```
277
-
278
- Response:
279
- ```json
280
- {
281
- "wallet": "0x1234...",
282
- "erc20": {
283
- "usdc": {
284
- "balance": "1000.00",
285
- "allowances": [
286
- { "contract": "CTF Exchange", "approved": true, "allowance": "unlimited" },
287
- { "contract": "Neg Risk CTF Exchange", "approved": true, "allowance": "unlimited" }
288
- ]
289
- }
290
- },
291
- "erc1155": {
292
- "conditionalTokens": {
293
- "approvals": [
294
- { "contract": "CTF Exchange", "approved": true },
295
- { "contract": "Neg Risk CTF Exchange", "approved": true }
296
- ]
297
- }
298
- },
299
- "tradingReady": true,
300
- "issues": []
301
- }
302
- ```
303
-
304
- ### Get Deposit Addresses
305
-
306
- ```json
307
- {
308
- "name": "get_deposit_addresses",
309
- "arguments": {}
310
- }
311
- ```
312
-
313
- Response:
314
- ```json
315
- {
316
- "wallet": "0x1234...",
317
- "addresses": {
318
- "evm": "0xabc...",
319
- "solana": "ABC123...",
320
- "bitcoin": "bc1q..."
321
- },
322
- "evmChains": ["Ethereum", "Polygon", "Arbitrum", "Base", "Optimism"],
323
- "instructions": "Send assets to these addresses to deposit to your Polymarket account..."
324
- }
325
- ```
326
-
327
- ## Programmatic Usage
328
-
329
- You can also use the MCP tools programmatically:
330
-
331
- ```typescript
332
- import { PolymarketSDK, createMcpHandler } from '@prediction-router/poly-sdk';
333
-
334
- const sdk = new PolymarketSDK();
335
- const handler = createMcpHandler(sdk);
336
-
337
- // Call any tool
338
- const result = await handler('get_market', {
339
- identifier: 'btc-100k',
340
- });
341
-
342
- console.log(result);
343
- ```
344
-
345
- Or register with an MCP server instance:
346
-
347
- ```typescript
348
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
349
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
350
- import { PolymarketSDK, registerMcpTools } from '@prediction-router/poly-sdk';
351
-
352
- const server = new Server(
353
- { name: 'my-server', version: '1.0.0' },
354
- { capabilities: { tools: {} } }
355
- );
356
-
357
- const sdk = new PolymarketSDK();
358
- registerMcpTools(server, sdk);
359
-
360
- const transport = new StdioServerTransport();
361
- await server.connect(transport);
362
- ```
363
-
364
- ## Architecture
365
-
366
- ```
367
- src/mcp/
368
- ├── index.ts # Main exports and handler factory
369
- ├── server.ts # Standalone MCP server (CLI entry point)
370
- ├── types.ts # TypeScript type definitions
371
- ├── errors.ts # Error handling utilities
372
- ├── tools/
373
- │ ├── index.ts # Tool exports
374
- │ ├── trader.ts # Trader analysis tools
375
- │ ├── market.ts # Market discovery tools
376
- │ ├── order.ts # Orderbook analysis tools
377
- │ ├── trade.ts # Trading execution tools
378
- │ └── wallet.ts # Deposit and authorization tools
379
- └── README.md # This file
380
- ```
package/src/mcp/errors.ts DELETED
@@ -1,124 +0,0 @@
1
- /**
2
- * MCP Error Handling for Polymarket Tools
3
- */
4
-
5
- export enum ErrorCode {
6
- INVALID_ADDRESS = 'INVALID_ADDRESS',
7
- MARKET_NOT_FOUND = 'MARKET_NOT_FOUND',
8
- INSUFFICIENT_BALANCE = 'INSUFFICIENT_BALANCE',
9
- ORDER_REJECTED = 'ORDER_REJECTED',
10
- RATE_LIMITED = 'RATE_LIMITED',
11
- AUTH_REQUIRED = 'AUTH_REQUIRED',
12
- INVALID_INPUT = 'INVALID_INPUT',
13
- INTERNAL_ERROR = 'INTERNAL_ERROR',
14
- }
15
-
16
- export class McpToolError extends Error {
17
- constructor(
18
- public code: ErrorCode,
19
- message: string,
20
- public details?: Record<string, unknown>
21
- ) {
22
- super(message);
23
- this.name = 'McpToolError';
24
- }
25
-
26
- toResponse() {
27
- return {
28
- error: {
29
- code: this.code,
30
- message: this.message,
31
- details: this.details,
32
- },
33
- };
34
- }
35
- }
36
-
37
- export function validateAddress(address: string): void {
38
- if (!address || typeof address !== 'string') {
39
- throw new McpToolError(
40
- ErrorCode.INVALID_ADDRESS,
41
- 'Address is required'
42
- );
43
- }
44
- if (!address.startsWith('0x') || address.length !== 42) {
45
- throw new McpToolError(
46
- ErrorCode.INVALID_ADDRESS,
47
- 'Address must be a valid Ethereum address (0x...)',
48
- { address }
49
- );
50
- }
51
- }
52
-
53
- export function validateConditionId(conditionId: string): void {
54
- if (!conditionId || typeof conditionId !== 'string') {
55
- throw new McpToolError(
56
- ErrorCode.INVALID_INPUT,
57
- 'Condition ID is required'
58
- );
59
- }
60
- if (!conditionId.startsWith('0x')) {
61
- throw new McpToolError(
62
- ErrorCode.INVALID_INPUT,
63
- 'Condition ID must start with 0x',
64
- { conditionId }
65
- );
66
- }
67
- }
68
-
69
- export function validateOutcome(outcome: string): void {
70
- if (!['Yes', 'No'].includes(outcome)) {
71
- throw new McpToolError(
72
- ErrorCode.INVALID_INPUT,
73
- 'Outcome must be "Yes" or "No"',
74
- { outcome }
75
- );
76
- }
77
- }
78
-
79
- export function validateSide(side: string): void {
80
- if (!['BUY', 'SELL'].includes(side)) {
81
- throw new McpToolError(
82
- ErrorCode.INVALID_INPUT,
83
- 'Side must be "BUY" or "SELL"',
84
- { side }
85
- );
86
- }
87
- }
88
-
89
- export function validatePrice(price: number): void {
90
- if (typeof price !== 'number' || price < 0.001 || price > 0.999) {
91
- throw new McpToolError(
92
- ErrorCode.INVALID_INPUT,
93
- 'Price must be between 0.001 and 0.999',
94
- { price }
95
- );
96
- }
97
- }
98
-
99
- export function validatePositiveNumber(value: number, name: string): void {
100
- if (typeof value !== 'number' || value <= 0) {
101
- throw new McpToolError(
102
- ErrorCode.INVALID_INPUT,
103
- `${name} must be a positive number`,
104
- { [name]: value }
105
- );
106
- }
107
- }
108
-
109
- export function wrapError(err: unknown): McpToolError {
110
- if (err instanceof McpToolError) {
111
- return err;
112
- }
113
- if (err instanceof Error) {
114
- // Check for known error types
115
- if (err.message.includes('rate limit')) {
116
- return new McpToolError(ErrorCode.RATE_LIMITED, 'Rate limit exceeded');
117
- }
118
- if (err.message.includes('not found')) {
119
- return new McpToolError(ErrorCode.MARKET_NOT_FOUND, err.message);
120
- }
121
- return new McpToolError(ErrorCode.INTERNAL_ERROR, err.message);
122
- }
123
- return new McpToolError(ErrorCode.INTERNAL_ERROR, 'An unexpected error occurred');
124
- }