@orderly.network/hooks 1.0.28 → 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.
package/dist/index.mjs CHANGED
@@ -6,23 +6,23 @@ import useSWRMutation from 'swr/mutation';
6
6
  import useConstant4 from 'use-constant';
7
7
  export { default as useConstant } from 'use-constant';
8
8
  import { SimpleDI, Account, EventEmitter, DefaultConfigStore, LocalStorageStore, EtherAdapter, utils } from '@orderly.network/core';
9
- import { AccountStatusEnum, OrderStatus, OrderSide, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, WS_WalletStatusEnum, OrderType } from '@orderly.network/types';
9
+ import { OrderType, AccountStatusEnum, SDKError, OrderStatus, OrderSide, chainsInfoMap, TestnetChains, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, MaxUint256, DEPOSIT_FEE_RATE, WS_WalletStatusEnum } from '@orderly.network/types';
10
10
  import useSWRInfinite, { unstable_serialize } from 'swr/infinite';
11
11
  import { useDebouncedCallback } from 'use-debounce';
12
12
  export * from 'use-debounce';
13
13
  import { jsx } from 'react/jsx-runtime';
14
- import { Decimal, zero, getPrecisionByNumber, timeConvertString } from '@orderly.network/utils';
14
+ import { Decimal, zero, getPrecisionByNumber, timeConvertString, isTestnet } from '@orderly.network/utils';
15
15
  import useSWRSubscription from 'swr/subscription';
16
- import { pathOr, propOr, compose, head, prop, mergeDeepRight, pick, min } from 'ramda';
16
+ import { pathOr, propOr, compose, head, prop, mergeDeepRight, pick, includes, omit, min } from 'ramda';
17
17
  import { positions, account, order } from '@orderly.network/perp';
18
18
  import { createClient } from '@layerzerolabs/scan-client';
19
19
 
20
20
  // src/version.ts
21
21
  if (typeof window !== "undefined") {
22
22
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
23
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.0.28";
23
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.1.0";
24
24
  }
25
- var version_default = "1.0.28";
25
+ var version_default = "1.1.0";
26
26
  var fetcher = (url, init = {}, queryOptions) => get(url, init, queryOptions?.formatter);
27
27
  var OrderlyContext = createContext({
28
28
  // configStore: new MemoryConfigStore(),
@@ -193,7 +193,6 @@ var signatureMiddleware = (useSWRNext) => {
193
193
  };
194
194
  return useSWRNext(key, extendedFetcher, config);
195
195
  } catch (e) {
196
- console.error("signature error:", e);
197
196
  throw e;
198
197
  }
199
198
  };
@@ -338,23 +337,16 @@ function useSessionStorage(key, initialValue) {
338
337
  const item = window.sessionStorage.getItem(key);
339
338
  return item ? parseJSON(item) : initialValue;
340
339
  } catch (error) {
341
- console.warn(`Error reading sessionStorage key \u201C${key}\u201D:`, error);
342
340
  return initialValue;
343
341
  }
344
342
  }, [initialValue, key]);
345
343
  const [storedValue, setStoredValue] = useState(readValue);
346
344
  const setValue = (value) => {
347
- if (typeof window == "undefined") {
348
- console.warn(
349
- `Tried setting sessionStorage key \u201C${key}\u201D even though environment is not a client`
350
- );
351
- }
352
345
  try {
353
346
  const newValue = value instanceof Function ? value(storedValue) : value;
354
347
  window.sessionStorage.setItem(key, JSON.stringify(newValue));
355
348
  setStoredValue(newValue);
356
349
  } catch (error) {
357
- console.warn(`Error setting sessionStorage key \u201C${key}\u201D:`, error);
358
350
  }
359
351
  };
360
352
  useEffect(() => {
@@ -389,24 +381,18 @@ function useLocalStorage(key, initialValue) {
389
381
  const item = window.localStorage.getItem(key);
390
382
  return item ? parseJSON(item) : initialValue;
391
383
  } catch (error) {
392
- console.warn(`Error reading localStorage key \u201C${key}\u201D:`, error);
393
384
  return initialValue;
394
385
  }
395
386
  }, [initialValue, key]);
396
387
  const [storedValue, setStoredValue] = useState(readValue);
397
388
  const setValue = useCallback(
398
389
  (value) => {
399
- if (typeof window === "undefined") {
400
- console.warn(
401
- `Tried setting localStorage key \u201C${key}\u201D even though environment is not a client`
402
- );
403
- }
404
390
  try {
405
391
  const newValue = value instanceof Function ? value(storedValue) : value;
406
392
  window.localStorage.setItem(key, JSON.stringify(newValue));
393
+ window.dispatchEvent(new Event("storage"));
407
394
  setStoredValue(() => newValue);
408
395
  } catch (error) {
409
- console.warn(`Error setting localStorage key \u201C${key}\u201D:`, error);
410
396
  }
411
397
  },
412
398
  [storedValue]
@@ -414,6 +400,18 @@ function useLocalStorage(key, initialValue) {
414
400
  useEffect(() => {
415
401
  setStoredValue(readValue());
416
402
  }, []);
403
+ useEffect(() => {
404
+ const handleStorageChange = (event) => {
405
+ if (event?.key && event.key !== key) {
406
+ return;
407
+ }
408
+ setStoredValue(readValue());
409
+ };
410
+ window.addEventListener("storage", handleStorageChange);
411
+ return () => {
412
+ window.removeEventListener("storage", handleStorageChange);
413
+ };
414
+ }, [key]);
417
415
  return [storedValue, setValue];
418
416
  }
419
417
  var WS_NAME = "nativeWebsocketClient";
@@ -451,36 +449,48 @@ var useWS = () => {
451
449
  });
452
450
  return ws;
453
451
  };
