@0xmonaco/core 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 (50) hide show
  1. package/README.md +421 -0
  2. package/dist/api/auth/api.d.ts +198 -0
  3. package/dist/api/auth/api.d.ts.map +1 -0
  4. package/dist/api/auth/api.js +359 -0
  5. package/dist/api/auth/api.js.map +1 -0
  6. package/dist/api/auth/index.d.ts +6 -0
  7. package/dist/api/auth/index.d.ts.map +1 -0
  8. package/dist/api/auth/index.js +5 -0
  9. package/dist/api/auth/index.js.map +1 -0
  10. package/dist/api/index.d.ts +8 -0
  11. package/dist/api/index.d.ts.map +1 -0
  12. package/dist/api/index.js +8 -0
  13. package/dist/api/index.js.map +1 -0
  14. package/dist/api/trading/api.d.ts +152 -0
  15. package/dist/api/trading/api.d.ts.map +1 -0
  16. package/dist/api/trading/api.js +229 -0
  17. package/dist/api/trading/api.js.map +1 -0
  18. package/dist/api/trading/index.d.ts +6 -0
  19. package/dist/api/trading/index.d.ts.map +1 -0
  20. package/dist/api/trading/index.js +5 -0
  21. package/dist/api/trading/index.js.map +1 -0
  22. package/dist/api/vault/api.d.ts +224 -0
  23. package/dist/api/vault/api.d.ts.map +1 -0
  24. package/dist/api/vault/api.js +514 -0
  25. package/dist/api/vault/api.js.map +1 -0
  26. package/dist/api/vault/index.d.ts +6 -0
  27. package/dist/api/vault/index.d.ts.map +1 -0
  28. package/dist/api/vault/index.js +5 -0
  29. package/dist/api/vault/index.js.map +1 -0
  30. package/dist/chains.d.ts +90 -0
  31. package/dist/chains.d.ts.map +1 -0
  32. package/dist/chains.js +56 -0
  33. package/dist/chains.js.map +1 -0
  34. package/dist/errors.d.ts +132 -0
  35. package/dist/errors.d.ts.map +1 -0
  36. package/dist/errors.js +165 -0
  37. package/dist/errors.js.map +1 -0
  38. package/dist/index.d.ts +11 -0
  39. package/dist/index.d.ts.map +1 -0
  40. package/dist/index.js +19 -0
  41. package/dist/index.js.map +1 -0
  42. package/dist/networks.d.ts +8 -0
  43. package/dist/networks.d.ts.map +1 -0
  44. package/dist/networks.js +16 -0
  45. package/dist/networks.js.map +1 -0
  46. package/dist/sdk.d.ts +76 -0
  47. package/dist/sdk.d.ts.map +1 -0
  48. package/dist/sdk.js +203 -0
  49. package/dist/sdk.js.map +1 -0
  50. package/package.json +38 -0
