@megatao/sdk 1.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 (228) hide show
  1. package/.env.example +37 -0
  2. package/CHANGELOG.md +19 -0
  3. package/README.md +199 -0
  4. package/bin/alf +4 -0
  5. package/cli/README.md +198 -0
  6. package/cli/TEST_MANUAL.md +577 -0
  7. package/cli/commands/account.ts +545 -0
  8. package/cli/commands/funding.ts +481 -0
  9. package/cli/commands/liquidation.ts +523 -0
  10. package/cli/commands/market.ts +590 -0
  11. package/cli/commands/orders.ts +395 -0
  12. package/cli/commands/position.ts +1085 -0
  13. package/cli/commands/shared/positionUtils.ts +239 -0
  14. package/cli/commands/trading.ts +483 -0
  15. package/cli/commands/utils.ts +281 -0
  16. package/cli/commands/vault.ts +522 -0
  17. package/cli/index.ts +169 -0
  18. package/cli/interactive.ts +530 -0
  19. package/cli/utils/client.ts +457 -0
  20. package/cli/utils/config.ts +226 -0
  21. package/cli/utils/display.ts +258 -0
  22. package/cli/utils/index.ts +10 -0
  23. package/cli/utils/prompts.ts +364 -0
  24. package/config.example.json +23 -0
  25. package/dist/AlphaFuturesClient.d.ts +36 -0
  26. package/dist/AlphaFuturesClient.d.ts.map +1 -0
  27. package/dist/AlphaFuturesClient.js +116 -0
  28. package/dist/AlphaFuturesClient.js.map +1 -0
  29. package/dist/abi/Alpha.json +5987 -0
  30. package/dist/abi/abis.d.ts +319 -0
  31. package/dist/abi/abis.d.ts.map +1 -0
  32. package/dist/abi/abis.js +128 -0
  33. package/dist/abi/abis.js.map +1 -0
  34. package/dist/abi/index.d.ts +11 -0
  35. package/dist/abi/index.d.ts.map +1 -0
  36. package/dist/abi/index.js +15 -0
  37. package/dist/abi/index.js.map +1 -0
  38. package/dist/config/contracts.config.d.ts +70 -0
  39. package/dist/config/contracts.config.d.ts.map +1 -0
  40. package/dist/config/contracts.config.js +137 -0
  41. package/dist/config/contracts.config.js.map +1 -0
  42. package/dist/config/environments/alpha.config.d.ts +17 -0
  43. package/dist/config/environments/alpha.config.d.ts.map +1 -0
  44. package/dist/config/environments/alpha.config.js +140 -0
  45. package/dist/config/environments/alpha.config.js.map +1 -0
  46. package/dist/config/environments/beta.config.d.ts +16 -0
  47. package/dist/config/environments/beta.config.d.ts.map +1 -0
  48. package/dist/config/environments/beta.config.js +131 -0
  49. package/dist/config/environments/beta.config.js.map +1 -0
  50. package/dist/config/environments/dev.config.d.ts +13 -0
  51. package/dist/config/environments/dev.config.d.ts.map +1 -0
  52. package/dist/config/environments/dev.config.js +123 -0
  53. package/dist/config/environments/dev.config.js.map +1 -0
  54. package/dist/config/environments/index.d.ts +48 -0
  55. package/dist/config/environments/index.d.ts.map +1 -0
  56. package/dist/config/environments/index.js +81 -0
  57. package/dist/config/environments/index.js.map +1 -0
  58. package/dist/config/environments/localhost.config.d.ts +16 -0
  59. package/dist/config/environments/localhost.config.d.ts.map +1 -0
  60. package/dist/config/environments/localhost.config.js +152 -0
  61. package/dist/config/environments/localhost.config.js.map +1 -0
  62. package/dist/config/environments/prod.config.d.ts +20 -0
  63. package/dist/config/environments/prod.config.d.ts.map +1 -0
  64. package/dist/config/environments/prod.config.js +143 -0
  65. package/dist/config/environments/prod.config.js.map +1 -0
  66. package/dist/config/index.d.ts +7 -0
  67. package/dist/config/index.d.ts.map +1 -0
  68. package/dist/config/index.js +41 -0
  69. package/dist/config/index.js.map +1 -0
  70. package/dist/constants/assets.d.ts +76 -0
  71. package/dist/constants/assets.d.ts.map +1 -0
  72. package/dist/constants/assets.js +277 -0
  73. package/dist/constants/assets.js.map +1 -0
  74. package/dist/constants/contracts.d.ts +41 -0
  75. package/dist/constants/contracts.d.ts.map +1 -0
  76. package/dist/constants/contracts.js +57 -0
  77. package/dist/constants/contracts.js.map +1 -0
  78. package/dist/constants/index.d.ts +36 -0
  79. package/dist/constants/index.d.ts.map +1 -0
  80. package/dist/constants/index.js +75 -0
  81. package/dist/constants/index.js.map +1 -0
  82. package/dist/constants/networks.d.ts +32 -0
  83. package/dist/constants/networks.d.ts.map +1 -0
  84. package/dist/constants/networks.js +174 -0
  85. package/dist/constants/networks.js.map +1 -0
  86. package/dist/contracts/index.d.ts +5 -0
  87. package/dist/contracts/index.d.ts.map +1 -0
  88. package/dist/contracts/index.js +21 -0
  89. package/dist/contracts/index.js.map +1 -0
  90. package/dist/contracts/viem/AlphaViem.d.ts +518 -0
  91. package/dist/contracts/viem/AlphaViem.d.ts.map +1 -0
  92. package/dist/contracts/viem/AlphaViem.js +1287 -0
  93. package/dist/contracts/viem/AlphaViem.js.map +1 -0
  94. package/dist/contracts/viem/PriceOracleViem.d.ts +71 -0
  95. package/dist/contracts/viem/PriceOracleViem.d.ts.map +1 -0
  96. package/dist/contracts/viem/PriceOracleViem.js +212 -0
  97. package/dist/contracts/viem/PriceOracleViem.js.map +1 -0
  98. package/dist/contracts/viem/index.d.ts +9 -0
  99. package/dist/contracts/viem/index.d.ts.map +1 -0
  100. package/dist/contracts/viem/index.js +17 -0
  101. package/dist/contracts/viem/index.js.map +1 -0
  102. package/dist/errors/index.d.ts +44 -0
  103. package/dist/errors/index.d.ts.map +1 -0
  104. package/dist/errors/index.js +83 -0
  105. package/dist/errors/index.js.map +1 -0
  106. package/dist/index.d.ts +19 -0
  107. package/dist/index.d.ts.map +1 -0
  108. package/dist/index.js +60 -0
  109. package/dist/index.js.map +1 -0
  110. package/dist/types/alpha.d.ts +299 -0
  111. package/dist/types/alpha.d.ts.map +1 -0
  112. package/dist/types/alpha.js +6 -0
  113. package/dist/types/alpha.js.map +1 -0
  114. package/dist/types/client.d.ts +24 -0
  115. package/dist/types/client.d.ts.map +1 -0
  116. package/dist/types/client.js +13 -0
  117. package/dist/types/client.js.map +1 -0
  118. package/dist/types/contracts.d.ts +48 -0
  119. package/dist/types/contracts.d.ts.map +1 -0
  120. package/dist/types/contracts.js +6 -0
  121. package/dist/types/contracts.js.map +1 -0
  122. package/dist/types/funding.d.ts +27 -0
  123. package/dist/types/funding.d.ts.map +1 -0
  124. package/dist/types/funding.js +6 -0
  125. package/dist/types/funding.js.map +1 -0
  126. package/dist/types/index.d.ts +92 -0
  127. package/dist/types/index.d.ts.map +1 -0
  128. package/dist/types/index.js +47 -0
  129. package/dist/types/index.js.map +1 -0
  130. package/dist/types/liquidation.d.ts +20 -0
  131. package/dist/types/liquidation.d.ts.map +1 -0
  132. package/dist/types/liquidation.js +6 -0
  133. package/dist/types/liquidation.js.map +1 -0
  134. package/dist/types/margin.d.ts +29 -0
  135. package/dist/types/margin.d.ts.map +1 -0
  136. package/dist/types/margin.js +6 -0
  137. package/dist/types/margin.js.map +1 -0
  138. package/dist/types/oracle.d.ts +21 -0
  139. package/dist/types/oracle.d.ts.map +1 -0
  140. package/dist/types/oracle.js +6 -0
  141. package/dist/types/oracle.js.map +1 -0
  142. package/dist/types/positions.d.ts +43 -0
  143. package/dist/types/positions.d.ts.map +1 -0
  144. package/dist/types/positions.js +13 -0
  145. package/dist/types/positions.js.map +1 -0
  146. package/dist/utils/calculations.d.ts +84 -0
  147. package/dist/utils/calculations.d.ts.map +1 -0
  148. package/dist/utils/calculations.js +155 -0
  149. package/dist/utils/calculations.js.map +1 -0
  150. package/dist/utils/errors.d.ts +24 -0
  151. package/dist/utils/errors.d.ts.map +1 -0
  152. package/dist/utils/errors.js +129 -0
  153. package/dist/utils/errors.js.map +1 -0
  154. package/dist/utils/events.d.ts +40 -0
  155. package/dist/utils/events.d.ts.map +1 -0
  156. package/dist/utils/events.js +73 -0
  157. package/dist/utils/events.js.map +1 -0
  158. package/dist/utils/format.d.ts +40 -0
  159. package/dist/utils/format.d.ts.map +1 -0
  160. package/dist/utils/format.js +86 -0
  161. package/dist/utils/format.js.map +1 -0
  162. package/dist/utils/index.d.ts +10 -0
  163. package/dist/utils/index.d.ts.map +1 -0
  164. package/dist/utils/index.js +26 -0
  165. package/dist/utils/index.js.map +1 -0
  166. package/dist/utils/network.d.ts +52 -0
  167. package/dist/utils/network.d.ts.map +1 -0
  168. package/dist/utils/network.js +192 -0
  169. package/dist/utils/network.js.map +1 -0
  170. package/dist/utils/positionCalculations.d.ts +145 -0
  171. package/dist/utils/positionCalculations.d.ts.map +1 -0
  172. package/dist/utils/positionCalculations.js +278 -0
  173. package/dist/utils/positionCalculations.js.map +1 -0
  174. package/dist/utils/validation.d.ts +28 -0
  175. package/dist/utils/validation.d.ts.map +1 -0
  176. package/dist/utils/validation.js +68 -0
  177. package/dist/utils/validation.js.map +1 -0
  178. package/docs/README.md +40 -0
  179. package/docs/api/API.md +831 -0
  180. package/docs/guides/GETTING_STARTED.md +316 -0
  181. package/docs/guides/TRADING_GUIDE.md +677 -0
  182. package/docs/integration/INTEGRATION_GUIDE.md +1679 -0
  183. package/docs/integration/VIEM_INTEGRATION.md +294 -0
  184. package/docs/reference/CLI_QUICK_REFERENCE.md +197 -0
  185. package/docs/reference/TROUBLESHOOTING.md +922 -0
  186. package/package.json +113 -0
  187. package/src/AlphaFuturesClient.ts +158 -0
  188. package/src/abi/.gitkeep +1 -0
  189. package/src/abi/Alpha.json +5987 -0
  190. package/src/abi/README.md +99 -0
  191. package/src/abi/abis.ts +131 -0
  192. package/src/abi/index.ts +13 -0
  193. package/src/config/contracts.config.ts +186 -0
  194. package/src/config/environments/alpha.config.ts +139 -0
  195. package/src/config/environments/beta.config.ts +130 -0
  196. package/src/config/environments/dev.config.ts +122 -0
  197. package/src/config/environments/index.ts +87 -0
  198. package/src/config/environments/localhost.config.ts +153 -0
  199. package/src/config/environments/prod.config.ts +142 -0
  200. package/src/config/index.ts +29 -0
  201. package/src/constants/assets.ts +299 -0
  202. package/src/constants/contracts.ts +64 -0
  203. package/src/constants/index.ts +69 -0
  204. package/src/constants/networks.ts +182 -0
  205. package/src/contracts/index.ts +5 -0
  206. package/src/contracts/viem/AlphaViem.ts +1615 -0
  207. package/src/contracts/viem/PriceOracleViem.ts +272 -0
  208. package/src/contracts/viem/index.ts +11 -0
  209. package/src/errors/index.ts +87 -0
  210. package/src/index.ts +59 -0
  211. package/src/types/VIEM_TYPES_README.md +70 -0
  212. package/src/types/alpha.ts +358 -0
  213. package/src/types/client.ts +27 -0
  214. package/src/types/contracts.ts +74 -0
  215. package/src/types/funding.ts +31 -0
  216. package/src/types/index.ts +108 -0
  217. package/src/types/liquidation.ts +23 -0
  218. package/src/types/margin.ts +34 -0
  219. package/src/types/oracle.ts +24 -0
  220. package/src/types/positions.ts +48 -0
  221. package/src/utils/calculations.ts +175 -0
  222. package/src/utils/errors.ts +147 -0
  223. package/src/utils/events.ts +98 -0
  224. package/src/utils/format.ts +84 -0
  225. package/src/utils/index.ts +10 -0
  226. package/src/utils/network.ts +212 -0
  227. package/src/utils/positionCalculations.ts +317 -0
  228. package/src/utils/validation.ts +76 -0
