@catalyst-team/poly-sdk 0.2.0 → 0.3.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 (242) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +548 -813
  3. package/README.zh-CN.md +805 -0
  4. package/dist/__tests__/clob-api.test.d.ts +5 -0
  5. package/dist/__tests__/clob-api.test.d.ts.map +1 -0
  6. package/dist/__tests__/clob-api.test.js +240 -0
  7. package/dist/__tests__/clob-api.test.js.map +1 -0
  8. package/dist/__tests__/integration/arbitrage-service.integration.test.d.ts +12 -0
  9. package/dist/__tests__/integration/arbitrage-service.integration.test.d.ts.map +1 -0
  10. package/dist/__tests__/integration/arbitrage-service.integration.test.js +267 -0
  11. package/dist/__tests__/integration/arbitrage-service.integration.test.js.map +1 -0
  12. package/dist/__tests__/integration/bridge-client.integration.test.d.ts +11 -0
  13. package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +1 -0
  14. package/dist/__tests__/integration/bridge-client.integration.test.js +260 -0
  15. package/dist/__tests__/integration/bridge-client.integration.test.js.map +1 -0
  16. package/dist/__tests__/integration/clob-api.integration.test.d.ts +13 -0
  17. package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +1 -0
  18. package/dist/__tests__/integration/clob-api.integration.test.js +170 -0
  19. package/dist/__tests__/integration/clob-api.integration.test.js.map +1 -0
  20. package/dist/__tests__/integration/ctf-client.integration.test.d.ts +17 -0
  21. package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +1 -0
  22. package/dist/__tests__/integration/ctf-client.integration.test.js +234 -0
  23. package/dist/__tests__/integration/ctf-client.integration.test.js.map +1 -0
  24. package/dist/__tests__/integration/data-api.integration.test.d.ts +9 -0
  25. package/dist/__tests__/integration/data-api.integration.test.d.ts.map +1 -0
  26. package/dist/__tests__/integration/data-api.integration.test.js +164 -0
  27. package/dist/__tests__/integration/data-api.integration.test.js.map +1 -0
  28. package/dist/__tests__/integration/gamma-api.integration.test.d.ts +9 -0
  29. package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +1 -0
  30. package/dist/__tests__/integration/gamma-api.integration.test.js +170 -0
  31. package/dist/__tests__/integration/gamma-api.integration.test.js.map +1 -0
  32. package/dist/__tests__/integration/market-service.integration.test.d.ts +10 -0
  33. package/dist/__tests__/integration/market-service.integration.test.d.ts.map +1 -0
  34. package/dist/__tests__/integration/market-service.integration.test.js +173 -0
  35. package/dist/__tests__/integration/market-service.integration.test.js.map +1 -0
  36. package/dist/__tests__/integration/realtime-service-v2.integration.test.d.ts +10 -0
  37. package/dist/__tests__/integration/realtime-service-v2.integration.test.d.ts.map +1 -0
  38. package/dist/__tests__/integration/realtime-service-v2.integration.test.js +307 -0
  39. package/dist/__tests__/integration/realtime-service-v2.integration.test.js.map +1 -0
  40. package/dist/__tests__/integration/trading-service.integration.test.d.ts +10 -0
  41. package/dist/__tests__/integration/trading-service.integration.test.d.ts.map +1 -0
  42. package/dist/__tests__/integration/trading-service.integration.test.js +58 -0
  43. package/dist/__tests__/integration/trading-service.integration.test.js.map +1 -0
  44. package/dist/__tests__/test-utils.d.ts +92 -0
  45. package/dist/__tests__/test-utils.d.ts.map +1 -0
  46. package/dist/__tests__/test-utils.js +143 -0
  47. package/dist/__tests__/test-utils.js.map +1 -0
  48. package/dist/clients/bridge-client.d.ts +388 -0
  49. package/dist/clients/bridge-client.d.ts.map +1 -0
  50. package/dist/clients/bridge-client.js +587 -0
  51. package/dist/clients/bridge-client.js.map +1 -0
  52. package/dist/clients/clob-api.d.ts +391 -0
  53. package/dist/clients/clob-api.d.ts.map +1 -0
  54. package/dist/clients/clob-api.js +448 -0
  55. package/dist/clients/clob-api.js.map +1 -0
  56. package/dist/clients/ctf-client.d.ts +473 -0
  57. package/dist/clients/ctf-client.d.ts.map +1 -0
  58. package/dist/clients/ctf-client.js +915 -0
  59. package/dist/clients/ctf-client.js.map +1 -0
  60. package/dist/clients/data-api.d.ts +439 -0
  61. package/dist/clients/data-api.d.ts.map +1 -0
  62. package/dist/clients/data-api.js +592 -0
  63. package/dist/clients/data-api.js.map +1 -0
  64. package/dist/clients/gamma-api.d.ts +401 -0
  65. package/dist/clients/gamma-api.d.ts.map +1 -0
  66. package/dist/clients/gamma-api.js +352 -0
  67. package/dist/clients/gamma-api.js.map +1 -0
  68. package/dist/clients/subgraph.d.ts +196 -0
  69. package/dist/clients/subgraph.d.ts.map +1 -0
  70. package/dist/clients/subgraph.js +332 -0
  71. package/dist/clients/subgraph.js.map +1 -0
  72. package/dist/clients/trading-client.d.ts +252 -0
  73. package/dist/clients/trading-client.d.ts.map +1 -0
  74. package/dist/clients/trading-client.js +543 -0
  75. package/dist/clients/trading-client.js.map +1 -0
  76. package/dist/clients/websocket-manager.d.ts +103 -0
  77. package/dist/clients/websocket-manager.d.ts.map +1 -0
  78. package/dist/clients/websocket-manager.js +200 -0
  79. package/dist/clients/websocket-manager.js.map +1 -0
  80. package/dist/core/cache-adapter-bridge.d.ts +36 -0
  81. package/dist/core/cache-adapter-bridge.d.ts.map +1 -0
  82. package/dist/core/cache-adapter-bridge.js +81 -0
  83. package/dist/core/cache-adapter-bridge.js.map +1 -0
  84. package/dist/core/cache.d.ts +41 -0
  85. package/dist/core/cache.d.ts.map +1 -0
  86. package/dist/core/cache.js +72 -0
  87. package/dist/core/cache.js.map +1 -0
  88. package/dist/core/errors.d.ts +39 -0
  89. package/dist/core/errors.d.ts.map +1 -0
  90. package/dist/core/errors.js +86 -0
  91. package/dist/core/errors.js.map +1 -0
  92. package/dist/core/rate-limiter.d.ts +32 -0
  93. package/dist/core/rate-limiter.d.ts.map +1 -0
  94. package/dist/core/rate-limiter.js +75 -0
  95. package/dist/core/rate-limiter.js.map +1 -0
  96. package/dist/core/types.d.ts +402 -0
  97. package/dist/core/types.d.ts.map +1 -0
  98. package/dist/core/types.js +19 -0
  99. package/dist/core/types.js.map +1 -0
  100. package/dist/core/types.test.d.ts +7 -0
  101. package/dist/core/types.test.d.ts.map +1 -0
  102. package/dist/core/types.test.js +122 -0
  103. package/dist/core/types.test.js.map +1 -0
  104. package/dist/core/unified-cache.d.ts +63 -0
  105. package/dist/core/unified-cache.d.ts.map +1 -0
  106. package/dist/core/unified-cache.js +114 -0
  107. package/dist/core/unified-cache.js.map +1 -0
  108. package/dist/index.d.ts +151 -0
  109. package/dist/index.d.ts.map +1 -0
  110. package/dist/index.js +248 -0
  111. package/dist/index.js.map +1 -0
  112. package/dist/services/arbitrage-service.d.ts +409 -0
  113. package/dist/services/arbitrage-service.d.ts.map +1 -0
  114. package/dist/services/arbitrage-service.js +1440 -0
  115. package/dist/services/arbitrage-service.js.map +1 -0
  116. package/dist/services/authorization-service.d.ts +97 -0
  117. package/dist/services/authorization-service.d.ts.map +1 -0
  118. package/dist/services/authorization-service.js +279 -0
  119. package/dist/services/authorization-service.js.map +1 -0
  120. package/dist/services/market-service.d.ts +208 -0
  121. package/dist/services/market-service.d.ts.map +1 -0
  122. package/dist/services/market-service.js +774 -0
  123. package/dist/services/market-service.js.map +1 -0
  124. package/dist/services/onchain-service.d.ts +309 -0
  125. package/dist/services/onchain-service.d.ts.map +1 -0
  126. package/dist/services/onchain-service.js +417 -0
  127. package/dist/services/onchain-service.js.map +1 -0
  128. package/dist/services/realtime-service-v2.d.ts +361 -0
  129. package/dist/services/realtime-service-v2.d.ts.map +1 -0
  130. package/dist/services/realtime-service-v2.js +840 -0
  131. package/dist/services/realtime-service-v2.js.map +1 -0
  132. package/dist/services/realtime-service.d.ts +82 -0
  133. package/dist/services/realtime-service.d.ts.map +1 -0
  134. package/dist/services/realtime-service.js +182 -0
  135. package/dist/services/realtime-service.js.map +1 -0
  136. package/dist/services/smart-money-service.d.ts +196 -0
  137. package/dist/services/smart-money-service.d.ts.map +1 -0
  138. package/dist/services/smart-money-service.js +358 -0
  139. package/dist/services/smart-money-service.js.map +1 -0
  140. package/dist/services/swap-service.d.ts +217 -0
  141. package/dist/services/swap-service.d.ts.map +1 -0
  142. package/dist/services/swap-service.js +695 -0
  143. package/dist/services/swap-service.js.map +1 -0
  144. package/dist/services/trading-service.d.ts +156 -0
  145. package/dist/services/trading-service.d.ts.map +1 -0
  146. package/dist/services/trading-service.js +356 -0
  147. package/dist/services/trading-service.js.map +1 -0
  148. package/dist/services/wallet-service.d.ts +275 -0
  149. package/dist/services/wallet-service.d.ts.map +1 -0
  150. package/dist/services/wallet-service.js +630 -0
  151. package/dist/services/wallet-service.js.map +1 -0
  152. package/dist/utils/price-utils.d.ts +153 -0
  153. package/dist/utils/price-utils.d.ts.map +1 -0
  154. package/dist/utils/price-utils.js +236 -0
  155. package/dist/utils/price-utils.js.map +1 -0
  156. package/dist/utils/price-utils.test.d.ts +5 -0
  157. package/dist/utils/price-utils.test.d.ts.map +1 -0
  158. package/dist/utils/price-utils.test.js +192 -0
  159. package/dist/utils/price-utils.test.js.map +1 -0
  160. package/package.json +10 -4
  161. package/README.en.md +0 -538
  162. package/docs/00-design.md +0 -760
  163. package/docs/02-API.md +0 -1148
  164. package/docs/arb/test-plan.md +0 -387
  165. package/docs/arb/test-results.md +0 -336
  166. package/docs/arbitrage.md +0 -754
  167. package/docs/reports/smart-money-analysis-2025-12-23-cn.md +0 -840
  168. package/examples/01-basic-usage.ts +0 -68
  169. package/examples/02-smart-money.ts +0 -95
  170. package/examples/03-market-analysis.ts +0 -108
  171. package/examples/04-kline-aggregation.ts +0 -158
  172. package/examples/05-follow-wallet-strategy.ts +0 -156
  173. package/examples/06-services-demo.ts +0 -124
  174. package/examples/07-realtime-websocket.ts +0 -117
  175. package/examples/08-trading-orders.ts +0 -278
  176. package/examples/09-rewards-tracking.ts +0 -187
  177. package/examples/10-ctf-operations.ts +0 -336
  178. package/examples/11-live-arbitrage-scan.ts +0 -221
  179. package/examples/12-trending-arb-monitor.ts +0 -406
  180. package/examples/13-arbitrage-service.ts +0 -211
  181. package/examples/README.md +0 -179
  182. package/scripts/README.md +0 -163
  183. package/scripts/approvals/approve-erc1155.ts +0 -129
  184. package/scripts/approvals/approve-neg-risk-erc1155.ts +0 -149
  185. package/scripts/approvals/approve-neg-risk.ts +0 -102
  186. package/scripts/approvals/check-all-allowances.ts +0 -150
  187. package/scripts/approvals/check-allowance.ts +0 -129
  188. package/scripts/approvals/check-ctf-approval.ts +0 -158
  189. package/scripts/arb/faze-bo3-arb.ts +0 -385
  190. package/scripts/arb/settle-position.ts +0 -190
  191. package/scripts/arb/token-rebalancer.ts +0 -420
  192. package/scripts/arb-tests/01-unit-tests.ts +0 -495
  193. package/scripts/arb-tests/02-integration-tests.ts +0 -412
  194. package/scripts/arb-tests/03-e2e-tests.ts +0 -503
  195. package/scripts/arb-tests/README.md +0 -109
  196. package/scripts/datas/001-report.md +0 -486
  197. package/scripts/datas/clone-modal-screenshot.png +0 -0
  198. package/scripts/deposit/deposit-native-usdc.ts +0 -179
  199. package/scripts/deposit/deposit-usdc.ts +0 -155
  200. package/scripts/deposit/swap-usdc-to-usdce.ts +0 -375
  201. package/scripts/research/research-markets.ts +0 -166
  202. package/scripts/trading/check-orders.ts +0 -50
  203. package/scripts/trading/sell-nvidia-positions.ts +0 -206
  204. package/scripts/trading/test-order.ts +0 -172
  205. package/scripts/verify/test-approve-trading.ts +0 -98
  206. package/scripts/verify/test-provider-fix.ts +0 -43
  207. package/scripts/verify/test-search-mcp.ts +0 -113
  208. package/scripts/verify/verify-all-apis.ts +0 -160
  209. package/scripts/wallet/check-wallet-balances.ts +0 -75
  210. package/scripts/wallet/test-wallet-operations.ts +0 -191
  211. package/scripts/wallet/verify-wallet-tools.ts +0 -124
  212. package/src/__tests__/clob-api.test.ts +0 -301
  213. package/src/__tests__/integration/bridge-client.integration.test.ts +0 -314
  214. package/src/__tests__/integration/clob-api.integration.test.ts +0 -218
  215. package/src/__tests__/integration/ctf-client.integration.test.ts +0 -331
  216. package/src/__tests__/integration/data-api.integration.test.ts +0 -194
  217. package/src/__tests__/integration/gamma-api.integration.test.ts +0 -206
  218. package/src/__tests__/test-utils.ts +0 -170
  219. package/src/clients/bridge-client.ts +0 -841
  220. package/src/clients/clob-api.ts +0 -629
  221. package/src/clients/ctf-client.ts +0 -1216
  222. package/src/clients/data-api.ts +0 -469
  223. package/src/clients/gamma-api.ts +0 -597
  224. package/src/clients/trading-client.ts +0 -749
  225. package/src/clients/websocket-manager.ts +0 -267
  226. package/src/core/cache-adapter-bridge.ts +0 -94
  227. package/src/core/cache.ts +0 -85
  228. package/src/core/errors.ts +0 -117
  229. package/src/core/rate-limiter.ts +0 -74
  230. package/src/core/types.ts +0 -360
  231. package/src/core/unified-cache.ts +0 -153
  232. package/src/index.ts +0 -461
  233. package/src/services/arbitrage-service.ts +0 -1807
  234. package/src/services/authorization-service.ts +0 -357
  235. package/src/services/market-service.ts +0 -544
  236. package/src/services/realtime-service.ts +0 -196
  237. package/src/services/swap-service.ts +0 -896
  238. package/src/services/wallet-service.ts +0 -259
  239. package/src/utils/price-utils.ts +0 -307
  240. package/tsconfig.json +0 -8
  241. package/vitest.config.ts +0 -19
  242. package/vitest.integration.config.ts +0 -18
