@0xmonaco/core 0.8.7-develop.0e951a5 → 0.8.7-develop.0f12336
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.
- package/README.md +3 -12
- package/dist/api/applications/api.d.ts +61 -8
- package/dist/api/applications/api.js +75 -11
- package/dist/api/auth/api.d.ts +8 -27
- package/dist/api/auth/api.js +16 -52
- package/dist/api/base.d.ts +37 -2
- package/dist/api/base.js +62 -2
- package/dist/api/delegated-agents/api.d.ts +2 -1
- package/dist/api/delegated-agents/api.js +15 -11
- package/dist/api/faucet/api.d.ts +25 -0
- package/dist/api/faucet/api.js +29 -0
- package/dist/api/faucet/index.d.ts +1 -0
- package/dist/api/faucet/index.js +1 -0
- package/dist/api/fees/api.d.ts +13 -13
- package/dist/api/fees/api.js +18 -18
- package/dist/api/index.d.ts +4 -0
- package/dist/api/index.js +4 -0
- package/dist/api/margin-accounts/api.d.ts +9 -5
- package/dist/api/margin-accounts/api.js +70 -33
- package/dist/api/market/api.d.ts +5 -2
- package/dist/api/market/api.js +32 -16
- package/dist/api/orderbook/api.js +13 -13
- package/dist/api/perp/routes.d.ts +113 -37
- package/dist/api/perp/routes.js +37 -6
- package/dist/api/positions/api.d.ts +2 -1
- package/dist/api/positions/api.js +30 -18
- package/dist/api/profile/api.d.ts +40 -23
- package/dist/api/profile/api.js +78 -38
- package/dist/api/sub-accounts/api.d.ts +62 -0
- package/dist/api/sub-accounts/api.js +80 -0
- package/dist/api/sub-accounts/index.d.ts +1 -0
- package/dist/api/sub-accounts/index.js +1 -0
- package/dist/api/trades/api.d.ts +21 -10
- package/dist/api/trades/api.js +22 -10
- package/dist/api/trading/api.d.ts +11 -8
- package/dist/api/trading/api.js +70 -87
- package/dist/api/vault/api.d.ts +96 -29
- package/dist/api/vault/api.js +172 -43
- package/dist/api/vault/index.d.ts +1 -1
- package/dist/api/vault/index.js +1 -1
- package/dist/api/whitelist/api.d.ts +27 -0
- package/dist/api/whitelist/api.js +32 -0
- package/dist/api/whitelist/index.d.ts +1 -0
- package/dist/api/whitelist/index.js +1 -0
- package/dist/api/withdrawals/api.d.ts +18 -0
- package/dist/api/withdrawals/api.js +35 -0
- package/dist/api/withdrawals/index.d.ts +1 -0
- package/dist/api/withdrawals/index.js +1 -0
- package/dist/coverage.d.ts +93 -0
- package/dist/coverage.js +93 -0
- package/dist/sdk.d.ts +34 -3
- package/dist/sdk.js +88 -0
- package/package.json +3 -3
package/dist/api/trading/api.js
CHANGED
|
@@ -21,19 +21,19 @@
|
|
|
21
21
|
* );
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
import { BatchCreateOrdersSchema, BatchReplaceOrdersSchema, CancelConditionalOrderSchema, CancelOrderSchema,
|
|
24
|
+
import { BatchCreateOrdersSchema, BatchReplaceOrdersSchema, CancelConditionalOrderSchema, CancelOrderSchema, GetPaginatedOrdersSchema, ListConditionalOrdersSchema, PlaceLimitOrderSchema, PlaceMarketOrderSchema, ReplaceOrderSchema, validate, } from "@0xmonaco/types";
|
|
25
25
|
import { BaseAPI } from "../base";
|
|
26
26
|
import { perpRoutes } from "../perp";
|
|
27
27
|
function parentTpSlLegToRequest(leg) {
|
|
28
28
|
if (!leg)
|
|
29
29
|
return undefined;
|
|
30
30
|
return {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
triggerPrice: leg.triggerPrice,
|
|
32
|
+
orderType: leg.orderType,
|
|
33
|
+
limitPrice: leg.limitPrice,
|
|
34
|
+
timeInForce: leg.timeInForce,
|
|
35
|
+
slippageToleranceBps: leg.slippageToleranceBps,
|
|
36
|
+
expiresAt: leg.expiresAt,
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
export class TradingAPIImpl extends BaseAPI {
|
|
@@ -97,22 +97,24 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
97
97
|
options,
|
|
98
98
|
});
|
|
99
99
|
const requestBody = {
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
tradingPairId: tradingPairId,
|
|
101
|
+
orderType: "LIMIT",
|
|
102
102
|
side,
|
|
103
103
|
price,
|
|
104
104
|
quantity,
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
tradingMode: options?.tradingMode || "SPOT",
|
|
106
|
+
useMasterBalance: options?.useMasterBalance,
|
|
107
|
+
expirationDate: options?.expirationDate,
|
|
108
|
+
timeInForce: options?.timeInForce,
|
|
109
|
+
marginAccountId: options?.marginAccountId,
|
|
110
|
+
riskBucketId: options?.riskBucketId,
|
|
111
|
+
riskBucketCollateral: options?.riskBucketCollateral,
|
|
112
|
+
strategyKey: options?.strategyKey,
|
|
113
|
+
positionSide: options?.positionSide,
|
|
112
114
|
leverage: options?.leverage,
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
reduceOnly: options?.reduceOnly,
|
|
116
|
+
takeProfit: parentTpSlLegToRequest(options?.takeProfit),
|
|
117
|
+
stopLoss: parentTpSlLegToRequest(options?.stopLoss),
|
|
116
118
|
};
|
|
117
119
|
return await this.makeAuthenticatedRequest(perpRoutes.orders.create(), {
|
|
118
120
|
method: "POST",
|
|
@@ -169,20 +171,22 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
169
171
|
options,
|
|
170
172
|
});
|
|
171
173
|
const requestBody = {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
+
tradingPairId: tradingPairId,
|
|
175
|
+
orderType: "MARKET",
|
|
174
176
|
side,
|
|
175
|
-
|
|
177
|
+
slippageToleranceBps: options?.slippageTolerance !== undefined ? Math.round(options.slippageTolerance * 10000) : undefined,
|
|
176
178
|
price: null, // Market orders don't need price
|
|
177
179
|
quantity,
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
tradingMode: options?.tradingMode || "SPOT",
|
|
181
|
+
marginAccountId: options?.marginAccountId,
|
|
182
|
+
riskBucketId: options?.riskBucketId,
|
|
183
|
+
riskBucketCollateral: options?.riskBucketCollateral,
|
|
184
|
+
strategyKey: options?.strategyKey,
|
|
185
|
+
positionSide: options?.positionSide,
|
|
182
186
|
leverage: options?.leverage,
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
187
|
+
reduceOnly: options?.reduceOnly,
|
|
188
|
+
takeProfit: parentTpSlLegToRequest(options?.takeProfit),
|
|
189
|
+
stopLoss: parentTpSlLegToRequest(options?.stopLoss),
|
|
186
190
|
};
|
|
187
191
|
return await this.makeAuthenticatedRequest(perpRoutes.orders.create(), {
|
|
188
192
|
method: "POST",
|
|
@@ -209,36 +213,13 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
209
213
|
// Validate inputs before making API call
|
|
210
214
|
validate(CancelOrderSchema, { orderId });
|
|
211
215
|
const requestBody = {
|
|
212
|
-
|
|
216
|
+
orderId: orderId,
|
|
213
217
|
};
|
|
214
218
|
return await this.makeAuthenticatedRequest(perpRoutes.orders.cancel(), {
|
|
215
219
|
method: "POST",
|
|
216
220
|
body: JSON.stringify(requestBody),
|
|
217
221
|
});
|
|
218
222
|
}
|
|
219
|
-
async createConditionalOrder(params) {
|
|
220
|
-
validate(CreateConditionalOrderSchema, params);
|
|
221
|
-
const requestBody = {
|
|
222
|
-
trading_pair_id: params.tradingPairId,
|
|
223
|
-
margin_account_id: params.marginAccountId,
|
|
224
|
-
condition_type: params.conditionType,
|
|
225
|
-
trigger_price: params.triggerPrice,
|
|
226
|
-
trigger_source: params.triggerSource ?? "MARK_PRICE",
|
|
227
|
-
side: params.side,
|
|
228
|
-
position_side: params.positionSide,
|
|
229
|
-
order_type: params.orderType,
|
|
230
|
-
limit_price: params.limitPrice,
|
|
231
|
-
quantity: params.quantity,
|
|
232
|
-
reduce_only: params.reduceOnly ?? true,
|
|
233
|
-
time_in_force: params.timeInForce,
|
|
234
|
-
slippage_tolerance_bps: params.slippageToleranceBps,
|
|
235
|
-
expires_at: params.expiresAt,
|
|
236
|
-
};
|
|
237
|
-
return await this.makeAuthenticatedRequest(perpRoutes.orders.createConditional(), {
|
|
238
|
-
method: "POST",
|
|
239
|
-
body: JSON.stringify(requestBody),
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
223
|
async cancelConditionalOrder(conditionalOrderId) {
|
|
243
224
|
validate(CancelConditionalOrderSchema, { conditionalOrderId });
|
|
244
225
|
return await this.makeAuthenticatedRequest(perpRoutes.orders.cancelConditional(conditionalOrderId), {
|
|
@@ -249,12 +230,12 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
249
230
|
if (params) {
|
|
250
231
|
validate(ListConditionalOrdersSchema, params);
|
|
251
232
|
}
|
|
252
|
-
const { page = 1,
|
|
233
|
+
const { page = 1, pageSize = 20, marginAccountId, tradingPairId, state } = params || {};
|
|
253
234
|
return await this.makeAuthenticatedRequest(perpRoutes.orders.listConditional({
|
|
254
235
|
page,
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
236
|
+
pageSize: Math.min(Math.max(pageSize, 1), 100),
|
|
237
|
+
marginAccountId,
|
|
238
|
+
tradingPairId,
|
|
258
239
|
state,
|
|
259
240
|
}), { method: "GET" });
|
|
260
241
|
}
|
|
@@ -276,7 +257,7 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
276
257
|
}
|
|
277
258
|
return this.makeAuthenticatedRequest(perpRoutes.orders.batchCancel(), {
|
|
278
259
|
method: "POST",
|
|
279
|
-
body: JSON.stringify({
|
|
260
|
+
body: JSON.stringify({ orderIds: orderIds }),
|
|
280
261
|
});
|
|
281
262
|
}
|
|
282
263
|
/**
|
|
@@ -305,7 +286,7 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
305
286
|
// Validate inputs before making API call
|
|
306
287
|
validate(ReplaceOrderSchema, { orderId, newOrder });
|
|
307
288
|
const requestBody = {
|
|
308
|
-
|
|
289
|
+
useMasterBalance: newOrder.useMasterBalance ?? false,
|
|
309
290
|
};
|
|
310
291
|
if (newOrder.price !== undefined) {
|
|
311
292
|
requestBody.price = newOrder.price;
|
|
@@ -352,21 +333,23 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
352
333
|
validate(BatchCreateOrdersSchema, { orders });
|
|
353
334
|
const requestBody = {
|
|
354
335
|
orders: orders.map((order) => ({
|
|
355
|
-
|
|
356
|
-
|
|
336
|
+
tradingPairId: order.tradingPairId,
|
|
337
|
+
orderType: order.orderType,
|
|
357
338
|
side: order.side,
|
|
358
339
|
price: order.price,
|
|
359
340
|
quantity: order.quantity,
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
341
|
+
tradingMode: order.tradingMode || "SPOT",
|
|
342
|
+
slippageToleranceBps: order.slippageTolerance !== undefined ? Math.round(order.slippageTolerance * 10000) : undefined,
|
|
343
|
+
useMasterBalance: order.useMasterBalance,
|
|
344
|
+
expirationDate: order.expirationDate,
|
|
345
|
+
timeInForce: order.timeInForce,
|
|
346
|
+
marginAccountId: order.marginAccountId,
|
|
347
|
+
riskBucketId: order.riskBucketId,
|
|
348
|
+
riskBucketCollateral: order.riskBucketCollateral,
|
|
349
|
+
strategyKey: order.strategyKey,
|
|
350
|
+
positionSide: order.positionSide,
|
|
368
351
|
leverage: order.leverage,
|
|
369
|
-
|
|
352
|
+
reduceOnly: order.reduceOnly,
|
|
370
353
|
})),
|
|
371
354
|
};
|
|
372
355
|
return this.makeAuthenticatedRequest(perpRoutes.orders.batchCreate(), {
|
|
@@ -403,10 +386,10 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
403
386
|
validate(BatchReplaceOrdersSchema, { orders });
|
|
404
387
|
const requestBody = {
|
|
405
388
|
orders: orders.map((order) => ({
|
|
406
|
-
|
|
389
|
+
orderId: order.orderId,
|
|
407
390
|
price: order.price,
|
|
408
391
|
quantity: order.quantity,
|
|
409
|
-
|
|
392
|
+
useMasterBalance: order.useMasterBalance,
|
|
410
393
|
})),
|
|
411
394
|
};
|
|
412
395
|
return this.makeAuthenticatedRequest(perpRoutes.orders.batchReplace(), {
|
|
@@ -422,9 +405,9 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
422
405
|
*
|
|
423
406
|
* @param params - Query parameters for filtering orders
|
|
424
407
|
* @param params.status - Filter by order status (e.g., "SUBMITTED", "FILLED") (optional)
|
|
425
|
-
* @param params.
|
|
408
|
+
* @param params.tradingPairId - Filter by trading pair UUID (optional)
|
|
426
409
|
* @param params.page - Page number for pagination (defaults to 1, must be > 0)
|
|
427
|
-
* @param params.
|
|
410
|
+
* @param params.pageSize - Number of orders per page (defaults to 10, max 100, must be > 0)
|
|
428
411
|
* @returns Promise resolving to GetPaginatedOrdersResponse with orders and pagination info
|
|
429
412
|
* @throws {APIError} When API communication fails
|
|
430
413
|
*
|
|
@@ -433,13 +416,13 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
433
416
|
* // Get submitted orders for a specific trading pair with custom pagination
|
|
434
417
|
* const orders = await tradingAPI.getPaginatedOrders({
|
|
435
418
|
* status: "SUBMITTED",
|
|
436
|
-
*
|
|
419
|
+
* tradingPairId: "456e7890-e12b-12d3-a456-426614174000",
|
|
437
420
|
* page: 1,
|
|
438
|
-
*
|
|
421
|
+
* pageSize: 20
|
|
439
422
|
* });
|
|
440
423
|
* console.log(`Found ${orders.total} orders, showing page ${orders.page}`);
|
|
441
424
|
*
|
|
442
|
-
* // Get all orders with default pagination (page=1,
|
|
425
|
+
* // Get all orders with default pagination (page=1, pageSize=10)
|
|
443
426
|
* const allOrders = await tradingAPI.getPaginatedOrders();
|
|
444
427
|
* ```
|
|
445
428
|
*/
|
|
@@ -449,25 +432,25 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
449
432
|
validate(GetPaginatedOrdersSchema, params);
|
|
450
433
|
}
|
|
451
434
|
// Set pagination defaults with destructuring and validation
|
|
452
|
-
const { page = 1,
|
|
435
|
+
const { page = 1, pageSize: requestedPageSize = 10, status, tradingPairId, tradingMode, marginAccountId } = params || {};
|
|
453
436
|
const validPage = page > 0 ? page : 1;
|
|
454
|
-
const validPageSize =
|
|
437
|
+
const validPageSize = requestedPageSize > 0 ? requestedPageSize : 10;
|
|
455
438
|
const pageSize = Math.min(validPageSize, 100);
|
|
456
439
|
const searchParams = new URLSearchParams();
|
|
457
440
|
// Always include pagination parameters for consistent API behavior
|
|
458
441
|
searchParams.append("page", validPage.toString());
|
|
459
|
-
searchParams.append("
|
|
442
|
+
searchParams.append("pageSize", pageSize.toString());
|
|
460
443
|
if (status) {
|
|
461
444
|
searchParams.append("status", status);
|
|
462
445
|
}
|
|
463
|
-
if (
|
|
464
|
-
searchParams.append("
|
|
446
|
+
if (tradingPairId) {
|
|
447
|
+
searchParams.append("tradingPairId", tradingPairId);
|
|
465
448
|
}
|
|
466
|
-
if (
|
|
467
|
-
searchParams.append("
|
|
449
|
+
if (tradingMode) {
|
|
450
|
+
searchParams.append("tradingMode", tradingMode);
|
|
468
451
|
}
|
|
469
|
-
if (
|
|
470
|
-
searchParams.append("
|
|
452
|
+
if (marginAccountId) {
|
|
453
|
+
searchParams.append("marginAccountId", marginAccountId);
|
|
471
454
|
}
|
|
472
455
|
const endpoint = `/api/v1/orders?${searchParams.toString()}`;
|
|
473
456
|
return await this.makeAuthenticatedRequest(endpoint, {
|
|
@@ -489,7 +472,7 @@ export class TradingAPIImpl extends BaseAPI {
|
|
|
489
472
|
* // Get order details by ID
|
|
490
473
|
* const orderDetails = await tradingAPI.getOrder("order_123");
|
|
491
474
|
* console.log(`Order status: ${orderDetails.order.status}`);
|
|
492
|
-
* console.log(`Remaining quantity: ${orderDetails.order.
|
|
475
|
+
* console.log(`Remaining quantity: ${orderDetails.order.remainingQuantity}`);
|
|
493
476
|
* ```
|
|
494
477
|
*/
|
|
495
478
|
async getOrder(orderId) {
|
package/dist/api/vault/api.d.ts
CHANGED
|
@@ -23,9 +23,21 @@
|
|
|
23
23
|
* console.log(`Deposit transaction: ${result.hash}`);
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
|
-
import type { ApplicationsAPI, Balance, ProfileAPI, TransactionResult, VaultAPI, WithdrawResult } from "@0xmonaco/types";
|
|
26
|
+
import type { ApplicationsAPI, Balance, DepositTarget, ProfileAPI, TransactionResult, VaultAPI, WithdrawalRetryOptions, WithdrawalSource, WithdrawResult } from "@0xmonaco/types";
|
|
27
27
|
import { type Address, type Chain, type PublicClient, type WalletClient } from "viem";
|
|
28
28
|
import { BaseAPI } from "../base";
|
|
29
|
+
/**
|
|
30
|
+
* Encode the on-chain `applicationData` for a deposit.
|
|
31
|
+
*
|
|
32
|
+
* For `"spot"` (the default) this stays the bare `clientId` string — byte-for-byte
|
|
33
|
+
* identical to the legacy encoding, so existing deposits are unchanged. For
|
|
34
|
+
* `"margin"` it emits a small JSON routing hint the indexer decodes to credit the
|
|
35
|
+
* deposit to margin collateral.
|
|
36
|
+
*
|
|
37
|
+
* MUST stay in sync with the indexer decoder in
|
|
38
|
+
* `indexer/src/listeners/deposit_routing.rs`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function encodeDepositApplicationData(clientId: string, target: DepositTarget): string;
|
|
29
41
|
export declare class VaultAPIImpl extends BaseAPI implements VaultAPI {
|
|
30
42
|
private readonly publicClient;
|
|
31
43
|
private readonly chain;
|
|
@@ -121,6 +133,10 @@ export declare class VaultAPIImpl extends BaseAPI implements VaultAPI {
|
|
|
121
133
|
* @param assetId - The asset identifier (UUID) to deposit
|
|
122
134
|
* @param amount - The amount of tokens to deposit (as bigint)
|
|
123
135
|
* @param autoWait - Whether to automatically wait for transaction confirmation (defaults to true)
|
|
136
|
+
* @param target - Destination ledger: `"spot"` (default) credits the spot
|
|
137
|
+
* wallet; `"margin"` routes the deposit into the parent margin account's
|
|
138
|
+
* collateral (auto-creating the account if needed). Margin deposits that
|
|
139
|
+
* cannot be routed safely fall back to spot.
|
|
124
140
|
* @returns Promise resolving to TransactionResult with transaction details
|
|
125
141
|
* @throws {ContractError} When deposit fails or approval is insufficient
|
|
126
142
|
* @throws {APIError} When the asset is not found or the assetId is invalid
|
|
@@ -136,6 +152,14 @@ export declare class VaultAPIImpl extends BaseAPI implements VaultAPI {
|
|
|
136
152
|
* console.log(`Deposit transaction: ${result.hash}`);
|
|
137
153
|
* console.log(`Status: ${result.status}`); // "confirmed" if successful
|
|
138
154
|
*
|
|
155
|
+
* // Deposit straight into margin collateral
|
|
156
|
+
* const marginDeposit = await vaultAPI.deposit(
|
|
157
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
158
|
+
* parseUnits("100", 6),
|
|
159
|
+
* true,
|
|
160
|
+
* "margin"
|
|
161
|
+
* );
|
|
162
|
+
*
|
|
139
163
|
* // Or skip auto-waiting
|
|
140
164
|
* const result = await vaultAPI.deposit(
|
|
141
165
|
* "123e4567-e89b-12d3-a456-426614174000",
|
|
@@ -146,23 +170,54 @@ export declare class VaultAPIImpl extends BaseAPI implements VaultAPI {
|
|
|
146
170
|
* const receipt = await sdk.waitForTransaction(result.hash);
|
|
147
171
|
* ```
|
|
148
172
|
*/
|
|
149
|
-
deposit(assetId: string, amount: bigint, autoWait?: boolean): Promise<TransactionResult>;
|
|
173
|
+
deposit(assetId: string, amount: bigint, autoWait?: boolean, target?: DepositTarget): Promise<TransactionResult>;
|
|
150
174
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
175
|
+
* Polls `GET /api/v1/withdrawals/{index}` until the gateway can serve the
|
|
176
|
+
* `executeWithdrawal` calldata, then returns it with the vault address.
|
|
177
|
+
*
|
|
178
|
+
* The calldata only exists once the withdrawal's root is confirmed on-chain
|
|
179
|
+
* and its merkle proof is persisted. Until then the gateway returns 404 (the
|
|
180
|
+
* row is written asynchronously by the persistor after `initiate`) or 409
|
|
181
|
+
* (the proof is not confirmed yet); both are transient for a freshly-allocated
|
|
182
|
+
* index, so we retry on each until ready or the timeout elapses.
|
|
153
183
|
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
184
|
+
* Returns `null` when the timeout elapses before the proof is available — the
|
|
185
|
+
* index is still valid and the balance already debited, so callers surface
|
|
186
|
+
* this as `awaiting_proof` (with the index intact) rather than as a thrown
|
|
187
|
+
* error that would strand the index. Only non-transient errors (anything
|
|
188
|
+
* other than 404/409) throw.
|
|
189
|
+
*
|
|
190
|
+
* @throws {APIError} On non-transient errors while polling.
|
|
191
|
+
*/
|
|
192
|
+
private pollWithdrawalCalldata;
|
|
193
|
+
/**
|
|
194
|
+
* Initiates a withdrawal through the API Gateway, waits for its on-chain
|
|
195
|
+
* confirmation, then submits the resulting `executeWithdrawal` calldata via
|
|
196
|
+
* the connected wallet.
|
|
197
|
+
*
|
|
198
|
+
* The gateway allocates a `withdrawalIndex` via the matching engine and debits
|
|
199
|
+
* the balance immediately, but the executable calldata needs the withdrawal's
|
|
200
|
+
* merkle proof, which only exists once its root is confirmed on-chain. This
|
|
201
|
+
* method polls until the calldata is available (see `retry`), then submits it.
|
|
202
|
+
* The connected wallet pays gas; the contract authorises the withdrawal
|
|
203
|
+
* against the merkle proof embedded in the calldata. The wallet's address is
|
|
204
|
+
* sent as the on-chain `destination`.
|
|
205
|
+
*
|
|
206
|
+
* If the proof does not become available within the poll window, this method
|
|
207
|
+
* does NOT throw — it returns `{ withdrawalIndex, status: "awaiting_proof" }`
|
|
208
|
+
* with nothing submitted on-chain. The balance is already debited, so losing
|
|
209
|
+
* the index would strand the funds; the caller keeps it and finishes the
|
|
210
|
+
* withdrawal later with `retryWithdrawal(withdrawalIndex)`.
|
|
160
211
|
*
|
|
161
212
|
* @param assetId - The asset identifier (UUID) to withdraw
|
|
162
213
|
* @param amount - The raw token amount to withdraw (as bigint)
|
|
163
214
|
* @param autoWait - Whether to automatically wait for transaction confirmation (defaults to true)
|
|
164
|
-
* @
|
|
165
|
-
* @
|
|
215
|
+
* @param sourceOrRetry - Source ledger (`"spot"` or `"margin"`) or legacy retry options
|
|
216
|
+
* @param retry - Polling cadence/timeout while waiting for the proof when a source is supplied
|
|
217
|
+
* @returns Promise resolving to a [`WithdrawResult`]: on success the submitted
|
|
218
|
+
* transaction plus `withdrawalIndex`; on poll-timeout `{ withdrawalIndex,
|
|
219
|
+
* status: "awaiting_proof" }`.
|
|
220
|
+
* @throws {APIError} When the asset is not found or the initiate request is rejected
|
|
166
221
|
* @throws {ContractError} When the on-chain submission fails
|
|
167
222
|
* @throws {InvalidConfigError} When wallet account is not available
|
|
168
223
|
*
|
|
@@ -174,38 +229,50 @@ export declare class VaultAPIImpl extends BaseAPI implements VaultAPI {
|
|
|
174
229
|
* parseUnits("50", 6),
|
|
175
230
|
* );
|
|
176
231
|
* console.log(`Withdrawal index: ${result.withdrawalIndex}`);
|
|
177
|
-
*
|
|
232
|
+
* if (result.status === "awaiting_proof") {
|
|
233
|
+
* // Proof not ready yet — balance already debited. Finish later:
|
|
234
|
+
* await vaultAPI.retryWithdrawal(result.withdrawalIndex);
|
|
235
|
+
* } else {
|
|
236
|
+
* console.log(`Tx hash: ${result.hash}, status: ${result.status}`);
|
|
237
|
+
* }
|
|
178
238
|
*
|
|
179
|
-
* //
|
|
239
|
+
* // Custom polling (check every 10s, wait up to an hour)
|
|
180
240
|
* const result = await vaultAPI.withdraw(
|
|
181
241
|
* "123e4567-e89b-12d3-a456-426614174000",
|
|
182
242
|
* parseUnits("50", 6),
|
|
183
|
-
*
|
|
243
|
+
* true,
|
|
244
|
+
* { pollIntervalMs: 10_000, timeoutMs: 3_600_000 },
|
|
184
245
|
* );
|
|
185
|
-
* const receipt = await sdk.waitForTransaction(result.hash);
|
|
186
246
|
* ```
|
|
187
247
|
*/
|
|
188
|
-
withdraw(assetId: string, amount: bigint, autoWait?: boolean): Promise<WithdrawResult>;
|
|
248
|
+
withdraw(assetId: string, amount: bigint, autoWait?: boolean, sourceOrRetry?: WithdrawalSource | WithdrawalRetryOptions, retry?: WithdrawalRetryOptions): Promise<WithdrawResult>;
|
|
189
249
|
/**
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
250
|
+
* Submits (or resubmits) a previously-initiated withdrawal on-chain.
|
|
251
|
+
*
|
|
252
|
+
* Use this when the original `withdraw()` could not complete — e.g. the proof
|
|
253
|
+
* was not yet confirmed within its polling window, the wallet rejected the tx,
|
|
254
|
+
* the page reloaded before the receipt came back, or a stuck mempool entry
|
|
255
|
+
* needs resending. Polls `GET /withdrawals/{index}` for the
|
|
256
|
+
* `executeWithdrawal` calldata (retrying while it is not yet confirmed), then
|
|
257
|
+
* submits it through the connected wallet. Does NOT initiate a new withdrawal
|
|
258
|
+
* — the matching engine already debited the balance and allocated the index.
|
|
259
|
+
* The contract is idempotent against double-submission of a settled
|
|
260
|
+
* withdrawal: it reverts once the index is consumed on-chain.
|
|
193
261
|
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
* already debited the balance and allocated the index. The contract is
|
|
198
|
-
* idempotent against double-submission of a settled withdrawal: it will
|
|
199
|
-
* revert once the index is consumed on-chain.
|
|
262
|
+
* Like `withdraw()`, if the proof is still unavailable within the poll window
|
|
263
|
+
* this returns `{ withdrawalIndex, status: "awaiting_proof" }` instead of
|
|
264
|
+
* throwing, so the caller can simply retry again later.
|
|
200
265
|
*
|
|
201
266
|
* @param withdrawalIndex - The index returned by the original `withdraw()` call
|
|
202
267
|
* @param autoWait - Whether to await on-chain confirmation (defaults to true)
|
|
203
|
-
* @
|
|
204
|
-
* @
|
|
268
|
+
* @param retry - Polling cadence/timeout while waiting for the proof
|
|
269
|
+
* @returns Promise resolving to a [`WithdrawResult`]: the submitted transaction
|
|
270
|
+
* plus `withdrawalIndex`, or `{ withdrawalIndex, status: "awaiting_proof" }`
|
|
271
|
+
* if the proof did not arrive within the poll window.
|
|
205
272
|
* @throws {ContractError} When the on-chain submission fails
|
|
206
273
|
* @throws {InvalidConfigError} When wallet account is not available
|
|
207
274
|
*/
|
|
208
|
-
retryWithdrawal(withdrawalIndex: number, autoWait?: boolean): Promise<WithdrawResult>;
|
|
275
|
+
retryWithdrawal(withdrawalIndex: number, autoWait?: boolean, retry?: WithdrawalRetryOptions): Promise<WithdrawResult>;
|
|
209
276
|
/**
|
|
210
277
|
* Retrieves the user's token balance in the vault.
|
|
211
278
|
*
|