@@ -0,0 +1,316 @@
1
+ # Getting Started with Alpha Futures SDK
2
+
3
+ This guide will help you get up and running with the Alpha Futures SDK and CLI in just a few minutes.
4
+
5
+ ## Prerequisites
6
+
7
+ - Node.js 16.0.0 or higher
8
+ - npm or yarn package manager
9
+ - An Ethereum wallet with TAO tokens for trading
10
+ - Access to an Ethereum RPC endpoint
11
+
12
+ ## Installation
13
+
14
+ ### Option 1: Global Installation (Recommended for CLI usage)
15
+
16
+ ```bash
17
+ npm install -g @alpha-futures/sdk
18
+ ```
19
+
20
+ ### Option 2: Project Installation
21
+
22
+ ```bash
23
+ npm install @alpha-futures/sdk
24
+ ```
25
+
26
+ ### Option 3: Run without Installation
27
+
28
+ ```bash
29
+ npx @alpha-futures/sdk --help
30
+ ```
31
+
32
+ ## Initial Setup
33
+
34
+ ### 1. Create Configuration File
35
+
36
+ Copy the example configuration file:
37
+
38
+ ```bash
39
+ cp config.example.json config.json
40
+ ```
41
+
42
+ Edit `config.json` with your settings:
43
+
44
+ ```json
45
+ {
46
+ "network": "mainnet",
47
+ "rpcUrl": "https://your-rpc-url.com"
48
+ }
49
+ ```
50
+
51
+ ### 2. Set Up Environment Variables
52
+
53
+ Copy the example environment file:
54
+
55
+ ```bash
56
+ cp .env.example .env
57
+ ```
58
+
59
+ Edit `.env` with your private key:
60
+
61
+ ```env
62
+ PRIVATE_KEY=your_private_key_here
63
+ NETWORK=mainnet
64
+ RPC_URL=https://your-rpc-url.com
65
+ ```
66
+
67
+ **Security Note**: Never commit `.env` files to version control!
68
+
69
+ ### 3. Test Your Setup
70
+
71
+ Run the CLI to verify installation:
72
+
73
+ ```bash
74
+ alpha-futures --version
75
+ alpha-futures account info
76
+ ```
77
+
78
+ ## Your First Trade
79
+
80
+ ### Using the Interactive CLI
81
+
82
+ 1. Launch the interactive interface:
83
+
84
+ ```bash
85
+ alpha-futures interactive
86
+ ```
87
+
88
+ 2. Select "Open Position" from the menu
89
+
90
+ 3. Follow the prompts to:
91
+ - Choose an asset (e.g., ALPHA)
92
+ - Enter position size
93
+ - Select leverage (1-10x)
94
+ - Choose direction (long/short)
95
+
96
+ ### Using Command Line
97
+
98
+ Open a long position with 3x leverage:
99
+
100
+ ```bash
101
+ alpha-futures position open --asset ALPHA --size 1000 --leverage 3 --long
102
+ ```
103
+
104
+ ### Using the SDK
105
+
106
+ ```typescript
107
+ import { AlphaFuturesClient } from '@alpha-futures/sdk';
108
+
109
+ async function openFirstPosition() {
110
+ // Initialize client
111
+ const client = new AlphaFuturesClient({
112
+ network: 'mainnet',
113
+ privateKey: process.env.PRIVATE_KEY
114
+ });
115
+
116
+ // Check account balance
117
+ const margin = await client.margin.getMarginAccount();
118
+ console.log('Available margin:', margin.availableMargin);
119
+
120
+ // Open a position
121
+ const position = await client.positions.openPosition({
122
+ asset: 'ALPHA',
123
+ size: 1000,
124
+ leverage: 3,
125
+ isLong: true
126
+ });
127
+
128
+ console.log('Position opened:', position.id);
129
+ }
130
+
131
+ openFirstPosition().catch(console.error);
132
+ ```
133
+
134
+ ## Common Operations
135
+
136
+ ### Check Your Positions
137
+
138
+ ```bash
139
+ # List all positions
140
+ alpha-futures position list
141
+
142
+ # Get specific position details
143
+ alpha-futures position info 0x00000000000000000000000000000001
144
+ ```
145
+
146
+ ### Monitor Market Data
147
+
148
+ ```bash
149
+ # Check current prices
150
+ alpha-futures market prices
151
+
152
+ # View funding rates
153
+ alpha-futures market funding-rates
154
+
155
+ # See market statistics
156
+ alpha-futures market stats
157
+ ```
158
+
159
+ ### Manage Your Account
160
+
161
+ ```bash
162
+ # Check balance
163
+ alpha-futures account balance
164
+
165
+ # Deposit more margin
166
+ alpha-futures account deposit --amount 500
167
+
168
+ # Withdraw profits
169
+ alpha-futures account withdraw --amount 200
170
+ ```
171
+
172
+ ## Best Practices
173
+
174
+ ### 1. Start Small
175
+
176
+ - Begin with small position sizes to understand the platform
177
+ - Use lower leverage (2-3x) initially
178
+ - Monitor funding rates before opening positions
179
+
180
+ ### 2. Risk Management
181
+
182
+ - Always maintain sufficient margin (>50% recommended)
183
+ - Set stop-loss levels mentally or through monitoring
184
+ - Diversify across multiple positions
185
+
186
+ ### 3. Monitor Positions
187
+
188
+ - Check funding payments regularly
189
+ - Watch for liquidation warnings
190
+ - Keep track of unrealized P&L
191
+
192
+ ### 4. Use the Interactive CLI
193
+
194
+ The interactive CLI provides:
195
+ - Visual feedback and confirmations
196
+ - Input validation
197
+ - Real-time price updates
198
+ - Transaction status tracking
199
+
200
+ ## Troubleshooting
201
+
202
+ ### Common Issues
203
+
204
+ 1. **"Insufficient margin" error**
205
+ - Check your account balance: `alpha-futures account balance`
206
+ - Deposit more TAO: `alpha-futures account deposit --amount 1000`
207
+
208
+ 2. **"Cannot connect to network"**
209
+ - Verify your RPC URL is correct
210
+ - Check your internet connection
211
+ - Try a different RPC endpoint
212
+
213
+ 3. **"Invalid private key"**
214
+ - Ensure your private key is correctly formatted
215
+ - Don't include '0x' prefix in .env file
216
+ - Check for extra spaces or newlines
217
+
218
+ 4. **"Gas estimation failed"**
219
+ - Increase gas limit in settings
220
+ - Check if you have enough ETH for gas
221
+ - Try again during lower network congestion
222
+
223
+ ### Debug Mode
224
+
225
+ Enable debug logging for more information:
226
+
227
+ ```bash
228
+ DEBUG=true alpha-futures position list
229
+ ```
230
+
231
+ ## Next Steps
232
+
233
+ 1. **Explore Advanced Features**
234
+ - Learn about limit orders
235
+ - Understand funding rate arbitrage
236
+ - Explore batch operations
237
+
238
+ 2. **Integrate the SDK**
239
+ - Build trading bots
240
+ - Create position monitors
241
+ - Develop custom strategies
242
+
243
+ 3. **Join the Community**
244
+ - Discord: [https://discord.gg/alphafutures](https://discord.gg/alphafutures)
245
+ - Follow updates on Twitter
246
+ - Contribute to the SDK on GitHub
247
+
248
+ ## Example Scripts
249
+
250
+ ### Position Monitor
251
+
252
+ ```typescript
253
+ // monitor.ts
254
+ import { AlphaFuturesClient } from '@alpha-futures/sdk';
255
+
256
+ const client = new AlphaFuturesClient({
257
+ network: 'mainnet',
258
+ privateKey: process.env.PRIVATE_KEY
259
+ });
260
+
261
+ async function monitorPositions() {
262
+ const positions = await client.positions.getAllPositions();
263
+
264
+ for (const position of positions) {
265
+ const pnl = await client.positions.getUnrealizedPnL(position.id);
266
+ const marginRatio = await client.positions.getMarginRatio(position.id);
267
+
268
+ console.log(`Position ${position.id}:`);
269
+ console.log(` P&L: ${pnl > 0 ? '+' : ''}${pnl.toFixed(2)} TAO`);
270
+ console.log(` Margin Ratio: ${marginRatio.toFixed(2)}%`);
271
+
272
+ if (marginRatio < 25) {
273
+ console.warn(' ⚠️ Low margin - consider adding margin!');
274
+ }
275
+ }
276
+ }
277
+
278
+ // Run every 60 seconds
279
+ setInterval(monitorPositions, 60000);
280
+ monitorPositions();
281
+ ```
282
+
283
+ ### Auto-Funding Collector
284
+
285
+ ```typescript
286
+ // funding-collector.ts
287
+ import { AlphaFuturesClient } from '@alpha-futures/sdk';
288
+
289
+ const client = new AlphaFuturesClient({
290
+ network: 'mainnet',
291
+ privateKey: process.env.PRIVATE_KEY
292
+ });
293
+
294
+ async function collectFunding() {
295
+ const positions = await client.positions.getAllPositions();
296
+
297
+ for (const position of positions) {
298
+ const funding = await client.funding.getAccruedFunding(position.id);
299
+
300
+ if (funding > 0) {
301
+ await client.funding.collectFunding(position.id);
302
+ console.log(`Collected ${funding.toFixed(4)} TAO from position ${position.id}`);
303
+ }
304
+ }
305
+ }
306
+
307
+ // Run every 8 hours
308
+ setInterval(collectFunding, 8 * 60 * 60 * 1000);
309
+ ```
310
+
311
+ ## Need Help?
312
+
313
+ - Read the full documentation: [README.md](../README.md)
314
+ - Check our FAQ: [https://docs.alphafutures.io/faq](https://docs.alphafutures.io/faq)
315
+ - Ask in Discord: [https://discord.gg/alphafutures](https://discord.gg/alphafutures)
316
+ - Report issues: [GitHub Issues](https://github.com/alpha-futures/sdk/issues)