@catalyst-team/poly-sdk 0.1.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 (244) hide show
  1. package/.env +0 -0
  2. package/README.md +803 -0
  3. package/dist/__tests__/clob-api.test.d.ts +5 -0
  4. package/dist/__tests__/clob-api.test.d.ts.map +1 -0
  5. package/dist/__tests__/clob-api.test.js +240 -0
  6. package/dist/__tests__/clob-api.test.js.map +1 -0
  7. package/dist/__tests__/integration/bridge-client.integration.test.d.ts +11 -0
  8. package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +1 -0
  9. package/dist/__tests__/integration/bridge-client.integration.test.js +260 -0
  10. package/dist/__tests__/integration/bridge-client.integration.test.js.map +1 -0
  11. package/dist/__tests__/integration/clob-api.integration.test.d.ts +13 -0
  12. package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +1 -0
  13. package/dist/__tests__/integration/clob-api.integration.test.js +170 -0
  14. package/dist/__tests__/integration/clob-api.integration.test.js.map +1 -0
  15. package/dist/__tests__/integration/ctf-client.integration.test.d.ts +17 -0
  16. package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +1 -0
  17. package/dist/__tests__/integration/ctf-client.integration.test.js +234 -0
  18. package/dist/__tests__/integration/ctf-client.integration.test.js.map +1 -0
  19. package/dist/__tests__/integration/data-api.integration.test.d.ts +9 -0
  20. package/dist/__tests__/integration/data-api.integration.test.d.ts.map +1 -0
  21. package/dist/__tests__/integration/data-api.integration.test.js +161 -0
  22. package/dist/__tests__/integration/data-api.integration.test.js.map +1 -0
  23. package/dist/__tests__/integration/gamma-api.integration.test.d.ts +9 -0
  24. package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +1 -0
  25. package/dist/__tests__/integration/gamma-api.integration.test.js +170 -0
  26. package/dist/__tests__/integration/gamma-api.integration.test.js.map +1 -0
  27. package/dist/__tests__/test-utils.d.ts +92 -0
  28. package/dist/__tests__/test-utils.d.ts.map +1 -0
  29. package/dist/__tests__/test-utils.js +143 -0
  30. package/dist/__tests__/test-utils.js.map +1 -0
  31. package/dist/clients/bridge-client.d.ts +388 -0
  32. package/dist/clients/bridge-client.d.ts.map +1 -0
  33. package/dist/clients/bridge-client.js +587 -0
  34. package/dist/clients/bridge-client.js.map +1 -0
  35. package/dist/clients/clob-api.d.ts +318 -0
  36. package/dist/clients/clob-api.d.ts.map +1 -0
  37. package/dist/clients/clob-api.js +388 -0
  38. package/dist/clients/clob-api.js.map +1 -0
  39. package/dist/clients/ctf-client.d.ts +473 -0
  40. package/dist/clients/ctf-client.d.ts.map +1 -0
  41. package/dist/clients/ctf-client.js +915 -0
  42. package/dist/clients/ctf-client.js.map +1 -0
  43. package/dist/clients/data-api.d.ts +134 -0
  44. package/dist/clients/data-api.d.ts.map +1 -0
  45. package/dist/clients/data-api.js +265 -0
  46. package/dist/clients/data-api.js.map +1 -0
  47. package/dist/clients/gamma-api.d.ts +401 -0
  48. package/dist/clients/gamma-api.d.ts.map +1 -0
  49. package/dist/clients/gamma-api.js +352 -0
  50. package/dist/clients/gamma-api.js.map +1 -0
  51. package/dist/clients/trading-client.d.ts +252 -0
  52. package/dist/clients/trading-client.d.ts.map +1 -0
  53. package/dist/clients/trading-client.js +543 -0
  54. package/dist/clients/trading-client.js.map +1 -0
  55. package/dist/clients/websocket-manager.d.ts +100 -0
  56. package/dist/clients/websocket-manager.d.ts.map +1 -0
  57. package/dist/clients/websocket-manager.js +193 -0
  58. package/dist/clients/websocket-manager.js.map +1 -0
  59. package/dist/core/cache-adapter-bridge.d.ts +36 -0
  60. package/dist/core/cache-adapter-bridge.d.ts.map +1 -0
  61. package/dist/core/cache-adapter-bridge.js +81 -0
  62. package/dist/core/cache-adapter-bridge.js.map +1 -0
  63. package/dist/core/cache.d.ts +40 -0
  64. package/dist/core/cache.d.ts.map +1 -0
  65. package/dist/core/cache.js +71 -0
  66. package/dist/core/cache.js.map +1 -0
  67. package/dist/core/errors.d.ts +38 -0
  68. package/dist/core/errors.d.ts.map +1 -0
  69. package/dist/core/errors.js +84 -0
  70. package/dist/core/errors.js.map +1 -0
  71. package/dist/core/rate-limiter.d.ts +31 -0
  72. package/dist/core/rate-limiter.d.ts.map +1 -0
  73. package/dist/core/rate-limiter.js +70 -0
  74. package/dist/core/rate-limiter.js.map +1 -0
  75. package/dist/core/types.d.ts +314 -0
  76. package/dist/core/types.d.ts.map +1 -0
  77. package/dist/core/types.js +19 -0
  78. package/dist/core/types.js.map +1 -0
  79. package/dist/core/unified-cache.d.ts +63 -0
  80. package/dist/core/unified-cache.d.ts.map +1 -0
  81. package/dist/core/unified-cache.js +114 -0
  82. package/dist/core/unified-cache.js.map +1 -0
  83. package/dist/index.d.ts +94 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +258 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/mcp/errors.d.ts +33 -0
  88. package/dist/mcp/errors.d.ts.map +1 -0
  89. package/dist/mcp/errors.js +86 -0
  90. package/dist/mcp/errors.js.map +1 -0
  91. package/dist/mcp/index.d.ts +62 -0
  92. package/dist/mcp/index.d.ts.map +1 -0
  93. package/dist/mcp/index.js +173 -0
  94. package/dist/mcp/index.js.map +1 -0
  95. package/dist/mcp/server.d.ts +17 -0
  96. package/dist/mcp/server.d.ts.map +1 -0
  97. package/dist/mcp/server.js +155 -0
  98. package/dist/mcp/server.js.map +1 -0
  99. package/dist/mcp/tools/guide.d.ts +12 -0
  100. package/dist/mcp/tools/guide.d.ts.map +1 -0
  101. package/dist/mcp/tools/guide.js +801 -0
  102. package/dist/mcp/tools/guide.js.map +1 -0
  103. package/dist/mcp/tools/index.d.ts +11 -0
  104. package/dist/mcp/tools/index.d.ts.map +1 -0
  105. package/dist/mcp/tools/index.js +27 -0
  106. package/dist/mcp/tools/index.js.map +1 -0
  107. package/dist/mcp/tools/market.d.ts +11 -0
  108. package/dist/mcp/tools/market.d.ts.map +1 -0
  109. package/dist/mcp/tools/market.js +314 -0
  110. package/dist/mcp/tools/market.js.map +1 -0
  111. package/dist/mcp/tools/order.d.ts +10 -0
  112. package/dist/mcp/tools/order.d.ts.map +1 -0
  113. package/dist/mcp/tools/order.js +258 -0
  114. package/dist/mcp/tools/order.js.map +1 -0
  115. package/dist/mcp/tools/trade.d.ts +38 -0
  116. package/dist/mcp/tools/trade.d.ts.map +1 -0
  117. package/dist/mcp/tools/trade.js +314 -0
  118. package/dist/mcp/tools/trade.js.map +1 -0
  119. package/dist/mcp/tools/trader.d.ts +11 -0
  120. package/dist/mcp/tools/trader.d.ts.map +1 -0
  121. package/dist/mcp/tools/trader.js +277 -0
  122. package/dist/mcp/tools/trader.js.map +1 -0
  123. package/dist/mcp/tools/wallet.d.ts +274 -0
  124. package/dist/mcp/tools/wallet.d.ts.map +1 -0
  125. package/dist/mcp/tools/wallet.js +579 -0
  126. package/dist/mcp/tools/wallet.js.map +1 -0
  127. package/dist/mcp/types.d.ts +413 -0
  128. package/dist/mcp/types.d.ts.map +1 -0
  129. package/dist/mcp/types.js +5 -0
  130. package/dist/mcp/types.js.map +1 -0
  131. package/dist/services/authorization-service.d.ts +97 -0
  132. package/dist/services/authorization-service.d.ts.map +1 -0
  133. package/dist/services/authorization-service.js +279 -0
  134. package/dist/services/authorization-service.js.map +1 -0
  135. package/dist/services/market-service.d.ts +108 -0
  136. package/dist/services/market-service.d.ts.map +1 -0
  137. package/dist/services/market-service.js +458 -0
  138. package/dist/services/market-service.js.map +1 -0
  139. package/dist/services/realtime-service.d.ts +82 -0
  140. package/dist/services/realtime-service.d.ts.map +1 -0
  141. package/dist/services/realtime-service.js +150 -0
  142. package/dist/services/realtime-service.js.map +1 -0
  143. package/dist/services/swap-service.d.ts +217 -0
  144. package/dist/services/swap-service.d.ts.map +1 -0
  145. package/dist/services/swap-service.js +695 -0
  146. package/dist/services/swap-service.js.map +1 -0
  147. package/dist/services/wallet-service.d.ts +94 -0
  148. package/dist/services/wallet-service.d.ts.map +1 -0
  149. package/dist/services/wallet-service.js +173 -0
  150. package/dist/services/wallet-service.js.map +1 -0
  151. package/dist/utils/price-utils.d.ts +153 -0
  152. package/dist/utils/price-utils.d.ts.map +1 -0
  153. package/dist/utils/price-utils.js +236 -0
  154. package/dist/utils/price-utils.js.map +1 -0
  155. package/docs/00-design.md +760 -0
  156. package/docs/01-mcp.md +2041 -0
  157. package/docs/02-API.md +1148 -0
  158. package/docs/e2e/01-trader-tools.md +159 -0
  159. package/docs/e2e/02-market-tools.md +180 -0
  160. package/docs/e2e/03-order-tools.md +166 -0
  161. package/docs/e2e/04-wallet-tools.md +224 -0
  162. package/docs/e2e/05-trading-tools.md +327 -0
  163. package/docs/e2e/06-integration-scenarios.md +481 -0
  164. package/docs/e2e/coordinator.md +376 -0
  165. package/examples/01-basic-usage.ts +68 -0
  166. package/examples/02-smart-money.ts +95 -0
  167. package/examples/03-market-analysis.ts +108 -0
  168. package/examples/04-kline-aggregation.ts +158 -0
  169. package/examples/05-follow-wallet-strategy.ts +156 -0
  170. package/examples/06-services-demo.ts +124 -0
  171. package/examples/07-realtime-websocket.ts +117 -0
  172. package/examples/08-trading-orders.ts +278 -0
  173. package/examples/09-rewards-tracking.ts +187 -0
  174. package/examples/10-ctf-operations.ts +336 -0
  175. package/examples/11-live-arbitrage-scan.ts +221 -0
  176. package/examples/12-trending-arb-monitor.ts +406 -0
  177. package/examples/README.md +179 -0
  178. package/package.json +62 -0
  179. package/scripts/README.md +163 -0
  180. package/scripts/approvals/approve-erc1155.ts +129 -0
  181. package/scripts/approvals/approve-neg-risk-erc1155.ts +149 -0
  182. package/scripts/approvals/approve-neg-risk.ts +102 -0
  183. package/scripts/approvals/check-all-allowances.ts +150 -0
  184. package/scripts/approvals/check-allowance.ts +129 -0
  185. package/scripts/approvals/check-ctf-approval.ts +158 -0
  186. package/scripts/datas/001-report.md +486 -0
  187. package/scripts/datas/clone-modal-screenshot.png +0 -0
  188. package/scripts/deposit/deposit-native-usdc.ts +179 -0
  189. package/scripts/deposit/deposit-usdc.ts +155 -0
  190. package/scripts/deposit/swap-usdc-to-usdce.ts +375 -0
  191. package/scripts/research/research-markets.ts +166 -0
  192. package/scripts/trading/check-orders.ts +50 -0
  193. package/scripts/trading/sell-nvidia-positions.ts +206 -0
  194. package/scripts/trading/test-order.ts +172 -0
  195. package/scripts/truth.md +440 -0
  196. package/scripts/verify/test-approve-trading.ts +98 -0
  197. package/scripts/verify/test-provider-fix.ts +43 -0
  198. package/scripts/verify/test-search-mcp.ts +113 -0
  199. package/scripts/verify/verify-all-apis.ts +160 -0
  200. package/scripts/wallet/check-wallet-balances.ts +75 -0
  201. package/scripts/wallet/test-wallet-operations.ts +191 -0
  202. package/scripts/wallet/verify-wallet-tools.ts +124 -0
  203. package/src/__tests__/clob-api.test.ts +301 -0
  204. package/src/__tests__/integration/bridge-client.integration.test.ts +314 -0
  205. package/src/__tests__/integration/clob-api.integration.test.ts +218 -0
  206. package/src/__tests__/integration/ctf-client.integration.test.ts +331 -0
  207. package/src/__tests__/integration/data-api.integration.test.ts +194 -0
  208. package/src/__tests__/integration/gamma-api.integration.test.ts +206 -0
  209. package/src/__tests__/test-utils.ts +170 -0
  210. package/src/clients/bridge-client.ts +841 -0
  211. package/src/clients/clob-api.ts +629 -0
  212. package/src/clients/ctf-client.ts +1216 -0
  213. package/src/clients/data-api.ts +469 -0
  214. package/src/clients/gamma-api.ts +597 -0
  215. package/src/clients/trading-client.ts +749 -0
  216. package/src/clients/websocket-manager.ts +267 -0
  217. package/src/core/cache-adapter-bridge.ts +94 -0
  218. package/src/core/cache.ts +85 -0
  219. package/src/core/errors.ts +117 -0
  220. package/src/core/rate-limiter.ts +74 -0
  221. package/src/core/types.ts +360 -0
  222. package/src/core/unified-cache.ts +153 -0
  223. package/src/index.ts +455 -0
  224. package/src/mcp/README.md +380 -0
  225. package/src/mcp/errors.ts +124 -0
  226. package/src/mcp/index.ts +309 -0
  227. package/src/mcp/server.ts +183 -0
  228. package/src/mcp/tools/guide.ts +821 -0
  229. package/src/mcp/tools/index.ts +73 -0
  230. package/src/mcp/tools/market.ts +363 -0
  231. package/src/mcp/tools/order.ts +326 -0
  232. package/src/mcp/tools/trade.ts +417 -0
  233. package/src/mcp/tools/trader.ts +322 -0
  234. package/src/mcp/tools/wallet.ts +683 -0
  235. package/src/mcp/types.ts +472 -0
  236. package/src/services/authorization-service.ts +357 -0
  237. package/src/services/market-service.ts +544 -0
  238. package/src/services/realtime-service.ts +196 -0
  239. package/src/services/swap-service.ts +896 -0
  240. package/src/services/wallet-service.ts +259 -0
  241. package/src/utils/price-utils.ts +307 -0
  242. package/tsconfig.json +8 -0
  243. package/vitest.config.ts +19 -0
  244. package/vitest.integration.config.ts +18 -0
