@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,395 @@
1
+ /**
2
+ * Order Management Commands - Manage limit orders and order book
3
+ */
4
+
5
+ import { Command } from 'commander';
6
+ import { parseEther } from 'viem';
7
+ import chalk from 'chalk';
8
+ import ora from 'ora';
9
+ import Table from 'cli-table3';
10
+ import { formatUSD, formatTAO, formatPercentage } from '../../src/utils';
11
+ import { getClient, handleError, getMarketAddress } from '../utils/client';
12
+ import { confirmAction } from '../utils/prompts';
13
+
14
+ export function orderCommands(program: Command) {
15
+ const orders = program
16
+ .command('orders')
17
+ .alias('order')
18
+ .description('Manage limit orders and view order book');
19
+
20
+ // Place limit order command
21
+ orders
22
+ .command('place')
23
+ .description('Place a limit order')
24
+ .requiredOption('-a, --asset <asset>', 'Asset to trade (e.g., BITMIND)')
25
+ .requiredOption('-s, --side <side>', 'Order side: buy or sell')
26
+ .requiredOption('-z, --size <size>', 'Order size in USD')
27
+ .requiredOption('-p, --price <price>', 'Limit price in USD')
28
+ .requiredOption('-l, --leverage <leverage>', 'Leverage (1-10x)')
29
+ .option('-t, --type <type>', 'Order type: limit, post-only, ioc, fok', 'limit')
30
+ .option('-y, --yes', 'Skip confirmation')
31
+ .action(async (options) => {
32
+ const spinner = ora('Preparing order...').start();
33
+
34
+ try {
35
+ const client = await getClient(program.opts());
36
+ const alpha = client.getAlpha();
37
+ const userAddress = await client.getSignerAddress();
38
+
39
+ // Parse inputs
40
+ const asset = options.asset.toUpperCase();
41
+ const isBuy = options.side.toLowerCase() === 'buy';
42
+ const isLong = isBuy; // For perpetuals, buy = long, sell = short
43
+ const orderSizeUSD = parseEther(options.size);
44
+ const limitPrice = parseEther(options.price);
45
+ const leverage = parseEther(options.leverage);
46
+
47
+ // Validate leverage
48
+ if (parseFloat(options.leverage) > 10 || parseFloat(options.leverage) < 1) {
49
+ spinner.fail('Invalid leverage: must be between 1x and 10x');
50
+ process.exit(1);
51
+ }
52
+
53
+ // Get market address
54
+ const marketAddress = getMarketAddress(asset);
55
+
56
+ // Calculate required margin
57
+ const requiredMargin = orderSizeUSD / leverage;
58
+
59
+ // Check available balance
60
+ const available = await alpha.getAvailableMargin(userAddress);
61
+ if (requiredMargin > available) {
62
+ spinner.fail('Insufficient available margin');
63
+ console.log(` Required: ${formatTAO(requiredMargin)} TAO`);
64
+ console.log(` Available: ${formatTAO(available)} TAO`);
65
+ process.exit(1);
66
+ }
67
+
68
+ spinner.stop();
69
+
70
+ // Get current price
71
+ const currentPrice = await client.oracle.getPrice(marketAddress);
72
+
73
+ // Convert USD position size to token amount at limit price
74
+ // Both orderSizeUSD and limitPrice are already in wei (18 decimals)
75
+ const positionSize = orderSizeUSD / limitPrice;
76
+
77
+ // Display order preview
78
+ console.log(chalk.bold('\n📋 Limit Order Preview:'));
79
+ console.log(` Asset: ${chalk.cyan(asset)}`);
80
+ console.log(` Side: ${isBuy ? chalk.green('BUY (LONG)') : chalk.red('SELL (SHORT)')}`);
81
+ console.log(` Current Price: ${formatUSD(currentPrice)}`);
82
+ console.log(` Limit Price: ${formatUSD(limitPrice)}`);
83
+ console.log(` Size: ${formatUSD(orderSizeUSD)} (${formatUSD(positionSize)} tokens)`);
84
+ console.log(` Required Margin: ${formatTAO(requiredMargin)} TAO`);
85
+ console.log(` Leverage: ${chalk.yellow(options.leverage + 'x')}`);
86
+ console.log(` Order Type: ${chalk.blue(options.type.toUpperCase())}`);
87
+
88
+ // Show price difference
89
+ const priceDiff =
90
+ ((Number(limitPrice) - Number(currentPrice)) / Number(currentPrice)) * 100;
91
+ console.log(
92
+ ` Price Difference: ${priceDiff >= 0 ? chalk.green('+' + priceDiff.toFixed(2) + '%') : chalk.red(priceDiff.toFixed(2) + '%')}`,
93
+ );
94
+
95
+ // Calculate and display fees
96
+ const tradingFee = (orderSizeUSD * 10n) / 10000n; // 0.1% of notional
97
+ console.log(` Trading Fee: ${formatUSD(tradingFee)} (0.1%)`);
98
+
99
+ // Confirm action
100
+ if (!options.yes) {
101
+ const confirmed = await confirmAction('Place this order?');
102
+ if (!confirmed) {
103
+ console.log(chalk.yellow('Order cancelled'));
104
+ return;
105
+ }
106
+ }
107
+
108
+ spinner.start('Placing limit order...');
109
+
110
+ // Map order type to contract enum
111
+ // Assuming: 0 = LIMIT, 1 = POST_ONLY, 2 = IOC, 3 = FOK
112
+ let orderTypeEnum = 0;
113
+ switch (options.type.toLowerCase()) {
114
+ case 'post-only':
115
+ orderTypeEnum = 1;
116
+ break;
117
+ case 'ioc':
118
+ orderTypeEnum = 2;
119
+ break;
120
+ case 'fok':
121
+ orderTypeEnum = 3;
122
+ break;
123
+ }
124
+
125
+ const hash = await alpha.placeLimitOrder(
126
+ marketAddress,
127
+ isLong,
128
+ requiredMargin,
129
+ leverage,
130
+ limitPrice,
131
+ false, // reduceOnly
132
+ );
133
+
134
+ spinner.text = 'Waiting for confirmation...';
135
+ const publicClient = client.getPublicClient();
136
+ const receipt = await publicClient.waitForTransactionReceipt({ hash });
137
+
138
+ spinner.succeed('Limit order placed successfully!');
139
+ console.log(chalk.gray(` Transaction: ${receipt.transactionHash}`));
140
+ console.log(chalk.gray(` Gas used: ${receipt.gasUsed.toString()}`));
141
+
142
+ // Parse order placed event
143
+ const orderEvent = alpha.parseOrderPlacedEvent(receipt);
144
+ if (orderEvent) {
145
+ console.log(chalk.green(`\n✓ Order ID: ${orderEvent.orderId}`));
146
+ console.log(
147
+ chalk.gray(
148
+ ` Status: ${orderEvent.orderType === 0 ? 'Resting in book' : 'Processing'}`,
149
+ ),
150
+ );
151
+ }
152
+ } catch (error) {
153
+ spinner.fail('Failed to place order');
154
+ handleError(error, program.opts());
155
+ }
156
+ });
157
+
158
+ // List orders command
159
+ orders
160
+ .command('list [address]')
161
+ .alias('ls')
162
+ .description('List all orders for an address')
163
+ .option('-a, --asset <asset>', 'Filter by asset')
164
+ .option('-s, --status <status>', 'Filter by status: open, filled, cancelled')
165
+ .option('-l, --limit <limit>', 'Number of orders to show', '20')
166
+ .action(async (address, options) => {
167
+ const spinner = ora('Loading orders...').start();
168
+
169
+ try {
170
+ const client = await getClient(program.opts());
171
+ const alpha = client.getAlpha();
172
+ const userAddress = address || (await client.getSignerAddress());
173
+
174
+ if (!userAddress) {
175
+ spinner.fail('No address provided');
176
+ return;
177
+ }
178
+
179
+ // Get all user positions which may have associated orders
180
+ const positionIds = await alpha.getUserPositions(userAddress);
181
+
182
+ spinner.stop();
183
+
184
+ if (positionIds.length === 0) {
185
+ console.log(chalk.yellow('\nNo orders found'));
186
+ return;
187
+ }
188
+
189
+ console.log(chalk.bold(`\n📋 Orders for ${chalk.cyan(userAddress)}:`));
190
+
191
+ const table = new Table({
192
+ head: ['ID', 'Asset', 'Side', 'Size', 'Price', 'Filled', 'Status', 'Created'],
193
+ colWidths: [8, 8, 8, 15, 12, 12, 10, 12],
194
+ });
195
+
196
+ // Try to get order data from the orderbook client
197
+ try {
198
+ // Note: Order listing would require event indexing or state queries
199
+ console.log(chalk.yellow('\nNote: Order listing functionality is being developed'));
200
+ console.log(chalk.gray('This feature requires event indexing to show historical orders'));
201
+ console.log(
202
+ chalk.gray('For now, you can view your positions using "alpha-futures position list"'),
203
+ );
204
+ } catch (error) {
205
+ console.log(chalk.yellow('\nNote: Order listing requires orderbook integration'));
206
+ console.log(
207
+ chalk.gray('This feature will be available once orderbook queries are implemented'),
208
+ );
209
+ }
210
+
211
+ // Placeholder table data
212
+ table.push([
213
+ '1',
214
+ 'BITMIND',
215
+ chalk.green('BUY'),
216
+ '$5,000',
217
+ '$52,000',
218
+ '0%',
219
+ chalk.yellow('Open'),
220
+ '2 min ago',
221
+ ]);
222
+
223
+ console.log(table.toString());
224
+ } catch (error) {
225
+ spinner.fail('Failed to load orders');
226
+ handleError(error, program.opts());
227
+ }
228
+ });
229
+
230
+ // Cancel order command
231
+ orders
232
+ .command('cancel <market> <orderId>')
233
+ .description('Cancel an open order')
234
+ .option('-y, --yes', 'Skip confirmation')
235
+ .action(async (market, orderId, options) => {
236
+ const spinner = ora('Loading order...').start();
237
+
238
+ try {
239
+ const client = await getClient(program.opts());
240
+ const alpha = client.getAlpha();
241
+
242
+ spinner.stop();
243
+
244
+ console.log(chalk.bold('\n🚫 Cancel Order:'));
245
+ console.log(` Market: ${chalk.cyan(market)}`);
246
+ console.log(` Order ID: ${chalk.cyan(orderId)}`);
247
+
248
+ // Confirm action
249
+ if (!options.yes) {
250
+ const confirmed = await confirmAction('Cancel this order?');
251
+ if (!confirmed) {
252
+ console.log(chalk.yellow('Cancellation cancelled'));
253
+ return;
254
+ }
255
+ }
256
+
257
+ spinner.start('Cancelling order...');
258
+
259
+ const hash = await alpha.cancelOrder(market as `0x${string}`, orderId as `0x${string}`);
260
+
261
+ spinner.text = 'Waiting for confirmation...';
262
+ const publicClient = client.getPublicClient();
263
+ const receipt = await publicClient.waitForTransactionReceipt({ hash });
264
+
265
+ spinner.succeed('Order cancelled successfully!');
266
+ console.log(chalk.gray(` Transaction: ${receipt.transactionHash}`));
267
+ console.log(chalk.gray(` Gas used: ${receipt.gasUsed.toString()}`));
268
+ } catch (error) {
269
+ spinner.fail('Failed to cancel order');
270
+ handleError(error, program.opts());
271
+ }
272
+ });
273
+
274
+ // Cancel all orders command
275
+ orders
276
+ .command('cancel-all')
277
+ .description('Cancel all open orders')
278
+ .option('-a, --asset <asset>', 'Cancel orders for specific asset only')
279
+ .option('-y, --yes', 'Skip confirmation')
280
+ .action(async (options) => {
281
+ const spinner = ora('Loading orders...').start();
282
+
283
+ try {
284
+ const client = await getClient(program.opts());
285
+ const alpha = client.getAlpha();
286
+ const userAddress = await client.getSignerAddress();
287
+
288
+ spinner.stop();
289
+
290
+ console.log(chalk.bold('\n🚫 Cancel All Orders:'));
291
+ if (options.asset) {
292
+ console.log(` Asset: ${chalk.cyan(options.asset.toUpperCase())}`);
293
+ } else {
294
+ console.log(` Scope: ${chalk.yellow('All assets')}`);
295
+ }
296
+
297
+ // Confirm action
298
+ if (!options.yes) {
299
+ const confirmed = await confirmAction('Cancel all orders?');
300
+ if (!confirmed) {
301
+ console.log(chalk.yellow('Cancellation cancelled'));
302
+ return;
303
+ }
304
+ }
305
+
306
+ spinner.start('Cancelling all orders...');
307
+
308
+ // Try to use the batch cancel function if available
309
+ try {
310
+ const marketAddress = options.asset
311
+ ? getMarketAddress(options.asset.toUpperCase())
312
+ : (('0x' + '0'.repeat(40)) as `0x${string}`);
313
+
314
+ const hash = await alpha.cancelAllOrders(marketAddress);
315
+
316
+ spinner.text = 'Waiting for confirmation...';
317
+ const publicClient = client.getPublicClient();
318
+ const receipt = await publicClient.waitForTransactionReceipt({ hash });
319
+
320
+ spinner.succeed('All orders cancelled successfully!');
321
+ console.log(chalk.gray(` Transaction: ${receipt.transactionHash}`));
322
+ console.log(chalk.gray(` Gas used: ${receipt.gasUsed.toString()}`));
323
+ } catch (error) {
324
+ spinner.stop();
325
+ console.log(
326
+ chalk.yellow('\nNote: Batch order cancellation not available in current deployment'),
327
+ );
328
+ console.log(
329
+ chalk.gray(
330
+ 'Please cancel orders individually using "alpha-futures orders cancel <orderId>"',
331
+ ),
332
+ );
333
+ }
334
+ } catch (error) {
335
+ spinner.fail('Failed to cancel orders');
336
+ handleError(error, program.opts());
337
+ }
338
+ });
339
+
340
+ // Order history command
341
+ orders
342
+ .command('history [address]')
343
+ .description('View order history')
344
+ .option('-a, --asset <asset>', 'Filter by asset')
345
+ .option('-d, --days <days>', 'Number of days to look back', '7')
346
+ .option('-l, --limit <limit>', 'Number of orders to show', '50')
347
+ .action(async (address, options) => {
348
+ const spinner = ora('Loading order history...').start();
349
+
350
+ try {
351
+ const client = await getClient(program.opts());
352
+ const userAddress = address || (await client.getSignerAddress());
353
+
354
+ if (!userAddress) {
355
+ spinner.fail('No address provided');
356
+ return;
357
+ }
358
+
359
+ spinner.stop();
360
+
361
+ console.log(chalk.bold(`\n📜 Order History: ${chalk.cyan(userAddress)}`));
362
+ console.log(` Period: Last ${options.days} days`);
363
+
364
+ const table = new Table({
365
+ head: ['Date', 'Asset', 'Side', 'Size', 'Price', 'Status', 'P&L'],
366
+ colWidths: [20, 8, 8, 15, 12, 12, 15],
367
+ });
368
+
369
+ // Note: This would require event filtering from the blockchain
370
+ console.log(chalk.yellow('\nNote: Order history requires event indexing'));
371
+ console.log(
372
+ chalk.gray('This feature will be available once event queries are implemented'),
373
+ );
374
+ console.log(
375
+ chalk.gray('For now, you can view your positions using "alpha-futures position list"'),
376
+ );
377
+
378
+ // Placeholder data
379
+ table.push([
380
+ '2024-01-15 14:32',
381
+ 'BITMIND',
382
+ chalk.green('BUY'),
383
+ '$10,000',
384
+ '$48,500',
385
+ chalk.green('Filled'),
386
+ chalk.green('+$234'),
387
+ ]);
388
+
389
+ console.log(table.toString());
390
+ } catch (error) {
391
+ spinner.fail('Failed to load order history');
392
+ handleError(error, program.opts());
393
+ }
394
+ });
395
+ }