@orderly.network/hooks 2.7.3 → 2.7.4-alpha.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
@@ -13,6 +13,7 @@ import { SimpleDI, Account, EventEmitter, DefaultConfigStore, EVENT_NAMES, Local
13
13
  import { debounce } from 'lodash';
14
14
  import useSWRInfinite, { unstable_serialize } from 'swr/infinite';
15
15
  import { pathOr, min, max, omit, prop, pick, compose, head, mergeDeepRight, isNil, propOr, path, lensIndex, over } from 'ramda';
16
+ import { account, order, positions } from '@orderly.network/perp';
16
17
  import * as amplitude from '@amplitude/analytics-browser';
17
18
  import { jsx } from 'react/jsx-runtime';
18
19
  import { DefaultEVMWalletAdapter } from '@orderly.network/default-evm-adapter';
@@ -21,7 +22,6 @@ import { EthersProvider } from '@orderly.network/web3-provider-ethers';
21
22
  import useSWRSubscription from 'swr/subscription';
22
23
  import { create } from 'zustand';
23
24
  import { immer } from 'zustand/middleware/immer';
24
- import { account, order, positions } from '@orderly.network/perp';
25
25
  import { useDebouncedCallback } from 'use-debounce';
26
26
  export * from 'use-debounce';
27
27
  import { produce } from 'immer';
@@ -36,9 +36,9 @@ var __export = (target, all) => {
36
36
  // src/version.ts
37
37
  if (typeof window !== "undefined") {
38
38
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
39
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.7.3";
39
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.7.4-alpha.0";
40
40
  }
41
- var version_default = "2.7.3";
41
+ var version_default = "2.7.4-alpha.0";
42
42
  var fetcher = (url, init2 = {}, queryOptions) => get(url, init2, queryOptions?.formatter);
43
43
  var noCacheConfig = {
44
44
  dedupingInterval: 0,
@@ -401,7 +401,7 @@ var usePrivateQuery = (query, options) => {
401
401
  const { state } = useAccount();
402
402
  const middleware = Array.isArray(options?.use) ? options?.use ?? [] : [];
403
403
  return useSWR__default(
404
- () => state.status >= AccountStatusEnum.EnableTrading || state.status === AccountStatusEnum.EnableTradingWithoutConnected ? [query, state.accountId] : null,
404
+ () => query && (state.status >= AccountStatusEnum.EnableTrading || state.status === AccountStatusEnum.EnableTradingWithoutConnected) ? [query, state.accountId] : null,
405
405
  (url, init2) => {
406
406
  return fetcher(url, init2, { formatter });
407
407
  },
@@ -1174,6 +1174,42 @@ function priceToPnl(inputs, options = {}) {
1174
1174
  }
1175
1175
  return decimal.toNumber();
1176
1176
  }
1177
+ function priceToROI(inputs) {
1178
+ const { values, markPrice, symbolLeverage } = inputs;
1179
+ const orderPrice = values.order_type === OrderType.MARKET ? markPrice : Number(values.order_price);
1180
+ if (symbolLeverage && orderPrice) {
1181
+ let tp_ROI;
1182
+ let sl_ROI;
1183
+ const tpTriggerPrice = Number(values.tp_trigger_price);
1184
+ if (values.tp_pnl && values.tp_trigger_price && tpTriggerPrice > 0) {
1185
+ tp_ROI = order.tpslROI({
1186
+ side: values.side,
1187
+ type: "tp",
1188
+ closePrice: tpTriggerPrice,
1189
+ orderPrice,
1190
+ leverage: symbolLeverage
1191
+ });
1192
+ }
1193
+ const slTriggerPrice = Number(values.sl_trigger_price);
1194
+ if (values.sl_pnl && values.sl_trigger_price && slTriggerPrice > 0) {
1195
+ sl_ROI = order.tpslROI({
1196
+ side: values.side,
1197
+ type: "sl",
1198
+ closePrice: slTriggerPrice,
1199
+ orderPrice,
1200
+ leverage: symbolLeverage
1201
+ });
1202
+ }
1203
+ return {
1204
+ tp_ROI: tp_ROI?.toString(),
1205
+ sl_ROI: sl_ROI?.toString()
1206
+ };
1207
+ }
1208
+ return {
1209
+ tp_ROI: void 0,
1210
+ sl_ROI: void 0
1211
+ };
1212
+ }
1177
1213
  function calcTPSL_ROI(inputs) {
1178
1214
  const qtyNum = Number(inputs.qty);
1179
1215
  const priceNum = Number(inputs.price);
@@ -16826,7 +16862,7 @@ var useOrderStore = (initialOrder) => {
16826
16862
 
16827
16863
  // src/next/useOrderEntry/useOrderEntry.internal.ts
16828
16864
  var useOrderEntryNextInternal = (symbol, options = {}) => {
16829
- const { symbolInfo } = options;
16865
+ const { symbolInfo, symbolLeverage } = options;
16830
16866
  const initialOrder = {
16831
16867
  side: OrderSide.BUY,
16832
16868
  order_type: OrderType.LIMIT,
@@ -16878,21 +16914,16 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
16878
16914
  if ((key === "order_quantity" || key === "order_price") && hasTPSL(newValues)) {
16879
16915
  newValues = calculateTPSL(key, newValues, markPrice, symbolInfo);
16880
16916
  }
16881
- {
16882
- if (newValues.tp_pnl && newValues.order_quantity) {
16883
- newValues.tp_ROI = calcTPSL_ROI({
16884
- qty: newValues.order_quantity,
16885
- price: newValues.order_price || markPrice,
16886
- pnl: newValues.tp_pnl
16887
- });
16888
- }
16889
- if (newValues.sl_pnl && newValues.order_quantity) {
16890
- newValues.sl_ROI = calcTPSL_ROI({
16891
- qty: newValues.order_quantity,
16892
- price: newValues.order_price || markPrice,
16893
- pnl: newValues.sl_pnl
16894
- });
16895
- }
16917
+ const { tp_ROI, sl_ROI } = priceToROI({
16918
+ values: newValues,
16919
+ symbolLeverage,
16920
+ markPrice
16921
+ });
16922
+ if (tp_ROI) {
16923
+ newValues.tp_ROI = tp_ROI;
16924
+ }
16925
+ if (sl_ROI) {
16926
+ newValues.sl_ROI = sl_ROI;
16896
16927
  }
16897
16928
  orderEntryActions.updateOrder(newValues);
16898
16929
  return newValues;
@@ -16982,19 +17013,16 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
16982
17013
  });
16983
17014
  }
16984
17015
  if (hasTPSL(newValues)) {
16985
- if (newValues.tp_pnl && newValues.order_quantity) {
16986
- newValues.tp_ROI = calcTPSL_ROI({
16987
- qty: newValues.order_quantity,
16988
- price: newValues.order_price || markPrice,
16989
- pnl: newValues.tp_pnl
16990
- });
17016
+ const { tp_ROI, sl_ROI } = priceToROI({
17017
+ values: newValues,
17018
+ symbolLeverage,
17019
+ markPrice
17020
+ });
17021
+ if (tp_ROI) {
17022
+ newValues.tp_ROI = tp_ROI;
16991
17023
  }
16992
- if (newValues.sl_pnl && newValues.order_quantity) {
16993
- newValues.sl_ROI = calcTPSL_ROI({
16994
- qty: newValues.order_quantity,
16995
- price: newValues.order_price || markPrice,
16996
- pnl: newValues.sl_pnl
16997
- });
17024
+ if (sl_ROI) {
17025
+ newValues.sl_ROI = sl_ROI;
16998
17026
  }
16999
17027
  }
17000
17028
  orderEntryActions.updateOrder(newValues);
@@ -17056,6 +17084,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
17056
17084
  const symbolConfig = useSymbolsInfo();
17057
17085
  const accountInfo = useAccountInfo2();
17058
17086
  const positions3 = usePositions();
17087
+ const symbolLeverage = useLeverageBySymbol(symbol);
17059
17088
  const symbolInfo = symbolConfig[symbol]();
17060
17089
  const markPrice = actions.getMarkPriceBySymbol(symbol);
17061
17090
  const { orderMetadata } = useOrderlyContext();
@@ -17070,7 +17099,8 @@ var useOrderEntry2 = (symbol, options = {}) => {
17070
17099
  ...orderEntryActions
17071
17100
  } = useOrderEntryNextInternal(symbol, {
17072
17101
  ...options,
17073
- symbolInfo
17102
+ symbolInfo,
17103
+ symbolLeverage
17074
17104
  });
17075
17105
  const [estSlippage, setEstSlippage] = useState(null);
17076
17106
  const [doCreateOrder, { isMutating }] = useMutation(
@@ -17141,8 +17171,8 @@ var useOrderEntry2 = (symbol, options = {}) => {
17141
17171
  }, zero);
17142
17172
  avgExecutionPrice = sumPrice.div(order_quantity);
17143
17173
  }
17144
- if (avgExecutionPrice) {
17145
- const bestPrice = book[0][0];
17174
+ const bestPrice = book[0][0];
17175
+ if (avgExecutionPrice && bestPrice) {
17146
17176
  const estSlippage2 = avgExecutionPrice.minus(bestPrice).abs().div(bestPrice).toNumber();
17147
17177
  setEstSlippage(estSlippage2);
17148
17178
  }
@@ -17445,7 +17475,8 @@ var useOrderEntry2 = (symbol, options = {}) => {
17445
17475
  symbolInfo: symbolInfo || EMPTY_OBJECT,
17446
17476
  metaState: meta,
17447
17477
  isMutating,
17448
- markPrice
17478
+ markPrice,
17479
+ symbolLeverage
17449
17480
  };
17450
17481
  };
17451
17482
  var initialOrderState2 = {