@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.
- package/LICENSE +1 -1
- package/README.en.md +8 -44
- package/README.md +5 -3
- package/README.zh-CN.md +502 -0
- package/dist/__tests__/clob-api.test.d.ts +5 -0
- package/dist/__tests__/clob-api.test.d.ts.map +1 -0
- package/dist/__tests__/clob-api.test.js +240 -0
- package/dist/__tests__/clob-api.test.js.map +1 -0
- package/dist/__tests__/integration/bridge-client.integration.test.d.ts +11 -0
- package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration/bridge-client.integration.test.js +260 -0
- package/dist/__tests__/integration/bridge-client.integration.test.js.map +1 -0
- package/dist/__tests__/integration/clob-api.integration.test.d.ts +13 -0
- package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration/clob-api.integration.test.js +170 -0
- package/dist/__tests__/integration/clob-api.integration.test.js.map +1 -0
- package/dist/__tests__/integration/ctf-client.integration.test.d.ts +17 -0
- package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration/ctf-client.integration.test.js +234 -0
- package/dist/__tests__/integration/ctf-client.integration.test.js.map +1 -0
- package/dist/__tests__/integration/data-api.integration.test.d.ts +9 -0
- package/dist/__tests__/integration/data-api.integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration/data-api.integration.test.js +161 -0
- package/dist/__tests__/integration/data-api.integration.test.js.map +1 -0
- package/dist/__tests__/integration/gamma-api.integration.test.d.ts +9 -0
- package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration/gamma-api.integration.test.js +170 -0
- package/dist/__tests__/integration/gamma-api.integration.test.js.map +1 -0
- package/dist/__tests__/test-utils.d.ts +92 -0
- package/dist/__tests__/test-utils.d.ts.map +1 -0
- package/dist/__tests__/test-utils.js +143 -0
- package/dist/__tests__/test-utils.js.map +1 -0
- package/dist/clients/bridge-client.d.ts +388 -0
- package/dist/clients/bridge-client.d.ts.map +1 -0
- package/dist/clients/bridge-client.js +587 -0
- package/dist/clients/bridge-client.js.map +1 -0
- package/dist/clients/clob-api.d.ts +318 -0
- package/dist/clients/clob-api.d.ts.map +1 -0
- package/dist/clients/clob-api.js +388 -0
- package/dist/clients/clob-api.js.map +1 -0
- package/dist/clients/ctf-client.d.ts +473 -0
- package/dist/clients/ctf-client.d.ts.map +1 -0
- package/dist/clients/ctf-client.js +915 -0
- package/dist/clients/ctf-client.js.map +1 -0
- package/dist/clients/data-api.d.ts +134 -0
- package/dist/clients/data-api.d.ts.map +1 -0
- package/dist/clients/data-api.js +265 -0
- package/dist/clients/data-api.js.map +1 -0
- package/dist/clients/gamma-api.d.ts +401 -0
- package/dist/clients/gamma-api.d.ts.map +1 -0
- package/dist/clients/gamma-api.js +352 -0
- package/dist/clients/gamma-api.js.map +1 -0
- package/dist/clients/trading-client.d.ts +252 -0
- package/dist/clients/trading-client.d.ts.map +1 -0
- package/dist/clients/trading-client.js +543 -0
- package/dist/clients/trading-client.js.map +1 -0
- package/dist/clients/websocket-manager.d.ts +100 -0
- package/dist/clients/websocket-manager.d.ts.map +1 -0
- package/dist/clients/websocket-manager.js +193 -0
- package/dist/clients/websocket-manager.js.map +1 -0
- package/dist/core/cache-adapter-bridge.d.ts +36 -0
- package/dist/core/cache-adapter-bridge.d.ts.map +1 -0
- package/dist/core/cache-adapter-bridge.js +81 -0
- package/dist/core/cache-adapter-bridge.js.map +1 -0
- package/dist/core/cache.d.ts +40 -0
- package/dist/core/cache.d.ts.map +1 -0
- package/dist/core/cache.js +71 -0
- package/dist/core/cache.js.map +1 -0
- package/dist/core/errors.d.ts +38 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +84 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/rate-limiter.d.ts +31 -0
- package/dist/core/rate-limiter.d.ts.map +1 -0
- package/dist/core/rate-limiter.js +70 -0
- package/dist/core/rate-limiter.js.map +1 -0
- package/{src/core/types.ts → dist/core/types.d.ts} +169 -215
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +19 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/unified-cache.d.ts +63 -0
- package/dist/core/unified-cache.d.ts.map +1 -0
- package/dist/core/unified-cache.js +114 -0
- package/dist/core/unified-cache.js.map +1 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +255 -0
- package/dist/index.js.map +1 -0
- package/dist/services/arbitrage-service.d.ts +408 -0
- package/dist/services/arbitrage-service.d.ts.map +1 -0
- package/dist/services/arbitrage-service.js +1422 -0
- package/dist/services/arbitrage-service.js.map +1 -0
- package/dist/services/authorization-service.d.ts +97 -0
- package/dist/services/authorization-service.d.ts.map +1 -0
- package/dist/services/authorization-service.js +279 -0
- package/dist/services/authorization-service.js.map +1 -0
- package/dist/services/market-service.d.ts +108 -0
- package/dist/services/market-service.d.ts.map +1 -0
- package/dist/services/market-service.js +458 -0
- package/dist/services/market-service.js.map +1 -0
- package/dist/services/realtime-service.d.ts +82 -0
- package/dist/services/realtime-service.d.ts.map +1 -0
- package/dist/services/realtime-service.js +150 -0
- package/dist/services/realtime-service.js.map +1 -0
- package/dist/services/swap-service.d.ts +217 -0
- package/dist/services/swap-service.d.ts.map +1 -0
- package/dist/services/swap-service.js +695 -0
- package/dist/services/swap-service.js.map +1 -0
- package/dist/services/wallet-service.d.ts +94 -0
- package/dist/services/wallet-service.d.ts.map +1 -0
- package/dist/services/wallet-service.js +173 -0
- package/dist/services/wallet-service.js.map +1 -0
- package/dist/utils/price-utils.d.ts +153 -0
- package/dist/utils/price-utils.d.ts.map +1 -0
- package/dist/utils/price-utils.js +236 -0
- package/dist/utils/price-utils.js.map +1 -0
- package/package.json +7 -2
- package/docs/00-design.md +0 -760
- package/docs/02-API.md +0 -1148
- package/docs/arb/test-plan.md +0 -387
- package/docs/arb/test-results.md +0 -336
- package/docs/arbitrage.md +0 -754
- package/docs/reports/smart-money-analysis-2025-12-23-cn.md +0 -840
- package/examples/01-basic-usage.ts +0 -68
- package/examples/02-smart-money.ts +0 -95
- package/examples/03-market-analysis.ts +0 -108
- package/examples/04-kline-aggregation.ts +0 -158
- package/examples/05-follow-wallet-strategy.ts +0 -156
- package/examples/06-services-demo.ts +0 -124
- package/examples/07-realtime-websocket.ts +0 -117
- package/examples/08-trading-orders.ts +0 -278
- package/examples/09-rewards-tracking.ts +0 -187
- package/examples/10-ctf-operations.ts +0 -336
- package/examples/11-live-arbitrage-scan.ts +0 -221
- package/examples/12-trending-arb-monitor.ts +0 -406
- package/examples/13-arbitrage-service.ts +0 -211
- package/examples/README.md +0 -179
- package/scripts/README.md +0 -163
- package/scripts/approvals/approve-erc1155.ts +0 -129
- package/scripts/approvals/approve-neg-risk-erc1155.ts +0 -149
- package/scripts/approvals/approve-neg-risk.ts +0 -102
- package/scripts/approvals/check-all-allowances.ts +0 -150
- package/scripts/approvals/check-allowance.ts +0 -129
- package/scripts/approvals/check-ctf-approval.ts +0 -158
- package/scripts/arb/faze-bo3-arb.ts +0 -385
- package/scripts/arb/settle-position.ts +0 -190
- package/scripts/arb/token-rebalancer.ts +0 -420
- package/scripts/arb-tests/01-unit-tests.ts +0 -495
- package/scripts/arb-tests/02-integration-tests.ts +0 -412
- package/scripts/arb-tests/03-e2e-tests.ts +0 -503
- package/scripts/arb-tests/README.md +0 -109
- package/scripts/datas/001-report.md +0 -486
- package/scripts/datas/clone-modal-screenshot.png +0 -0
- package/scripts/deposit/deposit-native-usdc.ts +0 -179
- package/scripts/deposit/deposit-usdc.ts +0 -155
- package/scripts/deposit/swap-usdc-to-usdce.ts +0 -375
- package/scripts/research/research-markets.ts +0 -166
- package/scripts/trading/check-orders.ts +0 -50
- package/scripts/trading/sell-nvidia-positions.ts +0 -206
- package/scripts/trading/test-order.ts +0 -172
- package/scripts/verify/test-approve-trading.ts +0 -98
- package/scripts/verify/test-provider-fix.ts +0 -43
- package/scripts/verify/test-search-mcp.ts +0 -113
- package/scripts/verify/verify-all-apis.ts +0 -160
- package/scripts/wallet/check-wallet-balances.ts +0 -75
- package/scripts/wallet/test-wallet-operations.ts +0 -191
- package/scripts/wallet/verify-wallet-tools.ts +0 -124
- package/src/__tests__/clob-api.test.ts +0 -301
- package/src/__tests__/integration/bridge-client.integration.test.ts +0 -314
- package/src/__tests__/integration/clob-api.integration.test.ts +0 -218
- package/src/__tests__/integration/ctf-client.integration.test.ts +0 -331
- package/src/__tests__/integration/data-api.integration.test.ts +0 -194
- package/src/__tests__/integration/gamma-api.integration.test.ts +0 -206
- package/src/__tests__/test-utils.ts +0 -170
- package/src/clients/bridge-client.ts +0 -841
- package/src/clients/clob-api.ts +0 -629
- package/src/clients/ctf-client.ts +0 -1216
- package/src/clients/data-api.ts +0 -469
- package/src/clients/gamma-api.ts +0 -597
- package/src/clients/trading-client.ts +0 -749
- package/src/clients/websocket-manager.ts +0 -267
- package/src/core/cache-adapter-bridge.ts +0 -94
- package/src/core/cache.ts +0 -85
- package/src/core/errors.ts +0 -117
- package/src/core/rate-limiter.ts +0 -74
- package/src/core/unified-cache.ts +0 -153
- package/src/index.ts +0 -461
- package/src/services/arbitrage-service.ts +0 -1807
- package/src/services/authorization-service.ts +0 -357
- package/src/services/market-service.ts +0 -544
- package/src/services/realtime-service.ts +0 -196
- package/src/services/swap-service.ts +0 -896
- package/src/services/wallet-service.ts +0 -259
- package/src/utils/price-utils.ts +0 -307
- package/tsconfig.json +0 -8
- package/vitest.config.ts +0 -19
- package/vitest.integration.config.ts +0 -18
package/docs/arb/test-plan.md
DELETED
|
@@ -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
|