@catalyst-team/poly-sdk 0.2.0 → 0.3.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 (242) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +548 -813
  3. package/README.zh-CN.md +805 -0
  4. package/dist/__tests__/clob-api.test.d.ts +5 -0
  5. package/dist/__tests__/clob-api.test.d.ts.map +1 -0
  6. package/dist/__tests__/clob-api.test.js +240 -0
  7. package/dist/__tests__/clob-api.test.js.map +1 -0
  8. package/dist/__tests__/integration/arbitrage-service.integration.test.d.ts +12 -0
  9. package/dist/__tests__/integration/arbitrage-service.integration.test.d.ts.map +1 -0
  10. package/dist/__tests__/integration/arbitrage-service.integration.test.js +267 -0
  11. package/dist/__tests__/integration/arbitrage-service.integration.test.js.map +1 -0
  12. package/dist/__tests__/integration/bridge-client.integration.test.d.ts +11 -0
  13. package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +1 -0
  14. package/dist/__tests__/integration/bridge-client.integration.test.js +260 -0
  15. package/dist/__tests__/integration/bridge-client.integration.test.js.map +1 -0
  16. package/dist/__tests__/integration/clob-api.integration.test.d.ts +13 -0
  17. package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +1 -0
  18. package/dist/__tests__/integration/clob-api.integration.test.js +170 -0
  19. package/dist/__tests__/integration/clob-api.integration.test.js.map +1 -0
  20. package/dist/__tests__/integration/ctf-client.integration.test.d.ts +17 -0
  21. package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +1 -0
  22. package/dist/__tests__/integration/ctf-client.integration.test.js +234 -0
  23. package/dist/__tests__/integration/ctf-client.integration.test.js.map +1 -0
  24. package/dist/__tests__/integration/data-api.integration.test.d.ts +9 -0
  25. package/dist/__tests__/integration/data-api.integration.test.d.ts.map +1 -0
  26. package/dist/__tests__/integration/data-api.integration.test.js +164 -0
  27. package/dist/__tests__/integration/data-api.integration.test.js.map +1 -0
  28. package/dist/__tests__/integration/gamma-api.integration.test.d.ts +9 -0
  29. package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +1 -0
  30. package/dist/__tests__/integration/gamma-api.integration.test.js +170 -0
  31. package/dist/__tests__/integration/gamma-api.integration.test.js.map +1 -0
  32. package/dist/__tests__/integration/market-service.integration.test.d.ts +10 -0
  33. package/dist/__tests__/integration/market-service.integration.test.d.ts.map +1 -0
  34. package/dist/__tests__/integration/market-service.integration.test.js +173 -0
  35. package/dist/__tests__/integration/market-service.integration.test.js.map +1 -0
  36. package/dist/__tests__/integration/realtime-service-v2.integration.test.d.ts +10 -0
  37. package/dist/__tests__/integration/realtime-service-v2.integration.test.d.ts.map +1 -0
  38. package/dist/__tests__/integration/realtime-service-v2.integration.test.js +307 -0
  39. package/dist/__tests__/integration/realtime-service-v2.integration.test.js.map +1 -0
  40. package/dist/__tests__/integration/trading-service.integration.test.d.ts +10 -0
  41. package/dist/__tests__/integration/trading-service.integration.test.d.ts.map +1 -0
  42. package/dist/__tests__/integration/trading-service.integration.test.js +58 -0
  43. package/dist/__tests__/integration/trading-service.integration.test.js.map +1 -0
  44. package/dist/__tests__/test-utils.d.ts +92 -0
  45. package/dist/__tests__/test-utils.d.ts.map +1 -0
  46. package/dist/__tests__/test-utils.js +143 -0
  47. package/dist/__tests__/test-utils.js.map +1 -0
  48. package/dist/clients/bridge-client.d.ts +388 -0
  49. package/dist/clients/bridge-client.d.ts.map +1 -0
  50. package/dist/clients/bridge-client.js +587 -0
  51. package/dist/clients/bridge-client.js.map +1 -0
  52. package/dist/clients/clob-api.d.ts +391 -0
  53. package/dist/clients/clob-api.d.ts.map +1 -0
  54. package/dist/clients/clob-api.js +448 -0
  55. package/dist/clients/clob-api.js.map +1 -0
  56. package/dist/clients/ctf-client.d.ts +473 -0
  57. package/dist/clients/ctf-client.d.ts.map +1 -0
  58. package/dist/clients/ctf-client.js +915 -0
  59. package/dist/clients/ctf-client.js.map +1 -0
  60. package/dist/clients/data-api.d.ts +439 -0
  61. package/dist/clients/data-api.d.ts.map +1 -0
  62. package/dist/clients/data-api.js +592 -0
  63. package/dist/clients/data-api.js.map +1 -0
  64. package/dist/clients/gamma-api.d.ts +401 -0
  65. package/dist/clients/gamma-api.d.ts.map +1 -0
  66. package/dist/clients/gamma-api.js +352 -0
  67. package/dist/clients/gamma-api.js.map +1 -0
  68. package/dist/clients/subgraph.d.ts +196 -0
  69. package/dist/clients/subgraph.d.ts.map +1 -0
  70. package/dist/clients/subgraph.js +332 -0
  71. package/dist/clients/subgraph.js.map +1 -0
  72. package/dist/clients/trading-client.d.ts +252 -0
  73. package/dist/clients/trading-client.d.ts.map +1 -0
  74. package/dist/clients/trading-client.js +543 -0
  75. package/dist/clients/trading-client.js.map +1 -0
  76. package/dist/clients/websocket-manager.d.ts +103 -0
  77. package/dist/clients/websocket-manager.d.ts.map +1 -0
  78. package/dist/clients/websocket-manager.js +200 -0
  79. package/dist/clients/websocket-manager.js.map +1 -0
  80. package/dist/core/cache-adapter-bridge.d.ts +36 -0
  81. package/dist/core/cache-adapter-bridge.d.ts.map +1 -0
  82. package/dist/core/cache-adapter-bridge.js +81 -0
  83. package/dist/core/cache-adapter-bridge.js.map +1 -0
  84. package/dist/core/cache.d.ts +41 -0
  85. package/dist/core/cache.d.ts.map +1 -0
  86. package/dist/core/cache.js +72 -0
  87. package/dist/core/cache.js.map +1 -0
  88. package/dist/core/errors.d.ts +39 -0
  89. package/dist/core/errors.d.ts.map +1 -0
  90. package/dist/core/errors.js +86 -0
  91. package/dist/core/errors.js.map +1 -0
  92. package/dist/core/rate-limiter.d.ts +32 -0
  93. package/dist/core/rate-limiter.d.ts.map +1 -0
  94. package/dist/core/rate-limiter.js +75 -0
  95. package/dist/core/rate-limiter.js.map +1 -0
  96. package/dist/core/types.d.ts +402 -0
  97. package/dist/core/types.d.ts.map +1 -0
  98. package/dist/core/types.js +19 -0
  99. package/dist/core/types.js.map +1 -0
  100. package/dist/core/types.test.d.ts +7 -0
  101. package/dist/core/types.test.d.ts.map +1 -0
  102. package/dist/core/types.test.js +122 -0
  103. package/dist/core/types.test.js.map +1 -0
  104. package/dist/core/unified-cache.d.ts +63 -0
  105. package/dist/core/unified-cache.d.ts.map +1 -0
  106. package/dist/core/unified-cache.js +114 -0
  107. package/dist/core/unified-cache.js.map +1 -0
  108. package/dist/index.d.ts +151 -0
  109. package/dist/index.d.ts.map +1 -0
  110. package/dist/index.js +248 -0
  111. package/dist/index.js.map +1 -0
  112. package/dist/services/arbitrage-service.d.ts +409 -0
  113. package/dist/services/arbitrage-service.d.ts.map +1 -0
  114. package/dist/services/arbitrage-service.js +1440 -0
  115. package/dist/services/arbitrage-service.js.map +1 -0
  116. package/dist/services/authorization-service.d.ts +97 -0
  117. package/dist/services/authorization-service.d.ts.map +1 -0
  118. package/dist/services/authorization-service.js +279 -0
  119. package/dist/services/authorization-service.js.map +1 -0
  120. package/dist/services/market-service.d.ts +208 -0
  121. package/dist/services/market-service.d.ts.map +1 -0
  122. package/dist/services/market-service.js +774 -0
  123. package/dist/services/market-service.js.map +1 -0
  124. package/dist/services/onchain-service.d.ts +309 -0
  125. package/dist/services/onchain-service.d.ts.map +1 -0
  126. package/dist/services/onchain-service.js +417 -0
  127. package/dist/services/onchain-service.js.map +1 -0
  128. package/dist/services/realtime-service-v2.d.ts +361 -0
  129. package/dist/services/realtime-service-v2.d.ts.map +1 -0
  130. package/dist/services/realtime-service-v2.js +840 -0
  131. package/dist/services/realtime-service-v2.js.map +1 -0
  132. package/dist/services/realtime-service.d.ts +82 -0
  133. package/dist/services/realtime-service.d.ts.map +1 -0
  134. package/dist/services/realtime-service.js +182 -0
  135. package/dist/services/realtime-service.js.map +1 -0
  136. package/dist/services/smart-money-service.d.ts +196 -0
  137. package/dist/services/smart-money-service.d.ts.map +1 -0
  138. package/dist/services/smart-money-service.js +358 -0
  139. package/dist/services/smart-money-service.js.map +1 -0
  140. package/dist/services/swap-service.d.ts +217 -0
  141. package/dist/services/swap-service.d.ts.map +1 -0
  142. package/dist/services/swap-service.js +695 -0
  143. package/dist/services/swap-service.js.map +1 -0
  144. package/dist/services/trading-service.d.ts +156 -0
  145. package/dist/services/trading-service.d.ts.map +1 -0
  146. package/dist/services/trading-service.js +356 -0
  147. package/dist/services/trading-service.js.map +1 -0
  148. package/dist/services/wallet-service.d.ts +275 -0
  149. package/dist/services/wallet-service.d.ts.map +1 -0
  150. package/dist/services/wallet-service.js +630 -0
  151. package/dist/services/wallet-service.js.map +1 -0
  152. package/dist/utils/price-utils.d.ts +153 -0
  153. package/dist/utils/price-utils.d.ts.map +1 -0
  154. package/dist/utils/price-utils.js +236 -0
  155. package/dist/utils/price-utils.js.map +1 -0
  156. package/dist/utils/price-utils.test.d.ts +5 -0
  157. package/dist/utils/price-utils.test.d.ts.map +1 -0
  158. package/dist/utils/price-utils.test.js +192 -0
  159. package/dist/utils/price-utils.test.js.map +1 -0
  160. package/package.json +10 -4
  161. package/README.en.md +0 -538
  162. package/docs/00-design.md +0 -760
  163. package/docs/02-API.md +0 -1148
  164. package/docs/arb/test-plan.md +0 -387
  165. package/docs/arb/test-results.md +0 -336
  166. package/docs/arbitrage.md +0 -754
  167. package/docs/reports/smart-money-analysis-2025-12-23-cn.md +0 -840
  168. package/examples/01-basic-usage.ts +0 -68
  169. package/examples/02-smart-money.ts +0 -95
  170. package/examples/03-market-analysis.ts +0 -108
  171. package/examples/04-kline-aggregation.ts +0 -158
  172. package/examples/05-follow-wallet-strategy.ts +0 -156
  173. package/examples/06-services-demo.ts +0 -124
  174. package/examples/07-realtime-websocket.ts +0 -117
  175. package/examples/08-trading-orders.ts +0 -278
  176. package/examples/09-rewards-tracking.ts +0 -187
  177. package/examples/10-ctf-operations.ts +0 -336
  178. package/examples/11-live-arbitrage-scan.ts +0 -221
  179. package/examples/12-trending-arb-monitor.ts +0 -406
  180. package/examples/13-arbitrage-service.ts +0 -211
  181. package/examples/README.md +0 -179
  182. package/scripts/README.md +0 -163
  183. package/scripts/approvals/approve-erc1155.ts +0 -129
  184. package/scripts/approvals/approve-neg-risk-erc1155.ts +0 -149
  185. package/scripts/approvals/approve-neg-risk.ts +0 -102
  186. package/scripts/approvals/check-all-allowances.ts +0 -150
  187. package/scripts/approvals/check-allowance.ts +0 -129
  188. package/scripts/approvals/check-ctf-approval.ts +0 -158
  189. package/scripts/arb/faze-bo3-arb.ts +0 -385
  190. package/scripts/arb/settle-position.ts +0 -190
  191. package/scripts/arb/token-rebalancer.ts +0 -420
  192. package/scripts/arb-tests/01-unit-tests.ts +0 -495
  193. package/scripts/arb-tests/02-integration-tests.ts +0 -412
  194. package/scripts/arb-tests/03-e2e-tests.ts +0 -503
  195. package/scripts/arb-tests/README.md +0 -109
  196. package/scripts/datas/001-report.md +0 -486
  197. package/scripts/datas/clone-modal-screenshot.png +0 -0
  198. package/scripts/deposit/deposit-native-usdc.ts +0 -179
  199. package/scripts/deposit/deposit-usdc.ts +0 -155
  200. package/scripts/deposit/swap-usdc-to-usdce.ts +0 -375
  201. package/scripts/research/research-markets.ts +0 -166
  202. package/scripts/trading/check-orders.ts +0 -50
  203. package/scripts/trading/sell-nvidia-positions.ts +0 -206
  204. package/scripts/trading/test-order.ts +0 -172
  205. package/scripts/verify/test-approve-trading.ts +0 -98
  206. package/scripts/verify/test-provider-fix.ts +0 -43
  207. package/scripts/verify/test-search-mcp.ts +0 -113
  208. package/scripts/verify/verify-all-apis.ts +0 -160
  209. package/scripts/wallet/check-wallet-balances.ts +0 -75
  210. package/scripts/wallet/test-wallet-operations.ts +0 -191
  211. package/scripts/wallet/verify-wallet-tools.ts +0 -124
  212. package/src/__tests__/clob-api.test.ts +0 -301
  213. package/src/__tests__/integration/bridge-client.integration.test.ts +0 -314
  214. package/src/__tests__/integration/clob-api.integration.test.ts +0 -218
  215. package/src/__tests__/integration/ctf-client.integration.test.ts +0 -331
  216. package/src/__tests__/integration/data-api.integration.test.ts +0 -194
  217. package/src/__tests__/integration/gamma-api.integration.test.ts +0 -206
  218. package/src/__tests__/test-utils.ts +0 -170
  219. package/src/clients/bridge-client.ts +0 -841
  220. package/src/clients/clob-api.ts +0 -629
  221. package/src/clients/ctf-client.ts +0 -1216
  222. package/src/clients/data-api.ts +0 -469
  223. package/src/clients/gamma-api.ts +0 -597
  224. package/src/clients/trading-client.ts +0 -749
  225. package/src/clients/websocket-manager.ts +0 -267
  226. package/src/core/cache-adapter-bridge.ts +0 -94
  227. package/src/core/cache.ts +0 -85
  228. package/src/core/errors.ts +0 -117
  229. package/src/core/rate-limiter.ts +0 -74
  230. package/src/core/types.ts +0 -360
  231. package/src/core/unified-cache.ts +0 -153
  232. package/src/index.ts +0 -461
  233. package/src/services/arbitrage-service.ts +0 -1807
  234. package/src/services/authorization-service.ts +0 -357
  235. package/src/services/market-service.ts +0 -544
  236. package/src/services/realtime-service.ts +0 -196
  237. package/src/services/swap-service.ts +0 -896
  238. package/src/services/wallet-service.ts +0 -259
  239. package/src/utils/price-utils.ts +0 -307
  240. package/tsconfig.json +0 -8
  241. package/vitest.config.ts +0 -19
  242. package/vitest.integration.config.ts +0 -18