454
- var usePrivateDataObserver = () => {
452
+ var usePrivateDataObserver = (options) => {
455
453
  const ws = useWS();
456
454
  const { mutate: mutate2 } = useSWRConfig();
457
455
  const ee = useEventEmitter();
458
456
  const { state } = useAccount();
459
- const updateOrders = useDebouncedCallback(() => {
460
- mutate2(
461
- unstable_serialize(() => [
462
- `/v1/orders?size=100&page=1&status=${OrderStatus.INCOMPLETE}`,
463
- state.accountId
464
- ])
465
- );
466
- mutate2(
467
- unstable_serialize(() => [
468
- `/v1/orders?size=100&page=1&status=${OrderStatus.NEW}`,
469
- state.accountId
470
- ])
471
- );
457
+ const updateOrders = useDebouncedCallback((data) => {
458
+ const map = options.getKeysMap("orders");
459
+ map.forEach((getKey, key) => {
460
+ mutate2(
461
+ unstable_serialize((index, prevData) => [
462
+ getKey(index, prevData),
463
+ state.accountId
464
+ ])
465
+ );
466
+ });
472
467
  }, 500);
473
468
  useEffect(() => {
474
469
  if (!state.accountId)
475
470
  return;
476
471
  const unsubscribe = ws.privateSubscribe("executionreport", {
477
472
  onMessage: (data) => {
478
- updateOrders();
473
+ updateOrders(data);
479
474
  ee.emit("orders:changed", data);
480
475
  }
481
476
  });
482
477
  return () => unsubscribe?.();
483
478
  }, [state.accountId]);
479
+ useEffect(() => {
480
+ if (!state.accountId)
481
+ return;
482
+ const unsubscribe = ws.privateSubscribe("algoexecutionreport", {
483
+ onMessage: (data) => {
484
+ updateOrders(data);
485
+ if (Array.isArray(data)) {
486
+ data.forEach((item) => ee.emit("orders:changed", { ...item, status: item.algoStatus }));
487
+ } else {
488
+ ee.emit("orders:changed", { ...data, status: data.algoStatus });
489
+ }
490
+ }
491
+ });
492
+ return () => unsubscribe?.();
493
+ }, [state.accountId]);
484
494
  useEffect(() => {
485
495
  if (!state.accountId)
486
496
  return;
@@ -539,15 +549,31 @@ var usePrivateDataObserver = () => {
539
549
  var DataCenterContext = createContext(
540
550
  {}
541
551
  );
552
+ var useDataCenterContext = () => useContext(DataCenterContext);
542
553
  var DataCenterProvider = ({ children }) => {
543
554
  const { error, done } = usePreLoadData();
544
- usePrivateDataObserver();
555
+ const getKeyHandlerMapRef = useRef(/* @__PURE__ */ new Map());
556
+ usePrivateDataObserver({
557
+ getKeysMap(type) {
558
+ return getKeyHandlerMapRef.current;
559
+ }
560
+ });
545
561
  if (error) {
546
562
  return /* @__PURE__ */ jsx("div", { children: "Data load failed" });
547
563
  }
548
564
  if (!done)
549
565
  return null;
550
- return /* @__PURE__ */ jsx(DataCenterContext.Provider, { value: {}, children });
566
+ return /* @__PURE__ */ jsx(
567
+ DataCenterContext.Provider,
568
+ {
569
+ value: {
570
+ regesterKeyHandler: (key, fun) => {
571
+ getKeyHandlerMapRef.current.set(key, fun);
572
+ }
573
+ },
574
+ children
575
+ }
576
+ );
551
577
  };
552
578
  var OrderlyConfigProvider = (props) => {
553
579
  const [account5, setAccount] = React.useState(null);
@@ -560,9 +586,6 @@ var OrderlyConfigProvider = (props) => {
560
586
  enableSwapDeposit,
561
587
  contracts
562
588
  } = props;
563
- if (!brokerId && typeof configStore === "undefined") {
564
- console.error("[OrderlyConfigProvider]: brokerId is required");
565
- }
566
589
  const innerConfigStore = useConstant4(() => {
567
590
  return configStore || new DefaultConfigStore({ brokerId, networkId });
568
591
  });
@@ -786,7 +809,7 @@ var reduceItems = (depth, level, data, asks = false) => {
786
809
  const result = [];
787
810
  if (typeof depth !== "undefined") {
788
811
  const prices = /* @__PURE__ */ new Map();
789
- for (let i = 0; i < data.length; i++) {
812
+ for (let i = 0; i < min(level, data.length); i++) {
790
813
  const [price, quantity] = data[i];
791
814
  if (isNaN(price) || isNaN(quantity))
792
815
  continue;
@@ -821,7 +844,8 @@ var reduceItems = (depth, level, data, asks = false) => {
821
844
  if (isNaN(price) || isNaN(quantity))
822
845
  continue;
823
846
  const newQuantity = new Decimal(quantity).add(result.length > 0 ? result[result.length - 1][2] : 0).toNumber();
824
- result.push([price, quantity, newQuantity]);
847
+ const newAmount = new Decimal(quantity * price).add(result.length > 0 ? result[result.length - 1][3] : 0).toNumber();
848
+ result.push([price, quantity, newQuantity, newAmount]);
825
849
  }
826
850
  return result;
827
851
  };
@@ -852,9 +876,11 @@ var reduceOrderbook = (depth, level, data) => {
852
876
  }
853
877
  }
854
878
  asks = asks.reverse();
879
+ asks = asks.length < level ? paddingFn(level - asks.length).concat(asks) : asks;
880
+ bids = bids.length < level ? bids.concat(paddingFn(level - bids.length)) : bids;
855
881
  return {
856
- asks: asks.length < level ? paddingFn(level - asks.length).concat(asks) : asks,
857
- bids: bids.length < level ? bids.concat(paddingFn(level - bids.length)) : bids
882
+ asks,
883
+ bids
858
884
  };
859
885
  };
860
886
  var mergeItems = (data, update) => {
@@ -894,7 +920,7 @@ var mergeOrderbook = (data, update) => {
894
920
  var INIT_DATA = { asks: [], bids: [] };
895
921
  var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
896
922
  if (!symbol) {
897
- throw new Error("useOrderbookStream requires a symbol");
923
+ throw new SDKError("useOrderbookStream requires a symbol");
898
924
  }
899
925
  const level = options?.level ?? 10;
900
926
  const [requestData, setRequestData] = useState(null);
@@ -1002,6 +1028,12 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
1002
1028
  asks: [...data.asks],
1003
1029
  bids: [...data.bids]
1004
1030
  });
1031
+ useEffect(() => {
1032
+ eventEmitter.emit("orderbook:update", [
1033
+ reducedData.asks[0][0],
1034
+ reducedData.bids[0][0]
1035
+ ]);
1036
+ }, [reducedData.asks[0][0], reducedData.bids[0][0]]);
1005
1037
  return [
1006
1038
  {
1007
1039
  asks: reducedData.asks.slice(-level),
@@ -1012,13 +1044,28 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
1012
1044
  { onDepthChange, depth, allDepths: depths, isLoading, onItemClick }
1013
1045
  ];
1014
1046
  };
1015
- var needNumberOnlyFields = ["order_quantity", "order_price", "total"];
1047
+ var needNumberOnlyFields = [
1048
+ "order_quantity",
1049
+ "order_price",
1050
+ "total"
1051
+ ];
1052
+ var cleanStringStyle = (str) => {
1053
+ if (typeof str !== "string") {
1054
+ str = str.toString();
1055
+ }
1056
+ str = str.replace(/,/g, "");
1057
+ str = str.replace(/[^\d.]/g, "").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
1058
+ return str;
1059
+ };
1016
1060
  function baseInputHandle(inputs) {
1017
1061
  let [values, input, value, markPrice, config] = inputs;
1062
+ needNumberOnlyFields.forEach((field) => {
1063
+ if (typeof values[field] !== "undefined") {
1064
+ values[field] = cleanStringStyle(values[field]);
1065
+ }
1066
+ });
1018
1067
  if (needNumberOnlyFields.includes(input)) {
1019
- value = value.toString();
1020
- value = value.replace(/,/g, "");
1021
- value = value.replace(/[^\d.]/g, "");
1068
+ value = cleanStringStyle(value);
1022
1069
  }
1023
1070
  return [
1024
1071
  {
@@ -1048,15 +1095,23 @@ function priceInputHandle(inputs) {
1048
1095
  values.order_price = price.toDecimalPlaces(config.quoteDP).toString();
1049
1096
  }
1050
1097
  price.toDecimalPlaces(Math.min(priceDP, config.quoteDP));
1051
- if (!values.order_quantity) {
1098
+ if (!values.order_quantity && !values.total) {
1052
1099
  return [values, input, value, markPrice, config];
1053
1100
  }
1054
- const total = price.mul(values.order_quantity);
1101
+ const newValue = {
1102
+ ...values
1103
+ };
1104
+ if (values.order_quantity) {
1105
+ newValue.total = price.mul(values.order_quantity).todp(2).toString();
1106
+ } else if (values.total) {
1107
+ newValue.order_quantity = new Decimal(values.total).div(price).todp(config.baseDP).toString();
1108
+ }
1055
1109
  return [
1056
- {
1057
- ...values,
1058
- total: total.todp(2).toString()
1059
- },
1110
+ // {
1111
+ // ...values,
1112
+ // total: total.todp(2).toString(),
1113
+ // },
1114
+ newValue,
1060
1115
  input,
1061
1116
  value,
1062
1117
  markPrice,
@@ -1074,11 +1129,11 @@ function quantityInputHandle(inputs) {
1074
1129
  quantity = quantity.toDecimalPlaces(config.baseDP);
1075
1130
  values.order_quantity = quantity.toNumber();
1076
1131
  }
1077
- if (values.order_type === OrderType.MARKET) {
1132
+ if (values.order_type === OrderType.MARKET || values.order_type === OrderType.STOP_MARKET) {
1078
1133
  const price = markPrice;
1079
1134
  values.total = quantity.mul(price).todp(2).toNumber();
1080
1135
  }
1081
- if (values.order_type === OrderType.LIMIT) {
1136
+ if (values.order_type === OrderType.LIMIT || values.order_type === OrderType.STOP_LIMIT) {
1082
1137
  if (values.order_price) {
1083
1138
  const price = Number(values.order_price);
1084
1139
  const total = quantity.mul(price);
@@ -1104,7 +1159,7 @@ function totalInputHandle(inputs) {
1104
1159
  return [{ ...values, order_quantity: "" }, input, value, markPrice, config];
1105
1160
  }
1106
1161
  let price = markPrice;
1107
- if (values.order_type === OrderType.LIMIT && !!values.order_price) {
1162
+ if ((values.order_type === OrderType.LIMIT || values.order_type === OrderType.STOP_LIMIT) && !!values.order_price) {
1108
1163
  price = Number(values.order_price);
1109
1164
  }
1110
1165
  let total = new Decimal(value);
@@ -1212,6 +1267,60 @@ var parseHolding = (holding, markPrices) => {
1212
1267
  });
1213
1268
  return [USDC_holding, nonUSDC];
1214
1269
  };
1270
+ var useMarketsStream = () => {
1271
+ const ws = useWS();
1272
+ const { data: futures } = useQuery(`/v1/public/futures`, {
1273
+ revalidateOnFocus: false
1274
+ });
1275
+ const { data: tickers } = useSWRSubscription("tickers", (_, { next }) => {
1276
+ const unsubscribe = ws.subscribe(
1277
+ // { event: "subscribe", topic: "markprices" },
1278
+ "tickers",
1279
+ {
1280
+ onMessage: (message) => {
1281
+ next(null, message);
1282
+ }
1283
+ // onUnsubscribe: () => {
1284
+ // return "markprices";
1285
+ // },
1286
+ // onError: (error: any) => {
1287
+ //
1288
+ // },
1289
+ }
1290
+ );
1291
+ return () => {
1292
+ unsubscribe?.();
1293
+ };
1294
+ });
1295
+ const value = useMemo(() => {
1296
+ if (!futures)
1297
+ return null;
1298
+ if (!tickers)
1299
+ return futures;
1300
+ return futures.map((item) => {
1301
+ const ticker = tickers.find(
1302
+ (t) => t.symbol === item.symbol
1303
+ );
1304
+ if (ticker) {
1305
+ const data = {
1306
+ ...item,
1307
+ ["24h_close"]: ticker.close,
1308
+ ["24h_open"]: ticker.open,
1309
+ ["24h_volumn"]: ticker.volume,
1310
+ change: 0
1311
+ };
1312
+ if (ticker.close !== void 0 && ticker.open !== void 0) {
1313
+ data["change"] = new Decimal(ticker.close).minus(ticker.open).div(ticker.open).toNumber();
1314
+ }
1315
+ return data;
1316
+ }
1317
+ return item;
1318
+ });
1319
+ }, [futures, tickers]);
1320
+ return { data: value };
1321
+ };
1322
+
1323
+ // src/orderly/usePositionStream.ts
1215
1324
  var usePositionStream = (symbol, options) => {
1216
1325
  const symbolInfo = useSymbolsInfo();
1217
1326
  const { data: accountInfo } = usePrivateQuery("/v1/client/info");
@@ -1227,7 +1336,7 @@ var usePositionStream = (symbol, options) => {
1227
1336
  const {
1228
1337
  data,
1229
1338
  error,
1230
- mutate: updatePositions
1339
+ mutate: refreshPositions
1231
1340
  } = usePrivateQuery(`/v1/positions`, {
1232
1341
  // revalidateOnFocus: false,
1233
1342
  // revalidateOnReconnect: false,
@@ -1240,6 +1349,20 @@ var usePositionStream = (symbol, options) => {
1240
1349
  }
1241
1350
  });
1242
1351
  const { data: markPrices } = useMarkPricesStream();
1352
+ const [priceMode, setPriceMode] = useState(options?.calcMode || "markPrice");
1353
+ useEffect(() => {
1354
+ if (options?.calcMode && priceMode !== options?.calcMode) {
1355
+ setPriceMode(options?.calcMode);
1356
+ }
1357
+ }, [options?.calcMode]);
1358
+ const { data: tickers } = useMarketsStream();
1359
+ const tickerPrices = useMemo(() => {
1360
+ const data2 = /* @__PURE__ */ Object.create(null);
1361
+ tickers?.forEach((item) => {
1362
+ data2[item.symbol] = item["24h_close"];
1363
+ });
1364
+ return data2;
1365
+ }, [tickers]);
1243
1366
  const formatedPositions = useMemo(() => {
1244
1367
  if (!data?.rows || !symbolInfo || !accountInfo)
1245
1368
  return null;
@@ -1248,6 +1371,11 @@ var usePositionStream = (symbol, options) => {
1248
1371
  });
1249
1372
  let unrealPnL_total = zero, notional_total = zero, unsettlementPnL_total = zero;
1250
1373
  const formatted = filteredData.map((item) => {
1374
+ const unRealizedPrice = propOr(
1375
+ item.mark_price,
1376
+ item.symbol,
1377
+ priceMode === "markPrice" ? markPrices : tickerPrices
1378
+ );
1251
1379
  const price = propOr(
1252
1380
  item.mark_price,
1253
1381
  item.symbol,
@@ -1258,7 +1386,7 @@ var usePositionStream = (symbol, options) => {
1258
1386
  const unrealPnl = positions.unrealizedPnL({
1259
1387
  qty: item.position_qty,
1260
1388
  openPrice: item?.average_open_price,
1261
- markPrice: price
1389
+ markPrice: unRealizedPrice
1262
1390
  });
1263
1391
  const imr = account.IMR({
1264
1392
  maxLeverage: accountInfo.max_leverage,
@@ -1305,7 +1433,16 @@ var usePositionStream = (symbol, options) => {
1305
1433
  unsettledPnL: unsettlementPnL_total.toNumber()
1306
1434
  }
1307
1435
  ];
1308
- }, [data?.rows, symbolInfo, accountInfo, markPrices, symbol, holding]);
1436
+ }, [
1437
+ data?.rows,
1438
+ symbolInfo,
1439
+ accountInfo,
1440
+ markPrices,
1441
+ priceMode,
1442
+ tickerPrices,
1443
+ symbol,
1444
+ holding
1445
+ ]);
1309
1446
  const [totalCollateral, totalValue, totalUnrealizedROI] = useMemo(() => {
1310
1447
  if (!holding || !markPrices) {
1311
1448
  return [zero, zero, 0];
@@ -1335,7 +1472,7 @@ var usePositionStream = (symbol, options) => {
1335
1472
  if (!symbolInfo || !accountInfo)
1336
1473
  return formatedPositions[0];
1337
1474
  const total = totalCollateral.toNumber();
1338
- return formatedPositions[0].filter((item) => item.position_qty !== 0).map((item) => {
1475
+ let rows = formatedPositions[0].filter((item) => item.position_qty !== 0).map((item) => {
1339
1476
  const info = symbolInfo?.[item.symbol];
1340
1477
  const MMR = positions.MMR({
1341
1478
  baseMMR: info("base_mmr"),
@@ -1351,15 +1488,29 @@ var usePositionStream = (symbol, options) => {
1351
1488
  markPrice: item.mark_price,
1352
1489
  MMR
1353
1490
  }),
1354
- est_liq_price: positions.liqPrice({
1355
- markPrice: item.mark_price,
1356
- totalCollateral: total,
1357
- positionQty: item.position_qty,
1358
- MMR
1359
- }),
1360
- MMR
1491
+ // est_liq_price: positions.liqPrice({
1492
+ // markPrice: item.mark_price,
1493
+ // totalCollateral: total,
1494
+ // positionQty: item.position_qty,
1495
+ // MMR,
1496
+ // }),
1497
+ mmr: MMR
1498
+ };
1499
+ });
1500
+ rows = rows.map((item) => {
1501
+ const est_liq_price = positions.liqPrice({
1502
+ markPrice: item.mark_price,
1503
+ totalCollateral: total,
1504
+ positionQty: item.position_qty,
1505
+ positions: rows,
1506
+ MMR: item.mmr
1507
+ });
1508
+ return {
1509
+ ...item,
1510
+ est_liq_price
1361
1511
  };
1362
1512
  });
1513
+ return rows;
1363
1514
  }, [formatedPositions, symbolInfo, accountInfo, totalCollateral]);
1364
1515
  return [
1365
1516
  {
@@ -1378,10 +1529,8 @@ var usePositionStream = (symbol, options) => {
1378
1529
  loading: false,
1379
1530
  // showSymbol,
1380
1531
  error,
1381
- loadMore: () => {
1382
- },
1383
- refresh: () => {
1384
- }
1532
+ // loadMore: () => {},
1533
+ refresh: refreshPositions
1385
1534
  }
1386
1535
  ];
1387
1536
  };
@@ -1442,6 +1591,7 @@ var useHoldingStream = () => {
1442
1591
  var useOrderStream = (params) => {
1443
1592
  const { status, symbol, side, size = 100 } = params;
1444
1593
  const { data: markPrices = {} } = useMarkPricesStream();
1594
+ const { regesterKeyHandler } = useDataCenterContext();
1445
1595
  const [
1446
1596
  doCancelOrder,
1447
1597
  { error: cancelOrderError, isMutating: cancelMutating }
@@ -1450,58 +1600,113 @@ var useOrderStream = (params) => {
1450
1600
  doUpdateOrder,
1451
1601
  { error: updateOrderError, isMutating: updateMutating }
1452
1602
  ] = useMutation("/v1/order", "PUT");
1453
- const ordersResponse = usePrivateInfiniteQuery(
1454
- (pageIndex, previousPageData) => {
1455
- if (previousPageData && !previousPageData.rows?.length)
1456
- return null;
1457
- const search = new URLSearchParams([
1458
- ["size", size.toString()],
1459
- ["page", `${pageIndex + 1}`]
1460
- ]);
1461
- if (status) {
1462
- search.set(`status`, status);
1463
- }
1464
- if (symbol) {
1465
- search.set(`symbol`, symbol);
1466
- }
1467
- if (side) {
1468
- search.set(`side`, side);
1469
- }
1470
- return `/v1/orders?${search.toString()}`;
1471
- },
1472
- {
1473
- initialSize: 1,
1474
- // revalidateFirstPage: false,
1475
- // onError: (err) => {
1476
- // console.error("fetch failed::::", err);
1477
- // },
1478
- formatter: (data) => data
1603
+ const [
1604
+ doCanceAlgolOrder,
1605
+ { error: cancelAlgoOrderError, isMutating: cancelAlgoMutating }
1606
+ ] = useMutation("/v1/algo/order", "DELETE");
1607
+ const [
1608
+ doUpdateAlgoOrder,
1609
+ { error: updateAlgoOrderError, isMutating: updateAlgoMutating }
1610
+ ] = useMutation("/v1/algo/order", "PUT");
1611
+ const getKey = (pageIndex, previousPageData) => {
1612
+ if (previousPageData && !previousPageData.rows?.length)
1613
+ return null;
1614
+ const search = new URLSearchParams([
1615
+ ["size", size.toString()],
1616
+ ["page", `${pageIndex + 1}`],
1617
+ ["source_type", "ALL"]
1618
+ ]);
1619
+ if (status) {
1620
+ search.set(`status`, status);
1479
1621
  }
1480
- );
1481
- const orders = useMemo(() => {
1622
+ if (symbol) {
1623
+ search.set(`symbol`, symbol);
1624
+ }
1625
+ if (side) {
1626
+ search.set(`side`, side);
1627
+ }
1628
+ return `/v1/orders?${search.toString()}`;
1629
+ };
1630
+ useEffect(() => {
1631
+ const key = `orders:${status}:${symbol}:${side}`;
1632
+ regesterKeyHandler(key, getKey);
1633
+ }, [status, symbol, side]);
1634
+ const ordersResponse = usePrivateInfiniteQuery(getKey, {
1635
+ initialSize: 1,
1636
+ // revalidateFirstPage: false,
1637
+ // onError: (err) => {
1638
+ // console.error("fetch failed::::", err);
1639
+ // },
1640
+ formatter: (data) => data
1641
+ });
1642
+ const flattenOrders = useMemo(() => {
1482
1643
  if (!ordersResponse.data) {
1483
1644
  return null;
1484
1645
  }
1485
- return ordersResponse.data?.map((item) => item.rows)?.flat().map((item) => {
1646
+ return ordersResponse.data?.map((item) => item.rows)?.flat();
1647
+ }, [ordersResponse.data]);
1648
+ const orders = useMemo(() => {
1649
+ if (!flattenOrders) {
1650
+ return null;
1651
+ }
1652
+ if (status !== OrderStatus.NEW && status !== OrderStatus.INCOMPLETE) {
1653
+ return flattenOrders;
1654
+ }
1655
+ return flattenOrders.map((item) => {
1486
1656
  return {
1487
1657
  ...item,
1488
1658
  mark_price: markPrices[item.symbol] ?? 0
1489
1659
  };
1490
1660
  });
1491
- }, [ordersResponse.data, markPrices]);
1661
+ }, [flattenOrders, markPrices, status]);
1492
1662
  const total = useMemo(() => {
1493
1663
  return ordersResponse.data?.[0]?.meta?.total || 0;
1494
1664
  }, [ordersResponse.data?.[0]?.meta?.total]);
1495
1665
  const cancelAllOrders = useCallback(() => {
1496
1666
  }, [ordersResponse.data]);
1497
- const updateOrder = useCallback((orderId, order2) => {
1498
- return doUpdateOrder({ ...order2, order_id: orderId });
1667
+ const updateOrder = useCallback((orderId, order3) => {
1668
+ if (order3.algo_order_id !== void 0) {
1669
+ return doUpdateAlgoOrder({
1670
+ order_id: orderId,
1671
+ price: order3["order_price"],
1672
+ quantity: order3["order_quantity"],
1673
+ trigger_price: order3["trigger_price"]
1674
+ });
1675
+ }
1676
+ return doUpdateOrder({ ...order3, order_id: orderId });
1499
1677
  }, []);
1500
1678
  const cancelOrder = useCallback((orderId, symbol2) => {
1679
+ let isAlgoOrder = false;
1680
+ let order_id;
1681
+ if (typeof orderId === "number") {
1682
+ isAlgoOrder = false;
1683
+ order_id = orderId;
1684
+ } else {
1685
+ order_id = orderId?.order_id;
1686
+ if (orderId?.algo_order_id !== void 0) {
1687
+ isAlgoOrder = true;
1688
+ order_id = orderId?.algo_order_id;
1689
+ }
1690
+ }
1691
+ if (isAlgoOrder) {
1692
+ return doCanceAlgolOrder(null, {
1693
+ // @ts-ignore
1694
+ order_id,
1695
+ symbol: symbol2,
1696
+ source: `SDK${version_default}`
1697
+ }).then((res) => {
1698
+ if (res.success) {
1699
+ ordersResponse.mutate();
1700
+ return res;
1701
+ } else {
1702
+ throw new Error(res.message);
1703
+ }
1704
+ });
1705
+ }
1501
1706
  return doCancelOrder(null, {
1502
- order_id: orderId,
1707
+ order_id,
1503
1708
  symbol: symbol2,
1504
- source: "mweb"
1709
+ source: `SDK_${version_default}`
1505
1710
  }).then((res) => {
1506
1711
  if (res.success) {
1507
1712
  return res;
@@ -1518,17 +1723,22 @@ var useOrderStream = (params) => {
1518
1723
  {
1519
1724
  total,
1520
1725
  isLoading: ordersResponse.isLoading,
1726
+ refresh: ordersResponse.mutate,
1521
1727
  loadMore,
1522
1728
  cancelAllOrders,
1523
1729
  updateOrder,
1524
1730
  cancelOrder,
1525
1731
  errors: {
1526
1732
  cancelOrder: cancelOrderError,
1527
- updateOrder: updateOrderError
1733
+ updateOrder: updateOrderError,
1734
+ cancelAlgoOrder: cancelAlgoOrderError,
1735
+ updateAlgoOrder: updateAlgoOrderError
1528
1736
  },
1529
1737
  submitting: {
1530
1738
  cancelOrder: cancelMutating,
1531
- updateOrder: updateMutating
1739
+ updateOrder: updateMutating,
1740
+ cancelAlgoOrder: cancelAlgoMutating,
1741
+ updateAlglOrder: updateAlgoMutating
1532
1742
  }
1533
1743
  }
1534
1744
  ];
@@ -1539,31 +1749,32 @@ var positionsPath = pathOr([], [0, "rows"]);
1539
1749
  pathOr(0, [0, "totalCollateral"]);
1540
1750
  var useCollateral = (options = { dp: 6 }) => {
1541
1751
  const { dp } = options;
1542
- const positions2 = usePositionStream();
1752
+ const positions3 = usePositionStream();
1543
1753
  const [orders] = useOrderStream({ status: OrderStatus.NEW });
1544
1754
  const { data: accountInfo } = usePrivateQuery("/v1/client/info");
1545
1755
  const symbolInfo = useSymbolsInfo();
1546
1756
  const { data: markPrices } = useMarkPricesStream();
1547
1757
  const { usdc } = useHoldingStream();
1758
+ const filterAlgoOrders = orders?.filter((item) => item.algo_order_id === void 0) ?? [];
1548
1759
  const [totalCollateral, totalValue] = useMemo(() => {
1549
1760
  return [
1550
- pathOr(zero, [0, "totalCollateral"], positions2),
1551
- pathOr(zero, [0, "totalValue"], positions2)
1761
+ pathOr(zero, [0, "totalCollateral"], positions3),
1762
+ pathOr(zero, [0, "totalValue"], positions3)
1552
1763
  ];
1553
- }, [positions2, markPrices]);
1764
+ }, [positions3, markPrices]);
1554
1765
  const totalInitialMarginWithOrders = useMemo(() => {
1555
1766
  if (!accountInfo || !symbolInfo || !markPrices) {
1556
1767
  return 0;
1557
1768
  }
1558
1769
  return account.totalInitialMarginWithOrders({
1559
- positions: positionsPath(positions2),
1560
- orders: orders ?? [],
1770
+ positions: positionsPath(positions3),
1771
+ orders: filterAlgoOrders,
1561
1772
  markPrices,
1562
1773
  IMR_Factors: accountInfo.imr_factor,
1563
1774
  maxLeverage: accountInfo.max_leverage,
1564
1775
  symbolInfo
1565
1776
  });
1566
- }, [positions2, orders, markPrices, accountInfo, symbolInfo]);
1777
+ }, [positions3, filterAlgoOrders, markPrices, accountInfo, symbolInfo]);
1567
1778
  const freeCollateral = useMemo(() => {
1568
1779
  return account.freeCollateral({
1569
1780
  totalCollateral,
@@ -1573,15 +1784,18 @@ var useCollateral = (options = { dp: 6 }) => {
1573
1784
  const availableBalance = useMemo(() => {
1574
1785
  return account.availableBalance({
1575
1786
  USDCHolding: usdc?.holding ?? 0,
1576
- unsettlementPnL: pathOr_unsettledPnLPathOr(positions2)
1787
+ unsettlementPnL: pathOr_unsettledPnLPathOr(positions3)
1577
1788
  });
1578
- }, [usdc, pathOr_unsettledPnLPathOr(positions2)]);
1789
+ }, [usdc?.holding, pathOr_unsettledPnLPathOr(positions3)]);
1579
1790
  return {
1580
1791
  totalCollateral: totalCollateral.toDecimalPlaces(dp).toNumber(),
1581
1792
  freeCollateral: freeCollateral.toDecimalPlaces(dp).toNumber(),
1582
1793
  totalValue: totalValue.toDecimalPlaces(dp).toNumber(),
1583
1794
  availableBalance,
1584
- unsettledPnL: pathOr_unsettledPnLPathOr(positions2)
1795
+ unsettledPnL: pathOr_unsettledPnLPathOr(positions3),
1796
+ accountInfo,
1797
+ // @hidden
1798
+ positions: positionsPath(positions3)
1585
1799
  };
1586
1800
  };
1587
1801
  var positionsPath2 = pathOr([], [0, "rows"]);
@@ -1595,8 +1809,8 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
1595
1809
  const maxQty = useMemo(() => {
1596
1810
  if (!symbol)
1597
1811
  return 0;
1598
- const positions2 = positionsPath2(positionsData);
1599
- const positionQty = account.getQtyFromPositions(positions2, symbol);
1812
+ const positions3 = positionsPath2(positionsData);
1813
+ const positionQty = account.getQtyFromPositions(positions3, symbol);
1600
1814
  if (reduceOnly) {
1601
1815
  if (positionQty > 0) {
1602
1816
  if (side === OrderSide.BUY) {
@@ -1617,20 +1831,21 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
1617
1831
  if (!markPrices || !markPrices[symbol] || !orders || !accountInfo)
1618
1832
  return 0;
1619
1833
  const getSymbolInfo = symbolInfo[symbol];
1834
+ const filterAlgoOrders = orders.filter((item) => item.algo_order_id === void 0);
1620
1835
  const buyOrdersQty = account.getQtyFromOrdersBySide(
1621
- orders,
1836
+ filterAlgoOrders,
1622
1837
  symbol,
1623
1838
  OrderSide.BUY
1624
1839
  );
1625
1840
  const sellOrdersQty = account.getQtyFromOrdersBySide(
1626
- orders,
1841
+ filterAlgoOrders,
1627
1842
  symbol,
1628
1843
  OrderSide.SELL
1629
1844
  );
1630
- const otherPositions = positions2.filter(
1845
+ const otherPositions = positions3.filter(
1631
1846
  (item) => item.symbol !== symbol
1632
1847
  );
1633
- const otherOrders = orders.filter(
1848
+ const otherOrders = filterAlgoOrders.filter(
1634
1849
  (item) => item.symbol !== symbol
1635
1850
  );
1636
1851
  const otherIMs = account.otherIMs({
@@ -1666,27 +1881,36 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
1666
1881
  totalCollateral,
1667
1882
  reduceOnly
1668
1883
  ]);
1669
- return maxQty;
1884
+ return Math.max(maxQty, 0);
1670
1885
  };
1671
- var { maxPrice, minPrice } = order;
1886
+ var { maxPrice, minPrice, scropePrice } = order;
1672
1887
  var BaseOrderCreator = class {
1673
1888
  baseOrder(data) {
1674
- const order2 = {
1675
- // symbol: data.symbol,
1889
+ const order3 = {
1890
+ symbol: data.symbol,
1676
1891
  order_type: data.order_type === OrderType.LIMIT ? !!data.order_type_ext ? data.order_type_ext : data.order_type : data.order_type,
1677
1892
  side: data.side,
1678
1893
  reduce_only: data.reduce_only,
1679
- order_quantity: data.order_quantity
1894
+ order_quantity: data.order_quantity,
1895
+ total: data.total
1680
1896
  };
1681
1897
  if (data.visible_quantity === 0) {
1682
- order2.visible_quantity = data.visible_quantity;
1898
+ order3.visible_quantity = data.visible_quantity;
1683
1899
  }
1684
- return order2;
1900
+ return order3;
1685
1901
  }
1686
1902
  baseValidate(values, configs) {
1687
1903
  const errors = {};
1688
1904
  const { maxQty } = configs;
1689
- const { order_quantity, total } = values;
1905
+ let { order_quantity, total, order_price } = values;
1906
+ if (!order_quantity) {
1907
+ if (total && order_price) {
1908
+ const { quote_dp } = configs.symbol;
1909
+ const totalNumber = new Decimal(total);
1910
+ const qty = totalNumber.dividedBy(order_price).toFixed(quote_dp);
1911
+ order_quantity = qty;
1912
+ }
1913
+ }
1690
1914
  if (!order_quantity) {
1691
1915
  errors.order_quantity = {
1692
1916
  type: "required",
@@ -1732,13 +1956,28 @@ var BaseOrderCreator = class {
1732
1956
  }
1733
1957
  return Promise.resolve(errors);
1734
1958
  }
1959
+ fixOrderQuantity(order3, config) {
1960
+ if (!order3.order_quantity && order3.total && order3.order_price) {
1961
+ const { base_dp } = config.symbol;
1962
+ const totalNumber = new Decimal(order3.total);
1963
+ const qty = totalNumber.div(order3.order_price).toDecimalPlaces(base_dp);
1964
+ order3.order_quantity = qty.toNumber();
1965
+ delete order3.total;
1966
+ }
1967
+ return order3;
1968
+ }
1735
1969
  };
1736
1970
  var LimitOrderCreator = class extends BaseOrderCreator {
1737
- create(values) {
1738
- return {
1971
+ create(values, config) {
1972
+ const order3 = {
1739
1973
  ...this.baseOrder(values),
1740
1974
  order_price: values.order_price
1741
1975
  };
1976
+ this.fixOrderQuantity(order3, config);
1977
+ delete order3["total"];
1978
+ delete order3["trigger_price"];
1979
+ delete order3["isStopOrder"];
1980
+ return order3;
1742
1981
  }
1743
1982
  validate(values, config) {
1744
1983
  return this.baseValidate(values, config).then((errors) => {
@@ -1751,22 +1990,34 @@ var LimitOrderCreator = class extends BaseOrderCreator {
1751
1990
  } else {
1752
1991
  const price = new Decimal(order_price);
1753
1992
  const { symbol } = config;
1754
- const { price_range } = symbol;
1993
+ const { price_range, price_scope } = symbol;
1755
1994
  const maxPriceNumber = maxPrice(config.markPrice, price_range);
1756
1995
  const minPriceNumber = minPrice(config.markPrice, price_range);
1757
- console.log(`side: ${side} value:`, values);
1758
- if (side === "BUY" && price.gt(maxPriceNumber)) {
1996
+ const scropePriceNumbere = scropePrice(
1997
+ config.markPrice,
1998
+ price_scope,
1999
+ side
2000
+ );
2001
+ const priceRange = side === "BUY" ? {
2002
+ min: scropePriceNumbere,
2003
+ max: maxPriceNumber
2004
+ } : {
2005
+ min: minPriceNumber,
2006
+ max: scropePriceNumbere
2007
+ };
2008
+ if (price.gt(priceRange.max)) {
1759
2009
  errors.order_price = {
1760
2010
  type: "max",
1761
- message: `price must be less than ${new Decimal(
1762
- maxPriceNumber
2011
+ message: `Price must be less than ${new Decimal(
2012
+ priceRange.max
1763
2013
  ).todp(symbol.quote_dp)}`
1764
2014
  };
1765
- } else if (side === "SELL" && price.lt(minPriceNumber)) {
2015
+ }
2016
+ if (price.lt(priceRange.min)) {
1766
2017
  errors.order_price = {
1767
2018
  type: "min",
1768
- message: `price must be greater than ${new Decimal(
1769
- minPriceNumber
2019
+ message: `Price must be greater than ${new Decimal(
2020
+ priceRange.min
1770
2021
  ).todp(symbol.quote_dp)}`
1771
2022
  };
1772
2023
  }
@@ -1779,6 +2030,9 @@ var MarketOrderCreator = class extends BaseOrderCreator {
1779
2030
  create(values) {
1780
2031
  const data = this.baseOrder(values);
1781
2032
  delete data["order_price"];
2033
+ delete data["total"];
2034
+ delete data["trigger_price"];
2035
+ delete data["isStopOrder"];
1782
2036
  return {
1783
2037
  ...data
1784
2038
  };
@@ -1793,6 +2047,110 @@ var FOKOrderCreator = class extends LimitOrderCreator {
1793
2047
  };
1794
2048
  var IOCOrderCreator = class extends LimitOrderCreator {
1795
2049
  };
2050
+ var StopLimitOrderCreator = class extends LimitOrderCreator {
2051
+ create(values, config) {
2052
+ const order3 = {
2053
+ ...this.baseOrder(values),
2054
+ order_price: values.order_price,
2055
+ trigger_price: values.trigger_price,
2056
+ algo_type: "STOP",
2057
+ type: "LIMIT",
2058
+ quantity: values["order_quantity"],
2059
+ price: values["order_price"],
2060
+ trigger_price_type: "MARK_PRICE"
2061
+ };
2062
+ this.fixOrderQuantity(order3, config);
2063
+ delete order3["order_quantity"];
2064
+ delete order3["order_price"];
2065
+ delete order3["isStopOrder"];
2066
+ delete order3["total"];
2067
+ return order3;
2068
+ }
2069
+ validate(values, config) {
2070
+ return this.baseValidate(values, config).then((errors) => {
2071
+ const { order_price, trigger_price, side } = values;
2072
+ if (!order_price) {
2073
+ errors.order_price = {
2074
+ type: "required",
2075
+ message: "price is required"
2076
+ };
2077
+ }
2078
+ if (!trigger_price) {
2079
+ errors.trigger_price = {
2080
+ type: "required",
2081
+ message: "Trigger price is required"
2082
+ };
2083
+ }
2084
+ if (trigger_price && order_price) {
2085
+ const price = new Decimal(order_price);
2086
+ const { symbol } = config;
2087
+ const { price_range, price_scope } = symbol;
2088
+ const maxPriceNumber = maxPrice(trigger_price, price_range);
2089
+ const minPriceNumber = minPrice(trigger_price, price_range);
2090
+ const scropePriceNumbere = scropePrice(
2091
+ trigger_price,
2092
+ price_scope,
2093
+ side
2094
+ );
2095
+ const priceRange = side === "BUY" ? {
2096
+ min: scropePriceNumbere,
2097
+ max: maxPriceNumber
2098
+ } : {
2099
+ min: minPriceNumber,
2100
+ max: scropePriceNumbere
2101
+ };
2102
+ if (price.gt(priceRange.max)) {
2103
+ errors.order_price = {
2104
+ type: "max",
2105
+ message: `Price must be less than ${new Decimal(
2106
+ priceRange.max
2107
+ ).todp(symbol.quote_dp)}`
2108
+ };
2109
+ }
2110
+ if (price.lt(priceRange.min)) {
2111
+ errors.order_price = {
2112
+ type: "min",
2113
+ message: `Price must be greater than ${new Decimal(
2114
+ priceRange.min
2115
+ ).todp(symbol.quote_dp)}`
2116
+ };
2117
+ }
2118
+ }
2119
+ return errors;
2120
+ });
2121
+ }
2122
+ };
2123
+ var StopMarketOrderCreator = class extends LimitOrderCreator {
2124
+ create(values, _) {
2125
+ const result = {
2126
+ ...this.baseOrder(values),
2127
+ order_price: values.order_price,
2128
+ trigger_price: values.trigger_price,
2129
+ algo_type: "STOP",
2130
+ type: "MARKET",
2131
+ quantity: values["order_quantity"],
2132
+ price: values["order_price"],
2133
+ trigger_price_type: "MARK_PRICE"
2134
+ };
2135
+ delete result["order_quantity"];
2136
+ delete result["order_price"];
2137
+ delete result["isStopOrder"];
2138
+ delete result["total"];
2139
+ return result;
2140
+ }
2141
+ validate(values, config) {
2142
+ return this.baseValidate(values, config).then((errors) => {
2143
+ const { order_price, trigger_price, side } = values;
2144
+ if (!trigger_price) {
2145
+ errors.trigger_price = {
2146
+ type: "required",
2147
+ message: "Trigger price is required"
2148
+ };
2149
+ }
2150
+ return errors;
2151
+ });
2152
+ }
2153
+ };
1796
2154
  var GeneralOrderCreator = class extends BaseOrderCreator {
1797
2155
  create(data) {
1798
2156
  return {
@@ -1805,6 +2163,15 @@ var GeneralOrderCreator = class extends BaseOrderCreator {
1805
2163
  return super.baseValidate(values, configs);
1806
2164
  }
1807
2165
  };
2166
+ var availableOrderTypes = [
2167
+ OrderType.LIMIT,
2168
+ OrderType.MARKET,
2169
+ OrderType.IOC,
2170
+ OrderType.FOK,
2171
+ OrderType.POST_ONLY,
2172
+ OrderType.STOP_LIMIT,
2173
+ OrderType.STOP_MARKET
2174
+ ];
1808
2175
  var OrderFactory = class {
1809
2176
  static create(type) {
1810
2177
  switch (type) {
@@ -1818,17 +2185,68 @@ var OrderFactory = class {
1818
2185
  return new FOKOrderCreator();
1819
2186
  case OrderType.POST_ONLY:
1820
2187
  return new PostOnlyOrderCreator();
2188
+ case OrderType.STOP_LIMIT:
2189
+ return new StopLimitOrderCreator();
2190
+ case OrderType.STOP_MARKET:
2191
+ return new StopMarketOrderCreator();
1821
2192
  default:
1822
2193
  return new GeneralOrderCreator();
1823
2194
  }
1824
2195
  }
1825
2196
  };
1826
-
1827
- // src/orderly/useOrderEntry.ts
1828
- var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1829
- const [doCreateOrder, { data, error, reset, isMutating }] = useMutation("/v1/order");
1830
- const { freeCollateral } = useCollateral();
2197
+ function useOrderEntry(symbolOrOrder, sideOrOptions, reduceOnly, options) {
2198
+ if (typeof symbolOrOrder === "object") {
2199
+ if (!symbolOrOrder.symbol) {
2200
+ throw new SDKError("symbol is required");
2201
+ }
2202
+ if (!symbolOrOrder.side) {
2203
+ throw new SDKError("Order side is required");
2204
+ }
2205
+ if (!symbolOrOrder.order_type) {
2206
+ throw new SDKError("order_type is required");
2207
+ }
2208
+ }
2209
+ const prevOrderData = useRef(null);
2210
+ const orderDataCache = useRef(null);
2211
+ const notSupportData = useRef({});
2212
+ const [doCreateOrder, { data, error, reset, isMutating }] = useMutation(orderDataCache?.current?.isStopOrder ? "/v1/algo/order" : "/v1/order");
2213
+ const [errors, setErrors] = useState(null);
2214
+ const ee = useEventEmitter();
2215
+ const fieldDirty = useRef({});
2216
+ const submitted = useRef(false);
2217
+ const askAndBid = useRef([]);
2218
+ const onOrderbookUpdate = useDebouncedCallback((data2) => {
2219
+ askAndBid.current = data2;
2220
+ }, 200);
2221
+ const { freeCollateral, totalCollateral, positions: positions3, accountInfo } = useCollateral();
1831
2222
  const symbolInfo = useSymbolsInfo();
2223
+ const symbol = useMemo(() => {
2224
+ if (typeof symbolOrOrder === "string") {
2225
+ return symbolOrOrder;
2226
+ }
2227
+ return symbolOrOrder.symbol;
2228
+ }, [symbolOrOrder]);
2229
+ const optionsValue = useMemo(() => {
2230
+ if (typeof sideOrOptions === "object") {
2231
+ return sideOrOptions;
2232
+ }
2233
+ return options;
2234
+ }, [sideOrOptions]);
2235
+ const isReduceOnly = useMemo(() => {
2236
+ if (typeof reduceOnly === "boolean") {
2237
+ return reduceOnly;
2238
+ }
2239
+ if (typeof symbolOrOrder === "object") {
2240
+ return !!symbolOrOrder.reduce_only;
2241
+ }
2242
+ return false;
2243
+ }, [symbolOrOrder, reduceOnly]);
2244
+ const sideValue = useMemo(() => {
2245
+ if (typeof symbolOrOrder === "object") {
2246
+ return symbolOrOrder.side;
2247
+ }
2248
+ return sideOrOptions;
2249
+ }, [symbolOrOrder, sideOrOptions]);
1832
2250
  const baseDP = useMemo(
1833
2251
  () => getPrecisionByNumber(symbolInfo[symbol]("base_tick", 0)),
1834
2252
  [symbolInfo]
@@ -1836,42 +2254,123 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1836
2254
  const quoteDP = useMemo(() => {
1837
2255
  return getPrecisionByNumber(symbolInfo[symbol]("quote_tick", 0));
1838
2256
  }, [symbolInfo]);
2257
+ const baseIMR = useMemo(() => symbolInfo[symbol]("base_imr"), [symbolInfo]);
2258
+ const baseMMR = useMemo(() => symbolInfo[symbol]("base_mmr"), [symbolInfo]);
1839
2259
  const { data: markPrice } = useMarkPrice(symbol);
1840
- const maxQty = useMaxQty(
1841
- symbol,
1842
- side,
1843
- // orderExtraValues.reduce_only
1844
- reduceOnly
1845
- );
1846
- const onSubmit = (values) => {
1847
- if (!values || typeof values.order_type === "undefined" || values.order_type !== OrderType.MARKET && values.order_type !== OrderType.LIMIT) {
1848
- throw new Error("order_type is error");
2260
+ const diffOrderEntry = (prev, current) => {
2261
+ if (!prev)
2262
+ return null;
2263
+ let key, value;
2264
+ const keys = Object.keys(current);
2265
+ for (let i = 0; i < keys.length; i++) {
2266
+ const k = keys[i];
2267
+ let preveValue = prev[k];
2268
+ let currentValue = current[k];
2269
+ if (typeof preveValue === "undefined" && typeof currentValue === "undefined")
2270
+ continue;
2271
+ if (preveValue !== currentValue) {
2272
+ key = k;
2273
+ value = currentValue;
2274
+ break;
2275
+ }
2276
+ }
2277
+ if (!key)
2278
+ return null;
2279
+ return { key, value };
2280
+ };
2281
+ const maxQty = useMaxQty(symbol, sideValue, isReduceOnly);
2282
+ const parsedData = useMemo(() => {
2283
+ if (typeof symbolOrOrder === "string") {
2284
+ return null;
2285
+ }
2286
+ if (typeof symbolOrOrder.order_quantity === "string") {
2287
+ symbolOrOrder.order_quantity = symbolOrOrder.order_quantity.replace(
2288
+ /,/g,
2289
+ ""
2290
+ );
2291
+ }
2292
+ if (typeof symbolOrOrder.order_price === "string") {
2293
+ symbolOrOrder.order_price = symbolOrOrder.order_price.replace(/,/g, "");
2294
+ }
2295
+ if (typeof symbolOrOrder.total === "string") {
2296
+ symbolOrOrder.total = symbolOrOrder.total.replace(/,/g, "");
2297
+ }
2298
+ if (typeof symbolOrOrder.order_quantity === "number") {
2299
+ symbolOrOrder.order_quantity = new Decimal(symbolOrOrder.order_quantity).toDecimalPlaces(baseDP).toString();
2300
+ }
2301
+ return symbolOrOrder;
2302
+ }, [symbolOrOrder]);
2303
+ const createOrder = (values) => {
2304
+ if (!values.symbol) {
2305
+ throw new SDKError("symbol is error");
2306
+ }
2307
+ if (!values.side) {
2308
+ throw new SDKError("side is error");
2309
+ }
2310
+ if (!values || typeof values.order_type === "undefined" || !includes(values.order_type, availableOrderTypes)) {
2311
+ throw new SDKError("order_type is error");
1849
2312
  }
1850
2313
  const orderCreator = OrderFactory.create(
1851
- !!values.order_type_ext ? values.order_type_ext : values.order_type
2314
+ values.order_type_ext ? values.order_type_ext : values.order_type
1852
2315
  );
1853
2316
  if (!orderCreator) {
1854
- return Promise.reject(new Error("orderCreator is null"));
2317
+ return Promise.reject(new SDKError("orderCreator is null"));
1855
2318
  }
1856
- return orderCreator?.validate(values, {
1857
- symbol: symbolInfo[symbol](),
1858
- // token: tokenInfo[symbol](),
1859
- maxQty,
1860
- markPrice
1861
- }).then(() => {
1862
- if (!orderCreator) {
1863
- throw new Error("orderCreator is null");
1864
- }
1865
- if (!symbol) {
1866
- throw new Error("symbol is null");
1867
- }
1868
- const data2 = orderCreator.create(values);
1869
- return doCreateOrder({
1870
- ...data2,
1871
- symbol
2319
+ return new Promise((resolve, reject) => {
2320
+ return orderCreator.validate(values, {
2321
+ symbol: symbolInfo[symbol](),
2322
+ // token: tokenInfo[symbol](),
2323
+ maxQty,
2324
+ markPrice
2325
+ }).then((errors2) => {
2326
+ submitted.current = true;
2327
+ if (errors2.order_price || errors2.order_quantity || errors2.trigger_price) {
2328
+ setErrors(errors2);
2329
+ reject(
2330
+ errors2.order_price?.message || errors2.order_quantity?.message
2331
+ );
2332
+ } else {
2333
+ const data2 = orderCreator.create(values, {
2334
+ symbol: symbolInfo[symbol](),
2335
+ maxQty,
2336
+ markPrice
2337
+ });
2338
+ return doCreateOrder(
2339
+ omit(["order_type_ext"], {
2340
+ // ...values,
2341
+ // ...omit(["order_price"], values),
2342
+ ...data2
2343
+ })
2344
+ ).then((res) => {
2345
+ if (res.success) {
2346
+ resolve(res.data);
2347
+ } else {
2348
+ reject(res);
2349
+ }
2350
+ }, reject);
2351
+ }
1872
2352
  });
1873
2353
  });
1874
2354
  };
2355
+ const onSubmit = (values) => {
2356
+ if (typeof reduceOnly === "boolean" && reduceOnly && !values.reduce_only) {
2357
+ return Promise.reject(
2358
+ new SDKError(
2359
+ "The reduceOny parameter of hook does not match your order data"
2360
+ )
2361
+ );
2362
+ }
2363
+ return createOrder({
2364
+ ...values,
2365
+ symbol
2366
+ });
2367
+ };
2368
+ const submit = useCallback(() => {
2369
+ if (typeof symbolOrOrder === "string") {
2370
+ throw new SDKError("Function is not supported, please use onSubmit()");
2371
+ }
2372
+ return createOrder(symbolOrOrder);
2373
+ }, [symbolOrOrder]);
1875
2374
  const calculate = useCallback(
1876
2375
  (values, field, value) => {
1877
2376
  const fieldHandler = getCalculateHandler(field);
@@ -1894,75 +2393,407 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1894
2393
  markPrice
1895
2394
  });
1896
2395
  };
2396
+ const formattedOrder = useMemo(() => {
2397
+ if (!parsedData) {
2398
+ return notSupportData.current;
2399
+ }
2400
+ if (!prevOrderData.current) {
2401
+ prevOrderData.current = parsedData;
2402
+ orderDataCache.current = {
2403
+ ...parsedData,
2404
+ total: ""
2405
+ };
2406
+ return orderDataCache.current;
2407
+ }
2408
+ const item = diffOrderEntry(prevOrderData.current, parsedData);
2409
+ if (!item) {
2410
+ return orderDataCache.current;
2411
+ }
2412
+ if (typeof parsedData.order_price !== "undefined") {
2413
+ fieldDirty.current.order_price = true;
2414
+ }
2415
+ if (typeof parsedData.order_quantity !== "undefined") {
2416
+ fieldDirty.current.order_quantity = true;
2417
+ }
2418
+ const values = calculate(parsedData, item.key, item.value);
2419
+ values.isStopOrder = values.order_type?.startsWith("STOP") || false;
2420
+ values.total = values.total || "";
2421
+ prevOrderData.current = parsedData;
2422
+ orderDataCache.current = values;
2423
+ return values;
2424
+ }, [
2425
+ parsedData?.order_price,
2426
+ parsedData?.side,
2427
+ parsedData?.order_quantity,
2428
+ parsedData?.visible_quantity,
2429
+ parsedData?.order_type,
2430
+ parsedData?.order_type_ext,
2431
+ parsedData?.symbol,
2432
+ parsedData?.total,
2433
+ parsedData?.reduce_only,
2434
+ parsedData?.trigger_price,
2435
+ markPrice
2436
+ ]);
2437
+ useEffect(() => {
2438
+ if (!markPrice)
2439
+ return;
2440
+ validator(formattedOrder)?.then((err) => {
2441
+ setErrors(err);
2442
+ });
2443
+ }, [
2444
+ formattedOrder.broker_id,
2445
+ formattedOrder.order_quantity,
2446
+ formattedOrder.total,
2447
+ formattedOrder.trigger_price,
2448
+ markPrice
2449
+ ]);
2450
+ useEffect(() => {
2451
+ if (!optionsValue?.watchOrderbook)
2452
+ return;
2453
+ ee.on("orderbook:update", onOrderbookUpdate);
2454
+ return () => {
2455
+ ee.off("orderbook_update", onOrderbookUpdate);
2456
+ };
2457
+ }, [optionsValue?.watchOrderbook]);
2458
+ useEffect(() => {
2459
+ askAndBid.current = [];
2460
+ }, [parsedData?.symbol]);
2461
+ const getPriceAndQty = (symbolOrOrder2) => {
2462
+ let quantity = Number(symbolOrOrder2.order_quantity);
2463
+ const orderPrice = Number(symbolOrOrder2.order_price);
2464
+ if (isNaN(quantity) || quantity <= 0 || askAndBid.current.length === 0)
2465
+ return null;
2466
+ if ((symbolOrOrder2.order_type === OrderType.LIMIT || symbolOrOrder2.order_type === OrderType.STOP_LIMIT) && isNaN(orderPrice))
2467
+ return null;
2468
+ let price;
2469
+ if (symbolOrOrder2.order_type === OrderType.MARKET || symbolOrOrder2.order_type === OrderType.STOP_MARKET) {
2470
+ if (symbolOrOrder2.side === OrderSide.BUY) {
2471
+ price = askAndBid.current[0];
2472
+ } else {
2473
+ price = askAndBid.current[1];
2474
+ }
2475
+ } else {
2476
+ if (symbolOrOrder2.side === OrderSide.BUY) {
2477
+ if (orderPrice >= askAndBid.current[0]) {
2478
+ price = askAndBid.current[0];
2479
+ } else {
2480
+ price = orderPrice;
2481
+ }
2482
+ } else {
2483
+ if (orderPrice <= askAndBid.current[1]) {
2484
+ price = askAndBid.current[1];
2485
+ } else {
2486
+ price = orderPrice;
2487
+ }
2488
+ }
2489
+ }
2490
+ if (symbolOrOrder2.side === OrderSide.SELL) {
2491
+ quantity = -quantity;
2492
+ }
2493
+ return { price, quantity };
2494
+ };
2495
+ const estLiqPrice = useMemo(() => {
2496
+ if (!accountInfo || !parsedData || !markPrice)
2497
+ return null;
2498
+ const result = getPriceAndQty(formattedOrder);
2499
+ if (result === null)
2500
+ return null;
2501
+ const { price, quantity } = result;
2502
+ if (!price || !quantity)
2503
+ return null;
2504
+ const liqPrice = order.estLiqPrice({
2505
+ markPrice,
2506
+ baseIMR,
2507
+ baseMMR,
2508
+ totalCollateral,
2509
+ positions: positions3,
2510
+ IMR_Factor: accountInfo["imr_factor"][symbol],
2511
+ newOrder: {
2512
+ qty: quantity,
2513
+ price,
2514
+ symbol: parsedData.symbol
2515
+ }
2516
+ });
2517
+ if (liqPrice <= 0)
2518
+ return null;
2519
+ return liqPrice;
2520
+ }, [
2521
+ markPrice,
2522
+ baseIMR,
2523
+ baseMMR,
2524
+ totalCollateral,
2525
+ formattedOrder?.order_price,
2526
+ formattedOrder?.order_quantity,
2527
+ formattedOrder?.total,
2528
+ formattedOrder?.trigger_price,
2529
+ accountInfo
2530
+ ]);
2531
+ const estLeverage = useMemo(() => {
2532
+ if (!accountInfo || !parsedData)
2533
+ return null;
2534
+ const result = getPriceAndQty(formattedOrder);
2535
+ if (result === null || !result.price || !result.quantity)
2536
+ return null;
2537
+ const leverage = order.estLeverage({
2538
+ totalCollateral,
2539
+ positions: positions3,
2540
+ newOrder: {
2541
+ symbol: parsedData.symbol,
2542
+ qty: result.quantity,
2543
+ price: result.price
2544
+ }
2545
+ });
2546
+ return leverage;
2547
+ }, [
2548
+ baseIMR,
2549
+ baseMMR,
2550
+ totalCollateral,
2551
+ positions3,
2552
+ formattedOrder?.order_price,
2553
+ formattedOrder?.order_quantity,
2554
+ formattedOrder?.total,
2555
+ formattedOrder?.trigger_price
2556
+ ]);
1897
2557
  return {
1898
2558
  maxQty,
1899
2559
  freeCollateral,
1900
2560
  markPrice,
1901
2561
  onSubmit,
2562
+ submit,
1902
2563
  submitting: isMutating,
2564
+ formattedOrder,
2565
+ // errors,
2566
+ estLiqPrice,
2567
+ estLeverage,
1903
2568
  helper: {
1904
2569
  calculate,
1905
2570
  validator
2571
+ // clearErrors,
2572
+ },
2573
+ metaState: {
2574
+ dirty: fieldDirty.current,
2575
+ submitted: submitted.current,
2576
+ errors
1906
2577
  },
1907
2578
  symbolConfig: symbolInfo[symbol]()
1908
2579
  };
1909
- };
2580
+ }
1910
2581
 
1911
2582
  // src/orderly/useAccountInfo.ts
1912
2583
  var useAccountInfo = () => {
1913
2584
  return usePrivateQuery("/v1/client/info");
1914
2585
  };
1915
- var useMarketsStream = () => {
1916
- const ws = useWS();
1917
- const { data: futures } = useQuery(`/v1/public/futures`, {
1918
- revalidateOnFocus: false
1919
- });
1920
- const { data: tickers } = useSWRSubscription("tickers", (_, { next }) => {
1921
- const unsubscribe = ws.subscribe(
1922
- // { event: "subscribe", topic: "markprices" },
1923
- "tickers",
1924
- {
1925
- onMessage: (message) => {
1926
- next(null, message);
1927
- }
1928
- // onUnsubscribe: () => {
1929
- // return "markprices";
1930
- // },
1931
- // onError: (error: any) => {
1932
- //
1933
- // },
2586
+ var MarketsType = /* @__PURE__ */ ((MarketsType2) => {
2587
+ MarketsType2[MarketsType2["FAVORITES"] = 0] = "FAVORITES";
2588
+ MarketsType2[MarketsType2["RECENT"] = 1] = "RECENT";
2589
+ MarketsType2[MarketsType2["ALL"] = 2] = "ALL";
2590
+ return MarketsType2;
2591
+ })(MarketsType || {});
2592
+ var useMarkets = (type) => {
2593
+ const marketsKey = "markets";
2594
+ const { data } = useMarketsStream();
2595
+ const { configStore } = useContext(OrderlyContext);
2596
+ const publicInfo = useSymbolsInfo();
2597
+ if (!configStore.get(marketsKey)) {
2598
+ const jsonStr = localStorage.getItem(marketsKey);
2599
+ if (jsonStr) {
2600
+ configStore.set(marketsKey, JSON.parse(jsonStr));
2601
+ } else {
2602
+ const defaultTab = { name: "Popular", id: 1 };
2603
+ configStore.set(marketsKey, {
2604
+ recent: [],
2605
+ favorites: [
2606
+ { name: "PERP_ETH_USDC", tabs: [{ ...defaultTab }] },
2607
+ { name: "PERP_BTC_USDC", tabs: [{ ...defaultTab }] }
2608
+ ],
2609
+ favoriteTabs: [{ ...defaultTab }],
2610
+ lastSelectFavoriteTab: { ...defaultTab }
2611
+ });
2612
+ }
2613
+ }
2614
+ const getFavoriteTabs = useMemo(() => {
2615
+ const tabs2 = configStore.get(marketsKey)["favoriteTabs"];
2616
+ return tabs2 || [{ name: "Popular", id: 1 }];
2617
+ }, []);
2618
+ const getFavorites = useMemo(() => {
2619
+ const curData = configStore.get(marketsKey)["favorites"] || [];
2620
+ const tabs2 = getFavoriteTabs;
2621
+ const result = [];
2622
+ for (let index = 0; index < curData.length; index++) {
2623
+ const favData = curData[index];
2624
+ var favTabs = favData.tabs.filter((tab) => tabs2.findIndex((item) => tab.id === item.id) !== -1);
2625
+ if (favTabs.length > 0) {
2626
+ result.push({ ...favData, tabs: favTabs });
1934
2627
  }
1935
- );
1936
- return () => {
1937
- unsubscribe?.();
2628
+ }
2629
+ configStore.set(marketsKey, { ...configStore.getOr(marketsKey, {}), favorites: result });
2630
+ return result;
2631
+ }, [configStore]);
2632
+ const getRecent = useMemo(() => {
2633
+ const curData = configStore.get(marketsKey)["recent"];
2634
+ return (curData || []).filter((e) => e);
2635
+ }, []);
2636
+ const [favoriteTabs, setFavoriteTabs] = useState(getFavoriteTabs);
2637
+ const [favorites, setFavorites] = useState(getFavorites);
2638
+ const [recent, setRecent] = useState(getRecent);
2639
+ const updateFavoriteTabs = (tab, operator) => {
2640
+ const saveTabs = (tabs3) => {
2641
+ setFavoriteTabs(tabs3);
2642
+ configStore.set(marketsKey, {
2643
+ ...configStore.getOr(marketsKey, {}),
2644
+ "favoriteTabs": tabs3
2645
+ });
1938
2646
  };
1939
- });
1940
- const value = useMemo(() => {
1941
- if (!futures)
1942
- return null;
1943
- if (!tickers)
1944
- return futures;
1945
- return futures.map((item) => {
1946
- const ticker = tickers.find(
1947
- (t) => t.symbol === item.symbol
1948
- );
1949
- if (ticker) {
1950
- const data = {
1951
- ...item,
1952
- ["24h_close"]: ticker.close,
1953
- ["24h_open"]: ticker.open,
1954
- ["24h_volumn"]: ticker.volume,
1955
- change: 0
1956
- };
1957
- if (ticker.close !== void 0 && ticker.open !== void 0) {
1958
- data["change"] = new Decimal(ticker.close).minus(ticker.open).div(ticker.open).toNumber();
2647
+ if (Array.isArray(tab)) {
2648
+ saveTabs(tab);
2649
+ return;
2650
+ }
2651
+ var tabs2 = [...favoriteTabs];
2652
+ const index = tabs2.findIndex((item) => item.id === tab.id);
2653
+ if (operator?.add) {
2654
+ tabs2.push(tab);
2655
+ } else if (operator?.update) {
2656
+ if (index !== -1) {
2657
+ tabs2[index] = tab;
2658
+ }
2659
+ } else if (operator?.delete) {
2660
+ if (index !== -1) {
2661
+ tabs2.splice(index, 1);
2662
+ }
2663
+ }
2664
+ saveTabs(tabs2);
2665
+ };
2666
+ const setRecentData = (symbol) => {
2667
+ const curData = [...recent];
2668
+ const index = curData.findIndex((item) => item.name == symbol.symbol);
2669
+ if (index !== -1) {
2670
+ curData.splice(index, 1);
2671
+ }
2672
+ curData.unshift({ name: symbol.symbol });
2673
+ configStore.set(marketsKey, {
2674
+ ...configStore.getOr(marketsKey, {}),
2675
+ "recent": curData
2676
+ });
2677
+ setRecent(curData);
2678
+ };
2679
+ const setFavoritesData = (symbol, tab, remove = false) => {
2680
+ const curData = [...favorites];
2681
+ const index = curData.findIndex((item) => item.name == symbol.symbol);
2682
+ if (index === -1) {
2683
+ if (Array.isArray(tab)) {
2684
+ if (tab.length > 0) {
2685
+ curData.unshift({ name: symbol.symbol, tabs: tab });
2686
+ }
2687
+ } else {
2688
+ if (!remove) {
2689
+ curData.unshift({ name: symbol.symbol, tabs: [tab] });
1959
2690
  }
1960
- return data;
1961
2691
  }
1962
- return item;
2692
+ } else {
2693
+ const favorite = curData[index];
2694
+ if (Array.isArray(tab)) {
2695
+ if (tab.length === 0) {
2696
+ curData.splice(index, 1);
2697
+ } else {
2698
+ curData[index] = { ...favorite, tabs: tab };
2699
+ }
2700
+ } else {
2701
+ if (remove) {
2702
+ const tabs2 = favorite.tabs.filter((tab2) => tab2.id != tab2.id);
2703
+ if (tabs2.length === 0) {
2704
+ curData.splice(index, 1);
2705
+ } else {
2706
+ curData[index] = { ...favorite, tabs: tabs2 };
2707
+ }
2708
+ } else {
2709
+ const tabs2 = favorite.tabs;
2710
+ tabs2.push(tab);
2711
+ curData[index] = { ...favorite, tabs: tabs2 };
2712
+ }
2713
+ }
2714
+ }
2715
+ configStore.set(marketsKey, {
2716
+ ...configStore.getOr(marketsKey, {}),
2717
+ "favorites": curData
1963
2718
  });
1964
- }, [futures, tickers]);
1965
- return { data: value };
2719
+ setFavorites(() => curData);
2720
+ };
2721
+ const getData = (type2) => {
2722
+ const localData = type2 === 0 /* FAVORITES */ ? [...favorites] : [...recent];
2723
+ const keys = localData.map((item) => item.name);
2724
+ const filter = type2 == 2 /* ALL */ ? data : data?.filter((item) => keys.includes(item.symbol));
2725
+ const favoritesData = [...favorites];
2726
+ const favoriteKeys = favoritesData.map((item) => item.name);
2727
+ if (filter) {
2728
+ for (let index = 0; index < filter.length; index++) {
2729
+ const element = filter[index];
2730
+ const isFavorite = type2 == 0 /* FAVORITES */ ? true : favoriteKeys.includes(element.symbol);
2731
+ const fIndex = favoritesData.findIndex((item) => item.name === element.symbol);
2732
+ const tabs2 = fIndex === -1 ? [] : favoritesData[fIndex].tabs;
2733
+ let imr = void 0;
2734
+ if (publicInfo) {
2735
+ imr = publicInfo?.[element.symbol]("base_imr");
2736
+ }
2737
+ filter[index] = {
2738
+ ...filter[index],
2739
+ // @ts-ignore
2740
+ isFavorite,
2741
+ tabs: tabs2,
2742
+ leverage: imr ? 1 / imr : void 0
2743
+ };
2744
+ }
2745
+ }
2746
+ return filter;
2747
+ };
2748
+ const addToHistory = (symbol) => {
2749
+ setRecentData(symbol);
2750
+ };
2751
+ const updateSymbolFavoriteState = (symbol, tab, del = false) => {
2752
+ setFavoritesData(symbol, tab, del);
2753
+ };
2754
+ const markets = getData(type);
2755
+ const pinToTop = (symbol) => {
2756
+ const index = favorites.findIndex((item) => item.name === symbol.symbol);
2757
+ if (index !== -1) {
2758
+ const element = favorites[index];
2759
+ const list = [...favorites];
2760
+ list.splice(index, 1);
2761
+ list.unshift(element);
2762
+ configStore.set(marketsKey, {
2763
+ ...configStore.getOr(marketsKey, {}),
2764
+ "favorites": list
2765
+ });
2766
+ setFavorites(list);
2767
+ }
2768
+ };
2769
+ const tabs = useMemo(() => {
2770
+ return favoriteTabs;
2771
+ }, [favoriteTabs]);
2772
+ const getLastSelFavTab = () => {
2773
+ const curData = configStore.get(marketsKey)["lastSelectedFavoriteTab"];
2774
+ return curData || { name: "Popular", id: 1 };
2775
+ };
2776
+ const updateSelectedFavoriteTab = (tab) => {
2777
+ configStore.set(marketsKey, {
2778
+ ...configStore.getOr(marketsKey, {}),
2779
+ lastSelectedFavoriteTab: tab
2780
+ });
2781
+ };
2782
+ return [
2783
+ markets || [],
2784
+ {
2785
+ favoriteTabs: tabs,
2786
+ favorites,
2787
+ recent,
2788
+ addToHistory,
2789
+ // updateFavoriteTabs("tab", operator: {add/update/delete})
2790
+ updateFavoriteTabs,
2791
+ updateSymbolFavoriteState,
2792
+ pinToTop,
2793
+ getLastSelFavTab,
2794
+ updateSelectedFavoriteTab
2795
+ }
2796
+ ];
1966
2797
  };
1967
2798
  var useLeverage = () => {
1968
2799
  const { data, mutate: mutate2 } = usePrivateQuery("/v1/client/info");
@@ -2083,7 +2914,7 @@ var useMarketTradeStream = (symbol, options = {}) => {
2083
2914
  return { data: trades, isLoading };
2084
2915
  };
2085
2916
  var useMarginRatio = () => {
2086
- const [{ rows }] = usePositionStream();
2917
+ const [{ rows, aggregated }] = usePositionStream();
2087
2918
  const { data: markPrices } = useMarkPricesStream();
2088
2919
  const { totalCollateral } = useCollateral();
2089
2920
  const marginRatio = useMemo(() => {
@@ -2100,7 +2931,20 @@ var useMarginRatio = () => {
2100
2931
  const currentLeverage = useMemo(() => {
2101
2932
  return account.currentLeverage(marginRatio);
2102
2933
  }, [marginRatio]);
2103
- return { marginRatio, currentLeverage };
2934
+ const mmr = useMemo(() => {
2935
+ if (!rows || rows.length <= 0)
2936
+ return null;
2937
+ let positionsMM = zero;
2938
+ for (let index = 0; index < rows.length; index++) {
2939
+ const item = rows[index];
2940
+ positionsMM = positionsMM.add(item.mm);
2941
+ }
2942
+ return account.MMR({
2943
+ positionsMMR: positionsMM.toNumber(),
2944
+ positionsNotional: aggregated.notional
2945
+ });
2946
+ }, [rows, aggregated]);
2947
+ return { marginRatio, currentLeverage, mmr };
2104
2948
  };
2105
2949
 
2106
2950
  // src/woo/constants.ts
@@ -3116,53 +3960,41 @@ var woofiDexCrossChainRouterAbi = [
3116
3960
  ];
3117
3961
  var nativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
3118
3962
  var isNativeTokenChecker = (address) => address === nativeTokenAddress;
3119
-
3120
- // src/orderly/useChains.ts
3121
3963
  var useChains = (networkId, options = {}) => {
3122
- const { filter, pick: pick3, crossEnabled, wooSwapEnabled, ...swrOptions } = options;
3123
- const { configStore, networkId: networkEnv } = useContext(OrderlyContext);
3964
+ const { pick: pick3, crossEnabled, wooSwapEnabled, ...swrOptions } = options;
3965
+ const { configStore } = useContext(OrderlyContext);
3124
3966
  const field = options?.pick;
3967
+ const filterFun = useRef(options?.filter);
3968
+ filterFun.current = options?.filter;
3125
3969
  const map = useRef(
3126
3970
  /* @__PURE__ */ new Map()
3127
3971
  );
3128
- const { data, error: swapSupportError } = useSWR(
3972
+ const commonSwrOpts = {
3973
+ revalidateIfStale: false,
3974
+ revalidateOnFocus: false,
3975
+ revalidateOnReconnect: false,
3976
+ // If false, undefined data gets cached against the key.
3977
+ revalidateOnMount: true,
3978
+ // dont duplicate a request w/ same key for 1hr
3979
+ dedupingInterval: 36e5,
3980
+ ...swrOptions
3981
+ };
3982
+ const { data: wooSupportData, error: swapSupportError } = useSWR(
3129
3983
  () => wooSwapEnabled ? `${configStore.get("swapSupportApiUrl")}/swap_support` : null,
3130
- // `${configStore.get("swapSupportApiUrl")}/swap_support`,
3131
3984
  (url) => fetch(url).then((res) => res.json()),
3132
- {
3133
- revalidateIfStale: false,
3134
- revalidateOnFocus: false,
3135
- revalidateOnReconnect: false,
3136
- revalidateOnMount: true,
3137
- // If false, undefined data gets cached against the key.
3138
- dedupingInterval: 36e5,
3139
- // dont duplicate a request w/ same key for 1hr
3140
- ...swrOptions
3141
- }
3985
+ { ...commonSwrOpts, ...swrOptions }
3142
3986
  );
3143
3987
  const { data: chainInfos, error: chainInfoErr } = useQuery(
3144
3988
  "/v1/public/chain_info",
3145
3989
  {
3146
- revalidateIfStale: false,
3147
- revalidateOnFocus: false,
3148
- revalidateOnReconnect: false,
3149
- revalidateOnMount: true,
3150
- dedupingInterval: 36e5,
3151
- formatter: (data2) => data2.rows
3990
+ ...commonSwrOpts,
3991
+ formatter: (data) => data.rows
3152
3992
  }
3153
3993
  );
3154
3994
  const { data: orderlyChains, error: tokenError } = useQuery(
3155
- // wooSwapEnabled ? "/v1/public/token" :
3156
3995
  "https://api-evm.orderly.org/v1/public/token",
3157
- {
3158
- revalidateIfStale: false,
3159
- revalidateOnFocus: false,
3160
- revalidateOnReconnect: false,
3161
- revalidateOnMount: true,
3162
- dedupingInterval: 36e5
3163
- }
3996
+ { ...commonSwrOpts }
3164
3997
  );
3165
- configStore.get("apiBaseUrl");
3166
3998
  const chains = useMemo(() => {
3167
3999
  let orderlyChainsArr = [];
3168
4000
  const orderlyChainIds = /* @__PURE__ */ new Set();
@@ -3177,6 +4009,7 @@ var useChains = (networkId, options = {}) => {
3177
4009
  // "public_rpc_url": "https://arb1.arbitrum.io/rpc",
3178
4010
  chain_id: chainId,
3179
4011
  withdrawal_fee: chain.withdrawal_fee,
4012
+ cross_chain_withdrawal_fee: chain.cross_chain_withdrawal_fee,
3180
4013
  bridgeless: true
3181
4014
  },
3182
4015
  token_infos: [
@@ -3187,13 +4020,13 @@ var useChains = (networkId, options = {}) => {
3187
4020
  }
3188
4021
  ]
3189
4022
  };
3190
- if (typeof options?.filter === "function") {
3191
- if (!options.filter(_chain))
4023
+ if (typeof filterFun.current === "function") {
4024
+ if (!filterFun.current(_chain))
3192
4025
  return;
3193
4026
  }
3194
- if (_chain.chain_id === 421613) {
3195
- const index = testnetArr.findIndex(
3196
- (item2) => item2.network_infos.chain_id === 421613
4027
+ if (isTestnet(_chain.chain_id)) {
4028
+ const index = testnetArr?.findIndex(
4029
+ (item2) => isTestnet(item2.network_infos.chain_id)
3197
4030
  );
3198
4031
  if (index > -1) {
3199
4032
  testnetArr[index] = _chain;
@@ -3203,59 +4036,16 @@ var useChains = (networkId, options = {}) => {
3203
4036
  orderlyChainsArr.push(_chain);
3204
4037
  });
3205
4038
  });
3206
- let testnetArr = [
3207
- //@ts-ignore
3208
- {
3209
- network_infos: {
3210
- name: "Arbitrum Goerli",
3211
- shortName: "Arbitrum Goerli",
3212
- public_rpc_url: "https://goerli-rollup.arbitrum.io/rpc",
3213
- chain_id: 421613,
3214
- currency_symbol: "ETH",
3215
- bridge_enable: true,
3216
- mainnet: false,
3217
- explorer_base_url: "https://goerli.arbiscan.io/",
3218
- est_txn_mins: null,
3219
- woofi_dex_cross_chain_router: "",
3220
- woofi_dex_depositor: ""
3221
- },
3222
- token_infos: [
3223
- {
3224
- symbol: "USDC",
3225
- address: "0xfd064A18f3BF249cf1f87FC203E90D8f650f2d63",
3226
- decimals: 6,
3227
- swap_enable: false,
3228
- woofi_dex_precision: 2
3229
- }
3230
- ]
3231
- }
3232
- ];
3233
- if (networkEnv === "testnet") {
3234
- const opGoerli = {
3235
- network_infos: {
3236
- name: "Optimism Goerli",
3237
- shortName: "Optimism Goerli",
3238
- public_rpc_url: "https://optimism-goerli.gateway.tenderly.co",
3239
- chain_id: 420,
3240
- currency_symbol: "ETH",
3241
- bridge_enable: true,
3242
- mainnet: false,
3243
- explorer_base_url: "https://goerli-optimism.etherscan.io",
3244
- est_txn_mins: null,
3245
- woofi_dex_cross_chain_router: "",
3246
- woofi_dex_depositor: ""
3247
- }
3248
- };
3249
- testnetArr.push(opGoerli);
3250
- map.current.set(420, opGoerli);
3251
- }
4039
+ let testnetArr = [...TestnetChains];
4040
+ testnetArr.forEach((chain) => {
4041
+ map.current.set(chain.network_infos?.chain_id, chain);
4042
+ });
3252
4043
  let mainnetArr = [];
3253
- map.current.set(421613, testnetArr[0]);
3254
4044
  if (wooSwapEnabled) {
3255
- if (!data || !data.data)
3256
- return data;
3257
- Object.keys(data.data).forEach((key) => {
3258
- const chain = data.data[key];
4045
+ if (!wooSupportData || !wooSupportData.data)
4046
+ return wooSupportData;
4047
+ Object.keys(wooSupportData.data).forEach((key) => {
4048
+ const chain = wooSupportData.data[key];
3259
4049
  const item = mergeDeepRight(chain, {
3260
4050
  name: key,
3261
4051
  network_infos: {
@@ -3269,8 +4059,8 @@ var useChains = (networkId, options = {}) => {
3269
4059
  if (item.token_infos?.length === 0)
3270
4060
  return;
3271
4061
  map.current.set(item.network_infos.chain_id, item);
3272
- if (typeof options?.filter === "function") {
3273
- if (!options.filter(item))
4062
+ if (typeof filterFun.current === "function") {
4063
+ if (!filterFun.current(item))
3274
4064
  return;
3275
4065
  }
3276
4066
  if (item.network_infos.mainnet) {
@@ -3308,24 +4098,16 @@ var useChains = (networkId, options = {}) => {
3308
4098
  };
3309
4099
  }
3310
4100
  map.current.set(_chain.network_infos.chain_id, _chain);
3311
- if (_chain.network_infos.chain_id === 421613) {
3312
- const index = testnetArr.findIndex(
3313
- (item) => item.network_infos.chain_id === 421613
3314
- );
3315
- if (index > -1) {
3316
- testnetArr[index] = _chain;
3317
- }
3318
- }
3319
- if (_chain.network_infos.chain_id === 420) {
4101
+ if (isTestnet(_chain.network_infos.chain_id)) {
3320
4102
  const index = testnetArr.findIndex(
3321
- (item) => item.network_infos.chain_id === 420
4103
+ (item) => isTestnet(item.network_infos.chain_id)
3322
4104
  );
3323
4105
  if (index > -1) {
3324
4106
  testnetArr[index] = _chain;
3325
4107
  }
3326
4108
  }
3327
- if (typeof options?.filter === "function") {
3328
- if (!options.filter(_chain))
4109
+ if (typeof filterFun.current === "function") {
4110
+ if (!filterFun.current(_chain))
3329
4111
  return;
3330
4112
  }
3331
4113
  mainnetArr.push(_chain);
@@ -3352,10 +4134,9 @@ var useChains = (networkId, options = {}) => {
3352
4134
  mainnet: mainnetArr
3353
4135
  };
3354
4136
  }, [
3355
- data,
4137
+ wooSupportData,
3356
4138
  networkId,
3357
4139
  field,
3358
- options,
3359
4140
  orderlyChains,
3360
4141
  wooSwapEnabled,
3361
4142
  chainInfos
@@ -3422,7 +4203,21 @@ var useWithdraw = () => {
3422
4203
  const maxAmount = useMemo(() => {
3423
4204
  return freeCollateral;
3424
4205
  }, [freeCollateral]);
3425
- return { withdraw, isLoading, maxAmount, availableBalance, unsettledPnL };
4206
+ const availableWithdraw = useMemo(() => {
4207
+ if (unsettledPnL < 0) {
4208
+ return freeCollateral;
4209
+ } else {
4210
+ return freeCollateral - unsettledPnL;
4211
+ }
4212
+ }, [freeCollateral, unsettledPnL]);
4213
+ return {
4214
+ withdraw,
4215
+ isLoading,
4216
+ maxAmount,
4217
+ availableBalance,
4218
+ availableWithdraw,
4219
+ unsettledPnL
4220
+ };
3426
4221
  };
3427
4222
  var useDeposit = (options) => {
3428
4223
  const { enableSwapDeposit } = useContext(OrderlyContext);
@@ -3432,12 +4227,15 @@ var useDeposit = (options) => {
3432
4227
  const [_, { findByChainId }] = useChains(void 0, {
3433
4228
  wooSwapEnabled: enableSwapDeposit
3434
4229
  });
4230
+ const [quantity, setQuantity] = useState("");
4231
+ const [depositFee, setDepositFee] = useState(0n);
4232
+ const [depositFeeRevalidating, setDepositFeeRevalidating] = useState(false);
3435
4233
  const [balance, setBalance] = useState("0");
3436
4234
  const [allowance, setAllowance] = useState("0");
3437
4235
  const { account: account5, state } = useAccount();
3438
4236
  const prevAddress = useRef();
3439
4237
  const getBalanceListener = useRef();
3440
- const dst = useMemo(() => {
4238
+ const targetChain = useMemo(() => {
3441
4239
  let chain;
3442
4240
  if (networkId === "testnet") {
3443
4241
  chain = findByChainId(ARBITRUM_TESTNET_CHAINID);
@@ -3447,21 +4245,23 @@ var useDeposit = (options) => {
3447
4245
  chain = findByChainId(ARBITRUM_MAINNET_CHAINID);
3448
4246
  }
3449
4247
  }
3450
- const USDC = chain?.token_infos.find(
3451
- (token) => token.symbol === "USDC"
3452
- );
3453
- if (!chain) {
4248
+ return chain;
4249
+ }, [networkId, findByChainId, options?.srcChainId]);
4250
+ const dst = useMemo(() => {
4251
+ if (!targetChain) {
3454
4252
  throw new Error("dst chain not found");
3455
4253
  }
4254
+ const USDC = targetChain?.token_infos.find(
4255
+ (token) => token.symbol === "USDC"
4256
+ );
3456
4257
  return {
3457
4258
  symbol: "USDC",
3458
4259
  address: USDC?.address,
3459
4260
  decimals: USDC?.decimals,
3460
- chainId: chain.network_infos.chain_id,
3461
- network: chain.network_infos.shortName
3462
- // chainId: 42161,
4261
+ chainId: targetChain.network_infos.chain_id,
4262
+ network: targetChain.network_infos.shortName
3463
4263
  };
3464
- }, [networkId, findByChainId, options?.srcChainId]);
4264
+ }, [targetChain]);
3465
4265
  const isNativeToken = useMemo(
3466
4266
  () => isNativeTokenChecker(options?.address || ""),
3467
4267
  [options?.address]
@@ -3488,7 +4288,6 @@ var useDeposit = (options) => {
3488
4288
  const balance2 = await fetchBalanceHandler(address, decimals);
3489
4289
  setBalance(() => balance2);
3490
4290
  } catch (e) {
3491
- console.warn("----- refresh balance error -----", e);
3492
4291
  setBalance(() => "0");
3493
4292
  }
3494
4293
  },
@@ -3538,18 +4337,30 @@ var useDeposit = (options) => {
3538
4337
  }
3539
4338
  }
3540
4339
  }, [options, dst]);
4340
+ const queryBalance = useDebouncedCallback(
4341
+ (tokenAddress, decimals) => {
4342
+ fetchBalance(options?.address, options?.decimals).finally(() => {
4343
+ setBalanceRevalidating(false);
4344
+ });
4345
+ },
4346
+ 100
4347
+ );
4348
+ const queryAllowance = useDebouncedCallback(
4349
+ (tokenAddress, vaultAddress) => {
4350
+ getAllowance(tokenAddress, vaultAddress);
4351
+ },
4352
+ 100
4353
+ );
3541
4354
  useEffect(() => {
3542
4355
  if (state.status < AccountStatusEnum.Connected)
3543
4356
  return;
3544
4357
  setBalanceRevalidating(true);
3545
- fetchBalance(options?.address, options?.decimals).finally(() => {
3546
- setBalanceRevalidating(false);
3547
- });
4358
+ queryBalance(options?.address, options?.decimals);
3548
4359
  if (dst.chainId !== options?.srcChainId) {
3549
- getAllowance(options?.address, options?.crossChainRouteAddress);
4360
+ queryAllowance(options?.address, options?.crossChainRouteAddress);
3550
4361
  } else {
3551
4362
  if (dst.symbol !== options?.srcToken) {
3552
- getAllowance(options?.address, options?.depositorAddress);
4363
+ queryAllowance(options?.address, options?.depositorAddress);
3553
4364
  } else {
3554
4365
  getAllowanceByDefaultAddress(options?.address);
3555
4366
  }
@@ -3566,47 +4377,72 @@ var useDeposit = (options) => {
3566
4377
  dst.symbol
3567
4378
  ]);
3568
4379
  const approve = useCallback(
3569
- (amount) => {
4380
+ async (amount) => {
3570
4381
  if (!options?.address) {
3571
4382
  throw new Error("address is required");
3572
4383
  }
3573
4384
  const vaultAddress = getVaultAddress();
3574
4385
  return account5.assetsManager.approve(options.address, amount, vaultAddress).then((result) => {
3575
4386
  if (typeof amount !== "undefined") {
3576
- setAllowance((value) => new Decimal(value).add(amount).toString());
4387
+ setAllowance(
4388
+ (value) => new Decimal(value).add(amount || MaxUint256.toString()).toString()
4389
+ );
3577
4390
  }
3578
4391
  return result;
3579
4392
  });
3580
4393
  },
3581
4394
  [account5, getAllowance, options?.address]
3582
4395
  );
3583
- const getDepositFee = useCallback(
3584
- async (amount, chain) => {
3585
- return account5.assetsManager.getDepositFee(amount, chain);
3586
- },
3587
- [account5]
3588
- );
3589
- const deposit = useCallback(
3590
- (amount, fee) => {
3591
- return account5.assetsManager.deposit(amount, fee).then((res) => {
3592
- setAllowance((value) => new Decimal(value).sub(amount).toString());
3593
- setBalance((value) => new Decimal(value).sub(amount).toString());
3594
- return res;
3595
- });
3596
- },
3597
- [account5, fetchBalance, getAllowance]
3598
- );
4396
+ const deposit = useCallback(async () => {
4397
+ return account5.assetsManager.deposit(quantity, depositFee).then((res) => {
4398
+ setAllowance((value) => new Decimal(value).sub(quantity).toString());
4399
+ setBalance((value) => new Decimal(value).sub(quantity).toString());
4400
+ return res;
4401
+ });
4402
+ }, [account5, fetchBalance, getAllowance, quantity, depositFee]);
3599
4403
  const loopGetBalance = async () => {
3600
4404
  getBalanceListener.current && clearTimeout(getBalanceListener.current);
3601
4405
  getBalanceListener.current = setTimeout(async () => {
3602
- const balance2 = await fetchBalanceHandler(
3603
- options?.address,
3604
- options?.decimals
3605
- );
3606
- setBalance(balance2);
3607
- loopGetBalance();
4406
+ try {
4407
+ const balance2 = await fetchBalanceHandler(
4408
+ options?.address,
4409
+ options?.decimals
4410
+ );
4411
+ setBalance(balance2);
4412
+ loopGetBalance();
4413
+ } catch (err) {
4414
+ }
3608
4415
  }, 3e3);
3609
4416
  };
4417
+ const getDepositFee = useCallback(
4418
+ async (quantity2) => {
4419
+ return account5.assetsManager.getDepositFee(
4420
+ quantity2,
4421
+ targetChain?.network_infos
4422
+ );
4423
+ },
4424
+ [account5, targetChain]
4425
+ );
4426
+ const enquiryDepositFee = useCallback(() => {
4427
+ if (isNaN(Number(quantity)) || !quantity) {
4428
+ setDepositFee(0n);
4429
+ setDepositFeeRevalidating(false);
4430
+ return;
4431
+ }
4432
+ setDepositFeeRevalidating(true);
4433
+ getDepositFee(quantity).then((res = 0n) => {
4434
+ const fee = BigInt(
4435
+ new Decimal(res.toString()).mul(DEPOSIT_FEE_RATE).toFixed(0, Decimal.ROUND_UP).toString()
4436
+ );
4437
+ setDepositFee(fee);
4438
+ }).catch((error) => {
4439
+ }).finally(() => {
4440
+ setDepositFeeRevalidating(false);
4441
+ });
4442
+ }, [quantity]);
4443
+ useEffect(() => {
4444
+ enquiryDepositFee();
4445
+ }, [quantity]);
3610
4446
  useEffect(() => {
3611
4447
  if (!options?.address) {
3612
4448
  return;
@@ -3617,17 +4453,25 @@ var useDeposit = (options) => {
3617
4453
  };
3618
4454
  }, [options?.address, options?.decimals]);
3619
4455
  return {
4456
+ /** orderly support chain dst */
3620
4457
  dst,
3621
4458
  balance,
3622
4459
  allowance,
3623
4460
  isNativeToken,
3624
4461
  balanceRevalidating,
3625
4462
  allowanceRevalidating,
4463
+ /** input quantiy */
4464
+ quantity,
4465
+ /** orderly deposit fee, unit: wei */
4466
+ depositFee,
4467
+ /** enquiring depositFee status on chain */
4468
+ depositFeeRevalidating,
3626
4469
  approve,
3627
4470
  deposit,
3628
- getDepositFee,
3629
4471
  fetchBalances,
3630
- fetchBalance: fetchBalanceHandler
4472
+ fetchBalance: fetchBalanceHandler,
4473
+ /** set input quantity */
4474
+ setQuantity
3631
4475
  };
3632
4476
  };
3633
4477
  var useWalletSubscription = (options) => {
@@ -3670,6 +4514,32 @@ var useSettleSubscription = (options) => {
3670
4514
  return () => unsubscribe();
3671
4515
  });
3672
4516
  };
4517
+ var useSymbolPriceRange = (symbol, side, price) => {
4518
+ const config = useSymbolsInfo();
4519
+ const priceRange = config?.[symbol]("price_range");
4520
+ const priceScrope = config?.[symbol]("price_scope");
4521
+ const { data: prices } = useMarkPricesStream();
4522
+ const markPrice = price || prices?.[symbol];
4523
+ const range = useMemo(() => {
4524
+ if (config === void 0 || markPrice === void 0) {
4525
+ return void 0;
4526
+ }
4527
+ if (priceRange === void 0 || Number.isNaN(markPrice)) {
4528
+ return void 0;
4529
+ }
4530
+ if (side === "BUY") {
4531
+ return {
4532
+ max: new Decimal(markPrice).mul(1 + priceRange).toNumber(),
4533
+ min: new Decimal(markPrice).mul(1 - priceScrope).toNumber()
4534
+ };
4535
+ }
4536
+ return {
4537
+ max: new Decimal(markPrice).mul(1 + priceScrope).toNumber(),
4538
+ min: new Decimal(markPrice).mul(1 - priceRange).toNumber()
4539
+ };
4540
+ }, [symbol, side, priceRange, markPrice]);
4541
+ return range;
4542
+ };
3673
4543
  function useMediaQuery(query) {
3674
4544
  const getMatches = (query2) => {
3675
4545
  if (typeof window !== "undefined") {
@@ -4301,6 +5171,6 @@ var useSwap = () => {
4301
5171
  };
4302
5172
  };
4303
5173
 
4304
- export { OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMediaQuery, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery, version_default as version };
5174
+ export { MarketsType, OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarkets, useMarketsStream, useMaxQty, useMediaQuery, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolPriceRange, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery, version_default as version };
4305
5175
  //# sourceMappingURL=out.js.map
4306
5176
  //# sourceMappingURL=index.mjs.map