@@ -1,149 +0,0 @@
1
- #!/usr/bin/env npx tsx
2
- /**
3
- * Check and Approve ERC1155 for Neg Risk Adapter
4
- *
5
- * 检查并授权 Neg Risk Adapter 的 ERC1155 转账权限
6
- * 这可能是 SELL 订单在 Neg Risk 市场失败的原因
7
- */
8
-
9
- import { ethers } from 'ethers';
10
- import * as fs from 'fs';
11
- import * as path from 'path';
12
- import { fileURLToPath } from 'url';
13
-
14
- const __filename = fileURLToPath(import.meta.url);
15
- const __dirname = path.dirname(__filename);
16
-
17
- // Read private key
18
- const envPath = path.resolve(__dirname, '../../earning-engine/dashboard-api/.env');
19
- const envContent = fs.readFileSync(envPath, 'utf8');
20
- const match = envContent.match(/^PRIVATE_KEY=(.+)$/m);
21
- const PRIVATE_KEY = match ? match[1].trim() : '';
22
-
23
- if (!PRIVATE_KEY) {
24
- console.error('PRIVATE_KEY not found');
25
- process.exit(1);
26
- }
27
-
28
- // Contract addresses
29
- const CONDITIONAL_TOKENS = '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045';
30
- const CTF_EXCHANGE = '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E';
31
- const NEG_RISK_CTF_EXCHANGE = '0xC5d563A36AE78145C45a50134d48A1215220f80a';
32
- const NEG_RISK_ADAPTER = '0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296';
33
-
34
- const ERC1155_ABI = [
35
- 'function setApprovalForAll(address operator, bool approved) external',
36
- 'function isApprovedForAll(address account, address operator) view returns (bool)',
37
- 'function balanceOf(address account, uint256 id) view returns (uint256)',
38
- ];
39
-
40
- async function main() {
41
- console.log('╔════════════════════════════════════════════════════════════════╗');
42
- console.log('║ NEG RISK ERC1155 AUTHORIZATION CHECK ║');
43
- console.log('╚════════════════════════════════════════════════════════════════╝');
44
- console.log('');
45
-
46
- const rpcs = [
47
- 'https://polygon-bor-rpc.publicnode.com',
48
- 'https://polygon.drpc.org',
49
- 'https://polygon-rpc.com',
50
- ];
51
-
52
- for (const rpc of rpcs) {
53
- console.log(`Trying RPC: ${rpc}`);
54
- try {
55
- const provider = new ethers.providers.JsonRpcProvider({
56
- url: rpc,
57
- timeout: 30000,
58
- });
59
-
60
- const network = await provider.getNetwork();
61
- console.log(`Connected to chain ${network.chainId}`);
62
-
63
- const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
64
- console.log(`Wallet: ${wallet.address}`);
65
- console.log('');
66
-
67
- const conditionalTokens = new ethers.Contract(CONDITIONAL_TOKENS, ERC1155_ABI, wallet);
68
-
69
- // All operators to check
70
- const operators = [
71
- { name: 'CTF Exchange', address: CTF_EXCHANGE },
72
- { name: 'Neg Risk CTF Exchange', address: NEG_RISK_CTF_EXCHANGE },
73
- { name: 'Neg Risk Adapter', address: NEG_RISK_ADAPTER },
74
- ];
75
-
76
- // Check current approvals
77
- console.log('─── Current ERC1155 Approvals (Conditional Tokens) ───');
78
- let needsApproval = [];
79
-
80
- for (const op of operators) {
81
- const isApproved = await conditionalTokens.isApprovedForAll(wallet.address, op.address);
82
- console.log(`${op.name}: ${isApproved ? '✅ Approved' : '❌ Not Approved'}`);
83
- if (!isApproved) {
84
- needsApproval.push(op);
85
- }
86
- }
87
- console.log('');
88
-
89
- // Check token balances (YES and NO for NVIDIA market)
90
- const yesTokenId = '94850533403292240972948844256810904078895883844462287088135166537739765648754';
91
- const noTokenId = '69263280792958981516606123639467754139758192236863611059536531765186180114584';
92
-
93
- console.log('─── Token Balances ───');
94
- const yesBalance = await conditionalTokens.balanceOf(wallet.address, yesTokenId);
95
- const noBalance = await conditionalTokens.balanceOf(wallet.address, noTokenId);
96
- console.log(`YES Token: ${ethers.utils.formatUnits(yesBalance, 6)} tokens`);
97
- console.log(`NO Token: ${ethers.utils.formatUnits(noBalance, 6)} tokens`);
98
- console.log('');
99
-
100
- if (needsApproval.length === 0) {
101
- console.log('✅ All ERC1155 approvals are set!');
102
- console.log('');
103
- console.log('If SELL orders are still failing, the issue might be:');
104
- console.log('1. Neg Risk market uses wrapped tokens through the Adapter');
105
- console.log('2. The CLOB might require a different approval mechanism');
106
- console.log('3. Check if tokens need to be "unwrapped" before selling');
107
- return;
108
- }
109
-
110
- // Approve missing operators
111
- console.log('─── Setting Approvals ───');
112
- const gasPrice = await provider.getGasPrice();
113
- const adjustedGas = gasPrice.mul(2);
114
- console.log(`Gas price: ${ethers.utils.formatUnits(adjustedGas, 'gwei')} Gwei`);
115
- console.log('');
116
-
117
- for (const op of needsApproval) {
118
- console.log(`Approving ${op.name}...`);
119
- try {
120
- const tx = await conditionalTokens.setApprovalForAll(op.address, true, {
121
- gasPrice: adjustedGas,
122
- gasLimit: 100000,
123
- });
124
- console.log(` TX: ${tx.hash}`);
125
- const receipt = await tx.wait();
126
- console.log(` ✅ Confirmed in block ${receipt.blockNumber}`);
127
- } catch (error: any) {
128
- console.log(` ❌ Failed: ${error.message}`);
129
- }
130
- }
131
-
132
- console.log('');
133
- console.log('─── Verifying Approvals ───');
134
- for (const op of operators) {
135
- const isApproved = await conditionalTokens.isApprovedForAll(wallet.address, op.address);
136
- console.log(`${op.name}: ${isApproved ? '✅ Approved' : '❌ Not Approved'}`);
137
- }
138
-
139
- return;
140
- } catch (error: any) {
141
- console.log(`Failed: ${error.message}`);
142
- console.log('');
143
- }
144
- }
145
-
146
- console.log('❌ All RPCs failed!');
147
- }
148
-
149
- main().catch(console.error);
@@ -1,102 +0,0 @@
1
- #!/usr/bin/env npx tsx
2
- /**
3
- * Simple script to approve Neg Risk Adapter
4
- */
5
-
6
- import { ethers } from 'ethers';
7
- import * as fs from 'fs';
8
- import * as path from 'path';
9
- import { fileURLToPath } from 'url';
10
-
11
- const __filename = fileURLToPath(import.meta.url);
12
- const __dirname = path.dirname(__filename);
13
-
14
- // Read private key
15
- const envPath = path.resolve(__dirname, '../../earning-engine/dashboard-api/.env');
16
- const envContent = fs.readFileSync(envPath, 'utf8');
17
- const match = envContent.match(/^PRIVATE_KEY=(.+)$/m);
18
- const PRIVATE_KEY = match ? match[1].trim() : '';
19
-
20
- if (!PRIVATE_KEY) {
21
- console.error('PRIVATE_KEY not found');
22
- process.exit(1);
23
- }
24
-
25
- const USDC_E = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
26
- const NEG_RISK_ADAPTER = '0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296';
27
-
28
- const ERC20_ABI = [
29
- 'function approve(address spender, uint256 amount) returns (bool)',
30
- 'function allowance(address owner, address spender) view returns (uint256)',
31
- ];
32
-
33
- async function main() {
34
- console.log('Approving Neg Risk Adapter...');
35
-
36
- // Try multiple RPCs
37
- const rpcs = [
38
- 'https://polygon-bor-rpc.publicnode.com',
39
- 'https://polygon.drpc.org',
40
- 'https://polygon-rpc.com',
41
- ];
42
-
43
- for (const rpc of rpcs) {
44
- console.log(`\nTrying RPC: ${rpc}`);
45
- try {
46
- const provider = new ethers.providers.JsonRpcProvider({
47
- url: rpc,
48
- timeout: 30000,
49
- });
50
-
51
- // Test connection
52
- const network = await provider.getNetwork();
53
- console.log(`Connected to chain ${network.chainId}`);
54
-
55
- const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
56
- console.log(`Wallet: ${wallet.address}`);
57
-
58
- const usdc = new ethers.Contract(USDC_E, ERC20_ABI, wallet);
59
-
60
- // Check current allowance
61
- const currentAllowance = await usdc.allowance(wallet.address, NEG_RISK_ADAPTER);
62
- console.log(`Current allowance: ${ethers.utils.formatUnits(currentAllowance, 6)} USDC`);
63
-
64
- if (currentAllowance.gt(ethers.utils.parseUnits('1000000000', 6))) {
65
- console.log('✅ Already approved!');
66
- return;
67
- }
68
-
69
- // Get gas price
70
- const gasPrice = await provider.getGasPrice();
71
- const adjustedGas = gasPrice.mul(2); // 2x for safety
72
- console.log(`Gas price: ${ethers.utils.formatUnits(adjustedGas, 'gwei')} Gwei`);
73
-
74
- // Approve
75
- console.log('Sending approval transaction...');
76
- const tx = await usdc.approve(NEG_RISK_ADAPTER, ethers.constants.MaxUint256, {
77
- gasPrice: adjustedGas,
78
- gasLimit: 100000,
79
- });
80
-
81
- console.log(`TX Hash: ${tx.hash}`);
82
- console.log('Waiting for confirmation...');
83
-
84
- const receipt = await tx.wait();
85
- console.log(`✅ Confirmed in block ${receipt.blockNumber}`);
86
-
87
- // Verify
88
- const newAllowance = await usdc.allowance(wallet.address, NEG_RISK_ADAPTER);
89
- console.log(`New allowance: Unlimited`);
90
-
91
- return;
92
- } catch (error: any) {
93
- console.log(`Failed: ${error.message}`);
94
- }
95
- }
96
-
97
- console.log('\n❌ All RPCs failed!');
98
- console.log('Please approve manually on Polygonscan:');
99
- console.log('https://polygonscan.com/token/0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174#writeContract');
100
- }
101
-
102
- main().catch(console.error);
@@ -1,150 +0,0 @@
1
- #!/usr/bin/env npx tsx
2
- /**
3
- * Check all Polymarket contract allowances
4
- */
5
-
6
- import { ethers } from 'ethers';
7
- import * as fs from 'fs';
8
- import * as path from 'path';
9
- import { fileURLToPath } from 'url';
10
-
11
- const __filename = fileURLToPath(import.meta.url);
12
- const __dirname = path.dirname(__filename);
13
-
14
- // Read private key from dashboard-api .env
15
- const envPath = path.resolve(__dirname, '../../earning-engine/dashboard-api/.env');
16
- const envContent = fs.readFileSync(envPath, 'utf8');
17
- const match = envContent.match(/^PRIVATE_KEY=(.+)$/m);
18
- const PRIVATE_KEY = match ? match[1].trim() : '';
19
-
20
- if (!PRIVATE_KEY) {
21
- console.error('Error: PRIVATE_KEY not found in .env');
22
- process.exit(1);
23
- }
24
-
25
- // Contract addresses on Polygon
26
- const USDC_E_ADDRESS = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
27
- const CTF_EXCHANGE = '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E';
28
- const NEG_RISK_CTF_EXCHANGE = '0xC5d563A36AE78145C45a50134d48A1215220f80a';
29
- const NEG_RISK_ADAPTER = '0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296';
30
-
31
- // Additional contracts that might need approval
32
- const CONDITIONAL_TOKENS = '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045';
33
-
34
- const ERC20_ABI = [
35
- 'function balanceOf(address owner) view returns (uint256)',
36
- 'function allowance(address owner, address spender) view returns (uint256)',
37
- 'function approve(address spender, uint256 amount) returns (bool)',
38
- ];
39
-
40
- async function main() {
41
- console.log('╔════════════════════════════════════════════════════════════════╗');
42
- console.log('║ POLYMARKET ALLOWANCE CHECKER (ALL CONTRACTS) ║');
43
- console.log('╚════════════════════════════════════════════════════════════════╝');
44
- console.log('');
45
-
46
- // Use multiple RPC providers for reliability
47
- const rpcUrls = [
48
- 'https://polygon-mainnet.g.alchemy.com/v2/demo',
49
- 'https://polygon.llamarpc.com',
50
- 'https://polygon-rpc.com',
51
- ];
52
-
53
- let provider: ethers.providers.JsonRpcProvider | null = null;
54
- for (const rpcUrl of rpcUrls) {
55
- try {
56
- const p = new ethers.providers.JsonRpcProvider(rpcUrl);
57
- await p.getNetwork(); // Test connection
58
- provider = p;
59
- console.log(`Using RPC: ${rpcUrl}`);
60
- break;
61
- } catch (e) {
62
- console.log(`RPC failed: ${rpcUrl}`);
63
- }
64
- }
65
-
66
- if (!provider) {
67
- console.error('All RPC providers failed!');
68
- process.exit(1);
69
- }
70
- const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
71
- const address = wallet.address;
72
- const usdc = new ethers.Contract(USDC_E_ADDRESS, ERC20_ABI, wallet);
73
-
74
- console.log(`Wallet: ${address}`);
75
- console.log('');
76
-
77
- // Check USDC balance
78
- const balance = await usdc.balanceOf(address);
79
- console.log(`USDC.e Balance: ${(parseFloat(balance.toString()) / 1e6).toFixed(6)} USDC`);
80
- console.log('');
81
-
82
- // Check all allowances
83
- console.log('─── Contract Allowances ───');
84
-
85
- const contracts = [
86
- { name: 'CTF Exchange', address: CTF_EXCHANGE },
87
- { name: 'Neg Risk CTF Exchange', address: NEG_RISK_CTF_EXCHANGE },
88
- { name: 'Neg Risk Adapter', address: NEG_RISK_ADAPTER },
89
- { name: 'Conditional Tokens', address: CONDITIONAL_TOKENS },
90
- ];
91
-
92
- let needsApproval = [];
93
-
94
- for (const contract of contracts) {
95
- const allowance = await usdc.allowance(address, contract.address);
96
- const allowanceNum = parseFloat(allowance.toString()) / 1e6;
97
- const isUnlimited = allowanceNum > 1e12;
98
- const status = isUnlimited ? '✅ Unlimited' : allowanceNum > 0 ? `⚠️ ${allowanceNum.toFixed(2)}` : '❌ None';
99
- console.log(`${contract.name}: ${status}`);
100
- if (!isUnlimited) {
101
- needsApproval.push(contract);
102
- }
103
- }
104
-
105
- console.log('');
106
-
107
- if (needsApproval.length > 0) {
108
- console.log('─── Approval Needed ───');
109
- console.log('The following contracts need unlimited approval:');
110
- for (const contract of needsApproval) {
111
- console.log(` - ${contract.name} (${contract.address})`);
112
- }
113
- console.log('');
114
- console.log('Run with --approve flag to approve all:');
115
- console.log(' npx tsx scripts/check-all-allowances.ts --approve');
116
- console.log('');
117
-
118
- if (process.argv.includes('--approve')) {
119
- console.log('─── Approving Contracts ───');
120
- const MAX_UINT256 = ethers.constants.MaxUint256;
121
-
122
- // Get current gas price and add buffer for Polygon
123
- const gasPrice = await provider.getGasPrice();
124
- const adjustedGasPrice = gasPrice.mul(150).div(100); // 1.5x current gas price
125
- console.log(`Using gas price: ${(adjustedGasPrice.toNumber() / 1e9).toFixed(2)} Gwei`);
126
-
127
- for (const contract of needsApproval) {
128
- console.log(`Approving ${contract.name}...`);
129
- try {
130
- const tx = await usdc.approve(contract.address, MAX_UINT256, { gasPrice: adjustedGasPrice });
131
- console.log(` TX: ${tx.hash}`);
132
- await tx.wait();
133
- console.log(` ✓ Approved`);
134
- } catch (error: any) {
135
- console.log(` ✗ Failed: ${error.message}`);
136
- }
137
- }
138
- console.log('');
139
- console.log('✅ Done! Please re-run without --approve to verify.');
140
- }
141
- } else {
142
- console.log('✅ All contracts have unlimited approval!');
143
- console.log('');
144
- console.log('If orders are still failing, the issue may be:');
145
- console.log('1. Polymarket requires a deposit via their UI to create a trading account');
146
- console.log('2. Or there may be a different issue with the API key');
147
- }
148
- }
149
-
150
- main().catch(console.error);
@@ -1,129 +0,0 @@
1
- /**
2
- * Check and Set USDC.e Allowance for Polymarket Trading
3
- *
4
- * Usage:
5
- * # Check current allowance
6
- * POLY_PRIVKEY=0x... npx tsx scripts/check-allowance.ts
7
- *
8
- * # Approve unlimited allowance
9
- * POLY_PRIVKEY=0x... npx tsx scripts/check-allowance.ts approve
10
- */
11
-
12
- import { TradingClient, RateLimiter } from '../src/index.js';
13
-
14
- const PRIVATE_KEY = process.env.POLY_PRIVKEY || '';
15
-
16
- async function main() {
17
- if (!PRIVATE_KEY) {
18
- console.error('Error: Set POLY_PRIVKEY environment variable');
19
- process.exit(1);
20
- }
21
-
22
- const command = process.argv[2] || 'check';
23
-
24
- console.log('╔════════════════════════════════════════════════════════════════╗');
25
- console.log('║ POLYMARKET ALLOWANCE CHECKER ║');
26
- console.log('╚════════════════════════════════════════════════════════════════╝');
27
- console.log('');
28
-
29
- const rateLimiter = new RateLimiter();
30
- const tradingClient = new TradingClient(rateLimiter, {
31
- privateKey: PRIVATE_KEY,
32
- chainId: 137,
33
- });
34
-
35
- await tradingClient.initialize();
36
- console.log(`Wallet: ${tradingClient.getAddress()}`);
37
- console.log('');
38
-
39
- // Check current balance and allowance
40
- console.log('─── Current Status ───');
41
- const { balance, allowance } = await tradingClient.getBalanceAllowance('COLLATERAL');
42
-
43
- const balanceUsdc = parseFloat(balance) / 1e6;
44
- const allowanceUsdc = allowance ? parseFloat(allowance) / 1e6 : Infinity;
45
-
46
- console.log(`USDC.e Balance: ${balanceUsdc.toFixed(6)} USDC`);
47
- console.log(`USDC.e Allowance: ${allowance === 'unlimited' || allowanceUsdc > 1e12 ? 'Unlimited' : allowanceUsdc.toFixed(6) + ' USDC'}`);
48
- console.log('');
49
-
50
- // Check if allowance is sufficient
51
- if (allowanceUsdc < balanceUsdc) {
52
- console.log('⚠️ Warning: Allowance is less than balance!');
53
- console.log(' This may cause "not enough balance / allowance" errors.');
54
- console.log(' Run with "approve" argument to fix.');
55
- console.log('');
56
- }
57
-
58
- if (command === 'approve') {
59
- console.log('─── Approving Unlimited Allowance ───');
60
- console.log('This will approve unlimited USDC.e spending for Polymarket.');
61
- console.log('');
62
-
63
- try {
64
- // Get the underlying CLOB client to access approve methods
65
- const client = (tradingClient as unknown as { clobClient: { approveCollateral: () => Promise<unknown> } }).clobClient;
66
-
67
- if (client && typeof client.approveCollateral === 'function') {
68
- console.log('Sending approval transaction...');
69
- const result = await client.approveCollateral();
70
- console.log('Approval result:', result);
71
- } else {
72
- // Manual approval using ethers
73
- console.log('Using direct contract call...');
74
-
75
- const { ethers } = await import('ethers');
76
- const provider = new ethers.providers.JsonRpcProvider('https://polygon-rpc.com');
77
- const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
78
-
79
- // USDC.e contract on Polygon
80
- const USDC_E_ADDRESS = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
81
- // CTF Exchange contract (where orders are executed)
82
- const CTF_EXCHANGE = '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E';
83
- // Neg Risk CTF Exchange
84
- const NEG_RISK_CTF_EXCHANGE = '0xC5d563A36AE78145C45a50134d48A1215220f80a';
85
-
86
- const ERC20_ABI = [
87
- 'function approve(address spender, uint256 amount) returns (bool)',
88
- 'function allowance(address owner, address spender) view returns (uint256)',
89
- ];
90
-
91
- const usdc = new ethers.Contract(USDC_E_ADDRESS, ERC20_ABI, wallet);
92
- const MAX_UINT256 = ethers.constants.MaxUint256;
93
-
94
- // Approve both exchanges
95
- console.log('');
96
- console.log('1. Approving CTF Exchange...');
97
- const tx1 = await usdc.approve(CTF_EXCHANGE, MAX_UINT256);
98
- console.log(` TX: ${tx1.hash}`);
99
- await tx1.wait();
100
- console.log(' ✓ Confirmed');
101
-
102
- console.log('');
103
- console.log('2. Approving Neg Risk CTF Exchange...');
104
- const tx2 = await usdc.approve(NEG_RISK_CTF_EXCHANGE, MAX_UINT256);
105
- console.log(` TX: ${tx2.hash}`);
106
- await tx2.wait();
107
- console.log(' ✓ Confirmed');
108
- }
109
-
110
- console.log('');
111
- console.log('✅ Approval complete!');
112
-
113
- // Re-check allowance
114
- const { allowance: newAllowance } = await tradingClient.getBalanceAllowance('COLLATERAL');
115
- console.log(`New Allowance: ${newAllowance === 'unlimited' ? 'Unlimited' : newAllowance}`);
116
-
117
- } catch (error) {
118
- console.error('❌ Approval failed:', (error as Error).message);
119
- }
120
- } else {
121
- console.log('─── Commands ───');
122
- console.log(' check - Check current allowance (default)');
123
- console.log(' approve - Approve unlimited allowance for trading');
124
- console.log('');
125
- console.log('Example: POLY_PRIVKEY=0x... npx tsx scripts/check-allowance.ts approve');
126
- }
127
- }
128
-
129
- main().catch(console.error);
@@ -1,158 +0,0 @@
1
- /**
2
- * Check and Set CTF Token Approvals for Polymarket Trading
3
- *
4
- * The CLOB Exchange requires approval for CTF tokens (ERC1155) in addition to USDC.e.
5
- * Without CTF token approval, orders fail with "not enough balance / allowance".
6
- *
7
- * Usage:
8
- * POLY_PRIVKEY=0x... npx tsx scripts/check-ctf-approval.ts
9
- * POLY_PRIVKEY=0x... npx tsx scripts/check-ctf-approval.ts approve
10
- */
11
-
12
- import { ethers } from 'ethers';
13
-
14
- const PRIVATE_KEY = process.env.POLY_PRIVKEY || '';
15
- const RPC_URL = process.env.POLYGON_RPC_URL || 'https://polygon-mainnet.g.alchemy.com/v2/demo';
16
-
17
- // Contracts
18
- const USDC_E_ADDRESS = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174';
19
- const CTF_EXCHANGE = '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E';
20
- const NEG_RISK_CTF_EXCHANGE = '0xC5d563A36AE78145C45a50134d48A1215220f80a';
21
- const CTF_TOKEN = '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045'; // Conditional Tokens (ERC1155)
22
-
23
- const ERC20_ABI = [
24
- 'function allowance(address owner, address spender) view returns (uint256)',
25
- 'function balanceOf(address account) view returns (uint256)',
26
- 'function approve(address spender, uint256 amount) returns (bool)',
27
- ];
28
-
29
- const ERC1155_ABI = [
30
- 'function isApprovedForAll(address account, address operator) view returns (bool)',
31
- 'function setApprovalForAll(address operator, bool approved)',
32
- ];
33
-
34
- async function main() {
35
- if (!PRIVATE_KEY) {
36
- console.error('Error: Set POLY_PRIVKEY environment variable');
37
- process.exit(1);
38
- }
39
-
40
- const command = process.argv[2] || 'check';
41
-
42
- console.log('╔════════════════════════════════════════════════════════════════╗');
43
- console.log('║ POLYMARKET CTF APPROVAL CHECKER ║');
44
- console.log('╚════════════════════════════════════════════════════════════════╝');
45
- console.log('');
46
-
47
- const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
48
- const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
49
- const address = wallet.address;
50
-
51
- console.log('Wallet:', address);
52
- console.log('');
53
-
54
- // Check USDC.e
55
- const usdc = new ethers.Contract(USDC_E_ADDRESS, ERC20_ABI, provider);
56
- const balance = await usdc.balanceOf(address);
57
- const ctfUsdcAllowance = await usdc.allowance(address, CTF_EXCHANGE);
58
- const negRiskUsdcAllowance = await usdc.allowance(address, NEG_RISK_CTF_EXCHANGE);
59
-
60
- console.log('─── USDC.e (Collateral) ───');
61
- const balanceUsdc = parseFloat(ethers.utils.formatUnits(balance, 6));
62
- console.log('Balance: ' + balanceUsdc.toFixed(6) + ' USDC');
63
- console.log('CTF Exchange Allow: ' + (ctfUsdcAllowance.gte(ethers.constants.MaxUint256.div(2)) ? 'Unlimited ✅' : ethers.utils.formatUnits(ctfUsdcAllowance, 6) + ' USDC'));
64
- console.log('Neg Risk CTF Allow: ' + (negRiskUsdcAllowance.gte(ethers.constants.MaxUint256.div(2)) ? 'Unlimited ✅' : ethers.utils.formatUnits(negRiskUsdcAllowance, 6) + ' USDC'));
65
- console.log('');
66
-
67
- // Check CTF Token (ERC1155) approvals - THIS IS THE KEY!
68
- const ctfToken = new ethers.Contract(CTF_TOKEN, ERC1155_ABI, provider);
69
- const ctfApproved = await ctfToken.isApprovedForAll(address, CTF_EXCHANGE);
70
- const negRiskApproved = await ctfToken.isApprovedForAll(address, NEG_RISK_CTF_EXCHANGE);
71
-
72
- console.log('─── CTF Tokens (ERC1155 Position Tokens) ───');
73
- console.log('CTF Exchange: ' + (ctfApproved ? '✅ Approved' : '❌ NOT APPROVED'));
74
- console.log('Neg Risk CTF: ' + (negRiskApproved ? '✅ Approved' : '❌ NOT APPROVED'));
75
- console.log('');
76
-
77
- const needsApproval = !ctfApproved || !negRiskApproved ||
78
- ctfUsdcAllowance.lt(ethers.constants.MaxUint256.div(2)) ||
79
- negRiskUsdcAllowance.lt(ethers.constants.MaxUint256.div(2));
80
-
81
- if (needsApproval) {
82
- console.log('⚠️ Missing approvals detected!');
83
- console.log(' Orders will fail with "not enough balance / allowance"');
84
- console.log('');
85
- }
86
-
87
- if (command === 'approve') {
88
- console.log('─── Approving All Required Contracts ───');
89
- console.log('');
90
-
91
- const usdcWithSigner = usdc.connect(wallet);
92
- const ctfTokenWithSigner = ctfToken.connect(wallet);
93
-
94
- // Get current gas price and add buffer
95
- const feeData = await provider.getFeeData();
96
- const gasPrice = feeData.gasPrice?.mul(2) || ethers.utils.parseUnits('50', 'gwei');
97
- console.log('Gas Price:', ethers.utils.formatUnits(gasPrice, 'gwei'), 'gwei');
98
- console.log('');
99
-
100
- // 1. Approve USDC.e for CTF Exchange
101
- if (ctfUsdcAllowance.lt(ethers.constants.MaxUint256.div(2))) {
102
- console.log('1. Approving USDC.e for CTF Exchange...');
103
- const tx1 = await usdcWithSigner.approve(CTF_EXCHANGE, ethers.constants.MaxUint256, { gasPrice });
104
- console.log(' TX:', tx1.hash);
105
- await tx1.wait();
106
- console.log(' ✅ Confirmed');
107
- } else {
108
- console.log('1. USDC.e CTF Exchange: Already approved ✓');
109
- }
110
-
111
- // 2. Approve USDC.e for Neg Risk CTF Exchange
112
- if (negRiskUsdcAllowance.lt(ethers.constants.MaxUint256.div(2))) {
113
- console.log('2. Approving USDC.e for Neg Risk CTF Exchange...');
114
- const tx2 = await usdcWithSigner.approve(NEG_RISK_CTF_EXCHANGE, ethers.constants.MaxUint256, { gasPrice });
115
- console.log(' TX:', tx2.hash);
116
- await tx2.wait();
117
- console.log(' ✅ Confirmed');
118
- } else {
119
- console.log('2. USDC.e Neg Risk CTF: Already approved ✓');
120
- }
121
-
122
- // 3. Approve CTF Tokens for CTF Exchange
123
- if (!ctfApproved) {
124
- console.log('3. Approving CTF Tokens for CTF Exchange...');
125
- const tx3 = await ctfTokenWithSigner.setApprovalForAll(CTF_EXCHANGE, true, { gasPrice });
126
- console.log(' TX:', tx3.hash);
127
- await tx3.wait();
128
- console.log(' ✅ Confirmed');
129
- } else {
130
- console.log('3. CTF Tokens CTF Exchange: Already approved ✓');
131
- }
132
-
133
- // 4. Approve CTF Tokens for Neg Risk CTF Exchange
134
- if (!negRiskApproved) {
135
- console.log('4. Approving CTF Tokens for Neg Risk CTF Exchange...');
136
- const tx4 = await ctfTokenWithSigner.setApprovalForAll(NEG_RISK_CTF_EXCHANGE, true, { gasPrice });
137
- console.log(' TX:', tx4.hash);
138
- await tx4.wait();
139
- console.log(' ✅ Confirmed');
140
- } else {
141
- console.log('4. CTF Tokens Neg Risk CTF: Already approved ✓');
142
- }
143
-
144
- console.log('');
145
- console.log('═══════════════════════════════════════════════════════════════');
146
- console.log('✅ All approvals complete! You can now trade on Polymarket.');
147
- console.log('═══════════════════════════════════════════════════════════════');
148
-
149
- } else {
150
- console.log('─── Commands ───');
151
- console.log(' check - Check current approvals (default)');
152
- console.log(' approve - Approve all contracts for trading');
153
- console.log('');
154
- console.log('Example: POLY_PRIVKEY=0x... npx tsx scripts/check-ctf-approval.ts approve');
155
- }
156
- }
157
-
158
- main().catch(console.error);