@@ -1,387 +0,0 @@
1
- # ArbitrageService E2E Test Plan
2
-
3
- > **Status**: In Progress
4
- > **Date**: 2024-12-24
5
- > **Author**: Claude Code
6
-
7
- ## Overview
8
-
9
- This document outlines the comprehensive test plan for the ArbitrageService based on first principles understanding of the Polymarket arbitrage mechanism.
10
-
11
- ## First Principles: Understanding the Arbitrage Mechanism
12
-
13
- ### Core Concept: Mirror Orderbook
14
-
15
- Polymarket orderbooks have a critical property that's often misunderstood:
16
-
17
- ```
18
- Buy YES @ P = Sell NO @ (1-P)
19
- ```
20
-
21
- This means **the same order appears in both orderbooks**. Naive addition of prices leads to incorrect calculations.
22
-
23
- ### Effective Prices
24
-
25
- To correctly calculate arbitrage opportunities, we must use **effective prices**:
26
-
27
- ```typescript
28
- // Long Arb (Buy both + Merge)
29
- effectiveBuyYes = min(YES.ask, 1 - NO.bid)
30
- effectiveBuyNo = min(NO.ask, 1 - YES.bid)
31
- longCost = effectiveBuyYes + effectiveBuyNo
32
- longProfit = 1 - longCost // > 0 means arbitrage exists
33
-
34
- // Short Arb (Split + Sell both)
35
- effectiveSellYes = max(YES.bid, 1 - NO.ask)
36
- effectiveSellNo = max(NO.bid, 1 - YES.ask)
37
- shortRevenue = effectiveSellYes + effectiveSellNo
38
- shortProfit = shortRevenue - 1 // > 0 means arbitrage exists
39
- ```
40
-
41
- ### Why We Need the Rebalancer
42
-
43
- Arbitrage execution requires:
44
- - **Long Arb**: USDC to buy tokens
45
- - **Short Arb**: YES + NO tokens to sell
46
-
47
- The Rebalancer maintains the optimal ratio:
48
- - USDC < 20% → Merge tokens to recover USDC
49
- - USDC > 80% → Split USDC to create tokens
50
-
51
- ### Partial Fill Protection
52
-
53
- When buying both YES and NO in parallel, one order might succeed while the other fails:
54
- - `sizeSafetyFactor = 0.8` → Use only 80% of orderbook depth
55
- - `autoFixImbalance = true` → Auto-sell excess if imbalance occurs
56
- - `imbalanceThreshold = 5` → Trigger fix when YES-NO diff > $5
57
-
58
- ---
59
-
60
- ## Test Hierarchy
61
-
62
- ```
63
- ┌─────────────────────────────────────────────────────────────────────────────┐
64
- │ Level 1: Unit Tests (No Network) │
65
- ├─────────────────────────────────────────────────────────────────────────────┤
66
- │ • getEffectivePrices() - Correct calculation of effective prices │
67
- │ • checkArbitrage() - Correct arbitrage detection │
68
- │ • calculateRebalanceAction() - Rebalance strategy logic │
69
- ├─────────────────────────────────────────────────────────────────────────────┤
70
- │ Level 2: Integration Tests (Network, No Private Key) │
71
- ├─────────────────────────────────────────────────────────────────────────────┤
72
- │ • scanMarkets() - Market scanning and filtering │
73
- │ • WebSocket subscription - Real-time orderbook updates │
74
- │ • checkOpportunity() - Opportunity detection with real data │
75
- ├─────────────────────────────────────────────────────────────────────────────┤
76
- │ Level 3: E2E Tests (Real Market, With Private Key) │
77
- ├─────────────────────────────────────────────────────────────────────────────┤
78
- │ • Wallet connection and balance query │
79
- │ • CTF Split/Merge operations │
80
- │ • Order execution (small amounts) │
81
- │ • Rebalancer functionality │
82
- │ • clearPositions() smart clearing │
83
- └─────────────────────────────────────────────────────────────────────────────┘
84
- ```
85
-
86
- ---
87
-
88
- ## Test Cases
89
-
90
- ### Level 1: Unit Tests
91
-
92
- #### Test 1.1: Effective Price Calculation
93
- ```typescript
94
- // Input
95
- yesAsk = 0.52, yesBid = 0.48
96
- noAsk = 0.50, noBid = 0.46
97
-
98
- // Expected
99
- effectiveBuyYes = min(0.52, 1 - 0.46) = min(0.52, 0.54) = 0.52
100
- effectiveBuyNo = min(0.50, 1 - 0.48) = min(0.50, 0.52) = 0.50
101
- effectiveSellYes = max(0.48, 1 - 0.50) = max(0.48, 0.50) = 0.50
102
- effectiveSellNo = max(0.46, 1 - 0.52) = max(0.46, 0.48) = 0.48
103
- ```
104
-
105
- #### Test 1.2: Long Arbitrage Detection
106
- ```typescript
107
- // Scenario: Long arb exists
108
- yesAsk = 0.48, noAsk = 0.50, yesBid = 0.46, noBid = 0.48
109
- longCost = 0.48 + 0.50 = 0.98 < 1.00
110
- // Expected: longProfit = 0.02 (2%)
111
- ```
112
-
113
- #### Test 1.3: Short Arbitrage Detection
114
- ```typescript
115
- // Scenario: Short arb exists
116
- yesBid = 0.52, noBid = 0.50, yesAsk = 0.54, noAsk = 0.52
117
- shortRevenue = 0.52 + 0.50 = 1.02 > 1.00
118
- // Expected: shortProfit = 0.02 (2%)
119
- ```
120
-
121
- #### Test 1.4: No Arbitrage (Normal Market)
122
- ```typescript
123
- // Normal efficient market
124
- yesAsk = 0.52, yesBid = 0.48, noAsk = 0.52, noBid = 0.48
125
- longCost ≈ 1.02-1.04
126
- shortRevenue ≈ 0.96-0.98
127
- // Expected: No arbitrage opportunity
128
- ```
129
-
130
- #### Test 1.5: Rebalance Action Calculation
131
- ```typescript
132
- // Scenario: USDC too low
133
- usdc = 10, yesTokens = 80, noTokens = 80
134
- usdcRatio = 10 / (10 + 80) = 0.11 < 0.20
135
- // Expected: action = 'merge', reason = 'USDC 11% < 20% min'
136
-
137
- // Scenario: USDC too high
138
- usdc = 90, yesTokens = 10, noTokens = 10
139
- usdcRatio = 90 / (90 + 10) = 0.90 > 0.80
140
- // Expected: action = 'split', reason = 'USDC 90% > 80% max'
141
-
142
- // Scenario: Token imbalance
143
- yesTokens = 60, noTokens = 45
144
- imbalance = 15 > threshold(5)
145
- // Expected: action = 'sell_yes', priority = 100 (highest)
146
- ```
147
-
148
- ---
149
-
150
- ### Level 2: Integration Tests
151
-
152
- #### Test 2.1: Market Scanning
153
- ```typescript
154
- const service = new ArbitrageService({ enableLogging: true });
155
-
156
- const results = await service.scanMarkets({
157
- minVolume24h: 5000,
158
- limit: 20
159
- }, 0.005);
160
-
161
- // Verify:
162
- // - Returns array of ScanResult
163
- // - Each result has valid market config
164
- // - Effective prices are calculated correctly
165
- // - Score is computed based on profit * volume
166
- ```
167
-
168
- #### Test 2.2: WebSocket Monitoring
169
- ```typescript
170
- const service = new ArbitrageService({ enableLogging: true });
171
-
172
- // Find a market to monitor
173
- const results = await service.quickScan(0, 1);
174
- const market = results[0].market;
175
-
176
- // Start monitoring (no private key = no execution)
177
- await service.start(market);
178
-
179
- // Wait for orderbook updates
180
- await new Promise(resolve => setTimeout(resolve, 10000));
181
-
182
- // Verify:
183
- // - Orderbook state is populated
184
- // - Events are emitted
185
- const orderbook = service.getOrderbook();
186
- // Should have bids/asks for both YES and NO
187
- ```
188
-
189
- #### Test 2.3: Opportunity Detection
190
- ```typescript
191
- // After monitoring for some time:
192
- const opportunity = service.checkOpportunity();
193
-
194
- // If opportunity exists:
195
- // - type is 'long' or 'short'
196
- // - profitRate > 0
197
- // - recommendedSize respects config limits
198
- // - description is accurate
199
- ```
200
-
201
- ---
202
-
203
- ### Level 3: E2E Tests
204
-
205
- #### Test 3.1: Wallet Connection
206
- ```typescript
207
- const service = new ArbitrageService({
208
- privateKey: process.env.PRIVATE_KEY,
209
- enableLogging: true,
210
- });
211
-
212
- // Find a market
213
- const results = await service.quickScan(0, 1);
214
- await service.start(results[0].market);
215
-
216
- // Verify:
217
- const balance = service.getBalance();
218
- console.log(`USDC: ${balance.usdc}`);
219
- console.log(`YES: ${balance.yesTokens}`);
220
- console.log(`NO: ${balance.noTokens}`);
221
-
222
- // Expected: All balance fields are populated
223
- ```
224
-
225
- #### Test 3.2: CTF Split Operation
226
- ```typescript
227
- import { CTFClient } from '@catalyst-team/poly-sdk';
228
-
229
- const ctf = new CTFClient({
230
- privateKey: process.env.PRIVATE_KEY,
231
- });
232
-
233
- // Split a small amount
234
- const splitResult = await ctf.split(conditionId, '5'); // $5 USDC
235
-
236
- // Verify:
237
- // - txHash is returned
238
- // - Balance now has 5 YES + 5 NO tokens
239
- // - USDC decreased by $5
240
- ```
241
-
242
- #### Test 3.3: CTF Merge Operation
243
- ```typescript
244
- // After split, merge back
245
- const tokenIds = {
246
- yesTokenId: market.yesTokenId,
247
- noTokenId: market.noTokenId,
248
- };
249
-
250
- const mergeResult = await ctf.mergeByTokenIds(conditionId, tokenIds, '5');
251
-
252
- // Verify:
253
- // - txHash is returned
254
- // - YES and NO tokens decreased by 5
255
- // - USDC increased by $5
256
- ```
257
-
258
- #### Test 3.4: Order Execution
259
- ```typescript
260
- // Buy a small amount of YES tokens
261
- const buyResult = await tradingClient.createMarketOrder({
262
- tokenId: market.yesTokenId,
263
- side: 'BUY',
264
- amount: 5, // $5 USDC
265
- orderType: 'FOK',
266
- });
267
-
268
- // Verify:
269
- // - success is true
270
- // - orderId is returned
271
- // - Balance reflects the purchase
272
- ```
273
-
274
- #### Test 3.5: Rebalancer
275
- ```typescript
276
- const service = new ArbitrageService({
277
- privateKey: process.env.PRIVATE_KEY,
278
- enableRebalancer: true,
279
- minUsdcRatio: 0.2,
280
- maxUsdcRatio: 0.8,
281
- targetUsdcRatio: 0.5,
282
- });
283
-
284
- // Create imbalanced state (e.g., split all USDC)
285
- // Then let rebalancer run
286
-
287
- // Verify:
288
- // - Rebalance event is emitted
289
- // - Balance moves toward target ratio
290
- ```
291
-
292
- #### Test 3.6: Clear Positions
293
- ```typescript
294
- // After some operations, clear all positions
295
- const clearResult = await service.clearPositions(market, false);
296
-
297
- console.log('Plan:', clearResult.actions);
298
- console.log('Expected recovery:', clearResult.totalUsdcRecovered);
299
-
300
- // If executing:
301
- const executeResult = await service.clearPositions(market, true);
302
- // - Merged tokens are recovered as USDC
303
- // - Unpaired tokens are sold
304
- ```
305
-
306
- ---
307
-
308
- ## Test Environment
309
-
310
- ### Configuration
311
- ```typescript
312
- // .env file
313
- PRIVATE_KEY=0x... // Test wallet private key
314
-
315
- // Test parameters
316
- MIN_TRADE_SIZE = 5 // Minimum $5 for safety
317
- MAX_TRADE_SIZE = 20 // Maximum $20 for safety
318
- PROFIT_THRESHOLD = 0 // Monitor all opportunities
319
- ```
320
-
321
- ### Safety Measures
322
-
323
- 1. **Small amounts only**: All tests use $5-20 amounts
324
- 2. **Monitor mode first**: Verify detection before execution
325
- 3. **High profit threshold**: Production should use 0.5%+
326
- 4. **Dry run first**: Use `execute = false` before real execution
327
-
328
- ---
329
-
330
- ## Execution Plan
331
-
332
- ### Phase 1: Unit Tests (No Network)
333
- 1. Create test file: `scripts/arb-tests/01-unit-tests.ts`
334
- 2. Test price utilities
335
- 3. Test rebalance logic
336
- 4. Verify all calculations match expected values
337
-
338
- ### Phase 2: Integration Tests (Network, No Execution)
339
- 1. Create test file: `scripts/arb-tests/02-integration-tests.ts`
340
- 2. Test market scanning
341
- 3. Test WebSocket monitoring
342
- 4. Verify opportunity detection on real data
343
-
344
- ### Phase 3: E2E Tests (Real Execution)
345
- 1. Create test file: `scripts/arb-tests/03-e2e-tests.ts`
346
- 2. Test wallet connection
347
- 3. Test CTF operations with small amounts
348
- 4. Test order execution with small amounts
349
- 5. Test clearPositions
350
-
351
- ### Phase 4: Full Flow Test
352
- 1. Create test file: `scripts/arb-tests/04-full-flow.ts`
353
- 2. Scan → Select → Monitor → Execute → Clear
354
- 3. Generate report
355
-
356
- ---
357
-
358
- ## Success Criteria
359
-
360
- | Test Level | Criteria |
361
- |------------|----------|
362
- | Unit | All calculations match expected values |
363
- | Integration | Market scanning returns valid results |
364
- | Integration | WebSocket receives orderbook updates |
365
- | E2E | Wallet balance is correctly queried |
366
- | E2E | Split/Merge operations succeed |
367
- | E2E | Order execution works |
368
- | E2E | clearPositions recovers funds |
369
-
370
- ---
371
-
372
- ## Risk Mitigation
373
-
374
- 1. **Financial Risk**: Test with small amounts ($5-20 max)
375
- 2. **Execution Risk**: Always dry-run first
376
- 3. **Network Risk**: Handle errors gracefully
377
- 4. **Timing Risk**: Use FOK orders to avoid partial fills
378
-
379
- ---
380
-
381
- ## Next Steps
382
-
383
- 1. [x] Create test plan document
384
- 2. [ ] Implement unit tests
385
- 3. [ ] Implement integration tests
386
- 4. [ ] Implement E2E tests
387
- 5. [ ] Generate test report