@arcaresearch/sdk 0.0.3 → 0.0.5

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/dist/arca.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { ArcaConfig, ArcaObject, ArcaObjectBrowseResponse, ArcaObjectDetailResponse, ArcaObjectListResponse, ArcaObjectVersionsResponse, ArcaBalance, AggregationSourceDto, BrowseObjectsOptions, CreateArcaObjectResponse, CreateArcaOptions, CreateDenominatedArcaOptions, CreateWatchResponse, DeleteArcaObjectResponse, DepositOptions, EnsureDeletedOptions, WithdrawalOptions, InitiateWithdrawalResponse, EventCallback, EventDetailResponse, EventSubscription, EventSubscriptionOptions, ExplorerSummary, InitiateDepositResponse, ListObjectsOptions, ListEventsOptions, ListOperationsOptions, EventListResponse, NonceResponse, Operation, OperationDetailResponse, OperationListResponse, OrderOperationResponse, PathAggregation, PnlResponse, EquityHistoryResponse, ReconciliationStateListResponse, SnapshotBalancesResponse, StateDeltaListResponse, TransferOptions, TransferResponse, CancelOrderOptions, CreatePerpsExchangeOptions, PlaceOrderOptions, UpdateLeverageOptions, UpdateLeverageResponse, LeverageSetting, ExchangeState, SimOrder, SimOrderWithFills, SimPosition, SimMetaResponse, SimMidsResponse, SimBookResponse, InvariantCheckResponse, ActiveAssetData, CandleInterval, CandlesResponse, CreatePaymentLinkOptions, CreatePaymentLinkResponse, ListPaymentLinksOptions, PaymentLinkListResponse } from './types';
1
+ import { OperationHandle, OrderHandle } from './operation-handle';
2
+ import { ArcaConfig, ArcaObject, ArcaObjectBrowseResponse, ArcaObjectDetailResponse, ArcaObjectListResponse, ArcaObjectVersionsResponse, ArcaBalance, AggregationSourceDto, BrowseObjectsOptions, CreateArcaObjectResponse, CreateArcaOptions, CreateDenominatedArcaOptions, CreateWatchResponse, DeleteArcaObjectResponse, FundAccountOptions, EnsureDeletedOptions, DefundAccountOptions, DefundAccountResponse, EventCallback, EventDetailResponse, EventSubscription, EventSubscriptionOptions, ExplorerSummary, FundAccountResponse, ListObjectsOptions, ListEventsOptions, ListOperationsOptions, EventListResponse, NonceResponse, Operation, OperationDetailResponse, OperationListResponse, OrderOperationResponse, PathAggregation, PnlResponse, EquityHistoryResponse, ReconciliationStateListResponse, SnapshotBalancesResponse, StateDeltaListResponse, TransferOptions, TransferResponse, CancelOrderOptions, CreatePerpsExchangeOptions, PlaceOrderOptions, UpdateLeverageOptions, UpdateLeverageResponse, LeverageSetting, ExchangeState, SimOrder, SimOrderWithFills, SimPosition, SimMetaResponse, SimMidsResponse, SimBookResponse, InvariantCheckResponse, ActiveAssetData, CandleInterval, CandlesResponse, CreatePaymentLinkOptions, CreatePaymentLinkResponse, ListPaymentLinksOptions, PaymentLinkListResponse } from './types';
2
3
  import { WebSocketManager } from './websocket';
3
4
  /**
4
5
  * The Arca SDK client.
@@ -52,29 +53,28 @@ export declare class Arca {
52
53
  * If one already exists with matching type/denomination, it is returned.
53
54
  * Otherwise a new one is created.
54
55
  *
55
- * Pass `operationPath` (from the nonce API with separator ":") for
56
- * explicit operation-level idempotency, especially when delete+recreate
57
- * scenarios are expected.
56
+ * `await` waits for the object to be fully created (operation completed).
57
+ * Use `.submitted` to access the HTTP response before the operation settles.
58
58
  */
