@orderly.network/hooks 0.0.84 → 0.0.86

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
@@ -7,7 +7,7 @@ import useConstant from 'use-constant';
7
7
  export { default as useConstant } from 'use-constant';
8
8
  import { SimpleDI, Account, EventEmitter, utils } from '@orderly.network/core';
9
9
  import { AccountStatusEnum, OrderSide, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, OrderStatus as OrderStatus$1, WS_WalletStatusEnum, OrderType } from '@orderly.network/types';
10
- import { Decimal, zero, getPrecisionByNumber, timeConvertString, capitalizeString, transSymbolformString } from '@orderly.network/utils';
10
+ import { Decimal, zero, getPrecisionByNumber, timeConvertString } from '@orderly.network/utils';
11
11
  import useSWRSubscription from 'swr/subscription';
12
12
  import { pathOr, propOr, compose, head, prop, mergeDeepRight, pick } from 'ramda';
13
13
  import { positions, account, order } from '@orderly.network/futures';
@@ -746,6 +746,7 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
746
746
  const eventEmitter = useEventEmitter();
747
747
  useEffect(() => {
748
748
  setIsLoading(true);
749
+ let ignore = false;
749
750
  ws.onceSubscribe(
750
751
  {
751
752
  event: "request",
@@ -757,6 +758,8 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
757
758
  },
758
759
  {
759
760
  onMessage: (message) => {
761
+ if (ignore)
762
+ return;
760
763
  if (!!message) {
761
764
  const reduceOrderbookData = reduceOrderbook(depth, level, message);
762
765
  setRequestData(reduceOrderbookData);
@@ -768,6 +771,7 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
768
771
  );
769
772
  return () => {
770
773
  setRequestData(null);
774
+ ignore = true;
771
775
  setData(INIT_DATA);
772
776
  };
773
777
  }, [symbol, depth]);
@@ -1028,7 +1032,6 @@ var parseHolding = (holding, markPrices) => {
1028
1032
  };
1029
1033
  var usePositionStream = (symbol, options) => {
1030
1034
  const symbolInfo = useSymbolsInfo();
1031
- useEventEmitter();
1032
1035
  const { data: accountInfo } = usePrivateQuery("/v1/client/info");
1033
1036
  const { data: holding } = usePrivateQuery(
1034
1037
  "/v1/client/holding",
@@ -1068,12 +1071,27 @@ var usePositionStream = (symbol, options) => {
1068
1071
  item.symbol,
1069
1072
  markPrices
1070
1073
  );
1074
+ const info = symbolInfo?.[item.symbol];
1071
1075
  const notional = positions.notional(item.position_qty, price);
1072
1076
  const unrealPnl = positions.unrealizedPnL({
1073
1077
  qty: item.position_qty,
1074
1078
  openPrice: item.average_open_price,
1075
1079
  markPrice: price
1076
1080
  });
1081
+ const imr = account.IMR({
1082
+ maxLeverage: accountInfo.max_leverage,
1083
+ baseIMR: info("base_imr"),
1084
+ IMR_Factor: accountInfo.imr_factor[item.symbol],
1085
+ positionNotional: notional,
1086
+ ordersNotional: 0,
1087
+ IMR_factor_power: 4 / 5
1088
+ });
1089
+ const unrealPnlROI = positions.unrealizedPnLROI({
1090
+ positionQty: item.position_qty,
1091
+ openPrice: item.average_open_price,
1092
+ IMR: imr,
1093
+ unrealizedPnL: unrealPnl
1094
+ });
1077
1095
  const unsettlementPnL = positions.unsettlementPnL({
1078
1096
  positionQty: item.position_qty,
1079
1097
  markPrice: price,
@@ -1093,7 +1111,8 @@ var usePositionStream = (symbol, options) => {
1093
1111
  mm: 0,
1094
1112
  notional,
1095
1113
  unsettlement_pnl: unsettlementPnL,
1096
- unrealized_pnl: unrealPnl
1114
+ unrealized_pnl: unrealPnl,
1115
+ unsettled_pnl_ROI: unrealPnlROI
1097
1116
  };
1098
1117
  });
1099
1118
  return [
@@ -1448,7 +1467,7 @@ var LimitOrderCreator = class extends BaseOrderCreator {
1448
1467
  }
1449
1468
  validate(values, config) {
1450
1469
  return this.baseValidate(values, config).then((errors) => {
1451
- const { order_price } = values;
1470
+ const { order_price, side } = values;
1452
1471
  if (!order_price) {
1453
1472
  errors.order_price = {
1454
1473
  type: "required",
@@ -1460,20 +1479,21 @@ var LimitOrderCreator = class extends BaseOrderCreator {
1460
1479
  const { price_range } = symbol;
1461
1480
  const maxPriceNumber = maxPrice(config.markPrice, price_range);
1462
1481
  const minPriceNumber = minPrice(config.markPrice, price_range);
1463
- if (price.lt(minPriceNumber)) {
1464
- errors.order_price = {
1465
- type: "min",
1466
- message: `price must be greater than ${new Decimal(
1467
- minPriceNumber
1468
- ).todp(symbol.quote_dp)}`
1469
- };
1470
- } else if (price.gt(maxPriceNumber)) {
1482
+ console.log(`side: ${side} value:`, values);
1483
+ if (side === "BUY" && price.gt(maxPriceNumber)) {
1471
1484
  errors.order_price = {
1472
1485
  type: "max",
1473
1486
  message: `price must be less than ${new Decimal(
1474
1487
  maxPriceNumber
1475
1488
  ).todp(symbol.quote_dp)}`
1476
1489
  };
1490
+ } else if (side === "SELL" && price.lt(minPriceNumber)) {
1491
+ errors.order_price = {
1492
+ type: "min",
1493
+ message: `price must be greater than ${new Decimal(
1494
+ minPriceNumber
1495
+ ).todp(symbol.quote_dp)}`
1496
+ };
1477
1497
  }
1478
1498
  }
1479
1499
  return errors;
@@ -3540,46 +3560,6 @@ var useExecutionReport = (options) => {
3540
3560
  });
3541
3561
  return data;
3542
3562
  };
3543
- function parseExecutionReportToToastMsg(data, symbolsInfo) {
3544
- const { symbol } = data;
3545
- const getSymbolInfo = symbolsInfo[symbol];
3546
- const baseTick = getSymbolInfo("base_dp");
3547
- const { status } = data;
3548
- const { side } = data;
3549
- const { quantity } = data;
3550
- const displaySide = capitalizeString(side);
3551
- const displaySymbol = transSymbolformString(symbol);
3552
- const displayQuantity = baseTick === void 0 ? quantity : quantity.toFixed(baseTick);
3553
- let msg = "";
3554
- let title = "";
3555
- switch (status) {
3556
- case "NEW":
3557
- title = "Order opened";
3558
- msg = `Order opened ${displaySide} ${displaySymbol} ${displayQuantity}`;
3559
- break;
3560
- case "FILLED":
3561
- case "PARTIAL_FILLED":
3562
- const { totalExecutedQuantity } = data;
3563
- const displayTotalExecutedQuantity = baseTick === void 0 ? totalExecutedQuantity : totalExecutedQuantity.toFixed(baseTick);
3564
- title = "Order filled";
3565
- msg = `Order filled ${displaySide} ${displaySymbol} ${displayTotalExecutedQuantity} / ${displayQuantity}`;
3566
- break;
3567
- case "CANCELLED":
3568
- title = "Order cancelled";
3569
- msg = `Order cancelled ${displaySide} ${displaySymbol} ${displayQuantity}`;
3570
- break;
3571
- case "REJECTED":
3572
- title = "Order rejected";
3573
- msg = `Order rejected ${displaySide} ${displaySymbol} ${displayQuantity}`;
3574
- break;
3575
- }
3576
- if (msg.length === 0)
3577
- return null;
3578
- return {
3579
- title,
3580
- msg
3581
- };
3582
- }
3583
3563
 
3584
3564
  // src/apis/index.ts
3585
3565
  var apis_exports = {};
@@ -4209,6 +4189,6 @@ var useSwap = () => {
4209
4189
  };
4210
4190
  };
4211
4191
 
4212
- export { DataSourceProvider, OrderStatus, OrderlyContext, OrderlyProvider, apis_exports as apis, parseExecutionReportToToastMsg, useAccount, useAccountInfo, useAccountInstance, useAppState, useBalance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useExecutionReport, useFetures, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOpenInterest, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateQuery, useQuery, useRunOnce, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useTokenInfo, useTopicObserve, useTradingView, useWS, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
4192
+ export { DataSourceProvider, OrderStatus, OrderlyContext, OrderlyProvider, apis_exports as apis, useAccount, useAccountInfo, useAccountInstance, useAppState, useBalance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useExecutionReport, useFetures, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOpenInterest, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateQuery, useQuery, useRunOnce, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useTokenInfo, useTopicObserve, useTradingView, useWS, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
4213
4193
  //# sourceMappingURL=out.js.map
4214
4194
  //# sourceMappingURL=index.mjs.map