@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,915 @@
1
+ /**
2
+ * CTF (Conditional Token Framework) Client
3
+ *
4
+ * Provides on-chain operations for Polymarket's conditional tokens:
5
+ * - Split: USDC → YES + NO token pair
6
+ * - Merge: YES + NO → USDC
7
+ * - Redeem: Winning tokens → USDC (after market resolution)
8
+ *
9
+ * ⚠️ CRITICAL: Polymarket CTF uses USDC.e (bridged), NOT native USDC!
10
+ *
11
+ * | Token | Address | CTF Compatible |
12
+ * |---------------|--------------------------------------------|-----------------
13
+ * | USDC.e | 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 | ✅ Yes |
14
+ * | Native USDC | 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 | ❌ No |
15
+ *
16
+ * Common Mistake:
17
+ * - Your wallet has native USDC but CTF operations fail
18
+ * - Solution: Use SwapService.transferUsdcE() or swap native USDC to USDC.e
19
+ *
20
+ * Based on: docs/01-product-research/06-poly-sdk/05-ctf-integration-plan.md
21
+ *
22
+ * Contract: Gnosis Conditional Tokens on Polygon
23
+ * https://docs.polymarket.com/developers/CTF/overview
24
+ */
25
+ import { ethers, Contract, Wallet, BigNumber } from 'ethers';
26
+ // ===== Contract Addresses (Polygon Mainnet) =====
27
+ export const CTF_CONTRACT = '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045';
28
+ /**
29
+ * USDC.e (Bridged USDC) - The ONLY USDC accepted by Polymarket CTF
30
+ *
31
+ * ⚠️ WARNING: This is NOT native USDC (0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359)
32
+ *
33
+ * If your wallet has native USDC but CTF operations fail with "Insufficient USDC balance",
34
+ * you need to swap your native USDC to USDC.e first using:
35
+ * - SwapService.swap('USDC', 'USDC_E', amount)
36
+ * - Or transfer USDC.e using SwapService.transferUsdcE()
37
+ */
38
+ export const USDC_CONTRACT = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
39
+ /** Native USDC on Polygon - NOT compatible with CTF */
40
+ export const NATIVE_USDC_CONTRACT = '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359';
41
+ export const NEG_RISK_CTF_EXCHANGE = '0xC5d563A36AE78145C45a50134d48A1215220f80a';
42
+ export const NEG_RISK_ADAPTER = '0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296';
43
+ // USDC.e uses 6 decimals
44
+ export const USDC_DECIMALS = 6;
45
+ // ===== ABIs =====
46
+ const CTF_ABI = [
47
+ // Split: USDC → YES + NO
48
+ 'function splitPosition(address collateralToken, bytes32 parentCollectionId, bytes32 conditionId, uint256[] partition, uint256 amount) external',
49
+ // Merge: YES + NO → USDC
50
+ 'function mergePositions(address collateralToken, bytes32 parentCollectionId, bytes32 conditionId, uint256[] partition, uint256 amount) external',
51
+ // Redeem: Winning tokens → USDC
52
+ 'function redeemPositions(address collateralToken, bytes32 parentCollectionId, bytes32 conditionId, uint256[] indexSets) external',
53
+ // Balance query
54
+ 'function balanceOf(address account, uint256 positionId) view returns (uint256)',
55
+ // Check if condition is resolved
56
+ 'function payoutNumerators(bytes32 conditionId, uint256 outcomeIndex) view returns (uint256)',
57
+ 'function payoutDenominator(bytes32 conditionId) view returns (uint256)',
58
+ ];
59
+ const ERC20_ABI = [
60
+ 'function approve(address spender, uint256 amount) returns (bool)',
61
+ 'function allowance(address owner, address spender) view returns (uint256)',
62
+ 'function balanceOf(address account) view returns (uint256)',
63
+ 'function decimals() view returns (uint8)',
64
+ ];
65
+ /** Common revert reasons */
66
+ export var RevertReason;
67
+ (function (RevertReason) {
68
+ RevertReason["INSUFFICIENT_BALANCE"] = "INSUFFICIENT_BALANCE";
69
+ RevertReason["INSUFFICIENT_ALLOWANCE"] = "INSUFFICIENT_ALLOWANCE";
70
+ RevertReason["CONDITION_NOT_RESOLVED"] = "CONDITION_NOT_RESOLVED";
71
+ RevertReason["INVALID_PARTITION"] = "INVALID_PARTITION";
72
+ RevertReason["INVALID_CONDITION"] = "INVALID_CONDITION";
73
+ RevertReason["EXECUTION_REVERTED"] = "EXECUTION_REVERTED";
74
+ RevertReason["TIMEOUT"] = "TIMEOUT";
75
+ RevertReason["UNKNOWN"] = "UNKNOWN";
76
+ })(RevertReason || (RevertReason = {}));
77
+ // ===== CTF Client =====
78
+ // Default MATIC price (updated via getMaticPrice)
79
+ const DEFAULT_MATIC_PRICE = 0.50;
80
+ export class CTFClient {
81
+ provider;
82
+ wallet;
83
+ ctfContract;
84
+ usdcContract;
85
+ gasPriceMultiplier;
86
+ confirmations;
87
+ txTimeout;
88
+ cachedMaticPrice = DEFAULT_MATIC_PRICE;
89
+ maticPriceLastUpdated = 0;
90
+ constructor(config) {
91
+ const rpcUrl = config.rpcUrl || 'https://polygon-rpc.com';
92
+ this.provider = new ethers.providers.JsonRpcProvider(rpcUrl);
93
+ this.wallet = new Wallet(config.privateKey, this.provider);
94
+ this.ctfContract = new Contract(CTF_CONTRACT, CTF_ABI, this.wallet);
95
+ this.usdcContract = new Contract(USDC_CONTRACT, ERC20_ABI, this.wallet);
96
+ this.gasPriceMultiplier = config.gasPriceMultiplier || 1.2;
97
+ this.confirmations = config.confirmations || 1;
98
+ this.txTimeout = config.txTimeout || 60000;
99
+ }
100
+ /**
101
+ * Get wallet address
102
+ */
103
+ getAddress() {
104
+ return this.wallet.address;
105
+ }
106
+ /**
107
+ * Get USDC.e (bridged USDC) balance - the token used by Polymarket CTF
108
+ *
109
+ * ⚠️ Note: This returns USDC.e balance, NOT native USDC balance.
110
+ * Polymarket CTF only accepts USDC.e (0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174).
111
+ *
112
+ * Common issue: Your wallet shows USDC balance but this returns 0
113
+ * - This means you have native USDC, not USDC.e
114
+ * - Use SwapService.swap('USDC', 'USDC_E', amount) to convert
115
+ */
116
+ async getUsdcBalance() {
117
+ const balance = await this.usdcContract.balanceOf(this.wallet.address);
118
+ return ethers.utils.formatUnits(balance, USDC_DECIMALS);
119
+ }
120
+ /**
121
+ * Get native USDC balance (for comparison/debugging)
122
+ *
123
+ * This is NOT the token used by CTF. Use getUsdcBalance() for CTF operations.
124
+ */
125
+ async getNativeUsdcBalance() {
126
+ const nativeUsdcContract = new Contract(NATIVE_USDC_CONTRACT, ERC20_ABI, this.provider);
127
+ const balance = await nativeUsdcContract.balanceOf(this.wallet.address);
128
+ return ethers.utils.formatUnits(balance, USDC_DECIMALS);
129
+ }
130
+ /**
131
+ * Check if wallet is ready for CTF trading operations
132
+ *
133
+ * Verifies:
134
+ * - Has sufficient USDC.e (not native USDC)
135
+ * - Has MATIC for gas fees
136
+ *
137
+ * @param amount - Minimum USDC.e amount needed (e.g., "100" for 100 USDC.e)
138
+ * @returns Ready status with balances and suggestions
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * const status = await ctf.checkReadyForCTF('100');
143
+ * if (!status.ready) {
144
+ * console.log(status.suggestion);
145
+ * // "You have 50 native USDC but 0 USDC.e. Swap native USDC to USDC.e first."
146
+ * }
147
+ * ```
148
+ */
149
+ async checkReadyForCTF(amount) {
150
+ const [usdcE, nativeUsdc, matic] = await Promise.all([
151
+ this.getUsdcBalance(),
152
+ this.getNativeUsdcBalance(),
153
+ this.provider.getBalance(this.wallet.address),
154
+ ]);
155
+ const usdcEBalance = parseFloat(usdcE);
156
+ const nativeUsdcBalance = parseFloat(nativeUsdc);
157
+ const maticBalance = parseFloat(ethers.utils.formatEther(matic));
158
+ const amountNeeded = parseFloat(amount);
159
+ const result = {
160
+ ready: false,
161
+ usdcEBalance: usdcE,
162
+ nativeUsdcBalance: nativeUsdc,
163
+ maticBalance: ethers.utils.formatEther(matic),
164
+ suggestion: undefined,
165
+ };
166
+ // Check MATIC for gas
167
+ if (maticBalance < 0.01) {
168
+ result.suggestion = `Insufficient MATIC for gas fees. Have: ${maticBalance.toFixed(4)} MATIC, need at least 0.01 MATIC.`;
169
+ return result;
170
+ }
171
+ // Check USDC.e balance
172
+ if (usdcEBalance < amountNeeded) {
173
+ if (nativeUsdcBalance >= amountNeeded) {
174
+ result.suggestion = `You have ${nativeUsdcBalance.toFixed(2)} native USDC but only ${usdcEBalance.toFixed(2)} USDC.e. ` +
175
+ `Polymarket CTF requires USDC.e. Use SwapService.swap('USDC', 'USDC_E', '${amount}') to convert.`;
176
+ }
177
+ else if (nativeUsdcBalance > 0) {
178
+ result.suggestion = `Insufficient USDC.e. Have: ${usdcEBalance.toFixed(2)} USDC.e + ${nativeUsdcBalance.toFixed(2)} native USDC, need: ${amount} USDC.e. ` +
179
+ `Swap all native USDC to USDC.e, then add more funds.`;
180
+ }
181
+ else {
182
+ result.suggestion = `Insufficient USDC.e. Have: ${usdcEBalance.toFixed(2)} USDC.e, need: ${amount} USDC.e.`;
183
+ }
184
+ return result;
185
+ }
186
+ result.ready = true;
187
+ return result;
188
+ }
189
+ /**
190
+ * Split USDC into YES + NO tokens
191
+ *
192
+ * @param conditionId - Market condition ID
193
+ * @param amount - USDC amount (e.g., "100" for 100 USDC)
194
+ * @returns SplitResult with transaction details
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * const result = await ctf.split(conditionId, "100");
199
+ * console.log(`Split ${result.amount} USDC into tokens`);
200
+ * console.log(`TX: ${result.txHash}`);
201
+ * ```
202
+ */
203
+ async split(conditionId, amount) {
204
+ const amountWei = ethers.utils.parseUnits(amount, USDC_DECIMALS);
205
+ // 1. Check USDC balance
206
+ const balance = await this.usdcContract.balanceOf(this.wallet.address);
207
+ if (balance.lt(amountWei)) {
208
+ throw new Error(`Insufficient USDC balance. Have: ${ethers.utils.formatUnits(balance, USDC_DECIMALS)}, Need: ${amount}`);
209
+ }
210
+ // 2. Check and approve USDC if needed
211
+ const allowance = await this.usdcContract.allowance(this.wallet.address, CTF_CONTRACT);
212
+ if (allowance.lt(amountWei)) {
213
+ const approveTx = await this.usdcContract.approve(CTF_CONTRACT, ethers.constants.MaxUint256, await this.getGasOptions());
214
+ await approveTx.wait();
215
+ }
216
+ // 3. Execute split
217
+ // Partition [1, 2] represents [YES, NO] outcomes
218
+ const tx = await this.ctfContract.splitPosition(USDC_CONTRACT, ethers.constants.HashZero, // parentCollectionId = 0 for Polymarket
219
+ conditionId, [1, 2], // partition for YES/NO
220
+ amountWei, await this.getGasOptions());
221
+ const receipt = await tx.wait();
222
+ return {
223
+ success: true,
224
+ txHash: receipt.transactionHash,
225
+ amount,
226
+ yesTokens: amount, // 1:1 split
227
+ noTokens: amount,
228
+ gasUsed: receipt.gasUsed.toString(),
229
+ };
230
+ }
231
+ /**
232
+ * Merge YES + NO tokens back to USDC
233
+ *
234
+ * @param conditionId - Market condition ID
235
+ * @param amount - Number of token pairs to merge (e.g., "100" for 100 YES + 100 NO)
236
+ * @returns MergeResult with transaction details
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * // After buying 100 YES and 100 NO via TradingClient
241
+ * const result = await ctf.merge(conditionId, "100");
242
+ * console.log(`Received ${result.usdcReceived} USDC`);
243
+ * ```
244
+ */
245
+ async merge(conditionId, amount) {
246
+ const amountWei = ethers.utils.parseUnits(amount, USDC_DECIMALS);
247
+ // Check token balances
248
+ const balances = await this.getPositionBalance(conditionId);
249
+ const yesBalance = ethers.utils.parseUnits(balances.yesBalance, USDC_DECIMALS);
250
+ const noBalance = ethers.utils.parseUnits(balances.noBalance, USDC_DECIMALS);
251
+ if (yesBalance.lt(amountWei) || noBalance.lt(amountWei)) {
252
+ throw new Error(`Insufficient token balance. Need ${amount} of each. Have: YES=${balances.yesBalance}, NO=${balances.noBalance}`);
253
+ }
254
+ // Execute merge
255
+ const tx = await this.ctfContract.mergePositions(USDC_CONTRACT, ethers.constants.HashZero, conditionId, [1, 2], amountWei, await this.getGasOptions());
256
+ const receipt = await tx.wait();
257
+ return {
258
+ success: true,
259
+ txHash: receipt.transactionHash,
260
+ amount,
261
+ usdcReceived: amount, // 1:1 merge
262
+ gasUsed: receipt.gasUsed.toString(),
263
+ };
264
+ }
265
+ /**
266
+ * Merge YES and NO tokens back into USDC using explicit token IDs
267
+ *
268
+ * This method uses the provided token IDs for balance checking, which is
269
+ * necessary when working with Polymarket CLOB markets where token IDs
270
+ * don't match the calculated position IDs.
271
+ *
272
+ * @param conditionId - Market condition ID
273
+ * @param tokenIds - Token IDs from CLOB API
274
+ * @param amount - Amount of tokens to merge
275
+ * @returns MergeResult with transaction details
276
+ */
277
+ async mergeByTokenIds(conditionId, tokenIds, amount) {
278
+ const amountWei = ethers.utils.parseUnits(amount, USDC_DECIMALS);
279
+ // Check token balances using the provided token IDs
280
+ const balances = await this.getPositionBalanceByTokenIds(conditionId, tokenIds);
281
+ const yesBalance = ethers.utils.parseUnits(balances.yesBalance, USDC_DECIMALS);
282
+ const noBalance = ethers.utils.parseUnits(balances.noBalance, USDC_DECIMALS);
283
+ if (yesBalance.lt(amountWei) || noBalance.lt(amountWei)) {
284
+ throw new Error(`Insufficient token balance. Need ${amount} of each. Have: YES=${balances.yesBalance}, NO=${balances.noBalance}`);
285
+ }
286
+ // Execute merge
287
+ const tx = await this.ctfContract.mergePositions(USDC_CONTRACT, ethers.constants.HashZero, conditionId, [1, 2], amountWei, await this.getGasOptions());
288
+ const receipt = await tx.wait();
289
+ return {
290
+ success: true,
291
+ txHash: receipt.transactionHash,
292
+ amount,
293
+ usdcReceived: amount, // 1:1 merge
294
+ gasUsed: receipt.gasUsed.toString(),
295
+ };
296
+ }
297
+ /**
298
+ * Redeem winning tokens after market resolution (Standard CTF)
299
+ *
300
+ * ⚠️ IMPORTANT: This method uses standard CTF position ID calculation.
301
+ * It is ONLY suitable for:
302
+ * - Standard Gnosis CTF markets (non-Polymarket)
303
+ * - Markets where position IDs are calculated from conditionId using standard formula
304
+ * - Direct CTF contract interactions without CLOB
305
+ *
306
+ * ❌ DO NOT USE for Polymarket CLOB markets!
307
+ * Polymarket uses custom token IDs that differ from standard CTF position IDs.
308
+ * For Polymarket, use `redeemByTokenIds()` instead.
309
+ *
310
+ * Position ID calculation: keccak256(collectionId, conditionId, indexSet)
311
+ * - This formula may NOT match Polymarket's token IDs
312
+ *
313
+ * @param conditionId - Market condition ID
314
+ * @param outcome - 'YES' or 'NO' (optional, auto-detects if not provided)
315
+ * @returns RedeemResult with transaction details
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * // For standard CTF markets (NOT Polymarket)
320
+ * const result = await ctf.redeem(conditionId);
321
+ * console.log(`Redeemed ${result.tokensRedeemed} ${result.outcome} tokens`);
322
+ * ```
323
+ *
324
+ * @see redeemByTokenIds - Use this for Polymarket CLOB markets
325
+ */
326
+ async redeem(conditionId, outcome) {
327
+ // Check resolution status
328
+ const resolution = await this.getMarketResolution(conditionId);
329
+ if (!resolution.isResolved) {
330
+ throw new Error('Market is not resolved yet');
331
+ }
332
+ // Auto-detect outcome if not provided
333
+ const winningOutcome = outcome || resolution.winningOutcome;
334
+ if (!winningOutcome) {
335
+ throw new Error('Could not determine winning outcome');
336
+ }
337
+ // Get token balance
338
+ const balances = await this.getPositionBalance(conditionId);
339
+ const tokenBalance = winningOutcome === 'YES' ? balances.yesBalance : balances.noBalance;
340
+ if (parseFloat(tokenBalance) === 0) {
341
+ throw new Error(`No ${winningOutcome} tokens to redeem`);
342
+ }
343
+ // indexSets: [1] for YES, [2] for NO
344
+ const indexSets = winningOutcome === 'YES' ? [1] : [2];
345
+ const tx = await this.ctfContract.redeemPositions(USDC_CONTRACT, ethers.constants.HashZero, conditionId, indexSets, await this.getGasOptions());
346
+ const receipt = await tx.wait();
347
+ return {
348
+ success: true,
349
+ txHash: receipt.transactionHash,
350
+ outcome: winningOutcome,
351
+ tokensRedeemed: tokenBalance,
352
+ usdcReceived: tokenBalance, // 1:1 for winning outcome
353
+ gasUsed: receipt.gasUsed.toString(),
354
+ };
355
+ }
356
+ /**
357
+ * Redeem winning tokens using Polymarket token IDs (Polymarket CLOB)
358
+ *
359
+ * ✅ USE THIS for Polymarket CLOB markets!
360
+ *
361
+ * Polymarket uses custom token IDs that are different from standard CTF position IDs.
362
+ * These token IDs are provided by the CLOB API and must be used for:
363
+ * - Querying balances (getPositionBalanceByTokenIds)
364
+ * - Redeeming positions (this method)
365
+ * - Trading via CLOB API
366
+ *
367
+ * Why Polymarket token IDs differ:
368
+ * - Polymarket wraps CTF positions into ERC-1155 tokens with custom IDs
369
+ * - The token IDs from CLOB API (e.g., "25064375...") are NOT the same as
370
+ * calculated position IDs from keccak256(collectionId, conditionId, indexSet)
371
+ *
372
+ * @param conditionId - The condition ID of the market
373
+ * @param tokenIds - The Polymarket token IDs for YES and NO outcomes (from CLOB API)
374
+ * @param outcome - Optional: which outcome to redeem ('YES' or 'NO'). Auto-detects if not provided.
375
+ * @returns RedeemResult with transaction details
376
+ *
377
+ * @example
378
+ * ```typescript
379
+ * // For Polymarket CLOB markets
380
+ * const tokenIds = {
381
+ * yesTokenId: '25064375110792967023484002819116042931016336431092144471807003884255851454283',
382
+ * noTokenId: '98190367690492181203391990709979106077460946443309150166954079213761598385827',
383
+ * };
384
+ * const result = await ctf.redeemByTokenIds(conditionId, tokenIds);
385
+ * console.log(`Redeemed ${result.tokensRedeemed} ${result.outcome} tokens`);
386
+ * console.log(`Received ${result.usdcReceived} USDC`);
387
+ * ```
388
+ *
389
+ * @see redeem - Only use for standard CTF markets (non-Polymarket)
390
+ */
391
+ async redeemByTokenIds(conditionId, tokenIds, outcome) {
392
+ // Check resolution status
393
+ const resolution = await this.getMarketResolution(conditionId);
394
+ if (!resolution.isResolved) {
395
+ throw new Error('Market is not resolved yet');
396
+ }
397
+ // Auto-detect outcome if not provided
398
+ const winningOutcome = outcome || resolution.winningOutcome;
399
+ if (!winningOutcome) {
400
+ throw new Error('Could not determine winning outcome');
401
+ }
402
+ // Get token balance using Polymarket token IDs
403
+ const balances = await this.getPositionBalanceByTokenIds(conditionId, tokenIds);
404
+ const tokenBalance = winningOutcome === 'YES' ? balances.yesBalance : balances.noBalance;
405
+ if (parseFloat(tokenBalance) === 0) {
406
+ throw new Error(`No ${winningOutcome} tokens to redeem`);
407
+ }
408
+ // indexSets: [1] for YES, [2] for NO
409
+ const indexSets = winningOutcome === 'YES' ? [1] : [2];
410
+ const tx = await this.ctfContract.redeemPositions(USDC_CONTRACT, ethers.constants.HashZero, conditionId, indexSets, await this.getGasOptions());
411
+ const receipt = await tx.wait();
412
+ return {
413
+ success: true,
414
+ txHash: receipt.transactionHash,
415
+ outcome: winningOutcome,
416
+ tokensRedeemed: tokenBalance,
417
+ usdcReceived: tokenBalance, // 1:1 for winning outcome
418
+ gasUsed: receipt.gasUsed.toString(),
419
+ };
420
+ }
421
+ /**
422
+ * Get token balances for a market using calculated position IDs
423
+ *
424
+ * NOTE: This method calculates position IDs from conditionId, which may not match
425
+ * the token IDs used by Polymarket's CLOB API. For accurate balances when working
426
+ * with CLOB markets, use getPositionBalanceByTokenIds() with the token IDs from
427
+ * the CLOB API.
428
+ *
429
+ * @deprecated Use getPositionBalanceByTokenIds for CLOB markets
430
+ */
431
+ async getPositionBalance(conditionId) {
432
+ const yesPositionId = this.calculatePositionId(conditionId, 1);
433
+ const noPositionId = this.calculatePositionId(conditionId, 2);
434
+ const [yesBalance, noBalance] = await Promise.all([
435
+ this.ctfContract.balanceOf(this.wallet.address, yesPositionId),
436
+ this.ctfContract.balanceOf(this.wallet.address, noPositionId),
437
+ ]);
438
+ return {
439
+ conditionId,
440
+ yesBalance: ethers.utils.formatUnits(yesBalance, USDC_DECIMALS),
441
+ noBalance: ethers.utils.formatUnits(noBalance, USDC_DECIMALS),
442
+ yesPositionId,
443
+ noPositionId,
444
+ };
445
+ }
446
+ /**
447
+ * Get token balances using CLOB API token IDs
448
+ *
449
+ * This is the recommended method for checking balances when working with
450
+ * Polymarket CLOB markets. The token IDs should be obtained from the CLOB API
451
+ * (e.g., from ClobApiClient.getMarket()).
452
+ *
453
+ * @param conditionId - Market condition ID (for reference)
454
+ * @param tokenIds - Token IDs from CLOB API { yesTokenId, noTokenId }
455
+ * @returns PositionBalance with accurate balances
456
+ *
457
+ * @example
458
+ * ```typescript
459
+ * // Get token IDs from CLOB API
460
+ * const market = await clobApi.getMarket(conditionId);
461
+ * const tokenIds = {
462
+ * yesTokenId: market.tokens[0].tokenId,
463
+ * noTokenId: market.tokens[1].tokenId,
464
+ * };
465
+ *
466
+ * // Check balances
467
+ * const balance = await ctf.getPositionBalanceByTokenIds(conditionId, tokenIds);
468
+ * console.log(`YES: ${balance.yesBalance}, NO: ${balance.noBalance}`);
469
+ * ```
470
+ */
471
+ async getPositionBalanceByTokenIds(conditionId, tokenIds) {
472
+ const [yesBalance, noBalance] = await Promise.all([
473
+ this.ctfContract.balanceOf(this.wallet.address, tokenIds.yesTokenId),
474
+ this.ctfContract.balanceOf(this.wallet.address, tokenIds.noTokenId),
475
+ ]);
476
+ return {
477
+ conditionId,
478
+ yesBalance: ethers.utils.formatUnits(yesBalance, USDC_DECIMALS),
479
+ noBalance: ethers.utils.formatUnits(noBalance, USDC_DECIMALS),
480
+ yesPositionId: tokenIds.yesTokenId,
481
+ noPositionId: tokenIds.noTokenId,
482
+ };
483
+ }
484
+ /**
485
+ * Check if a market is resolved and get payout info
486
+ */
487
+ async getMarketResolution(conditionId) {
488
+ const [yesNumerator, noNumerator, denominator] = await Promise.all([
489
+ this.ctfContract.payoutNumerators(conditionId, 0),
490
+ this.ctfContract.payoutNumerators(conditionId, 1),
491
+ this.ctfContract.payoutDenominator(conditionId),
492
+ ]);
493
+ const isResolved = denominator.gt(0);
494
+ let winningOutcome;
495
+ if (isResolved) {
496
+ if (yesNumerator.gt(0) && noNumerator.eq(0)) {
497
+ winningOutcome = 'YES';
498
+ }
499
+ else if (noNumerator.gt(0) && yesNumerator.eq(0)) {
500
+ winningOutcome = 'NO';
501
+ }
502
+ // If both are non-zero, it's a split resolution (rare)
503
+ }
504
+ return {
505
+ conditionId,
506
+ isResolved,
507
+ winningOutcome,
508
+ payoutNumerators: [yesNumerator.toNumber(), noNumerator.toNumber()],
509
+ payoutDenominator: denominator.toNumber(),
510
+ };
511
+ }
512
+ /**
513
+ * Estimate gas for split operation
514
+ */
515
+ async estimateSplitGas(conditionId, amount) {
516
+ const amountWei = ethers.utils.parseUnits(amount, USDC_DECIMALS);
517
+ try {
518
+ const gas = await this.ctfContract.estimateGas.splitPosition(USDC_CONTRACT, ethers.constants.HashZero, conditionId, [1, 2], amountWei);
519
+ return gas.toString();
520
+ }
521
+ catch {
522
+ // Default estimate if call fails (e.g., insufficient balance)
523
+ return '250000';
524
+ }
525
+ }
526
+ /**
527
+ * Estimate gas for merge operation
528
+ */
529
+ async estimateMergeGas(conditionId, amount) {
530
+ const amountWei = ethers.utils.parseUnits(amount, USDC_DECIMALS);
531
+ try {
532
+ const gas = await this.ctfContract.estimateGas.mergePositions(USDC_CONTRACT, ethers.constants.HashZero, conditionId, [1, 2], amountWei);
533
+ return gas.toString();
534
+ }
535
+ catch {
536
+ return '200000';
537
+ }
538
+ }
539
+ // ===== Gas Estimation (Phase 3) =====
540
+ /**
541
+ * Get detailed gas estimate for a split operation
542
+ */
543
+ async getDetailedSplitGasEstimate(conditionId, amount) {
544
+ const gasUnits = await this.estimateSplitGas(conditionId, amount);
545
+ return this.calculateGasCost(gasUnits);
546
+ }
547
+ /**
548
+ * Get detailed gas estimate for a merge operation
549
+ */
550
+ async getDetailedMergeGasEstimate(conditionId, amount) {
551
+ const gasUnits = await this.estimateMergeGas(conditionId, amount);
552
+ return this.calculateGasCost(gasUnits);
553
+ }
554
+ /**
555
+ * Get current gas price info
556
+ */
557
+ async getGasPrice() {
558
+ const gasPrice = await this.provider.getGasPrice();
559
+ return {
560
+ gwei: ethers.utils.formatUnits(gasPrice, 'gwei'),
561
+ wei: gasPrice.toString(),
562
+ };
563
+ }
564
+ /**
565
+ * Get or refresh MATIC price (cached for 5 minutes)
566
+ */
567
+ async getMaticPrice() {
568
+ const now = Date.now();
569
+ const cacheAge = now - this.maticPriceLastUpdated;
570
+ // Use cache if less than 5 minutes old
571
+ if (cacheAge < 5 * 60 * 1000 && this.maticPriceLastUpdated > 0) {
572
+ return this.cachedMaticPrice;
573
+ }
574
+ // In production, this would fetch from an oracle or price feed
575
+ // For now, we return a reasonable estimate
576
+ // Could integrate with Chainlink price feeds or CoinGecko API
577
+ this.cachedMaticPrice = DEFAULT_MATIC_PRICE;
578
+ this.maticPriceLastUpdated = now;
579
+ return this.cachedMaticPrice;
580
+ }
581
+ /**
582
+ * Set MATIC price manually (for testing or when external price is available)
583
+ */
584
+ setMaticPrice(price) {
585
+ this.cachedMaticPrice = price;
586
+ this.maticPriceLastUpdated = Date.now();
587
+ }
588
+ // ===== Transaction Monitoring (Phase 3) =====
589
+ /**
590
+ * Get transaction status with detailed info
591
+ */
592
+ async getTransactionStatus(txHash) {
593
+ try {
594
+ const receipt = await this.provider.getTransactionReceipt(txHash);
595
+ if (!receipt) {
596
+ // Transaction is pending
597
+ const tx = await this.provider.getTransaction(txHash);
598
+ if (!tx) {
599
+ return {
600
+ txHash,
601
+ status: 'failed',
602
+ confirmations: 0,
603
+ errorReason: 'Transaction not found',
604
+ };
605
+ }
606
+ return {
607
+ txHash,
608
+ status: 'pending',
609
+ confirmations: 0,
610
+ };
611
+ }
612
+ const currentBlock = await this.provider.getBlockNumber();
613
+ const confirmations = currentBlock - receipt.blockNumber + 1;
614
+ if (receipt.status === 0) {
615
+ // Transaction reverted
616
+ const reason = await this.getRevertReason(txHash);
617
+ return {
618
+ txHash,
619
+ status: 'reverted',
620
+ confirmations,
621
+ blockNumber: receipt.blockNumber,
622
+ gasUsed: receipt.gasUsed.toString(),
623
+ effectiveGasPrice: receipt.effectiveGasPrice?.toString(),
624
+ errorReason: reason,
625
+ };
626
+ }
627
+ return {
628
+ txHash,
629
+ status: 'confirmed',
630
+ confirmations,
631
+ blockNumber: receipt.blockNumber,
632
+ gasUsed: receipt.gasUsed.toString(),
633
+ effectiveGasPrice: receipt.effectiveGasPrice?.toString(),
634
+ };
635
+ }
636
+ catch (error) {
637
+ return {
638
+ txHash,
639
+ status: 'failed',
640
+ confirmations: 0,
641
+ errorReason: error instanceof Error ? error.message : 'Unknown error',
642
+ };
643
+ }
644
+ }
645
+ /**
646
+ * Wait for transaction confirmation with timeout
647
+ */
648
+ async waitForTransaction(txHash, confirmations) {
649
+ const targetConfirmations = confirmations ?? this.confirmations;
650
+ const startTime = Date.now();
651
+ while (Date.now() - startTime < this.txTimeout) {
652
+ const status = await this.getTransactionStatus(txHash);
653
+ if (status.status === 'reverted' || status.status === 'failed') {
654
+ return status;
655
+ }
656
+ if (status.status === 'confirmed' && status.confirmations >= targetConfirmations) {
657
+ return status;
658
+ }
659
+ // Wait 2 seconds before checking again
660
+ await new Promise(resolve => setTimeout(resolve, 2000));
661
+ }
662
+ return {
663
+ txHash,
664
+ status: 'pending',
665
+ confirmations: 0,
666
+ errorReason: `Timeout after ${this.txTimeout}ms`,
667
+ };
668
+ }
669
+ /**
670
+ * Parse revert reason from transaction
671
+ */
672
+ async getRevertReason(txHash) {
673
+ try {
674
+ const tx = await this.provider.getTransaction(txHash);
675
+ if (!tx)
676
+ return RevertReason.UNKNOWN;
677
+ const receipt = await this.provider.getTransactionReceipt(txHash);
678
+ if (!receipt || receipt.status !== 0)
679
+ return RevertReason.UNKNOWN;
680
+ // Try to call the transaction to get the revert reason
681
+ try {
682
+ await this.provider.call(tx, tx.blockNumber);
683
+ return RevertReason.UNKNOWN;
684
+ }
685
+ catch (error) {
686
+ const err = error;
687
+ if (err.reason)
688
+ return err.reason;
689
+ if (err.message) {
690
+ // Parse common error messages
691
+ if (err.message.includes('insufficient balance')) {
692
+ return RevertReason.INSUFFICIENT_BALANCE;
693
+ }
694
+ if (err.message.includes('allowance')) {
695
+ return RevertReason.INSUFFICIENT_ALLOWANCE;
696
+ }
697
+ if (err.message.includes('condition not resolved')) {
698
+ return RevertReason.CONDITION_NOT_RESOLVED;
699
+ }
700
+ return err.message;
701
+ }
702
+ return RevertReason.EXECUTION_REVERTED;
703
+ }
704
+ }
705
+ catch {
706
+ return RevertReason.UNKNOWN;
707
+ }
708
+ }
709
+ // ===== Position Tracking (Phase 3) =====
710
+ /**
711
+ * Get all positions for the wallet across multiple markets
712
+ */
713
+ async getAllPositions(conditionIds) {
714
+ const positions = [];
715
+ for (const conditionId of conditionIds) {
716
+ try {
717
+ const balance = await this.getPositionBalance(conditionId);
718
+ // Only include non-zero balances
719
+ if (parseFloat(balance.yesBalance) > 0 || parseFloat(balance.noBalance) > 0) {
720
+ positions.push(balance);
721
+ }
722
+ }
723
+ catch {
724
+ // Skip errors for individual markets
725
+ }
726
+ }
727
+ return positions;
728
+ }
729
+ /**
730
+ * Check if wallet has sufficient tokens for merge
731
+ *
732
+ * @deprecated Use canMergeWithTokenIds for CLOB markets
733
+ */
734
+ async canMerge(conditionId, amount) {
735
+ try {
736
+ const balances = await this.getPositionBalance(conditionId);
737
+ return this.checkMergeBalance(balances, amount);
738
+ }
739
+ catch (error) {
740
+ return {
741
+ canMerge: false,
742
+ reason: error instanceof Error ? error.message : 'Failed to check balances'
743
+ };
744
+ }
745
+ }
746
+ /**
747
+ * Check if wallet has sufficient tokens for merge using CLOB token IDs
748
+ *
749
+ * @param conditionId - Market condition ID
750
+ * @param tokenIds - Token IDs from CLOB API
751
+ * @param amount - Amount to merge
752
+ */
753
+ async canMergeWithTokenIds(conditionId, tokenIds, amount) {
754
+ try {
755
+ const balances = await this.getPositionBalanceByTokenIds(conditionId, tokenIds);
756
+ return this.checkMergeBalance(balances, amount);
757
+ }
758
+ catch (error) {
759
+ return {
760
+ canMerge: false,
761
+ reason: error instanceof Error ? error.message : 'Failed to check balances'
762
+ };
763
+ }
764
+ }
765
+ checkMergeBalance(balances, amount) {
766
+ const amountNum = parseFloat(amount);
767
+ const yesBalance = parseFloat(balances.yesBalance);
768
+ const noBalance = parseFloat(balances.noBalance);
769
+ if (yesBalance < amountNum) {
770
+ return {
771
+ canMerge: false,
772
+ reason: `Insufficient YES tokens. Have: ${yesBalance}, Need: ${amountNum}`
773
+ };
774
+ }
775
+ if (noBalance < amountNum) {
776
+ return {
777
+ canMerge: false,
778
+ reason: `Insufficient NO tokens. Have: ${noBalance}, Need: ${amountNum}`
779
+ };
780
+ }
781
+ return { canMerge: true };
782
+ }
783
+ /**
784
+ * Check if wallet has sufficient USDC for split
785
+ */
786
+ async canSplit(amount) {
787
+ try {
788
+ const balance = await this.getUsdcBalance();
789
+ const balanceNum = parseFloat(balance);
790
+ const amountNum = parseFloat(amount);
791
+ if (balanceNum < amountNum) {
792
+ return {
793
+ canSplit: false,
794
+ reason: `Insufficient USDC. Have: ${balance}, Need: ${amount}`
795
+ };
796
+ }
797
+ return { canSplit: true };
798
+ }
799
+ catch (error) {
800
+ return {
801
+ canSplit: false,
802
+ reason: error instanceof Error ? error.message : 'Failed to check balance'
803
+ };
804
+ }
805
+ }
806
+ /**
807
+ * Get total portfolio value across positions
808
+ */
809
+ async getPortfolioValue(positions, prices) {
810
+ let totalValue = 0;
811
+ const breakdown = [];
812
+ for (const position of positions) {
813
+ const price = prices.get(position.conditionId);
814
+ if (!price)
815
+ continue;
816
+ const yesValue = parseFloat(position.yesBalance) * price.yes;
817
+ const noValue = parseFloat(position.noBalance) * price.no;
818
+ const positionValue = yesValue + noValue;
819
+ totalValue += positionValue;
820
+ breakdown.push({
821
+ conditionId: position.conditionId,
822
+ yesValue,
823
+ noValue,
824
+ totalValue: positionValue,
825
+ });
826
+ }
827
+ return { totalValue, breakdown };
828
+ }
829
+ // ===== Private Helpers =====
830
+ /**
831
+ * Calculate position ID for a given outcome (INTERNAL USE ONLY)
832
+ *
833
+ * ⚠️ WARNING: This calculation does NOT produce correct Polymarket token IDs!
834
+ *
835
+ * Polymarket uses custom token IDs that differ from standard CTF position ID calculation.
836
+ * The token IDs from CLOB API (e.g., "104173557214744537570424345347209544585775842950109756851652855913015295701992")
837
+ * are NOT the same as what this function calculates.
838
+ *
839
+ * For Polymarket CLOB markets, ALWAYS:
840
+ * 1. Get token IDs from CLOB API: https://clob.polymarket.com/markets/{conditionId}
841
+ * 2. Use getPositionBalanceByTokenIds() instead of getPositionBalance()
842
+ * 3. Use mergeByTokenIds() instead of merge()
843
+ * 4. Use redeemByTokenIds() instead of redeem()
844
+ *
845
+ * This method is kept for potential non-Polymarket CTF markets only.
846
+ *
847
+ * @deprecated Use CLOB API token IDs for Polymarket markets
848
+ */
849
+ calculatePositionId(conditionId, indexSet) {
850
+ // Collection ID - must use solidityPack (abi.encodePacked) to match CTF contract
851
+ const collectionId = ethers.utils.keccak256(ethers.utils.solidityPack(['bytes32', 'bytes32', 'uint256'], [ethers.constants.HashZero, conditionId, indexSet]));
852
+ // Position ID - must use solidityPack (abi.encodePacked) to match CTF contract
853
+ const positionId = ethers.utils.keccak256(ethers.utils.solidityPack(['address', 'bytes32'], [USDC_CONTRACT, collectionId]));
854
+ return positionId;
855
+ }
856
+ /**
857
+ * Get gas options for Polygon network using EIP-1559
858
+ *
859
+ * Polygon requires higher priority fees than default ethers.js estimates.
860
+ * Uses minimum 30 gwei priority fee to ensure transactions don't get stuck.
861
+ */
862
+ async getGasOptions() {
863
+ const feeData = await this.provider.getFeeData();
864
+ const baseFee = feeData.lastBaseFeePerGas || feeData.gasPrice || ethers.utils.parseUnits('100', 'gwei');
865
+ // Minimum 30 gwei priority fee for Polygon
866
+ const minPriorityFee = ethers.utils.parseUnits('30', 'gwei');
867
+ const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas && feeData.maxPriorityFeePerGas.gt(minPriorityFee)
868
+ ? feeData.maxPriorityFeePerGas
869
+ : minPriorityFee;
870
+ // Apply multiplier to base fee and add priority fee
871
+ const adjustedBaseFee = baseFee.mul(Math.floor(this.gasPriceMultiplier * 100)).div(100);
872
+ const maxFeePerGas = adjustedBaseFee.add(maxPriorityFeePerGas);
873
+ return { maxPriorityFeePerGas, maxFeePerGas };
874
+ }
875
+ /**
876
+ * Calculate gas cost from gas units
877
+ */
878
+ async calculateGasCost(gasUnits) {
879
+ const gasOptions = await this.getGasOptions();
880
+ const effectiveGasPrice = gasOptions.maxFeePerGas;
881
+ const gasUnitsNum = BigNumber.from(gasUnits);
882
+ const costWei = gasUnitsNum.mul(effectiveGasPrice);
883
+ const costMatic = parseFloat(ethers.utils.formatEther(costWei));
884
+ const maticPrice = await this.getMaticPrice();
885
+ const costUsdc = costMatic * maticPrice;
886
+ return {
887
+ gasUnits,
888
+ gasPriceGwei: ethers.utils.formatUnits(effectiveGasPrice, 'gwei'),
889
+ costMatic: costMatic.toFixed(6),
890
+ costUsdc: costUsdc.toFixed(4),
891
+ maticPrice,
892
+ };
893
+ }
894
+ }
895
+ // ===== Utility Functions =====
896
+ /**
897
+ * Calculate condition ID from oracle, question ID, and outcome count
898
+ * This is rarely needed as Polymarket provides conditionId directly
899
+ */
900
+ export function calculateConditionId(oracle, questionId, outcomeSlotCount = 2) {
901
+ return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['address', 'bytes32', 'uint256'], [oracle, questionId, outcomeSlotCount]));
902
+ }
903
+ /**
904
+ * Parse USDC amount to BigNumber (6 decimals)
905
+ */
906
+ export function parseUsdc(amount) {
907
+ return ethers.utils.parseUnits(amount, USDC_DECIMALS);
908
+ }
909
+ /**
910
+ * Format BigNumber to USDC string (6 decimals)
911
+ */
912
+ export function formatUsdc(amount) {
913
+ return ethers.utils.formatUnits(amount, USDC_DECIMALS);
914
+ }
915
+ //# sourceMappingURL=ctf-client.js.map