59
- createDenominatedArca(opts: CreateDenominatedArcaOptions): Promise<CreateArcaObjectResponse>;
59
+ createDenominatedArca(opts: CreateDenominatedArcaOptions): OperationHandle<CreateArcaObjectResponse>;
60
60
  /**
61
61
  * Create an Arca object of any type at the given path (idempotent).
62
62
  *
63
- * Pass `operationPath` (from the nonce API with separator ":") for
64
- * explicit operation-level idempotency.
63
+ * `await` waits for the object to be fully created (operation completed).
64
+ * Use `.submitted` to access the HTTP response before the operation settles.
65
65
  */
66
- createArca(opts: CreateArcaOptions): Promise<CreateArcaObjectResponse>;
66
+ createArca(opts: CreateArcaOptions): OperationHandle<CreateArcaObjectResponse>;
67
67
  /** @deprecated Use createDenominatedArca instead */
68
- ensureDenominatedArca(opts: CreateDenominatedArcaOptions): Promise<CreateArcaObjectResponse>;
68
+ ensureDenominatedArca(opts: CreateDenominatedArcaOptions): OperationHandle<CreateArcaObjectResponse>;
69
69
  /** @deprecated Use createArca instead */
70
- ensureArca(opts: CreateArcaOptions): Promise<CreateArcaObjectResponse>;
70
+ ensureArca(opts: CreateArcaOptions): OperationHandle<CreateArcaObjectResponse>;
71
71
  /**
72
72
  * Delete an Arca object by path. If sweepTo is provided, remaining funds
73
73
  * are transferred to that Arca before deletion.
74
74
  *
75
75
  * If the object is already deleted, returns the existing state.
76
76
  */
77
- ensureDeleted(opts: EnsureDeletedOptions): Promise<DeleteArcaObjectResponse>;
77
+ ensureDeleted(opts: EnsureDeletedOptions): OperationHandle<DeleteArcaObjectResponse>;
78
78
  /**
79
79
  * Get an Arca object by path.
80
80
  */
