@catalyst-team/poly-sdk 0.2.0 → 0.2.1

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 (197) hide show
  1. package/LICENSE +1 -1
  2. package/README.en.md +8 -44
  3. package/README.md +5 -3
  4. package/README.zh-CN.md +502 -0
  5. package/dist/__tests__/clob-api.test.d.ts +5 -0
  6. package/dist/__tests__/clob-api.test.d.ts.map +1 -0
  7. package/dist/__tests__/clob-api.test.js +240 -0
  8. package/dist/__tests__/clob-api.test.js.map +1 -0
  9. package/dist/__tests__/integration/bridge-client.integration.test.d.ts +11 -0
  10. package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +1 -0
  11. package/dist/__tests__/integration/bridge-client.integration.test.js +260 -0
  12. package/dist/__tests__/integration/bridge-client.integration.test.js.map +1 -0
  13. package/dist/__tests__/integration/clob-api.integration.test.d.ts +13 -0
  14. package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +1 -0
  15. package/dist/__tests__/integration/clob-api.integration.test.js +170 -0
  16. package/dist/__tests__/integration/clob-api.integration.test.js.map +1 -0
  17. package/dist/__tests__/integration/ctf-client.integration.test.d.ts +17 -0
  18. package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +1 -0
  19. package/dist/__tests__/integration/ctf-client.integration.test.js +234 -0
  20. package/dist/__tests__/integration/ctf-client.integration.test.js.map +1 -0
  21. package/dist/__tests__/integration/data-api.integration.test.d.ts +9 -0
  22. package/dist/__tests__/integration/data-api.integration.test.d.ts.map +1 -0
  23. package/dist/__tests__/integration/data-api.integration.test.js +161 -0
  24. package/dist/__tests__/integration/data-api.integration.test.js.map +1 -0
  25. package/dist/__tests__/integration/gamma-api.integration.test.d.ts +9 -0
  26. package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +1 -0
  27. package/dist/__tests__/integration/gamma-api.integration.test.js +170 -0
  28. package/dist/__tests__/integration/gamma-api.integration.test.js.map +1 -0
  29. package/dist/__tests__/test-utils.d.ts +92 -0
  30. package/dist/__tests__/test-utils.d.ts.map +1 -0
  31. package/dist/__tests__/test-utils.js +143 -0
  32. package/dist/__tests__/test-utils.js.map +1 -0
  33. package/dist/clients/bridge-client.d.ts +388 -0
  34. package/dist/clients/bridge-client.d.ts.map +1 -0
  35. package/dist/clients/bridge-client.js +587 -0
  36. package/dist/clients/bridge-client.js.map +1 -0
  37. package/dist/clients/clob-api.d.ts +318 -0
  38. package/dist/clients/clob-api.d.ts.map +1 -0
  39. package/dist/clients/clob-api.js +388 -0
  40. package/dist/clients/clob-api.js.map +1 -0
  41. package/dist/clients/ctf-client.d.ts +473 -0
  42. package/dist/clients/ctf-client.d.ts.map +1 -0
  43. package/dist/clients/ctf-client.js +915 -0
  44. package/dist/clients/ctf-client.js.map +1 -0
  45. package/dist/clients/data-api.d.ts +134 -0
  46. package/dist/clients/data-api.d.ts.map +1 -0
  47. package/dist/clients/data-api.js +265 -0
  48. package/dist/clients/data-api.js.map +1 -0
  49. package/dist/clients/gamma-api.d.ts +401 -0
  50. package/dist/clients/gamma-api.d.ts.map +1 -0
  51. package/dist/clients/gamma-api.js +352 -0
  52. package/dist/clients/gamma-api.js.map +1 -0
  53. package/dist/clients/trading-client.d.ts +252 -0
  54. package/dist/clients/trading-client.d.ts.map +1 -0
  55. package/dist/clients/trading-client.js +543 -0
  56. package/dist/clients/trading-client.js.map +1 -0
  57. package/dist/clients/websocket-manager.d.ts +100 -0
  58. package/dist/clients/websocket-manager.d.ts.map +1 -0
  59. package/dist/clients/websocket-manager.js +193 -0
  60. package/dist/clients/websocket-manager.js.map +1 -0
  61. package/dist/core/cache-adapter-bridge.d.ts +36 -0
  62. package/dist/core/cache-adapter-bridge.d.ts.map +1 -0
  63. package/dist/core/cache-adapter-bridge.js +81 -0
  64. package/dist/core/cache-adapter-bridge.js.map +1 -0
  65. package/dist/core/cache.d.ts +40 -0
  66. package/dist/core/cache.d.ts.map +1 -0
  67. package/dist/core/cache.js +71 -0
  68. package/dist/core/cache.js.map +1 -0
  69. package/dist/core/errors.d.ts +38 -0
  70. package/dist/core/errors.d.ts.map +1 -0
  71. package/dist/core/errors.js +84 -0
  72. package/dist/core/errors.js.map +1 -0
  73. package/dist/core/rate-limiter.d.ts +31 -0
  74. package/dist/core/rate-limiter.d.ts.map +1 -0
  75. package/dist/core/rate-limiter.js +70 -0
  76. package/dist/core/rate-limiter.js.map +1 -0
  77. package/{src/core/types.ts → dist/core/types.d.ts} +169 -215
  78. package/dist/core/types.d.ts.map +1 -0
  79. package/dist/core/types.js +19 -0
  80. package/dist/core/types.js.map +1 -0
  81. package/dist/core/unified-cache.d.ts +63 -0
  82. package/dist/core/unified-cache.d.ts.map +1 -0
  83. package/dist/core/unified-cache.js +114 -0
  84. package/dist/core/unified-cache.js.map +1 -0
  85. package/dist/index.d.ts +93 -0
  86. package/dist/index.d.ts.map +1 -0
  87. package/dist/index.js +255 -0
  88. package/dist/index.js.map +1 -0
  89. package/dist/services/arbitrage-service.d.ts +408 -0
  90. package/dist/services/arbitrage-service.d.ts.map +1 -0
  91. package/dist/services/arbitrage-service.js +1422 -0
  92. package/dist/services/arbitrage-service.js.map +1 -0
  93. package/dist/services/authorization-service.d.ts +97 -0
  94. package/dist/services/authorization-service.d.ts.map +1 -0
  95. package/dist/services/authorization-service.js +279 -0
  96. package/dist/services/authorization-service.js.map +1 -0
  97. package/dist/services/market-service.d.ts +108 -0
  98. package/dist/services/market-service.d.ts.map +1 -0
  99. package/dist/services/market-service.js +458 -0
  100. package/dist/services/market-service.js.map +1 -0
  101. package/dist/services/realtime-service.d.ts +82 -0
  102. package/dist/services/realtime-service.d.ts.map +1 -0
  103. package/dist/services/realtime-service.js +150 -0
  104. package/dist/services/realtime-service.js.map +1 -0
  105. package/dist/services/swap-service.d.ts +217 -0
  106. package/dist/services/swap-service.d.ts.map +1 -0
  107. package/dist/services/swap-service.js +695 -0
  108. package/dist/services/swap-service.js.map +1 -0
  109. package/dist/services/wallet-service.d.ts +94 -0
  110. package/dist/services/wallet-service.d.ts.map +1 -0
  111. package/dist/services/wallet-service.js +173 -0
  112. package/dist/services/wallet-service.js.map +1 -0
  113. package/dist/utils/price-utils.d.ts +153 -0
  114. package/dist/utils/price-utils.d.ts.map +1 -0
  115. package/dist/utils/price-utils.js +236 -0
  116. package/dist/utils/price-utils.js.map +1 -0
  117. package/package.json +7 -2
  118. package/docs/00-design.md +0 -760
  119. package/docs/02-API.md +0 -1148
  120. package/docs/arb/test-plan.md +0 -387
  121. package/docs/arb/test-results.md +0 -336
  122. package/docs/arbitrage.md +0 -754
  123. package/docs/reports/smart-money-analysis-2025-12-23-cn.md +0 -840
  124. package/examples/01-basic-usage.ts +0 -68
  125. package/examples/02-smart-money.ts +0 -95
  126. package/examples/03-market-analysis.ts +0 -108
  127. package/examples/04-kline-aggregation.ts +0 -158
  128. package/examples/05-follow-wallet-strategy.ts +0 -156
  129. package/examples/06-services-demo.ts +0 -124
  130. package/examples/07-realtime-websocket.ts +0 -117
  131. package/examples/08-trading-orders.ts +0 -278
  132. package/examples/09-rewards-tracking.ts +0 -187
  133. package/examples/10-ctf-operations.ts +0 -336
  134. package/examples/11-live-arbitrage-scan.ts +0 -221
  135. package/examples/12-trending-arb-monitor.ts +0 -406
  136. package/examples/13-arbitrage-service.ts +0 -211
  137. package/examples/README.md +0 -179
  138. package/scripts/README.md +0 -163
  139. package/scripts/approvals/approve-erc1155.ts +0 -129
  140. package/scripts/approvals/approve-neg-risk-erc1155.ts +0 -149
  141. package/scripts/approvals/approve-neg-risk.ts +0 -102
  142. package/scripts/approvals/check-all-allowances.ts +0 -150
  143. package/scripts/approvals/check-allowance.ts +0 -129
  144. package/scripts/approvals/check-ctf-approval.ts +0 -158
  145. package/scripts/arb/faze-bo3-arb.ts +0 -385
  146. package/scripts/arb/settle-position.ts +0 -190
  147. package/scripts/arb/token-rebalancer.ts +0 -420
  148. package/scripts/arb-tests/01-unit-tests.ts +0 -495
  149. package/scripts/arb-tests/02-integration-tests.ts +0 -412
  150. package/scripts/arb-tests/03-e2e-tests.ts +0 -503
  151. package/scripts/arb-tests/README.md +0 -109
  152. package/scripts/datas/001-report.md +0 -486
  153. package/scripts/datas/clone-modal-screenshot.png +0 -0
  154. package/scripts/deposit/deposit-native-usdc.ts +0 -179
  155. package/scripts/deposit/deposit-usdc.ts +0 -155
  156. package/scripts/deposit/swap-usdc-to-usdce.ts +0 -375
  157. package/scripts/research/research-markets.ts +0 -166
  158. package/scripts/trading/check-orders.ts +0 -50
  159. package/scripts/trading/sell-nvidia-positions.ts +0 -206
  160. package/scripts/trading/test-order.ts +0 -172
  161. package/scripts/verify/test-approve-trading.ts +0 -98
  162. package/scripts/verify/test-provider-fix.ts +0 -43
  163. package/scripts/verify/test-search-mcp.ts +0 -113
  164. package/scripts/verify/verify-all-apis.ts +0 -160
  165. package/scripts/wallet/check-wallet-balances.ts +0 -75
  166. package/scripts/wallet/test-wallet-operations.ts +0 -191
  167. package/scripts/wallet/verify-wallet-tools.ts +0 -124
  168. package/src/__tests__/clob-api.test.ts +0 -301
  169. package/src/__tests__/integration/bridge-client.integration.test.ts +0 -314
  170. package/src/__tests__/integration/clob-api.integration.test.ts +0 -218
  171. package/src/__tests__/integration/ctf-client.integration.test.ts +0 -331
  172. package/src/__tests__/integration/data-api.integration.test.ts +0 -194
  173. package/src/__tests__/integration/gamma-api.integration.test.ts +0 -206
  174. package/src/__tests__/test-utils.ts +0 -170
  175. package/src/clients/bridge-client.ts +0 -841
  176. package/src/clients/clob-api.ts +0 -629
  177. package/src/clients/ctf-client.ts +0 -1216
  178. package/src/clients/data-api.ts +0 -469
  179. package/src/clients/gamma-api.ts +0 -597
  180. package/src/clients/trading-client.ts +0 -749
  181. package/src/clients/websocket-manager.ts +0 -267
  182. package/src/core/cache-adapter-bridge.ts +0 -94
  183. package/src/core/cache.ts +0 -85
  184. package/src/core/errors.ts +0 -117
  185. package/src/core/rate-limiter.ts +0 -74
  186. package/src/core/unified-cache.ts +0 -153
  187. package/src/index.ts +0 -461
  188. package/src/services/arbitrage-service.ts +0 -1807
  189. package/src/services/authorization-service.ts +0 -357
  190. package/src/services/market-service.ts +0 -544
  191. package/src/services/realtime-service.ts +0 -196
  192. package/src/services/swap-service.ts +0 -896
  193. package/src/services/wallet-service.ts +0 -259
  194. package/src/utils/price-utils.ts +0 -307
  195. package/tsconfig.json +0 -8
  196. package/vitest.config.ts +0 -19
  197. 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