@@ -0,0 +1,359 @@
1
+ /**
2
+ * Auth API Implementation
3
+ *
4
+ * Handles authentication operations including challenge creation, signature verification,
5
+ * and backend authentication. All operations go through the API Gateway.
6
+ *
7
+ * This class provides a complete interface for authentication on the Monaco protocol,
8
+ * including frontend wallet authentication and backend service authentication.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const authAPI = new AuthAPIImpl(walletClient, apiUrl, chain);
13
+ *
14
+ * // Complete authentication flow
15
+ * const authResult = await authAPI.authenticate(clientId);
16
+ *
17
+ * // Or step-by-step flow
18
+ * const challenge = await authAPI.createChallenge(userAddress, clientId);
19
+ * const signature = await authAPI.signChallenge(challenge.message);
20
+ * const authResult = await authAPI.verifySignature(
21
+ * userAddress,
22
+ * signature,
23
+ * challenge.nonce,
24
+ * clientId
25
+ * );
26
+ *
27
+ * // Authenticate backend service
28
+ * const backendAuth = await authAPI.authenticateBackend(secretKey);
29
+ * ```
30
+ */
31
+ import { APIError, InvalidConfigError } from "../../errors";
32
+ export class AuthAPIImpl {
33
+ /**
34
+ * Creates a new AuthAPI instance.
35
+ *
36
+ * @param walletClient - The viem wallet client for signing operations
37
+ * @param apiUrl - The base URL for the Monaco API Gateway
38
+ * @param chain - The blockchain network configuration
39
+ */
40
+ constructor(walletClient, apiUrl, chain) {
41
+ this.walletClient = walletClient;
42
+ this.apiUrl = apiUrl;
43
+ this.chain = chain;
44
+ }
45
+ /**
46
+ * Complete authentication flow for frontend applications.
47
+ *
48
+ * This method handles the entire authentication process:
49
+ * 1. Creates a challenge
50
+ * 2. Signs the challenge message
51
+ * 3. Verifies the signature and returns JWT tokens
52
+ *
53
+ * @param clientId - Client ID of the application
54
+ * @returns Promise resolving to the verification response with JWT tokens
55
+ * @throws {APIError} When authentication fails
56
+ * @throws {InvalidConfigError} When wallet account is not available
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Complete authentication in one call
61
+ * const authResult = await authAPI.authenticate("my-app-client-id");
62
+ * console.log(`Access token: ${authResult.access_token}`);
63
+ * console.log(`User ID: ${authResult.user.id}`);
64
+ * ```
65
+ */
66
+ async authenticate(clientId) {
67
+ try {
68
+ const account = this.walletClient.account;
69
+ if (!account) {
70
+ throw new InvalidConfigError("No account available in wallet client", "account");
71
+ }
72
+ // 1. Create challenge
73
+ const challenge = await this.createChallenge(account.address, clientId);
74
+ // 2. Sign the challenge message
75
+ const signature = await this.signChallenge(challenge.message);
76
+ // 3. Verify signature and get tokens
77
+ return await this.verifySignature(account.address, signature, challenge.nonce, clientId);
78
+ }
79
+ catch (error) {
80
+ throw new APIError("Failed to complete authentication flow", `${this.apiUrl}/api/v1/auth`, undefined);
81
+ }
82
+ }
83
+ /**
84
+ * Signs a challenge message using the wallet client.
85
+ *
86
+ * Signs the provided message using the wallet's private key.
87
+ * This is used in the authentication flow to prove ownership of the wallet.
88
+ *
89
+ * @param message - The message to sign
90
+ * @returns Promise resolving to the signature
91
+ * @throws {InvalidConfigError} When wallet account is not available
92
+ * @throws {APIError} When signing fails
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * const challenge = await authAPI.createChallenge(address, clientId);
97
+ * const signature = await authAPI.signChallenge(challenge.message);
98
+ * console.log(`Signature: ${signature}`);
99
+ * ```
100
+ */
101
+ async signChallenge(message) {
102
+ try {
103
+ const account = this.walletClient.account;
104
+ if (!account) {
105
+ throw new InvalidConfigError("No account available in wallet client", "account");
106
+ }
107
+ // Sign the message using the wallet client
108
+ const signature = await this.walletClient.signMessage({
109
+ account,
110
+ message,
111
+ });
112
+ return signature;
113
+ }
114
+ catch (error) {
115
+ throw new APIError("Failed to sign challenge message", "wallet_client", undefined);
116
+ }
117
+ }
118
+ /**
119
+ * Creates a challenge for frontend authentication.
120
+ *
121
+ * Generates a unique nonce and message that the user must sign with their wallet.
122
+ * This is the first step in the authentication flow for frontend applications.
123
+ *
124
+ * @param address - Wallet address of the user
125
+ * @param clientId - Client ID of the application
126
+ * @returns Promise resolving to the challenge response
127
+ * @throws {APIError} When challenge creation fails
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * const challenge = await authAPI.createChallenge(
132
+ * "0x1234...",
133
+ * "my-app-client-id"
134
+ * );
135
+ * console.log(`Challenge message: ${challenge.message}`);
136
+ * console.log(`Nonce: ${challenge.nonce}`);
137
+ * ```
138
+ */
139
+ async createChallenge(address, clientId) {
140
+ try {
141
+ const response = await fetch(`${this.apiUrl}/api/v1/auth/challenge`, {
142
+ method: "POST",
143
+ headers: {
144
+ "Content-Type": "application/json",
145
+ },
146
+ body: JSON.stringify({
147
+ address,
148
+ client_id: clientId,
149
+ chain_id: this.chain.id,
150
+ }),
151
+ });
152
+ if (!response.ok) {
153
+ throw new APIError(`API request failed: ${response.status}`, `${this.apiUrl}/api/v1/auth/challenge`, response.status);
154
+ }
155
+ const data = await response.json();
156
+ return {
157
+ nonce: data.nonce,
158
+ message: data.message,
159
+ expiresAt: data.expires_at,
160
+ };
161
+ }
162
+ catch (error) {
163
+ throw new APIError("Failed to create authentication challenge", `${this.apiUrl}/api/v1/auth/challenge`, undefined);
164
+ }
165
+ }
166
+ /**
167
+ * Verifies a signature for frontend authentication.
168
+ *
169
+ * Validates the signature against the challenge and returns JWT tokens for
170
+ * authenticated API access. This is the second step in the authentication flow.
171
+ *
172
+ * @param address - Wallet address of the user
173
+ * @param signature - Signature of the challenge message
174
+ * @param nonce - Nonce from the challenge response
175
+ * @param clientId - Client ID of the application
176
+ * @returns Promise resolving to the verification response with JWT tokens
177
+ * @throws {APIError} When signature verification fails
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * // First create a challenge
182
+ * const challenge = await authAPI.createChallenge(address, clientId);
183
+ *
184
+ * // User signs the challenge message with their wallet
185
+ * const signature = await wallet.signMessage(challenge.message);
186
+ *
187
+ * // Verify the signature and get tokens
188
+ * const authResult = await authAPI.verifySignature(
189
+ * address,
190
+ * signature,
191
+ * challenge.nonce,
192
+ * clientId
193
+ * );
194
+ *
195
+ * console.log(`Access token: ${authResult.accessToken}`);
196
+ * console.log(`User ID: ${authResult.user.id}`);
197
+ * ```
198
+ */
199
+ async verifySignature(address, signature, nonce, clientId) {
200
+ try {
201
+ const response = await fetch(`${this.apiUrl}/api/v1/auth/verify`, {
202
+ method: "POST",
203
+ headers: {
204
+ "Content-Type": "application/json",
205
+ },
206
+ body: JSON.stringify({
207
+ address,
208
+ signature,
209
+ nonce,
210
+ client_id: clientId,
211
+ chain_id: this.chain.id,
212
+ }),
213
+ });
214
+ if (!response.ok) {
215
+ throw new APIError(`API request failed: ${response.status}`, `${this.apiUrl}/api/v1/auth/verify`, response.status);
216
+ }
217
+ const data = await response.json();
218
+ return {
219
+ accessToken: data.access_token,
220
+ refreshToken: data.refresh_token,
221
+ expiresAt: data.expires_at,
222
+ user: {
223
+ id: data.user.id,
224
+ address: data.user.address,
225
+ username: data.user.username,
226
+ },
227
+ };
228
+ }
229
+ catch (error) {
230
+ throw new APIError("Failed to verify signature", `${this.apiUrl}/api/v1/auth/verify`, undefined);
231
+ }
232
+ }
233
+ /**
234
+ * Authenticates a backend service using a secret key.
235
+ *
236
+ * Returns JWT tokens for API access. This method is used for backend services
237
+ * that need to authenticate with the Monaco API Gateway.
238
+ *
239
+ * @param secretKey - Secret key of the application
240
+ * @returns Promise resolving to the backend auth response with JWT tokens
241
+ * @throws {APIError} When backend authentication fails
242
+ *
243
+ * @example
244
+ * ```typescript
245
+ * const backendAuth = await authAPI.authenticateBackend("my-secret-key");
246
+ * console.log(`Backend access token: ${backendAuth.accessToken}`);
247
+ * console.log(`Application: ${backendAuth.application.name}`);
248
+ * ```
249
+ */
250
+ async authenticateBackend(secretKey) {
251
+ try {
252
+ const response = await fetch(`${this.apiUrl}/api/v1/auth/backend`, {
253
+ method: "POST",
254
+ headers: {
255
+ "Content-Type": "application/json",
256
+ },
257
+ body: JSON.stringify({
258
+ secret_key: secretKey,
259
+ chain_id: this.chain.id,
260
+ }),
261
+ });
262
+ if (!response.ok) {
263
+ throw new APIError(`API request failed: ${response.status}`, `${this.apiUrl}/api/v1/auth/backend`, response.status);
264
+ }
265
+ const data = await response.json();
266
+ return {
267
+ accessToken: data.access_token,
268
+ expiresAt: data.expires_at,
269
+ application: {
270
+ id: data.application.id,
271
+ name: data.application.name,
272
+ clientId: data.application.client_id,
273
+ },
274
+ };
275
+ }
276
+ catch (error) {
277
+ throw new APIError("Failed to authenticate backend service", `${this.apiUrl}/api/v1/auth/backend`, undefined);
278
+ }
279
+ }
280
+ /**
281
+ * Refreshes an access token using a refresh token.
282
+ *
283
+ * Obtains a new access token using a valid refresh token. This is useful for
284
+ * maintaining long-term authentication without requiring the user to sign
285
+ * a new challenge.
286
+ *
287
+ * @param refreshToken - The refresh token to use
288
+ * @returns Promise resolving to new access and refresh tokens
289
+ * @throws {APIError} When token refresh fails
290
+ *
291
+ * @example
292
+ * ```typescript
293
+ * const newTokens = await authAPI.refreshToken(refreshToken);
294
+ * console.log(`New access token: ${newTokens.accessToken}`);
295
+ * console.log(`Expires at: ${new Date(newTokens.expiresAt * 1000)}`);
296
+ * ```
297
+ */
298
+ async refreshToken(refreshToken) {
299
+ try {
300
+ const response = await fetch(`${this.apiUrl}/api/v1/auth/refresh`, {
301
+ method: "POST",
302
+ headers: {
303
+ "Content-Type": "application/json",
304
+ },
305
+ body: JSON.stringify({
306
+ refresh_token: refreshToken,
307
+ }),
308
+ });
309
+ if (!response.ok) {
310
+ throw new APIError(`API request failed: ${response.status}`, `${this.apiUrl}/api/v1/auth/refresh`, response.status);
311
+ }
312
+ const data = await response.json();
313
+ return {
314
+ accessToken: data.access_token,
315
+ expiresAt: data.expires_at,
316
+ };
317
+ }
318
+ catch (error) {
319
+ throw new APIError("Failed to refresh token", `${this.apiUrl}/api/v1/auth/refresh`, undefined);
320
+ }
321
+ }
322
+ /**
323
+ * Revokes a refresh token.
324
+ *
325
+ * Invalidates a refresh token, preventing it from being used to obtain
326
+ * new access tokens. This is useful for logout functionality or when
327
+ * a token has been compromised.
328
+ *
329
+ * @param refreshToken - The refresh token to revoke
330
+ * @returns Promise resolving when the token is revoked
331
+ * @throws {APIError} When token revocation fails
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * await authAPI.revokeToken(refreshToken);
336
+ * console.log("Token revoked successfully");
337
+ * ```
338
+ */
339
+ async revokeToken(refreshToken) {
340
+ try {
341
+ const response = await fetch(`${this.apiUrl}/api/v1/auth/revoke`, {
342
+ method: "POST",
343
+ headers: {
344
+ "Content-Type": "application/json",
345
+ },
346
+ body: JSON.stringify({
347
+ refresh_token: refreshToken,
348
+ }),
349
+ });
350
+ if (!response.ok) {
351
+ throw new APIError(`API request failed: ${response.status}`, `${this.apiUrl}/api/v1/auth/revoke`, response.status);
352
+ }
353
+ }
354
+ catch (error) {
355
+ throw new APIError("Failed to revoke token", `${this.apiUrl}/api/v1/auth/revoke`, undefined);
356
+ }
357
+ }
358
+ }
359
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/api/auth/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAUH,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAE5D,MAAM,OAAO,WAAW;IACtB;;;;;;OAMG;IACH,YACmB,YAA0B,EAC1B,MAAc,EACd,KAAY;QAFZ,iBAAY,GAAZ,YAAY,CAAc;QAC1B,WAAM,GAAN,MAAM,CAAQ;QACd,UAAK,GAAL,KAAK,CAAO;IAC5B,CAAC;IAEJ;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,KAAK,CAAC,YAAY,CAAC,QAAgB;QACjC,IAAI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module
3
+ */
4
+ export { AuthAPIImpl } from "./api";
5
+ export type { AuthAPI } from "@0xmonaco/types";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/auth/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,YAAY,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Auth API Module
3
+ */
4
+ export { AuthAPIImpl } from "./api";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/api/auth/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * API Module Exports
3
+ *
4
+ * This module exports the new simplified APIs for the Monaco SDK.
5
+ */
6
+ export * from "./vault";
7
+ export * from "./trading";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * API Module Exports
3
+ *
4
+ * This module exports the new simplified APIs for the Monaco SDK.
5
+ */
6
+ export * from "./vault";
7
+ export * from "./trading";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC"}
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Trading API Implementation
3
+ *
4
+ * Handles trading operations including order placement and management.
5
+ * All operations use JWT authentication and go through the API Gateway.
6
+ *
7
+ * This class provides a complete interface for trading operations on the Monaco protocol,
8
+ * including various order types, order management, and position operations.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const tradingAPI = new TradingAPIImpl(apiUrl);
13
+ * tradingAPI.setAccessToken(jwtToken);
14
+ *
15
+ * // Place a limit order
16
+ * const order = await tradingAPI.placeLimitOrder(
17
+ * "ETH-USD",
18
+ * "BUY",
19
+ * "1.5",
20
+ * "2000.50"
21
+ * );
22
+ *
23
+ * // Get open orders
24
+ * const openOrders = await tradingAPI.getOpenOrders({ market: "ETH-USD" });
25
+ * ```
26
+ */
27
+ import type { TradingAPI, CreateOrderResponse, CancelOrderResponse, UpdateOrderResponse, OrderSide } from "@0xmonaco/types";
28
+ export declare class TradingAPIImpl implements TradingAPI {
29
+ private readonly apiUrl;
30
+ private accessToken?;
31
+ /**
32
+ * Creates a new TradingAPI instance.
33
+ *
34
+ * @param apiUrl - The base URL for the Monaco API Gateway
35
+ */
36
+ constructor(apiUrl: string);
37
+ /**
38
+ * Set the access token for the TradingAPI
39
+ * @param token - The access token to set
40
+ */
41
+ setAccessToken(token: string): void;
42
+ /**
43
+ * Internal method to make authenticated API requests.
44
+ *
45
+ * @param endpoint - The API endpoint to call
46
+ * @param options - Request options
47
+ * @returns Promise resolving to the response data
48
+ * @throws {APIError} When API communication fails
49
+ */
50
+ private makeRequest;
51
+ /**
52
+ * Places a limit order on the order book.
53
+ *
54
+ * Limit orders are executed only when the market price reaches or improves upon
55
+ * the specified price. They provide price protection but may not execute immediately.
56
+ *
57
+ * @param market - The trading pair identifier (e.g., "ETH-USD")
58
+ * @param side - The order side ("BUY" or "SELL")
59
+ * @param quantity - The order quantity as string
60
+ * @param price - The limit price as string
61
+ * @param options - Optional parameters for the limit order
62
+ * @param options.tradingMode - Trading mode (e.g., "SPOT")
63
+ * @returns Promise resolving to CreateOrderResponse with order details
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * const order = await tradingAPI.placeLimitOrder(
68
+ * "ETH-USD",
69
+ * "BUY",
70
+ * "1.5",
71
+ * "2000.50",
72
+ * { tradingMode: "SPOT" }
73
+ * );
74
+ * console.log(`Limit order placed: ${order.order_id}`);
75
+ * ```
76
+ */
77
+ placeLimitOrder(market: string, side: OrderSide, quantity: string, price: string, options?: {
78
+ tradingMode?: string;
79
+ }): Promise<CreateOrderResponse>;
80
+ /**
81
+ * Places a market order for immediate execution.
82
+ *
83
+ * Market orders execute immediately at the current market price. They provide
84
+ * immediate execution but may experience slippage.
85
+ *
86
+ * @param market - The trading pair identifier (e.g., "ETH-USD")
87
+ * @param side - The order side ("BUY" or "SELL")
88
+ * @param quantity - The order quantity as string
89
+ * @param options - Optional parameters for the market order
90
+ * @param options.tradingMode - Trading mode (e.g., "SPOT")
91
+ * @returns Promise resolving to CreateOrderResponse with order details
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * const order = await tradingAPI.placeMarketOrder(
96
+ * "ETH-USD",
97
+ * "SELL",
98
+ * "0.5",
99
+ * { tradingMode: "SPOT" }
100
+ * );
101
+ * ```
102
+ */
103
+ placeMarketOrder(market: string, side: OrderSide, quantity: string, options?: {
104
+ tradingMode?: string;
105
+ }): Promise<CreateOrderResponse>;
106
+ /**
107
+ * Cancels an existing order.
108
+ *
109
+ * Cancels an open order by its ID. The order must be in an open state to be cancelled.
110
+ * Cancelled orders cannot be recovered.
111
+ *
112
+ * @param orderId - The ID of the order to cancel
113
+ * @returns Promise resolving to CancelOrderResponse with cancellation details
114
+ * @throws {OrderError} When cancellation fails
115
+ * @throws {APIError} When API communication fails
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * const result = await tradingAPI.cancelOrder("order_123");
120
+ * console.log(`Order cancelled: ${result.status}`);
121
+ * ```
122
+ */
123
+ cancelOrder(orderId: string): Promise<CancelOrderResponse>;
124
+ /**
125
+ * Updates an existing order (price and/or quantity).
126
+ *
127
+ * Updates an open order by changing its price or quantity.
128
+ * This creates a new order and cancels the old one.
129
+ *
130
+ * @param orderId - The ID of the order to update
131
+ * @param updates - Fields to update
132
+ * @param updates.price - New price (optional)
133
+ * @param updates.quantity - New quantity (optional)
134
+ * @returns Promise resolving to UpdateOrderResponse with update details
135
+ * @throws {OrderError} When update fails
136
+ * @throws {APIError} When API communication fails
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * const result = await tradingAPI.updateOrder("order_123", {
141
+ * price: "2100.00",
142
+ * quantity: "2.0"
143
+ * });
144
+ * console.log(`Order updated: ${result.order_id}`);
145
+ * ```
146
+ */
147
+ updateOrder(orderId: string, updates: {
148
+ price?: string;
149
+ quantity?: string;
150
+ }): Promise<UpdateOrderResponse>;
151
+ }
152
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/api/trading/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EAEV,MAAM,iBAAiB,CAAC;AAGzB,qBAAa,cAAe,YAAW,UAAU;IAQnC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAPnC,OAAO,CAAC,WAAW,CAAC,CAAS;IAE7B;;;;OAIG;gBAC0B,MAAM,EAAE,MAAM;IAE3C;;;OAGG;IACH,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAInC;;;;;;;OAOG;YACW,WAAW;IAgCzB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACG,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QACR,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,GACA,OAAO,CAAC,mBAAmB,CAAC;IA2B/B;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,g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