@0xmonaco/react 1.0.82 → 1.0.85

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.
@@ -28,6 +28,8 @@ export declare const COVERED: {
28
28
  close_position: string;
29
29
  batch_close_all_positions: string;
30
30
  create_challenge: string;
31
+ request_invite_codes: string;
32
+ list_invite_codes: string;
31
33
  create_order: string;
32
34
  get_candles: string;
33
35
  get_funding_state: string;
package/dist/coverage.js CHANGED
@@ -28,6 +28,8 @@ export const COVERED = {
28
28
  close_position: "usePositions",
29
29
  batch_close_all_positions: "usePositions",
30
30
  create_challenge: "useAuth",
31
+ request_invite_codes: "useAuth.requestInviteCodes",
32
+ list_invite_codes: "useAuth.listInviteCodes",
31
33
  create_order: "useTrade",
32
34
  get_candles: "useMarket",
33
35
  get_funding_state: "useMarket",
@@ -1,4 +1,4 @@
1
- import type { AuthState, ChallengeResponse, SessionRefreshResponse } from "@0xmonaco/types";
1
+ import type { AuthState, ChallengeResponse, InviteCodeList, InviteCodeRequestSummary, SessionRefreshResponse } from "@0xmonaco/types";
2
2
  import type { AuthenticationStatus } from "../../provider";
3
3
  export interface UseAuthState {
4
4
  /** Current authentication status */
@@ -13,7 +13,12 @@ export interface UseAuthReturn extends UseAuthState {
13
13
  */
14
14
  login: (options?: {
15
15
  connectWebSocket?: boolean;
16
+ inviteCode?: string;
16
17
  }) => Promise<AuthState>;
18
+ /** Mint one available Mainnet invite code immediately; each call consumes a slot. */
19
+ requestInviteCodes: () => Promise<InviteCodeRequestSummary>;
20
+ /** Read the invite allowance and minted codes without consuming a slot. */
21
+ listInviteCodes: () => Promise<InviteCodeList>;
17
22
  /** Logout user and clear state. Revokes the session on the server. */
18
23
  logout: () => Promise<void>;
19
24
  /** Refresh the current session, extending its expiry. */
@@ -4,14 +4,14 @@ import { useMonacoSDK } from "../useMonaco";
4
4
  export const useAuth = () => {
5
5
  const { sdk, clientId } = useMonacoSDK();
6
6
  const { authenticationStatus, setAuthenticationStatus, tokenLifecycle, isWalletConnected } = useMonacoContext();
7
- const login = useCallback(async () => {
7
+ const login = useCallback(async (options) => {
8
8
  if (!sdk)
9
9
  throw new Error("SDK not available");
10
10
  if (!isWalletConnected)
11
11
  throw new Error("Wallet not connected");
12
12
  setAuthenticationStatus(AuthenticationStatus.AUTHENTICATING);
13
13
  try {
14
- const authState = await sdk.login(clientId);
14
+ const authState = await sdk.login(clientId, options);
15
15
  // Initialize token lifecycle management (caching + auto-refresh)
16
16
  tokenLifecycle.initializeTokens(authState);
17
17
  const authenticated = sdk.isAuthenticated();
@@ -23,6 +23,16 @@ export const useAuth = () => {
23
23
  throw error;
24
24
  }
25
25
  }, [sdk, isWalletConnected, clientId, setAuthenticationStatus, tokenLifecycle]);
26
+ const requestInviteCodes = useCallback(async () => {
27
+ if (!sdk)
28
+ throw new Error("SDK not available");
29
+ return await sdk.auth.requestInviteCodes();
30
+ }, [sdk]);
31
+ const listInviteCodes = useCallback(async () => {
32
+ if (!sdk)
33
+ throw new Error("SDK not available");
34
+ return await sdk.auth.listInviteCodes();
35
+ }, [sdk]);
26
36
  const logout = useCallback(async () => {
27
37
  if (!sdk)
28
38
  throw new Error("SDK not available");
@@ -127,6 +137,8 @@ export const useAuth = () => {
127
137
  authenticationStatus,
128
138
  // Primary auth actions
129
139
  login,
140
+ requestInviteCodes,
141
+ listInviteCodes,
130
142
  logout,
131
143
  // Session management
132
144
  refreshAuth,
@@ -0,0 +1,6 @@
1
+ import type { Candlestick } from "@0xmonaco/types";
2
+ /**
3
+ * Live candle events may omit persisted OI. Keep the REST-sampled pair only
4
+ * while updating that same candle; a new candle must not inherit the old pair.
5
+ */
6
+ export declare function mergeLiveCandleOpenInterest(previous: Candlestick | undefined, live: Candlestick): Candlestick;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Live candle events may omit persisted OI. Keep the REST-sampled pair only
3
+ * while updating that same candle; a new candle must not inherit the old pair.
4
+ */
5
+ export function mergeLiveCandleOpenInterest(previous, live) {
6
+ if (previous?.T === live.T && live.openInterestBase == null && live.openInterestNotional == null) {
7
+ return {
8
+ ...live,
9
+ openInterestBase: previous.openInterestBase,
10
+ openInterestNotional: previous.openInterestNotional,
11
+ };
12
+ }
13
+ return live;
14
+ }
@@ -1,5 +1,6 @@
1
1
  import { useCallback, useEffect, useState } from "react";
2
2
  import { useMonacoSDK } from "../useMonaco";
3
+ import { mergeLiveCandleOpenInterest } from "./mergeLiveCandleOpenInterest";
3
4
  /**
4
5
  * Hook for subscribing to real-time OHLCV candlestick events via WebSocket (public)
5
6
  *
@@ -37,16 +38,18 @@ export function useOHLCV(params) {
37
38
  try {
38
39
  unsubscribe = sdk.ws.ohlcv(tradingPairId, tradingMode, interval, (event) => {
39
40
  setCandlesticks((prev) => {
41
+ const lastCandle = prev[prev.length - 1];
42
+ const liveCandle = event.candlestick;
43
+ const candle = mergeLiveCandleOpenInterest(lastCandle, liveCandle);
40
44
  if (prev.length === 0) {
41
- return [event.candlestick];
45
+ return [candle];
42
46
  }
43
- const lastCandle = prev[prev.length - 1];
44
47
  if (lastCandle && lastCandle.T === event.candlestick.T) {
45
48
  const updated = [...prev];
46
- updated[updated.length - 1] = event.candlestick;
49
+ updated[updated.length - 1] = candle;
47
50
  return updated;
48
51
  }
49
- return [...prev, event.candlestick];
52
+ return [...prev, candle];
50
53
  });
51
54
  });
52
55
  setSubscribed(true);
@@ -96,9 +96,15 @@ export function useUserOrders(maxOrders = 50) {
96
96
  pageToken: "",
97
97
  });
98
98
  // Deduplicate the paginated orders by id, keeping the first occurrence.
99
+ // A slice is dropped too. Only a server from before 0XM-3070 lists slices,
100
+ // and against one this page can come back short of `maxOrders`; the hook
101
+ // does not page further to make up the difference, since the next server
102
+ // release stops listing them.
99
103
  const seenIds = new Set();
100
104
  const dedupedOrders = [];
101
105
  for (const order of response.orders) {
106
+ if (isTwapSlice(order.parentOrderId))
107
+ continue;
102
108
  if (!seenIds.has(order.id)) {
103
109
  seenIds.add(order.id);
104
110
  dedupedOrders.push(order);
@@ -304,6 +310,9 @@ export function useUserOrders(maxOrders = 50) {
304
310
  // This prevents race conditions where WS events could be overwritten by REST response
305
311
  try {
306
312
  unsubscribe = sdk.ws.userOrders((event) => {
313
+ // A slice's frames carry its parent; the list never holds one.
314
+ if ("parentOrderId" in event.data && isTwapSlice(event.data.parentOrderId))
315
+ return;
307
316
  // Built OUTSIDE the state updater: an updater must stay pure, and
308
317
  // React may invoke it more than once. `null` here means the event
309
318
  // is an `OrderPlaced` this version cannot describe.
@@ -334,7 +343,10 @@ export function useUserOrders(maxOrders = 50) {
334
343
  // REST round trip. It matters most after a reconnect: the
335
344
  // resubscribe brings a fresh one, while a targeted read resolves
336
345
  // each formerly-resting order it omits.
337
- (items) => {
346
+ (snapshotItems) => {
347
+ // The snapshot carries resting slices with their parent; the list
348
+ // never holds one, so it is neither added nor looked for.
349
+ const items = snapshotItems.filter((item) => !isTwapSlice(item.parentOrderId));
338
350
  // Read BEFORE the updater: whether a row was declined depends only
339
351
  // on the rows themselves, and an updater must stay free of side
340
352
  // effects because React may invoke it more than once. A row this
@@ -443,6 +455,19 @@ function asKnown(known, value) {
443
455
  function asOrderStatus(value) {
444
456
  return asKnown(ORDER_STATUSES, value);
445
457
  }
458
+ /**
459
+ * Whether a row or frame belongs to a TWAP slice: a child order its TWAP
460
+ * parent placed, which names that parent in `parentOrderId`.
461
+ *
462
+ * The order listing leaves slices to their parent's slice list
463
+ * (`listTwapOrderSlices`, 0XM-3070), and this hook mirrors the listing, so it
464
+ * drops them from every source — the REST page, the subscribe-time snapshot and
465
+ * the live frames. Without the last two, a resting slice would appear live and
466
+ * then vanish on the next REST read.
467
+ */
468
+ function isTwapSlice(parentOrderId) {
469
+ return typeof parentOrderId === "string" && parentOrderId.length > 0;
470
+ }
446
471
  /**
447
472
  * `live`, with every REST-only field taken from the fetched row.
448
473
  *
@@ -573,6 +598,9 @@ export function orderFromSnapshotItem(item) {
573
598
  leverage: item.leverage ?? undefined,
574
599
  reduceOnly: item.reduceOnly,
575
600
  ...(item.clientOrderId === undefined ? {} : { clientOrderId: item.clientOrderId }),
601
+ // What placed the order, as REST reports it. Absent stays absent, so a
602
+ // snapshot that does not report one leaves the label the row already had.
603
+ ...(item.origin === undefined ? {} : { origin: item.origin }),
576
604
  // Carried, not dropped: it is what the merge ranks this row by, and a row
577
605
  // whose state came from the snapshot while its version stayed at the held
578
606
  // value would be internally inconsistent — reporting a step it does not
@@ -653,8 +681,13 @@ export function applyOrderSnapshot(current, items, limit) {
653
681
  // Unranked means neither side carries a version, which is the pre-0XM-2440
654
682
  // world: the snapshot is still the authority on resting state there, so it
655
683
  // keeps its original precedence.
656
- if (ranked === false)
657
- return [];
684
+ if (ranked === false) {
685
+ // What placed the order never changes, so it is merged outside the
686
+ // ranking, as `mergeOrders` does: the stored row's value stands, and a
687
+ // held row that has none gains it from a snapshot that lost on version.
688
+ const origin = row.origin ?? existing.origin;
689
+ return origin === existing.origin ? [] : [{ ...existing, origin }];
690
+ }
658
691
  return [{ ...existing, ...row }];
659
692
  });
660
693
  // Core validates the frame and every order id before delivery. A row this
@@ -785,10 +818,10 @@ export function mergeOrders(current, fetched, limit) {
785
818
  return incoming;
786
819
  const winner = reconcile(incoming, live);
787
820
  // What placed an order never changes, so it is not ranked: whichever row
788
- // knows it supplies it. A snapshot row carries none and learns it from
789
- // REST; a row that learned it from its acknowledgement keeps it against a
790
- // response from a server predating the field. If both report one and they
791
- // differ, the REST row is the stored order and stands.
821
+ // knows it supplies it. A row held without one (from a server predating
822
+ // the field) learns it from REST; a row that learned it from the socket
823
+ // keeps it against a REST response that does not report one. If both
824
+ // report one and they differ, the REST row is the stored order and stands.
792
825
  const origin = incoming.origin ?? live.origin ?? undefined;
793
826
  return origin === winner.origin ? winner : { ...winner, origin };
794
827
  });
@@ -964,7 +997,13 @@ export function updateOrderFromEvent(order, event) {
964
997
  // above is what keeps that value when this event carries none.
965
998
  ...(typeof data.version === "number" ? { version: data.version } : {}),
966
999
  };
967
- return liveEventSupersedes(order, updated, typeof data.version === "number" ? data.version : undefined) ? updated : order;
1000
+ const winner = liveEventSupersedes(order, updated, typeof data.version === "number" ? data.version : undefined) ? updated : order;
1001
+ // What placed the order never changes, so it is merged outside the ranking,
1002
+ // as `mergeOrders` and `applyOrderSnapshot` do: every order frame reports
1003
+ // it, and one labels a row held without it even when the frame loses on
1004
+ // version, but never replaces a value already held (which may be the stored
1005
+ // REST one).
1006
+ return order.origin === undefined && typeof data.origin === "string" ? { ...winner, origin: data.origin } : winner;
968
1007
  }
969
1008
  /**
970
1009
  * Whether a live event can advance the row currently held.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xmonaco/react",
3
- "version": "1.0.82",
3
+ "version": "1.0.85",
4
4
  "type": "module",
5
5
  "homepage": "https://docs.0xmonaco.com/sdk/typescript",
6
6
  "main": "./dist/index.js",
@@ -16,8 +16,8 @@
16
16
  "lint": "biome lint ."
17
17
  },
18
18
  "dependencies": {
19
- "@0xmonaco/core": "1.0.82",
20
- "@0xmonaco/types": "1.0.82"
19
+ "@0xmonaco/core": "1.0.85",
20
+ "@0xmonaco/types": "1.0.85"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/react": "^19.2.18",