@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,301 @@
1
+ /**
2
+ * CLOB API Client Unit Tests
3
+ */
4
+
5
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
6
+ import { ClobApiClient } from '../clients/clob-api.js';
7
+ import {
8
+ MockRateLimiter,
9
+ MockCache,
10
+ mockClobMarket,
11
+ mockOrderbook,
12
+ mockNoOrderbook,
13
+ expectOrderbookSorted,
14
+ } from './test-utils.js';
15
+
16
+ describe('ClobApiClient', () => {
17
+ let client: ClobApiClient;
18
+ let mockFetch: ReturnType<typeof vi.fn>;
19
+
20
+ beforeEach(() => {
21
+ mockFetch = vi.fn();
22
+ global.fetch = mockFetch;
23
+ client = new ClobApiClient(
24
+ new MockRateLimiter() as never,
25
+ new MockCache() as never
26
+ );
27
+ });
28
+
29
+ afterEach(() => {
30
+ vi.restoreAllMocks();
31
+ });
32
+
33
+ describe('getMarket', () => {
34
+ it('should fetch and normalize market data', async () => {
35
+ mockFetch.mockResolvedValueOnce({
36
+ ok: true,
37
+ json: async () => ({
38
+ condition_id: mockClobMarket.conditionId,
39
+ question: mockClobMarket.question,
40
+ description: mockClobMarket.description,
41
+ market_slug: mockClobMarket.marketSlug,
42
+ tokens: mockClobMarket.tokens.map((t) => ({
43
+ token_id: t.tokenId,
44
+ outcome: t.outcome,
45
+ price: t.price,
46
+ })),
47
+ accepting_orders: true,
48
+ end_date_iso: mockClobMarket.endDateIso,
49
+ active: true,
50
+ closed: false,
51
+ }),
52
+ });
53
+
54
+ const market = await client.getMarket(mockClobMarket.conditionId);
55
+
56
+ expect(market.conditionId).toBe(mockClobMarket.conditionId);
57
+ expect(market.question).toBe(mockClobMarket.question);
58
+ expect(market.tokens).toHaveLength(2);
59
+ expect(market.tokens[0].tokenId).toBe(mockClobMarket.tokens[0].tokenId);
60
+ expect(market.acceptingOrders).toBe(true);
61
+ });
62
+
63
+ it('should throw error for non-existent market', async () => {
64
+ mockFetch.mockResolvedValueOnce({
65
+ ok: false,
66
+ status: 404,
67
+ json: async () => ({ error: 'Market not found' }),
68
+ });
69
+
70
+ await expect(client.getMarket('invalid-id')).rejects.toThrow();
71
+ });
72
+
73
+ it('should cache market data', async () => {
74
+ mockFetch.mockResolvedValue({
75
+ ok: true,
76
+ json: async () => ({
77
+ condition_id: mockClobMarket.conditionId,
78
+ question: mockClobMarket.question,
79
+ tokens: [],
80
+ accepting_orders: true,
81
+ active: true,
82
+ closed: false,
83
+ }),
84
+ });
85
+
86
+ // First call
87
+ await client.getMarket(mockClobMarket.conditionId);
88
+ // Second call should use cache
89
+ await client.getMarket(mockClobMarket.conditionId);
90
+
91
+ // Should only fetch once due to caching
92
+ expect(mockFetch).toHaveBeenCalledTimes(1);
93
+ });
94
+ });
95
+
96
+ describe('getOrderbook', () => {
97
+ it('should fetch and sort orderbook correctly', async () => {
98
+ // Return unsorted data to test sorting
99
+ mockFetch.mockResolvedValueOnce({
100
+ ok: true,
101
+ json: async () => ({
102
+ bids: [
103
+ { price: '0.53', size: '750' },
104
+ { price: '0.55', size: '1000' },
105
+ { price: '0.54', size: '500' },
106
+ ],
107
+ asks: [
108
+ { price: '0.58', size: '600' },
109
+ { price: '0.57', size: '800' },
110
+ { price: '0.59', size: '400' },
111
+ ],
112
+ }),
113
+ });
114
+
115
+ const orderbook = await client.getOrderbook('test-token-id');
116
+
117
+ // Check bids are sorted descending
118
+ expect(orderbook.bids[0].price).toBe(0.55);
119
+ expect(orderbook.bids[1].price).toBe(0.54);
120
+ expect(orderbook.bids[2].price).toBe(0.53);
121
+
122
+ // Check asks are sorted ascending
123
+ expect(orderbook.asks[0].price).toBe(0.57);
124
+ expect(orderbook.asks[1].price).toBe(0.58);
125
+ expect(orderbook.asks[2].price).toBe(0.59);
126
+
127
+ expectOrderbookSorted(orderbook);
128
+ });
129
+
130
+ it('should handle empty orderbook', async () => {
131
+ mockFetch.mockResolvedValueOnce({
132
+ ok: true,
133
+ json: async () => ({ bids: [], asks: [] }),
134
+ });
135
+
136
+ const orderbook = await client.getOrderbook('test-token-id');
137
+
138
+ expect(orderbook.bids).toHaveLength(0);
139
+ expect(orderbook.asks).toHaveLength(0);
140
+ expect(orderbook.timestamp).toBeGreaterThan(0);
141
+ });
142
+
143
+ it('should convert string prices to numbers', async () => {
144
+ mockFetch.mockResolvedValueOnce({
145
+ ok: true,
146
+ json: async () => ({
147
+ bids: [{ price: '0.55', size: '1000.5' }],
148
+ asks: [{ price: '0.57', size: '800.25' }],
149
+ }),
150
+ });
151
+
152
+ const orderbook = await client.getOrderbook('test-token-id');
153
+
154
+ expect(typeof orderbook.bids[0].price).toBe('number');
155
+ expect(typeof orderbook.bids[0].size).toBe('number');
156
+ expect(orderbook.bids[0].price).toBe(0.55);
157
+ expect(orderbook.bids[0].size).toBe(1000.5);
158
+ });
159
+ });
160
+
161
+ describe('getProcessedOrderbook', () => {
162
+ it('should calculate effective prices correctly', async () => {
163
+ // First call: getMarket
164
+ mockFetch.mockResolvedValueOnce({
165
+ ok: true,
166
+ json: async () => ({
167
+ condition_id: mockClobMarket.conditionId,
168
+ tokens: [
169
+ { token_id: 'yes-token', outcome: 'Yes', price: 0.55 },
170
+ { token_id: 'no-token', outcome: 'No', price: 0.45 },
171
+ ],
172
+ accepting_orders: true,
173
+ active: true,
174
+ closed: false,
175
+ }),
176
+ });
177
+
178
+ // Second call: getOrderbook for YES
179
+ mockFetch.mockResolvedValueOnce({
180
+ ok: true,
181
+ json: async () => ({
182
+ bids: [{ price: '0.55', size: '1000' }],
183
+ asks: [{ price: '0.57', size: '800' }],
184
+ }),
185
+ });
186
+
187
+ // Third call: getOrderbook for NO
188
+ mockFetch.mockResolvedValueOnce({
189
+ ok: true,
190
+ json: async () => ({
191
+ bids: [{ price: '0.43', size: '900' }],
192
+ asks: [{ price: '0.45', size: '700' }],
193
+ }),
194
+ });
195
+
196
+ const processed = await client.getProcessedOrderbook(mockClobMarket.conditionId);
197
+
198
+ // Check YES orderbook
199
+ expect(processed.yes.bid).toBe(0.55);
200
+ expect(processed.yes.ask).toBe(0.57);
201
+
202
+ // Check NO orderbook
203
+ expect(processed.no.bid).toBe(0.43);
204
+ expect(processed.no.ask).toBe(0.45);
205
+
206
+ // Check effective prices
207
+ // effectiveBuyYes = min(YES.ask, 1 - NO.bid) = min(0.57, 0.57) = 0.57
208
+ expect(processed.summary.effectivePrices.effectiveBuyYes).toBeCloseTo(0.57, 6);
209
+ // effectiveBuyNo = min(NO.ask, 1 - YES.bid) = min(0.45, 0.45) = 0.45
210
+ expect(processed.summary.effectivePrices.effectiveBuyNo).toBeCloseTo(0.45, 6);
211
+
212
+ // Long cost = effectiveBuyYes + effectiveBuyNo = 0.57 + 0.45 = 1.02
213
+ expect(processed.summary.effectiveLongCost).toBeCloseTo(1.02, 2);
214
+
215
+ // Long arb profit = 1 - longCost = -0.02 (no opportunity)
216
+ expect(processed.summary.longArbProfit).toBeCloseTo(-0.02, 2);
217
+ });
218
+
219
+ it('should detect long arbitrage opportunity', async () => {
220
+ mockFetch.mockResolvedValueOnce({
221
+ ok: true,
222
+ json: async () => ({
223
+ condition_id: mockClobMarket.conditionId,
224
+ tokens: [
225
+ { token_id: 'yes-token', outcome: 'Yes', price: 0.50 },
226
+ { token_id: 'no-token', outcome: 'No', price: 0.50 },
227
+ ],
228
+ accepting_orders: true,
229
+ active: true,
230
+ closed: false,
231
+ }),
232
+ });
233
+
234
+ // YES orderbook with low ask
235
+ mockFetch.mockResolvedValueOnce({
236
+ ok: true,
237
+ json: async () => ({
238
+ bids: [{ price: '0.48', size: '1000' }],
239
+ asks: [{ price: '0.49', size: '800' }], // Low ask
240
+ }),
241
+ });
242
+
243
+ // NO orderbook with low ask
244
+ mockFetch.mockResolvedValueOnce({
245
+ ok: true,
246
+ json: async () => ({
247
+ bids: [{ price: '0.48', size: '900' }],
248
+ asks: [{ price: '0.49', size: '700' }], // Low ask
249
+ }),
250
+ });
251
+
252
+ const processed = await client.getProcessedOrderbook(mockClobMarket.conditionId);
253
+
254
+ // Long cost = 0.49 + 0.49 = 0.98 < 1.00
255
+ // Long arb profit = 1 - 0.98 = 0.02 (2% opportunity!)
256
+ expect(processed.summary.longArbProfit).toBeGreaterThan(0);
257
+ expect(processed.summary.longArbProfit).toBeCloseTo(0.02, 2);
258
+ });
259
+
260
+ it('should throw error if tokens are missing', async () => {
261
+ mockFetch.mockResolvedValueOnce({
262
+ ok: true,
263
+ json: async () => ({
264
+ condition_id: mockClobMarket.conditionId,
265
+ tokens: [], // No tokens
266
+ accepting_orders: true,
267
+ active: true,
268
+ closed: false,
269
+ }),
270
+ });
271
+
272
+ await expect(
273
+ client.getProcessedOrderbook(mockClobMarket.conditionId)
274
+ ).rejects.toThrow('Missing tokens');
275
+ });
276
+ });
277
+
278
+ describe('hasTradingCapabilities', () => {
279
+ it('should return false without config', () => {
280
+ expect(client.hasTradingCapabilities()).toBe(false);
281
+ });
282
+
283
+ it('should return true with signer', () => {
284
+ const clientWithSigner = new ClobApiClient(
285
+ new MockRateLimiter() as never,
286
+ new MockCache() as never,
287
+ { signer: {} }
288
+ );
289
+ expect(clientWithSigner.hasTradingCapabilities()).toBe(true);
290
+ });
291
+
292
+ it('should return true with credentials', () => {
293
+ const clientWithCreds = new ClobApiClient(
294
+ new MockRateLimiter() as never,
295
+ new MockCache() as never,
296
+ { creds: { key: 'k', secret: 's', passphrase: 'p' } }
297
+ );
298
+ expect(clientWithCreds.hasTradingCapabilities()).toBe(true);
299
+ });
300
+ });
301
+ });
@@ -0,0 +1,314 @@
1
+ /**
2
+ * Bridge Client Integration Tests
3
+ *
4
+ * These tests verify the Bridge Client functionality.
5
+ * Note: The bridge API (bridge.polymarket.com) may not be publicly accessible.
6
+ * These tests are designed to handle both success and failure gracefully.
7
+ *
8
+ * Run with: pnpm test:integration
9
+ */
10
+
11
+ import { describe, it, expect } from 'vitest';
12
+ import {
13
+ BridgeClient,
14
+ SUPPORTED_CHAINS,
15
+ BRIDGE_TOKENS,
16
+ estimateBridgeOutput,
17
+ getExplorerUrl,
18
+ } from '../../clients/bridge-client.js';
19
+
20
+ describe('BridgeClient Integration', () => {
21
+ const bridge = new BridgeClient();
22
+
23
+ describe('Constants', () => {
24
+ it('should have correct Ethereum chain info', () => {
25
+ expect(SUPPORTED_CHAINS.ETHEREUM.chainId).toBe(1);
26
+ expect(SUPPORTED_CHAINS.ETHEREUM.name).toBe('Ethereum');
27
+ expect(SUPPORTED_CHAINS.ETHEREUM.rpcUrl).toBeDefined();
28
+
29
+ console.log('Ethereum chain:', SUPPORTED_CHAINS.ETHEREUM);
30
+ });
31
+
32
+ it('should have correct Polygon chain info', () => {
33
+ expect(SUPPORTED_CHAINS.POLYGON.chainId).toBe(137);
34
+ expect(SUPPORTED_CHAINS.POLYGON.name).toBe('Polygon');
35
+ expect(SUPPORTED_CHAINS.POLYGON.rpcUrl).toBeDefined();
36
+
37
+ console.log('Polygon chain:', SUPPORTED_CHAINS.POLYGON);
38
+ });
39
+
40
+ it('should have correct token addresses', () => {
41
+ // Ethereum tokens
42
+ expect(BRIDGE_TOKENS.ETH_USDC).toMatch(/^0x[a-fA-F0-9]{40}$/);
43
+ expect(BRIDGE_TOKENS.ETH_WETH).toMatch(/^0x[a-fA-F0-9]{40}$/);
44
+ expect(BRIDGE_TOKENS.ETH_DAI).toMatch(/^0x[a-fA-F0-9]{40}$/);
45
+
46
+ // Polygon tokens
47
+ expect(BRIDGE_TOKENS.POLYGON_USDC_E).toMatch(/^0x[a-fA-F0-9]{40}$/);
48
+ expect(BRIDGE_TOKENS.POLYGON_NATIVE_USDC).toMatch(/^0x[a-fA-F0-9]{40}$/);
49
+
50
+ // Verify well-known addresses
51
+ expect(BRIDGE_TOKENS.ETH_USDC.toLowerCase()).toBe(
52
+ '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
53
+ );
54
+ expect(BRIDGE_TOKENS.POLYGON_USDC_E.toLowerCase()).toBe(
55
+ '0x2791bca1f2de4661ed88a30c99a7a9449aa84174'
56
+ );
57
+
58
+ console.log('Token addresses verified:', BRIDGE_TOKENS);
59
+ });
60
+ });
61
+
62
+ describe('Utility Functions', () => {
63
+ describe('estimateBridgeOutput', () => {
64
+ it('should estimate output for stablecoins', () => {
65
+ const input = 1000;
66
+ const output = estimateBridgeOutput(input, 'USDC');
67
+
68
+ // Should be slightly less than input due to fees (~1%)
69
+ expect(output).toBeLessThan(input);
70
+ expect(output).toBeGreaterThan(input * 0.95);
71
+
72
+ console.log(`Estimated output for ${input} USDC: ${output} USDC.e`);
73
+ });
74
+
75
+ it('should work for different stablecoins', () => {
76
+ const stablecoins = ['USDC', 'USDT', 'DAI', 'BUSD'];
77
+
78
+ for (const token of stablecoins) {
79
+ const output = estimateBridgeOutput(100, token);
80
+ expect(output).toBeCloseTo(99, 0); // ~1% fee
81
+ }
82
+
83
+ console.log('All stablecoins estimated correctly');
84
+ });
85
+
86
+ it('should work for ETH', () => {
87
+ const output = estimateBridgeOutput(1, 'ETH');
88
+ expect(output).toBeLessThan(1);
89
+ expect(output).toBeGreaterThan(0.95);
90
+
91
+ console.log(`Estimated output for 1 ETH: ${output} (in USDC.e equivalent)`);
92
+ });
93
+ });
94
+
95
+ describe('getExplorerUrl', () => {
96
+ const testTxHash = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
97
+
98
+ it('should return Etherscan URL for Ethereum', () => {
99
+ const url = getExplorerUrl(1, testTxHash);
100
+ expect(url).toContain('etherscan.io/tx/');
101
+ expect(url).toContain(testTxHash);
102
+
103
+ console.log(`Ethereum explorer URL: ${url}`);
104
+ });
105
+
106
+ it('should return Polygonscan URL for Polygon', () => {
107
+ const url = getExplorerUrl(137, testTxHash);
108
+ expect(url).toContain('polygonscan.com/tx/');
109
+ expect(url).toContain(testTxHash);
110
+
111
+ console.log(`Polygon explorer URL: ${url}`);
112
+ });
113
+
114
+ it('should return correct URLs for other chains', () => {
115
+ const chains = [
116
+ { id: 10, expected: 'optimistic.etherscan.io' },
117
+ { id: 42161, expected: 'arbiscan.io' },
118
+ { id: 8453, expected: 'basescan.org' },
119
+ ];
120
+
121
+ for (const chain of chains) {
122
+ const url = getExplorerUrl(chain.id, testTxHash);
123
+ expect(url).toContain(chain.expected);
124
+ }
125
+
126
+ console.log('All chain explorer URLs verified');
127
+ });
128
+
129
+ it('should return blockscan URL for unknown chains', () => {
130
+ const url = getExplorerUrl(99999, testTxHash);
131
+ expect(url).toContain('blockscan.com/tx/');
132
+
133
+ console.log(`Unknown chain explorer URL: ${url}`);
134
+ });
135
+ });
136
+ });
137
+
138
+ describe('API Calls (may fail if API not accessible)', () => {
139
+ it('should attempt to get supported assets', async () => {
140
+ try {
141
+ const assets = await bridge.getSupportedAssets();
142
+
143
+ expect(Array.isArray(assets)).toBe(true);
144
+
145
+ if (assets.length > 0) {
146
+ // Verify asset structure
147
+ for (const asset of assets) {
148
+ expect(typeof asset.chainId).toBe('number');
149
+ expect(typeof asset.chainName).toBe('string');
150
+ expect(typeof asset.tokenAddress).toBe('string');
151
+ expect(typeof asset.tokenSymbol).toBe('string');
152
+ expect(typeof asset.active).toBe('boolean');
153
+ }
154
+
155
+ console.log(`Got ${assets.length} supported assets`);
156
+ console.log('Sample asset:', assets[0]);
157
+ } else {
158
+ console.log('No assets returned (API may return empty array)');
159
+ }
160
+ } catch (error) {
161
+ console.log('getSupportedAssets failed (expected if API not public)');
162
+ console.log(`Error: ${(error as Error).message}`);
163
+
164
+ // Don't fail the test - API may not be publicly accessible
165
+ expect(true).toBe(true);
166
+ }
167
+ }, 30000);
168
+
169
+ it('should attempt to check if Ethereum USDC is supported', async () => {
170
+ try {
171
+ const isSupported = await bridge.isSupported(1, 'USDC');
172
+
173
+ expect(typeof isSupported).toBe('boolean');
174
+ console.log(`Ethereum USDC supported: ${isSupported}`);
175
+ } catch (error) {
176
+ console.log('isSupported check failed (expected if API not public)');
177
+ console.log(`Error: ${(error as Error).message}`);
178
+ expect(true).toBe(true);
179
+ }
180
+ }, 30000);
181
+
182
+ it('should attempt to get minimum deposit for Ethereum USDC', async () => {
183
+ try {
184
+ const minDeposit = await bridge.getMinDeposit(1, 'USDC');
185
+
186
+ if (minDeposit) {
187
+ expect(typeof minDeposit.amount).toBe('string');
188
+ expect(typeof minDeposit.usd).toBe('number');
189
+ console.log(`Min deposit for ETH USDC: ${minDeposit.amount} (~$${minDeposit.usd})`);
190
+ } else {
191
+ console.log('No minimum deposit info returned');
192
+ }
193
+ } catch (error) {
194
+ console.log('getMinDeposit failed (expected if API not public)');
195
+ console.log(`Error: ${(error as Error).message}`);
196
+ expect(true).toBe(true);
197
+ }
198
+ }, 30000);
199
+
200
+ it('should attempt to create deposit addresses', async () => {
201
+ const testAddress = '0x82a1b239c1ff9bc60a4c86caf5b6bdbd9fddfe20'; // Known whale
202
+
203
+ try {
204
+ const result = await bridge.createDepositAddresses(testAddress);
205
+
206
+ // API returns universal addresses for EVM, Solana, and Bitcoin
207
+ expect(typeof result.address.evm).toBe('string');
208
+ expect(typeof result.address.svm).toBe('string');
209
+ expect(typeof result.address.btc).toBe('string');
210
+
211
+ // EVM address should be valid Ethereum address format
212
+ expect(result.address.evm).toMatch(/^0x[a-fA-F0-9]{40}$/);
213
+
214
+ console.log('Got universal deposit addresses:');
215
+ console.log(` EVM: ${result.address.evm}`);
216
+ console.log(` Solana: ${result.address.svm}`);
217
+ console.log(` Bitcoin: ${result.address.btc}`);
218
+ } catch (error) {
219
+ console.log('createDepositAddresses failed (expected if API not public)');
220
+ console.log(`Error: ${(error as Error).message}`);
221
+ expect(true).toBe(true);
222
+ }
223
+ }, 30000);
224
+
225
+ it('should attempt to get deposit instructions', async () => {
226
+ const testAddress = '0x82a1b239c1ff9bc60a4c86caf5b6bdbd9fddfe20';
227
+
228
+ try {
229
+ const instructions = await bridge.getDepositInstructions(testAddress);
230
+
231
+ expect(typeof instructions).toBe('string');
232
+ expect(instructions.length).toBeGreaterThan(0);
233
+
234
+ // Should contain expected sections
235
+ expect(instructions).toContain('POLYMARKET');
236
+ expect(instructions).toContain(testAddress);
237
+
238
+ console.log('Got deposit instructions:');
239
+ console.log(instructions.slice(0, 500) + '...');
240
+ } catch (error) {
241
+ console.log('getDepositInstructions failed (expected if API not public)');
242
+ console.log(`Error: ${(error as Error).message}`);
243
+ expect(true).toBe(true);
244
+ }
245
+ }, 30000);
246
+ });
247
+
248
+ describe('BridgeClient Configuration', () => {
249
+ it('should use default configuration', () => {
250
+ const client = new BridgeClient();
251
+ // Client should be created without errors
252
+ expect(client).toBeDefined();
253
+ });
254
+
255
+ it('should accept custom configuration', () => {
256
+ const client = new BridgeClient({
257
+ baseUrl: 'https://custom-bridge.example.com',
258
+ timeout: 60000,
259
+ });
260
+ expect(client).toBeDefined();
261
+ });
262
+ });
263
+ });
264
+
265
+ describe('Bridge Documentation', () => {
266
+ it('should document the bridge deposit flow', () => {
267
+ /**
268
+ * Polymarket Bridge Deposit Flow:
269
+ *
270
+ * 1. User calls createDepositAddresses(walletAddress)
271
+ * - Returns unique deposit addresses for each supported chain/token
272
+ * - These addresses are specific to your Polymarket wallet
273
+ *
274
+ * 2. User sends tokens to the deposit address on the source chain
275
+ * - Example: Send USDC on Ethereum to the ETH/USDC deposit address
276
+ * - Must send more than the minimum deposit amount
277
+ *
278
+ * 3. Bridge automatically:
279
+ * - Detects the incoming deposit
280
+ * - Bridges funds to Polygon
281
+ * - Swaps to USDC.e if necessary
282
+ * - Credits USDC.e to user's Polymarket account
283
+ *
284
+ * 4. User can check deposit status
285
+ * - Typical time: 10-30 minutes
286
+ * - Status: pending → bridging → swapping → completed
287
+ *
288
+ * Supported chains (typical):
289
+ * - Ethereum (Chain ID: 1)
290
+ * - Arbitrum (Chain ID: 42161)
291
+ * - Optimism (Chain ID: 10)
292
+ * - Base (Chain ID: 8453)
293
+ *
294
+ * Supported tokens (typical):
295
+ * - USDC, USDT, DAI, WETH, ETH
296
+ */
297
+
298
+ console.log('');
299
+ console.log('Bridge Deposit Flow');
300
+ console.log('═══════════════════════════════════════════════════════════');
301
+ console.log('');
302
+ console.log('1. Get deposit addresses: bridge.createDepositAddresses(walletAddress)');
303
+ console.log('2. Send tokens to the unique deposit address on source chain');
304
+ console.log('3. Wait 10-30 minutes for automatic bridging and conversion');
305
+ console.log('4. USDC.e appears in your Polymarket account');
306
+ console.log('');
307
+ console.log('Supported chains: Ethereum, Arbitrum, Optimism, Base');
308
+ console.log('Supported tokens: USDC, USDT, DAI, WETH, ETH');
309
+ console.log('');
310
+ console.log('═══════════════════════════════════════════════════════════');
311
+
312
+ expect(true).toBe(true);
313
+ });
314
+ });