@@ -111,22 +111,27 @@ export declare class Arca {
111
111
  getSnapshotBalances(objectId: string, asOf: string): Promise<SnapshotBalancesResponse>;
112
112
  /**
113
113
  * Execute a transfer between two Arca objects.
114
- * Settlement is immediate for denominated targets, or async for
115
- * targets that require a receiver workflow (e.g. exchange objects).
114
+ * Settlement is immediate for denominated targets (await resolves instantly),
115
+ * or async for exchange targets (await waits for settlement).
116
116
  * The operation path serves as the idempotency key.
117
117
  */
118
- transfer(opts: TransferOptions): Promise<TransferResponse>;
118
+ transfer(opts: TransferOptions): OperationHandle<TransferResponse>;
119
119
  /**
120
- * Initiate a deposit to a denominated Arca object.
121
- * In demo realms, deposits are simulated with a configurable delay.
120
+ * Programmatically fund an Arca object. This is a developer/test tool for
121
+ * non-production use (testing, competitions, programmatic account seeding).
122
+ * For production deposit flows, use `createPaymentLink({ type: 'deposit' })`.
123
+ *
124
+ * `await` waits for settlement. Use `.submitted` to access
125
+ * the poolAddress and other response data before settlement.
122
126
  */
123
- deposit(opts: DepositOptions): Promise<InitiateDepositResponse>;
127
+ fundAccount(opts: FundAccountOptions): OperationHandle<FundAccountResponse>;
124
128
  /**
125
- * Initiate a withdrawal from a denominated Arca object to an on-chain address.
126
- * Requires custody service (on-chain mode) to be active.
129
+ * Programmatically withdraw from an Arca object. This is a developer/test tool
130
+ * for non-production use. For production withdrawal flows, use
131
+ * `createPaymentLink({ type: 'withdrawal' })`.
127
132
  */
128
- withdrawal(opts: WithdrawalOptions): Promise<InitiateWithdrawalResponse>;
129
- createPaymentLink(opts: CreatePaymentLinkOptions): Promise<CreatePaymentLinkResponse>;
133
+ defundAccount(opts: DefundAccountOptions): OperationHandle<DefundAccountResponse>;
134
+ createPaymentLink(opts: CreatePaymentLinkOptions): OperationHandle<CreatePaymentLinkResponse>;
130
135
  listPaymentLinks(opts?: ListPaymentLinksOptions): Promise<PaymentLinkListResponse>;
131
136
  /**
132
137
  * Get operation detail by ID (includes correlated events and deltas).
@@ -208,7 +213,7 @@ export declare class Arca {
208
213
  * Create a Perps Exchange Arca object.
209
214
  * Automatically sets type=exchange, denomination=USD, and exchangeType metadata.
210
215
  */
211
- createPerpsExchange(opts: CreatePerpsExchangeOptions): Promise<CreateArcaObjectResponse>;
216
+ createPerpsExchange(opts: CreatePerpsExchangeOptions): OperationHandle<CreateArcaObjectResponse>;
212
217
  /**
213
218
  * Get exchange account state (equity, margin, positions, orders).
214
219
  */
@@ -238,8 +243,11 @@ export declare class Arca {
238
243
  /**
239
244
  * Place an order on an exchange Arca object.
240
245
  * The operation path serves as the idempotency key.
246
+ *
247
+ * Returns an OrderHandle: `await` waits for placement. Use `.filled()`,
248
+ * `.onFill()`, `.fills()` for fill lifecycle, and `.cancel()` to cancel.
241
249
  */
242
- placeOrder(opts: PlaceOrderOptions): Promise<OrderOperationResponse>;
250
+ placeOrder(opts: PlaceOrderOptions): OrderHandle;
243
251
  /**
244
252
  * List orders for an exchange Arca object.
245
253
  */
@@ -252,7 +260,7 @@ export declare class Arca {
252
260
  * Cancel an order on an exchange Arca object.
253
261
  * The operation path serves as the idempotency key.
254
262
  */
255
- cancelOrder(opts: CancelOrderOptions): Promise<OrderOperationResponse>;
263
+ cancelOrder(opts: CancelOrderOptions): OperationHandle<OrderOperationResponse>;
256
264
  /**
257
265
  * List positions for an exchange Arca object.
258
266
  */
@@ -282,11 +290,11 @@ export declare class Arca {
282
290
  */
283
291
  checkInvariants(limit?: number): Promise<InvariantCheckResponse>;
284
292
  /**
285
- * Poll until all operations in the realm have reached a terminal state.
286
- * Useful after a batch of async operations (deposits, exchange transfers)
287
- * to wait for settlement before running invariant checks.
293
+ * Wait until all operations in the realm have reached a terminal state.
294
+ * Uses WebSocket events to react to completions instantly, with periodic
295
+ * HTTP polls as a safety net.
288
296
  *
289
- * @param opts.intervalMs - Polling interval in ms (default: 1000)
297
+ * @param opts.intervalMs - Safety-net poll interval in ms (default: 5000)
290
298
  * @param opts.timeoutMs - Max wait time in ms (default: 120000)
291
299
  * @param opts.onPoll - Optional callback with the current pending count
292
300
  * @returns The number of polls performed
@@ -298,11 +306,17 @@ export declare class Arca {
298
306
  }): Promise<number>;
299
307
  /**
300
308
  * Wait for a specific operation to reach a terminal state.
301
- * Uses WebSocket events to resolve immediately when the operation updates.
309
+ * Auto-connects the WebSocket and subscribes to the operations channel
310
+ * so callers don't need to manage the connection manually.
302
311
  * If the WS event arrives without embedded operation data (enrichment failure),
303
312
  * fetches the operation via HTTP as a fallback.
304
313
  */
305
314
  waitForOperation(operationId: string, timeoutMs?: number): Promise<Operation>;
315
+ /**
316
+ * Wrap a mutation HTTP call in an OperationHandle.
317
+ * Defers ready() + throwIfOperationFailed into the promise chain.
318
+ */
319
+ private op;
306
320
  private realmId;
307
321
  private throwIfOperationFailed;
308
322
  private resolveRealm;
@@ -1 +1 @@
1
- {"version":3,"file":"arca.d.ts","sourceRoot":"","sources":["../src/arca.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,UAAU,EAGV,UAAU,EACV,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,0BAA0B,EAC1B,WAAW,EAEX,oBAAoB,EACpB,oBAAoB,EACpB,wBAAwB,EACxB,iBAAiB,EACjB,4BA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MAAM,CAAC;IA0BnB;;;;;OAKG;IACG,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,SAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;IA2DlF,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,sBAAsB;YAMhB,YAAY;CA+B3B"}
1
+ {"version":3,"file":"arca.d.ts","sourceRoot":"","sources":["../src/arca.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAElE,OAAO,EACL,UAAU,EAGV,UAAU,EACV,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,0BAA0B,EAC1B,WAAW,EAEX,oBAAoB,EACpB,oBAAoB,EACpB,wBAAwB,EACxB,iBAAiB,EACjB,4BAA4B,EAC5B,mBAAmB,EACnB,wBAAwB,EACxB,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EAExB,eAAe,EACf,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EACjB,aAAa,EACb,SAAS,EACT,uBAAuB,EACvB,qBAAqB,EACrB,sBAAsB,EACtB,eAAe,EACf,WAAW,EACX,qBAAqB,EAIrB,+BAA+B,EAC/B,wBAAwB,EACxB,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,0BAA0B,EAC1B,iBAAiB,EACjB,qBAAqB,EACrB,sBAAsB,EACtB,eAAe,EACf,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,eAAe,EACf,eAAe,EACf,sBAAsB,EACtB,eAAe,EACf,cAAc,EACd,eAAe,EACf,wBAAwB,EACxB,yBAAyB,EACzB,uBAAuB,EACvB,uBAAuB,EACxB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,gBAAgB,EAAE,MAAM,a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d,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,cAAc,EACxB,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GACjD,OAAO,CAAC,eAAe,CAAC;IAU3B;;;OAGG;IACG,eAAe,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAOtE;;;;;;;;;OASG;IACG,iBAAiB,CAAC,IAAI,CAAC,EAAE;QAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;KACzC,GAAG,OAAO,CAAC,MAAM,CAAC;IAmEnB;;;;;;OAMG;IACG,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,SAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;IA+DlF;;;OAGG;IACH,OAAO,CAAC,EAAE;IAUV,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,sBAAsB;YAMhB,YAAY;CA+B3B"}
package/dist/arca.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Arca = void 0;
4
4
  const client_1 = require("./client");
5
5
  const errors_1 = require("./errors");
6
+ const operation_handle_1 = require("./operation-handle");
6
7
  const types_1 = require("./types");
7
8
  const websocket_1 = require("./websocket");
8
9
  const DEFAULT_BASE_URL = 'https://api.arcaos.io';
@@ -146,44 +147,41 @@ class Arca {
146
147
  * If one already exists with matching type/denomination, it is returned.
147
148
  * Otherwise a new one is created.
148
149
  *
149
- * Pass `operationPath` (from the nonce API with separator ":") for
150
- * explicit operation-level idempotency, especially when delete+recreate
151
- * scenarios are expected.
150
+ * `await` waits for the object to be fully created (operation completed).
151
+ * Use `.submitted` to access the HTTP response before the operation settles.
152
152
  */
153
- async createDenominatedArca(opts) {
154
- await this.ready();
155
- return this.client.post('/objects', {
153
+ createDenominatedArca(opts) {
154
+ return this.op(() => this.client.post('/objects', {
156
155
  realmId: this.realmId(),
157
156
  path: opts.ref,
158
157
  type: 'denominated',
159
158
  denomination: opts.denomination,
160
159
  metadata: opts.metadata ?? null,
161
160
  operationPath: opts.operationPath ?? null,
162
- });
161
+ }));
163
162
  }
164
163
  /**
165
164
  * Create an Arca object of any type at the given path (idempotent).
166
165
  *
167
- * Pass `operationPath` (from the nonce API with separator ":") for
168
- * explicit operation-level idempotency.
166
+ * `await` waits for the object to be fully created (operation completed).
167
+ * Use `.submitted` to access the HTTP response before the operation settles.
169
168
  */
170
- async createArca(opts) {
171
- await this.ready();
172
- return this.client.post('/objects', {
169
+ createArca(opts) {
170
+ return this.op(() => this.client.post('/objects', {
173
171
  realmId: this.realmId(),
174
172
  path: opts.ref,
175
173
  type: opts.type,
176
174
  denomination: opts.denomination ?? null,
177
175
  metadata: opts.metadata ?? null,
178
176
  operationPath: opts.operationPath ?? null,
179
- });
177
+ }));
180
178
  }
181
179
  /** @deprecated Use createDenominatedArca instead */
182
- async ensureDenominatedArca(opts) {
180
+ ensureDenominatedArca(opts) {
183
181
  return this.createDenominatedArca(opts);
184
182
  }
185
183
  /** @deprecated Use createArca instead */
186
- async ensureArca(opts) {
184
+ ensureArca(opts) {
187
185
  return this.createArca(opts);
188
186
  }
189
187
  /**
@@ -192,15 +190,14 @@ class Arca {
192
190
  *
193
191
  * If the object is already deleted, returns the existing state.
194
192
  */
195
- async ensureDeleted(opts) {
196
- await this.ready();
197
- return this.client.post('/objects/delete', {
193
+ ensureDeleted(opts) {
194
+ return this.op(() => this.client.post('/objects/delete', {
198
195
  realmId: this.realmId(),
199
196
  path: opts.ref,
200
197
  sweepToPath: opts.sweepTo ?? null,
201
198
  liquidatePositions: opts.liquidatePositions ?? false,
202
199
  operationPath: opts.operationPath ?? null,
203
- });
200
+ }));
204
201
  }
205
202
  /**
206
203
  * Get an Arca object by path.
@@ -281,60 +278,56 @@ class Arca {
281
278
  // ---- Transfers ----
282
279
  /**
283
280
  * Execute a transfer between two Arca objects.
284
- * Settlement is immediate for denominated targets, or async for
285
- * targets that require a receiver workflow (e.g. exchange objects).
281
+ * Settlement is immediate for denominated targets (await resolves instantly),
282
+ * or async for exchange targets (await waits for settlement).
286
283
  * The operation path serves as the idempotency key.
287
284
  */
288
- async transfer(opts) {
289
- await this.ready();
290
- const response = await this.client.post('/transfer', {
285
+ transfer(opts) {
286
+ return this.op(() => this.client.post('/transfer', {
291
287
  realmId: this.realmId(),
292
288
  path: opts.path,
293
289
  sourceArcaPath: opts.from,
294
290
  targetArcaPath: opts.to,
295
291
  amount: opts.amount,
296
- });
297
- this.throwIfOperationFailed(response.operation);
298
- return response;
292
+ }));
299
293
  }
300
- // ---- Deposits ----
294
+ // ---- Fund / Defund Account ----
301
295
  /**
302
- * Initiate a deposit to a denominated Arca object.
303
- * In demo realms, deposits are simulated with a configurable delay.
296
+ * Programmatically fund an Arca object. This is a developer/test tool for
297
+ * non-production use (testing, competitions, programmatic account seeding).
298
+ * For production deposit flows, use `createPaymentLink({ type: 'deposit' })`.
299
+ *
300
+ * `await` waits for settlement. Use `.submitted` to access
301
+ * the poolAddress and other response data before settlement.
304
302
  */
305
- async deposit(opts) {
306
- await this.ready();
307
- const response = await this.client.post('/deposit', {
303
+ fundAccount(opts) {
304
+ return this.op(() => this.client.post('/fund-account', {
308
305
  realmId: this.realmId(),
309
306
  arcaPath: opts.arcaRef,
310
307
  amount: opts.amount,
311
308
  path: opts.path ?? null,
312
309
  senderAddress: opts.senderAddress ?? null,
313
- });
314
- this.throwIfOperationFailed(response.operation);
315
- return response;
310
+ durationSeconds: opts.durationSeconds ?? null,
311
+ willSucceed: opts.willSucceed ?? null,
312
+ }));
316
313
  }
317
- // ---- Withdrawals ----
318
314
  /**
319
- * Initiate a withdrawal from a denominated Arca object to an on-chain address.
320
- * Requires custody service (on-chain mode) to be active.
315
+ * Programmatically withdraw from an Arca object. This is a developer/test tool
316
+ * for non-production use. For production withdrawal flows, use
317
+ * `createPaymentLink({ type: 'withdrawal' })`.
321
318
  */
322
- async withdrawal(opts) {
323
- await this.ready();
324
- const response = await this.client.post('/withdrawal', {
319
+ defundAccount(opts) {
320
+ return this.op(() => this.client.post('/defund-account', {
325
321
  realmId: this.realmId(),
326
322
  arcaPath: opts.arcaPath,
327
323
  amount: opts.amount,
328
324
  destinationAddress: opts.destinationAddress ?? '',
329
325
  path: opts.path ?? null,
330
- });
331
- this.throwIfOperationFailed(response.operation);
332
- return response;
326
+ }));
333
327
  }
334
328
  // ---- Payment Links ----
335
- async createPaymentLink(opts) {
336
- await this.ready();
337
- return this.client.post('/payment-links', {
329
+ createPaymentLink(opts) {
330
+ return this.op(() => this.client.post('/payment-links', {
338
331
  realmId: this.realmId(),
339
332
  type: opts.type,
340
333
  arcaPath: opts.arcaRef,
@@ -342,7 +335,7 @@ class Arca {
342
335
  returnUrl: opts.returnUrl ?? null,
343
336
  expiresInMinutes: opts.expiresInMinutes ?? null,
344
337
  metadata: opts.metadata ? JSON.stringify(opts.metadata) : null,
345
- });
338
+ }));
346
339
  }
347
340
  async listPaymentLinks(opts) {
348
341
  await this.ready();
@@ -526,16 +519,15 @@ class Arca {
526
519
  * Create a Perps Exchange Arca object.
527
520
  * Automatically sets type=exchange, denomination=USD, and exchangeType metadata.
528
521
  */
529
- async createPerpsExchange(opts) {
530
- await this.ready();
531
- return this.client.post('/objects', {
522
+ createPerpsExchange(opts) {
523
+ return this.op(() => this.client.post('/objects', {
532
524
  realmId: this.realmId(),
533
525
  path: opts.ref,
534
526
  type: 'exchange',
535
527
  denomination: 'USD',
536
528
  metadata: JSON.stringify({ exchangeType: opts.exchangeType || 'hyperliquid' }),
537
529
  operationPath: opts.operationPath,
538
- });
530
+ }));
539
531
  }
540
532
  /**
541
533
  * Get exchange account state (equity, margin, positions, orders).
@@ -590,10 +582,12 @@ class Arca {
590
582
  /**
591
583
  * Place an order on an exchange Arca object.
592
584
  * The operation path serves as the idempotency key.
585
+ *
586
+ * Returns an OrderHandle: `await` waits for placement. Use `.filled()`,
587
+ * `.onFill()`, `.fills()` for fill lifecycle, and `.cancel()` to cancel.
593
588
  */
594
- async placeOrder(opts) {
595
- await this.ready();
596
- const response = await this.client.post(`/objects/${opts.objectId}/exchange/orders`, {
589
+ placeOrder(opts) {
590
+ const httpCall = this.ready().then(() => this.client.post(`/objects/${opts.objectId}/exchange/orders`, {
597
591
  realmId: this.realmId(),
598
592
  path: opts.path,
599
593
  coin: opts.coin,
@@ -607,9 +601,21 @@ class Arca {
607
601
  timeInForce: opts.timeInForce ?? 'GTC',
608
602
  ...(opts.builderFeeBps != null && { builderFeeBps: opts.builderFeeBps }),
609
603
  ...(opts.feeTargets != null && { feeTargets: opts.feeTargets }),
604
+ })).then(response => {
605
+ this.throwIfOperationFailed(response.operation);
606
+ return response;
610
607
  });
611
- this.throwIfOperationFailed(response.operation);
612
- return response;
608
+ const deps = {
609
+ getOrder: (objectId, orderId) => this.getOrder(objectId, orderId),
610
+ onFillEvent: (handler) => {
611
+ this.ws.ensureConnected();
612
+ this.ws.subscribe(['exchange']);
613
+ this.ws.on(types_1.EventType.ExchangeFill, handler);
614
+ return () => this.ws.off(types_1.EventType.ExchangeFill, handler);
615
+ },
616
+ cancelOrder: (cancelOpts) => this.cancelOrder(cancelOpts),
617
+ };
618
+ return new operation_handle_1.OrderHandle(httpCall, (id, timeout) => this.waitForOperation(id, timeout), opts.objectId, opts.path, deps);
613
619
  }
614
620
  /**
615
621
  * List orders for an exchange Arca object.
@@ -632,15 +638,14 @@ class Arca {
632
638
  * Cancel an order on an exchange Arca object.
633
639
  * The operation path serves as the idempotency key.
634
640
  */
635
- async cancelOrder(opts) {
636
- await this.ready();
637
- const params = new URLSearchParams({
638
- realmId: this.realmId(),
639
- path: opts.path,
641
+ cancelOrder(opts) {
642
+ return this.op(() => {
643
+ const params = new URLSearchParams({
644
+ realmId: this.realmId(),
645
+ path: opts.path,
646
+ });
647
+ return this.client.delete(`/objects/${opts.objectId}/exchange/orders/${opts.orderId}?${params.toString()}`);
640
648
  });
641
- const response = await this.client.delete(`/objects/${opts.objectId}/exchange/orders/${opts.orderId}?${params.toString()}`);
642
- this.throwIfOperationFailed(response.operation);
643
- return response;
644
649
  }
645
650
  /**
646
651
  * List positions for an exchange Arca object.
@@ -695,43 +700,89 @@ class Arca {
695
700
  return this.client.get('/internal/invariant-check', params);
696
701
  }
697
702
  /**
698
- * Poll until all operations in the realm have reached a terminal state.
699
- * Useful after a batch of async operations (deposits, exchange transfers)
700
- * to wait for settlement before running invariant checks.
703
+ * Wait until all operations in the realm have reached a terminal state.
704
+ * Uses WebSocket events to react to completions instantly, with periodic
705
+ * HTTP polls as a safety net.
701
706
  *
702
- * @param opts.intervalMs - Polling interval in ms (default: 1000)
707
+ * @param opts.intervalMs - Safety-net poll interval in ms (default: 5000)
703
708
  * @param opts.timeoutMs - Max wait time in ms (default: 120000)
704
709
  * @param opts.onPoll - Optional callback with the current pending count
705
710
  * @returns The number of polls performed
706
711
  */
707
712
  async waitForQuiescence(opts) {
708
713
  await this.ready();
709
- const interval = opts?.intervalMs ?? 1000;
714
+ const interval = opts?.intervalMs ?? 5000;
710
715
  const timeout = opts?.timeoutMs ?? 120_000;
711
716
  const start = Date.now();
712
717
  let polls = 0;
713
- while (true) {
718
+ this.ws.ensureConnected();
719
+ this.ws.subscribe(['operations']);
720
+ const checkPending = async () => {
714
721
  const res = await this.listOperations();
715
- const pending = res.operations.filter(op => op.state === 'pending');
716
722
  polls++;
723
+ const pending = res.operations.filter(op => op.state === 'pending');
717
724
  opts?.onPoll?.(pending.length);
718
- if (pending.length === 0)
719
- return polls;
725
+ return pending.length;
726
+ };
727
+ let pendingCount = await checkPending();
728
+ if (pendingCount === 0)
729
+ return polls;
730
+ return new Promise((resolve, reject) => {
731
+ let settled = false;
732
+ const timer = setTimeout(() => {
733
+ cleanup();
734
+ reject(new Error(`Timed out waiting for quiescence after ${timeout}ms. ` +
735
+ `${pendingCount} operations still pending.`));
736
+ }, timeout);
737
+ const safetyPoll = setInterval(async () => {
738
+ if (settled)
739
+ return;
740
+ try {
741
+ pendingCount = await checkPending();
742
+ if (pendingCount === 0) {
743
+ cleanup();
744
+ resolve(polls);
745
+ }
746
+ }
747
+ catch { /* ignore, will retry */ }
748
+ }, interval);
749
+ const cleanup = () => {
750
+ settled = true;
751
+ clearTimeout(timer);
752
+ clearInterval(safetyPoll);
753
+ this.ws.off(types_1.EventType.OperationUpdated, handler);
754
+ };
755
+ const handler = async () => {
756
+ if (settled)
757
+ return;
758
+ try {
759
+ pendingCount = await checkPending();
760
+ if (pendingCount === 0) {
761
+ cleanup();
762
+ resolve(polls);
763
+ }
764
+ }
765
+ catch { /* ignore */ }
766
+ };
767
+ this.ws.on(types_1.EventType.OperationUpdated, handler);
720
768
  if (Date.now() - start > timeout) {
721
- throw new Error(`Timed out waiting for quiescence after ${timeout}ms. ` +
722
- `${pending.length} operations still pending.`);
769
+ cleanup();
770
+ reject(new Error(`Timed out waiting for quiescence after ${timeout}ms. ` +
771
+ `${pendingCount} operations still pending.`));
723
772
  }
724
- await new Promise(r => setTimeout(r, interval));
725
- }
773
+ });
726
774
  }
727
775
  /**
728
776
  * Wait for a specific operation to reach a terminal state.
729
- * Uses WebSocket events to resolve immediately when the operation updates.
777
+ * Auto-connects the WebSocket and subscribes to the operations channel
778
+ * so callers don't need to manage the connection manually.
730
779
  * If the WS event arrives without embedded operation data (enrichment failure),
731
780
  * fetches the operation via HTTP as a fallback.
732
781
  */
733
782
  async waitForOperation(operationId, timeoutMs = 30000) {
734
783
  await this.ready();
784
+ this.ws.ensureConnected();
785
+ this.ws.subscribe(['operations']);
735
786
  return new Promise((resolve, reject) => {
736
787
  let settled = false;
737
788
  const timeout = setTimeout(() => {
@@ -785,6 +836,17 @@ class Arca {
785
836
  });
786
837
  }
787
838
  // ---- Internal ----
839
+ /**
840
+ * Wrap a mutation HTTP call in an OperationHandle.
841
+ * Defers ready() + throwIfOperationFailed into the promise chain.
842
+ */
843
+ op(httpCall) {
844
+ const call = this.ready().then(() => httpCall()).then(response => {
845
+ this.throwIfOperationFailed(response.operation);
846
+ return response;
847
+ });
848
+ return new operation_handle_1.OperationHandle(call, (id, timeout) => this.waitForOperation(id, timeout));
849
+ }
788
850
  realmId() {
789
851
  if (!this.resolvedRealmId) {
790
852
  throw new Error('Arca SDK not initialized. Call await arca.ready() first.');