@@ -0,0 +1,218 @@
1
+ /**
2
+ * CLOB API Client Integration Tests
3
+ *
4
+ * These tests make REAL API calls to Polymarket.
5
+ * They verify that:
6
+ * 1. API endpoints are reachable
7
+ * 2. Response structures match our types
8
+ * 3. Data normalization works correctly
9
+ *
10
+ * Run with: pnpm test:integration
11
+ */
12
+
13
+ import { describe, it, expect, beforeAll } from 'vitest';
14
+ import { ClobApiClient } from '../../clients/clob-api.js';
15
+ import { RateLimiter } from '../../core/rate-limiter.js';
16
+ import { createUnifiedCache } from '../../core/unified-cache.js';
17
+
18
+ describe('ClobApiClient Integration', () => {
19
+ let client: ClobApiClient;
20
+
21
+ beforeAll(() => {
22
+ client = new ClobApiClient(new RateLimiter(), createUnifiedCache());
23
+ });
24
+
25
+ describe('getMarket', () => {
26
+ it('should fetch a real market from Polymarket', async () => {
27
+ // Get trending markets first to find a valid condition ID
28
+ const response = await fetch(
29
+ 'https://gamma-api.polymarket.com/markets?active=true&limit=1&order=volume24hr&ascending=false'
30
+ );
31
+ const markets = await response.json() as Array<{ conditionId: string }>;
32
+
33
+ if (markets.length === 0) {
34
+ console.log('No active markets found, skipping test');
35
+ return;
36
+ }
37
+
38
+ const conditionId = markets[0].conditionId;
39
+ const market = await client.getMarket(conditionId);
40
+
41
+ // Verify structure
42
+ expect(market.conditionId).toBe(conditionId);
43
+ expect(typeof market.question).toBe('string');
44
+ expect(market.question.length).toBeGreaterThan(0);
45
+ expect(Array.isArray(market.tokens)).toBe(true);
46
+ expect(market.tokens.length).toBeGreaterThanOrEqual(2);
47
+
48
+ // Verify tokens
49
+ for (const token of market.tokens) {
50
+ expect(typeof token.tokenId).toBe('string');
51
+ expect(token.tokenId.length).toBeGreaterThan(0);
52
+ expect(['Yes', 'No']).toContain(token.outcome);
53
+ expect(typeof token.price).toBe('number');
54
+ expect(token.price).toBeGreaterThanOrEqual(0);
55
+ expect(token.price).toBeLessThanOrEqual(1);
56
+ }
57
+
58
+ // Verify other fields
59
+ expect(typeof market.active).toBe('boolean');
60
+ expect(typeof market.closed).toBe('boolean');
61
+ expect(typeof market.acceptingOrders).toBe('boolean');
62
+
63
+ console.log(`✓ Successfully fetched market: "${market.question.slice(0, 50)}..."`);
64
+ }, 30000);
65
+
66
+ it('should handle non-existent market gracefully', async () => {
67
+ await expect(
68
+ client.getMarket('0x0000000000000000000000000000000000000000000000000000000000000000')
69
+ ).rejects.toThrow();
70
+ }, 30000);
71
+ });
72
+
73
+ describe('getOrderbook', () => {
74
+ it('should fetch real orderbook data', async () => {
75
+ // First get a market to get token ID
76
+ const response = await fetch(
77
+ 'https://gamma-api.polymarket.com/markets?active=true&limit=1&order=volume24hr&ascending=false'
78
+ );
79
+ const markets = await response.json() as Array<{ conditionId: string }>;
80
+
81
+ if (markets.length === 0) {
82
+ console.log('No active markets found, skipping test');
83
+ return;
84
+ }
85
+
86
+ const market = await client.getMarket(markets[0].conditionId);
87
+ const yesToken = market.tokens.find(t => t.outcome === 'Yes');
88
+
89
+ if (!yesToken) {
90
+ console.log('No YES token found, skipping test');
91
+ return;
92
+ }
93
+
94
+ const orderbook = await client.getOrderbook(yesToken.tokenId);
95
+
96
+ // Verify structure
97
+ expect(Array.isArray(orderbook.bids)).toBe(true);
98
+ expect(Array.isArray(orderbook.asks)).toBe(true);
99
+ expect(typeof orderbook.timestamp).toBe('number');
100
+ expect(orderbook.timestamp).toBeGreaterThan(0);
101
+
102
+ // Verify bids are sorted descending (if any exist)
103
+ for (let i = 1; i < orderbook.bids.length; i++) {
104
+ expect(orderbook.bids[i].price).toBeLessThanOrEqual(orderbook.bids[i - 1].price);
105
+ }
106
+
107
+ // Verify asks are sorted ascending (if any exist)
108
+ for (let i = 1; i < orderbook.asks.length; i++) {
109
+ expect(orderbook.asks[i].price).toBeGreaterThanOrEqual(orderbook.asks[i - 1].price);
110
+ }
111
+
112
+ // Verify price/size types
113
+ if (orderbook.bids.length > 0) {
114
+ expect(typeof orderbook.bids[0].price).toBe('number');
115
+ expect(typeof orderbook.bids[0].size).toBe('number');
116
+ expect(orderbook.bids[0].price).toBeGreaterThan(0);
117
+ expect(orderbook.bids[0].price).toBeLessThan(1);
118
+ }
119
+
120
+ console.log(`✓ Orderbook: ${orderbook.bids.length} bids, ${orderbook.asks.length} asks`);
121
+ }, 30000);
122
+ });
123
+
124
+ describe('getProcessedOrderbook', () => {
125
+ it('should return complete processed orderbook with analytics', async () => {
126
+ // Get an active market
127
+ const response = await fetch(
128
+ 'https://gamma-api.polymarket.com/markets?active=true&limit=5&order=volume24hr&ascending=false'
129
+ );
130
+ const markets = await response.json() as Array<{ conditionId: string; question: string }>;
131
+
132
+ // Find a market with good liquidity
133
+ let processed = null;
134
+ for (const m of markets) {
135
+ try {
136
+ processed = await client.getProcessedOrderbook(m.conditionId);
137
+ if (processed.yes.bid > 0 && processed.yes.ask < 1) {
138
+ console.log(`✓ Using market: "${m.question.slice(0, 50)}..."`);
139
+ break;
140
+ }
141
+ } catch {
142
+ continue;
143
+ }
144
+ }
145
+
146
+ if (!processed) {
147
+ console.log('No liquid market found, skipping test');
148
+ return;
149
+ }
150
+
151
+ // Verify YES orderbook
152
+ expect(typeof processed.yes.bid).toBe('number');
153
+ expect(typeof processed.yes.ask).toBe('number');
154
+ expect(processed.yes.bid).toBeLessThanOrEqual(processed.yes.ask);
155
+
156
+ // Verify NO orderbook
157
+ expect(typeof processed.no.bid).toBe('number');
158
+ expect(typeof processed.no.ask).toBe('number');
159
+ expect(processed.no.bid).toBeLessThanOrEqual(processed.no.ask);
160
+
161
+ // Verify summary
162
+ expect(typeof processed.summary.effectiveLongCost).toBe('number');
163
+ expect(typeof processed.summary.effectiveShortRevenue).toBe('number');
164
+ expect(typeof processed.summary.longArbProfit).toBe('number');
165
+ expect(typeof processed.summary.shortArbProfit).toBe('number');
166
+
167
+ // Verify effective prices
168
+ expect(typeof processed.summary.effectivePrices.effectiveBuyYes).toBe('number');
169
+ expect(typeof processed.summary.effectivePrices.effectiveBuyNo).toBe('number');
170
+ expect(typeof processed.summary.effectivePrices.effectiveSellYes).toBe('number');
171
+ expect(typeof processed.summary.effectivePrices.effectiveSellNo).toBe('number');
172
+
173
+ // Log arbitrage info
174
+ console.log(` YES: bid=${processed.yes.bid.toFixed(3)}, ask=${processed.yes.ask.toFixed(3)}`);
175
+ console.log(` NO: bid=${processed.no.bid.toFixed(3)}, ask=${processed.no.ask.toFixed(3)}`);
176
+ console.log(` Long arb profit: ${(processed.summary.longArbProfit * 100).toFixed(3)}%`);
177
+ console.log(` Short arb profit: ${(processed.summary.shortArbProfit * 100).toFixed(3)}%`);
178
+ }, 60000);
179
+
180
+ it('should correctly calculate effective prices with mirroring', async () => {
181
+ const response = await fetch(
182
+ 'https://gamma-api.polymarket.com/markets?active=true&limit=1&order=volume24hr&ascending=false'
183
+ );
184
+ const markets = await response.json() as Array<{ conditionId: string }>;
185
+
186
+ if (markets.length === 0) return;
187
+
188
+ const processed = await client.getProcessedOrderbook(markets[0].conditionId);
189
+
190
+ // Verify mirroring logic:
191
+ // effectiveBuyYes should be <= YES.ask (can't be worse than direct buy)
192
+ expect(processed.summary.effectivePrices.effectiveBuyYes).toBeLessThanOrEqual(processed.yes.ask || 1);
193
+
194
+ // effectiveBuyNo should be <= NO.ask
195
+ expect(processed.summary.effectivePrices.effectiveBuyNo).toBeLessThanOrEqual(processed.no.ask || 1);
196
+
197
+ // effectiveSellYes should be >= YES.bid (can't be worse than direct sell)
198
+ expect(processed.summary.effectivePrices.effectiveSellYes).toBeGreaterThanOrEqual(processed.yes.bid);
199
+
200
+ // effectiveSellNo should be >= NO.bid
201
+ expect(processed.summary.effectivePrices.effectiveSellNo).toBeGreaterThanOrEqual(processed.no.bid);
202
+
203
+ // Long cost should equal sum of effective buy prices
204
+ const expectedLongCost =
205
+ processed.summary.effectivePrices.effectiveBuyYes +
206
+ processed.summary.effectivePrices.effectiveBuyNo;
207
+ expect(processed.summary.effectiveLongCost).toBeCloseTo(expectedLongCost, 6);
208
+
209
+ // Short revenue should equal sum of effective sell prices
210
+ const expectedShortRevenue =
211
+ processed.summary.effectivePrices.effectiveSellYes +
212
+ processed.summary.effectivePrices.effectiveSellNo;
213
+ expect(processed.summary.effectiveShortRevenue).toBeCloseTo(expectedShortRevenue, 6);
214
+
215
+ console.log('✓ Effective price calculations verified');
216
+ }, 30000);
217
+ });
218
+ });
@@ -0,0 +1,331 @@
1
+ /**
2
+ * CTF Client Integration Tests
3
+ *
4
+ * These tests verify the CTF (Conditional Token Framework) implementation
5
+ * by making REAL on-chain calls to Polygon.
6
+ *
7
+ * IMPORTANT: These tests are READ-ONLY and do not require a wallet.
8
+ * They verify:
9
+ * 1. Contract addresses are correct
10
+ * 2. Position ID calculation is correct
11
+ * 3. Market resolution queries work
12
+ * 4. Balance queries work
13
+ *
14
+ * Run with: pnpm test:integration
15
+ */
16
+
17
+ import { describe, it, expect } from 'vitest';
18
+ import { ethers } from 'ethers';
19
+ import {
20
+ CTF_CONTRACT,
21
+ USDC_CONTRACT,
22
+ NEG_RISK_CTF_EXCHANGE,
23
+ NEG_RISK_ADAPTER,
24
+ USDC_DECIMALS,
25
+ } from '../../clients/ctf-client.js';
26
+
27
+ // Public RPC for read-only tests
28
+ const POLYGON_RPC = 'https://polygon-rpc.com';
29
+
30
+ // Known addresses and markets for testing
31
+ const KNOWN_WHALE_ADDRESS = '0x82a1b239c1ff9bc60a4c86caf5b6bdbd9fddfe20'; // Top trader
32
+ const SECOND_CTF_CONTRACT = '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E';
33
+
34
+ // ABIs for testing
35
+ const CTF_ABI = [
36
+ 'function balanceOf(address account, uint256 positionId) view returns (uint256)',
37
+ 'function payoutNumerators(bytes32 conditionId, uint256 outcomeIndex) view returns (uint256)',
38
+ 'function payoutDenominator(bytes32 conditionId) view returns (uint256)',
39
+ 'function getOutcomeSlotCount(bytes32 conditionId) view returns (uint256)',
40
+ ];
41
+
42
+ const ERC20_ABI = [
43
+ 'function balanceOf(address account) view returns (uint256)',
44
+ 'function decimals() view returns (uint8)',
45
+ 'function symbol() view returns (string)',
46
+ ];
47
+
48
+ describe('CTF Contract Verification', () => {
49
+ const provider = new ethers.providers.JsonRpcProvider(POLYGON_RPC);
50
+
51
+ describe('Contract Addresses', () => {
52
+ it('should verify CTF contract is deployed and accessible', async () => {
53
+ const ctf = new ethers.Contract(CTF_CONTRACT, CTF_ABI, provider);
54
+
55
+ // Query a known condition ID to verify contract works
56
+ // Using a random condition ID should return 0 for unresolved markets
57
+ const testConditionId = '0x0000000000000000000000000000000000000000000000000000000000000001';
58
+ const denominator = await ctf.payoutDenominator(testConditionId);
59
+
60
+ // Should return 0 for non-existent/unresolved condition
61
+ expect(denominator.toNumber()).toBe(0);
62
+
63
+ console.log(`✓ CTF Contract verified at ${CTF_CONTRACT}`);
64
+ }, 30000);
65
+
66
+ it('should verify USDC contract is deployed', async () => {
67
+ const usdc = new ethers.Contract(USDC_CONTRACT, ERC20_ABI, provider);
68
+
69
+ const [decimals, symbol] = await Promise.all([
70
+ usdc.decimals(),
71
+ usdc.symbol(),
72
+ ]);
73
+
74
+ expect(decimals).toBe(USDC_DECIMALS);
75
+ expect(symbol).toBe('USDC');
76
+
77
+ console.log(`✓ USDC Contract verified at ${USDC_CONTRACT}`);
78
+ console.log(` Symbol: ${symbol}, Decimals: ${decimals}`);
79
+ }, 30000);
80
+
81
+ it('should verify second CTF contract exists', async () => {
82
+ // The second contract address from Polymarket docs
83
+ // Note: This is the NegRisk CTF contract with a different ABI
84
+ // We just verify the contract code exists
85
+ const code = await provider.getCode(SECOND_CTF_CONTRACT);
86
+
87
+ expect(code).not.toBe('0x');
88
+ expect(code.length).toBeGreaterThan(10);
89
+
90
+ console.log(`✓ Second CTF Contract verified at ${SECOND_CTF_CONTRACT}`);
91
+ console.log(` Contract code size: ${(code.length - 2) / 2} bytes`);
92
+ console.log(' Note: This is the NegRisk CTF contract (different ABI from standard CTF)');
93
+ }, 30000);
94
+
95
+ it('should verify NegRisk Adapter contract exists', async () => {
96
+ // Check contract code exists
97
+ const code = await provider.getCode(NEG_RISK_ADAPTER);
98
+
99
+ expect(code).not.toBe('0x');
100
+ expect(code.length).toBeGreaterThan(10);
101
+
102
+ console.log(`✓ NegRisk Adapter verified at ${NEG_RISK_ADAPTER}`);
103
+ console.log(` Contract code size: ${(code.length - 2) / 2} bytes`);
104
+ }, 30000);
105
+
106
+ it('should verify NegRisk CTF Exchange contract exists', async () => {
107
+ const code = await provider.getCode(NEG_RISK_CTF_EXCHANGE);
108
+
109
+ expect(code).not.toBe('0x');
110
+ expect(code.length).toBeGreaterThan(10);
111
+
112
+ console.log(`✓ NegRisk CTF Exchange verified at ${NEG_RISK_CTF_EXCHANGE}`);
113
+ console.log(` Contract code size: ${(code.length - 2) / 2} bytes`);
114
+ }, 30000);
115
+ });
116
+
117
+ describe('Position ID Calculation', () => {
118
+ it('should calculate position ID correctly', () => {
119
+ // This matches the Polymarket gist for positionId calculation
120
+ // https://gist.github.com/polymarket/1e12f0ac3e23400ca53ec9b6e1ba00ce
121
+
122
+ const conditionId = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
123
+ const indexSet = 1; // YES outcome
124
+
125
+ // Collection ID = keccak256(parentCollectionId, conditionId, indexSet)
126
+ const collectionId = ethers.utils.keccak256(
127
+ ethers.utils.defaultAbiCoder.encode(
128
+ ['bytes32', 'bytes32', 'uint256'],
129
+ [ethers.constants.HashZero, conditionId, indexSet]
130
+ )
131
+ );
132
+
133
+ // Position ID = keccak256(collateralToken, collectionId)
134
+ const positionId = ethers.utils.keccak256(
135
+ ethers.utils.defaultAbiCoder.encode(
136
+ ['address', 'bytes32'],
137
+ [USDC_CONTRACT, collectionId]
138
+ )
139
+ );
140
+
141
+ // Verify the calculation produces valid bytes32
142
+ expect(positionId).toMatch(/^0x[a-f0-9]{64}$/);
143
+ expect(collectionId).toMatch(/^0x[a-f0-9]{64}$/);
144
+
145
+ console.log('✓ Position ID calculation verified');
146
+ console.log(` Condition ID: ${conditionId.slice(0, 20)}...`);
147
+ console.log(` Collection ID: ${collectionId.slice(0, 20)}...`);
148
+ console.log(` Position ID: ${positionId.slice(0, 20)}...`);
149
+ });
150
+
151
+ it('should produce different position IDs for YES and NO', () => {
152
+ const conditionId = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
153
+
154
+ // YES position (indexSet = 1)
155
+ const yesCollectionId = ethers.utils.keccak256(
156
+ ethers.utils.defaultAbiCoder.encode(
157
+ ['bytes32', 'bytes32', 'uint256'],
158
+ [ethers.constants.HashZero, conditionId, 1]
159
+ )
160
+ );
161
+ const yesPositionId = ethers.utils.keccak256(
162
+ ethers.utils.defaultAbiCoder.encode(
163
+ ['address', 'bytes32'],
164
+ [USDC_CONTRACT, yesCollectionId]
165
+ )
166
+ );
167
+
168
+ // NO position (indexSet = 2)
169
+ const noCollectionId = ethers.utils.keccak256(
170
+ ethers.utils.defaultAbiCoder.encode(
171
+ ['bytes32', 'bytes32', 'uint256'],
172
+ [ethers.constants.HashZero, conditionId, 2]
173
+ )
174
+ );
175
+ const noPositionId = ethers.utils.keccak256(
176
+ ethers.utils.defaultAbiCoder.encode(
177
+ ['address', 'bytes32'],
178
+ [USDC_CONTRACT, noCollectionId]
179
+ )
180
+ );
181
+
182
+ expect(yesPositionId).not.toBe(noPositionId);
183
+ expect(yesCollectionId).not.toBe(noCollectionId);
184
+
185
+ console.log('✓ YES and NO position IDs are different');
186
+ console.log(` YES Position ID: ${yesPositionId.slice(0, 20)}...`);
187
+ console.log(` NO Position ID: ${noPositionId.slice(0, 20)}...`);
188
+ });
189
+ });
190
+
191
+ describe('Balance Queries', () => {
192
+ it('should query USDC balance for known whale', async () => {
193
+ const usdc = new ethers.Contract(USDC_CONTRACT, ERC20_ABI, provider);
194
+
195
+ const balance = await usdc.balanceOf(KNOWN_WHALE_ADDRESS);
196
+ const formattedBalance = ethers.utils.formatUnits(balance, USDC_DECIMALS);
197
+
198
+ expect(balance.gte(0)).toBe(true);
199
+
200
+ console.log(`✓ USDC balance query works`);
201
+ console.log(` Whale ${KNOWN_WHALE_ADDRESS.slice(0, 10)}... has ${parseFloat(formattedBalance).toLocaleString()} USDC`);
202
+ }, 30000);
203
+
204
+ it('should query CTF token balance', async () => {
205
+ const ctf = new ethers.Contract(CTF_CONTRACT, CTF_ABI, provider);
206
+
207
+ // Use a known position ID (we'll use a random one, should return 0)
208
+ const randomPositionId = ethers.utils.keccak256(
209
+ ethers.utils.defaultAbiCoder.encode(
210
+ ['string'],
211
+ ['random-test-position']
212
+ )
213
+ );
214
+
215
+ const balance = await ctf.balanceOf(KNOWN_WHALE_ADDRESS, randomPositionId);
216
+
217
+ expect(balance.gte(0)).toBe(true);
218
+
219
+ console.log('✓ CTF balance query works');
220
+ console.log(` Balance for random position: ${ethers.utils.formatUnits(balance, USDC_DECIMALS)}`);
221
+ }, 30000);
222
+ });
223
+
224
+ describe('Market Resolution', () => {
225
+ it('should query payout info for a real market condition', async () => {
226
+ // First get a real market from Gamma API to get a valid condition ID
227
+ const response = await fetch(
228
+ 'https://gamma-api.polymarket.com/markets?closed=true&limit=1'
229
+ );
230
+ const markets = await response.json() as Array<{ conditionId: string; question: string }>;
231
+
232
+ if (markets.length === 0) {
233
+ console.log('No closed markets found, skipping test');
234
+ return;
235
+ }
236
+
237
+ const ctf = new ethers.Contract(CTF_CONTRACT, CTF_ABI, provider);
238
+ const conditionId = markets[0].conditionId;
239
+
240
+ try {
241
+ const [yesNumerator, noNumerator, denominator] = await Promise.all([
242
+ ctf.payoutNumerators(conditionId, 0),
243
+ ctf.payoutNumerators(conditionId, 1),
244
+ ctf.payoutDenominator(conditionId),
245
+ ]);
246
+
247
+ expect(yesNumerator.gte(0)).toBe(true);
248
+ expect(noNumerator.gte(0)).toBe(true);
249
+ expect(denominator.gte(0)).toBe(true);
250
+
251
+ const isResolved = denominator.gt(0);
252
+
253
+ console.log('✓ Market resolution query works');
254
+ console.log(` Market: "${markets[0].question.slice(0, 40)}..."`);
255
+ console.log(` Condition ID: ${conditionId.slice(0, 20)}...`);
256
+ console.log(` Is resolved: ${isResolved}`);
257
+ if (isResolved) {
258
+ console.log(` Payout numerators: [${yesNumerator.toString()}, ${noNumerator.toString()}]`);
259
+ console.log(` Payout denominator: ${denominator.toString()}`);
260
+ }
261
+ } catch (error) {
262
+ // Some markets might be NegRisk markets with different contract
263
+ console.log('✓ Query attempted (market might be NegRisk type)');
264
+ console.log(` Condition ID: ${conditionId.slice(0, 20)}...`);
265
+ }
266
+ }, 30000);
267
+ });
268
+
269
+ describe('Gas Price', () => {
270
+ it('should fetch current gas price', async () => {
271
+ const gasPrice = await provider.getGasPrice();
272
+ const gasPriceGwei = ethers.utils.formatUnits(gasPrice, 'gwei');
273
+
274
+ expect(gasPrice.gt(0)).toBe(true);
275
+
276
+ console.log('✓ Gas price fetched');
277
+ console.log(` Current gas price: ${parseFloat(gasPriceGwei).toFixed(2)} gwei`);
278
+ }, 30000);
279
+ });
280
+ });
281
+
282
+ describe('CTF Architecture Understanding', () => {
283
+ it('should document the two-contract architecture', () => {
284
+ /**
285
+ * Polymarket uses TWO different CTF systems:
286
+ *
287
+ * 1. STANDARD CTF (0x4D97DCd97eC945f40cF65F87097ACe5EA0476045)
288
+ * - Used for simple binary (Yes/No) markets
289
+ * - Operations: split, merge, redeem
290
+ * - Each market is independent
291
+ *
292
+ * 2. NEGRISK CTF (0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E)
293
+ * - Used for "winner-take-all" events (elections, championships)
294
+ * - Key innovation: NO shares can convert to YES shares in other markets
295
+ * - Example: "Trump NO" can become "Biden YES + Harris YES + ..."
296
+ *
297
+ * Why NegRisk exists:
298
+ * - Capital efficiency: Don't need to buy YES in every candidate
299
+ * - Buying NO in one candidate = betting on all other candidates
300
+ * - The Negative Adapter (0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296) handles conversions
301
+ *
302
+ * Which contract to use:
303
+ * - Check the market's metadata to determine if it's a NegRisk market
304
+ * - Most simple Yes/No markets use Standard CTF
305
+ * - Multi-outcome events (elections, sports champions) may use NegRisk
306
+ */
307
+
308
+ console.log('✓ CTF Architecture Documented');
309
+ console.log('');
310
+ console.log('Standard CTF Contract:');
311
+ console.log(` ${CTF_CONTRACT}`);
312
+ console.log(' - Simple Yes/No markets');
313
+ console.log(' - split/merge/redeem operations');
314
+ console.log('');
315
+ console.log('NegRisk CTF Contract:');
316
+ console.log(` ${SECOND_CTF_CONTRACT}`);
317
+ console.log(' - Winner-take-all events');
318
+ console.log(' - NO shares convertible to YES in other markets');
319
+ console.log('');
320
+ console.log('NegRisk Adapter:');
321
+ console.log(` ${NEG_RISK_ADAPTER}`);
322
+ console.log(' - Handles NO → YES conversions');
323
+ console.log('');
324
+ console.log('NegRisk CTF Exchange:');
325
+ console.log(` ${NEG_RISK_CTF_EXCHANGE}`);
326
+ console.log(' - Trading for NegRisk markets');
327
+
328
+ // This test always passes - it's documentation
329
+ expect(true).toBe(true);
330
+ });
331
+ });