@orderly.network/hooks 2.11.0 → 3.0.0-beta.1

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.d.mts CHANGED
@@ -37,7 +37,7 @@ declare global {
37
37
  };
38
38
  }
39
39
  }
40
- declare const _default: "2.11.0";
40
+ declare const _default: "3.0.0-beta.1";
41
41
 
42
42
  declare const fetcher: (url: string, init: RequestInit | undefined, queryOptions: useQueryOptions<any>) => Promise<any>;
43
43
  type useQueryOptions<T> = SWRConfiguration & {
@@ -47,7 +47,7 @@ declare const noCacheConfig: SWRConfiguration;
47
47
 
48
48
  /**
49
49
  * useQuery
50
- * @description for public api
50
+ * @description for public api. Injects X-Orderly-Plugin-Id when inside PluginScopeProvider.
51
51
  * @param query
52
52
  * @param options
53
53
  */
@@ -2806,6 +2806,10 @@ type OrderEntryReturn = {
2806
2806
  resetMetaState: () => void;
2807
2807
  formattedOrder: Partial<FullOrderState>;
2808
2808
  maxQty: number;
2809
+ maxQtys: {
2810
+ maxBuy: number;
2811
+ maxSell: number;
2812
+ };
2809
2813
  /**
2810
2814
  * The estimated liquidation price.
2811
2815
  */
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ declare global {
37
37
  };
38
38
  }
39
39
  }
40
- declare const _default: "2.11.0";
40
+ declare const _default: "3.0.0-beta.1";
41
41
 
42
42
  declare const fetcher: (url: string, init: RequestInit | undefined, queryOptions: useQueryOptions<any>) => Promise<any>;
43
43
  type useQueryOptions<T> = SWRConfiguration & {
@@ -47,7 +47,7 @@ declare const noCacheConfig: SWRConfiguration;
47
47
 
48
48
  /**
49
49
  * useQuery
50
- * @description for public api
50
+ * @description for public api. Injects X-Orderly-Plugin-Id when inside PluginScopeProvider.
51
51
  * @param query
52
52
  * @param options
53
53
  */
@@ -2806,6 +2806,10 @@ type OrderEntryReturn = {
2806
2806
  resetMetaState: () => void;
2807
2807
  formattedOrder: Partial<FullOrderState>;
2808
2808
  maxQty: number;
2809
+ maxQtys: {
2810
+ maxBuy: number;
2811
+ maxSell: number;
2812
+ };
2809
2813
  /**
2810
2814
  * The estimated liquidation price.
2811
2815
  */
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var net = require('@orderly.network/net');
4
+ var React = require('react');
4
5
  var useSWR5 = require('swr');
6
+ var pluginCore = require('@orderly.network/plugin-core');
5
7
  var types = require('@orderly.network/types');
6
- var React = require('react');
7
8
  var utils = require('@orderly.network/utils');
8
9
  var useSWRMutation = require('swr/mutation');
9
10
  var useConstant = require('use-constant');
@@ -46,8 +47,8 @@ function _interopNamespace(e) {
46
47
  return Object.freeze(n);
47
48
  }
48
49
 
49
- var useSWR5__namespace = /*#__PURE__*/_interopNamespace(useSWR5);
50
50
  var React__default = /*#__PURE__*/_interopDefault(React);
51
+ var useSWR5__namespace = /*#__PURE__*/_interopNamespace(useSWR5);
51
52
  var useSWRMutation__default = /*#__PURE__*/_interopDefault(useSWRMutation);
52
53
  var useConstant__default = /*#__PURE__*/_interopDefault(useConstant);
53
54
  var useSWRInfinite__default = /*#__PURE__*/_interopDefault(useSWRInfinite);
@@ -64,9 +65,9 @@ var __export = (target, all) => {
64
65
  // src/version.ts
65
66
  if (typeof window !== "undefined") {
66
67
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
67
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.11.0";
68
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "3.0.0-beta.1";
68
69
  }
69
- var version_default = "2.11.0";
70
+ var version_default = "3.0.0-beta.1";
70
71
  var fetcher = (url, init2 = {}, queryOptions) => net.get(url, init2, queryOptions?.formatter);
71
72
  var noCacheConfig = {
72
73
  dedupingInterval: 0,
@@ -99,19 +100,26 @@ function useConfig(key, defaultValue) {
99
100
  }
100
101
 
101
102
  // src/useQuery.ts
103
+ var PLUGIN_ID_HEADER = "X-Orderly-Plugin-Id";
102
104
  var useQuery = (query, options) => {
103
105
  const apiBaseUrl = useConfig("apiBaseUrl");
106
+ const pluginScope = pluginCore.usePluginScope();
104
107
  const { formatter, ...swrOptions } = options || {};
105
108
  if (typeof apiBaseUrl === "undefined") {
106
109
  throw new types.SDKError("please add OrderlyConfigProvider to your app");
107
110
  }
108
- return useSWR5__namespace.default(
109
- query,
110
- (url, init2) => fetcher(url.startsWith("http") ? url : `${apiBaseUrl}${url}`, init2, {
111
- formatter
112
- }),
113
- swrOptions
111
+ const fetcherFn = React.useCallback(
112
+ (url, init2) => {
113
+ const fullUrl = url.startsWith("http") ? url : `${apiBaseUrl}${url}`;
114
+ const headers = new Headers(init2?.headers);
115
+ if (pluginScope?.pluginId) {
116
+ headers.set(PLUGIN_ID_HEADER, pluginScope.pluginId);
117
+ }
118
+ return fetcher(fullUrl, { ...init2, headers }, { formatter });
119
+ },
120
+ [apiBaseUrl, pluginScope?.pluginId, formatter]
114
121
  );
122
+ return useSWR5__namespace.default(query, fetcherFn, swrOptions);
115
123
  };
116
124
  var timestampOffsetPromise = null;
117
125
  var timestampOffsetReady = false;
@@ -5543,34 +5551,18 @@ var PositionCalculator = class extends BaseCalculator {
5543
5551
  rows = rows.map((item) => {
5544
5552
  const info = symbolsInfo[item.symbol];
5545
5553
  const totalCollateral = item.margin_mode === types.MarginMode.ISOLATED ? new utils.Decimal(item.margin ?? 0).add(item.unsettlement_pnl ?? 0).toNumber() : crossMarginCollateral.toNumber();
5546
- let est_liq_price;
5547
- if (item.margin_mode === types.MarginMode.ISOLATED) {
5548
- est_liq_price = perp.positions.liquidationPriceIsolated({
5549
- isolatedPositionMargin: item.margin ?? 0,
5550
- costPosition: item.cost_position ?? 0,
5551
- positionQty: item.position_qty ?? 0,
5552
- sumUnitaryFunding: fundingRates?.[item.symbol]?.["sum_unitary_funding"] ?? 0,
5553
- lastSumUnitaryFunding: item.last_sum_unitary_funding ?? 0,
5554
- baseMMR: info?.["base_mmr"] ?? 0,
5555
- baseIMR: info?.["base_imr"] ?? 0,
5556
- IMRFactor: accountInfo.imr_factor[item.symbol],
5557
- referencePrice: item.mark_price,
5558
- leverage: item.leverage ?? 0
5559
- });
5560
- } else {
5561
- est_liq_price = perp.positions.liqPrice({
5562
- symbol: item.symbol,
5563
- markPrice: item.mark_price,
5564
- totalCollateral,
5565
- positionQty: item.position_qty,
5566
- positions: rows,
5567
- MMR: item.mmr,
5568
- baseMMR: info?.["base_mmr"],
5569
- baseIMR: info?.["base_imr"],
5570
- IMRFactor: accountInfo.imr_factor[item.symbol],
5571
- costPosition: item.cost_position
5572
- });
5573
- }
5554
+ const est_liq_price = perp.positions.liqPrice({
5555
+ symbol: item.symbol,
5556
+ markPrice: item.mark_price,
5557
+ totalCollateral,
5558
+ positionQty: item.position_qty,
5559
+ positions: rows,
5560
+ MMR: item.mmr,
5561
+ baseMMR: info?.["base_mmr"],
5562
+ baseIMR: info?.["base_imr"],
5563
+ IMRFactor: accountInfo.imr_factor[item.symbol],
5564
+ costPosition: item.cost_position
5565
+ });
5574
5566
  return {
5575
5567
  ...item,
5576
5568
  est_liq_price
@@ -10985,7 +10977,7 @@ var calcEstLiqPrice = (order, askAndBid, inputs) => {
10985
10977
  totalCollateral,
10986
10978
  futures_taker_fee_rate,
10987
10979
  positions: positions3,
10988
- symbolLeverage,
10980
+ // leverage,
10989
10981
  sumUnitaryFunding
10990
10982
  } = inputs;
10991
10983
  const orderFee = perp.order.orderFee({
@@ -11010,7 +11002,7 @@ var calcEstLiqPrice = (order, askAndBid, inputs) => {
11010
11002
  }
11011
11003
  });
11012
11004
  } else {
11013
- let isolatedPositionMargin = 0, costPosition = 0, positionQty = 0, lastSumUnitaryFunding = 0, leverage = symbolLeverage ?? 1;
11005
+ let isolatedPositionMargin = 0, costPosition = 0, positionQty = 0, lastSumUnitaryFunding = 0, leverage = 1;
11014
11006
  if (positions3) {
11015
11007
  const position = positions3.find(
11016
11008
  (p) => p.symbol === symbol && p.margin_mode === types.MarginMode.ISOLATED
@@ -18776,12 +18768,37 @@ var useOrderEntry2 = (symbol, options = {}) => {
18776
18768
  );
18777
18769
  const bestAskBid = askAndBid.current?.[0] || [];
18778
18770
  const referencePriceFromOrder = bestAskBid.length >= 2 && formattedOrder.order_type && formattedOrder.side ? getOrderReferencePriceFromOrder(formattedOrder, bestAskBid) : null;
18779
- const maxQtyValue = useMaxQty(symbol, formattedOrder.side, {
18771
+ const maxBuyQtyValue = useMaxQty(symbol, types.OrderSide.BUY, {
18772
+ reduceOnly: formattedOrder.reduce_only,
18773
+ marginMode: effectiveMarginMode,
18774
+ currentOrderReferencePrice: referencePriceFromOrder && referencePriceFromOrder > 0 ? referencePriceFromOrder : void 0
18775
+ });
18776
+ const maxSellQtyValue = useMaxQty(symbol, types.OrderSide.SELL, {
18780
18777
  reduceOnly: formattedOrder.reduce_only,
18781
18778
  marginMode: effectiveMarginMode,
18782
18779
  currentOrderReferencePrice: referencePriceFromOrder && referencePriceFromOrder > 0 ? referencePriceFromOrder : void 0
18783
18780
  });
18781
+ const maxQtyValue = formattedOrder.side === types.OrderSide.BUY ? maxBuyQtyValue : maxSellQtyValue;
18784
18782
  const maxQty = options.maxQty ?? maxQtyValue;
18783
+ const maxQtys = React.useMemo(
18784
+ () => ({
18785
+ maxBuy: formattedOrder.side === types.OrderSide.BUY ? (
18786
+ // @ts-ignore
18787
+ options.maxQty ?? maxBuyQtyValue
18788
+ ) : maxBuyQtyValue,
18789
+ maxSell: formattedOrder.side === types.OrderSide.SELL ? (
18790
+ // @ts-ignore
18791
+ options.maxQty ?? maxSellQtyValue
18792
+ ) : maxSellQtyValue
18793
+ }),
18794
+ [
18795
+ formattedOrder.side,
18796
+ maxBuyQtyValue,
18797
+ maxSellQtyValue,
18798
+ // @ts-ignore
18799
+ options.maxQty
18800
+ ]
18801
+ );
18785
18802
  const updateOrderPrice = () => {
18786
18803
  const order_type = formattedOrder.order_type;
18787
18804
  const order_type_ext = formattedOrder.order_type_ext ?? lastOrderTypeExt.current;
@@ -19017,8 +19034,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
19017
19034
  symbol,
19018
19035
  positions: positions3,
19019
19036
  symbolInfo,
19020
- sumUnitaryFunding,
19021
- symbolLeverage
19037
+ sumUnitaryFunding
19022
19038
  });
19023
19039
  return estLiqPrice2;
19024
19040
  }, [
@@ -19029,8 +19045,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
19029
19045
  symbol,
19030
19046
  maxQty,
19031
19047
  symbolInfo,
19032
- fundingRates,
19033
- symbolLeverage
19048
+ fundingRates
19034
19049
  ]);
19035
19050
  const estLiqPriceDistance = React.useMemo(() => {
19036
19051
  if (!estLiqPrice) {
@@ -19153,6 +19168,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
19153
19168
  resetMetaState,
19154
19169
  formattedOrder,
19155
19170
  maxQty,
19171
+ maxQtys,
19156
19172
  estLiqPrice,
19157
19173
  estLiqPriceDistance,
19158
19174
  currentPosition,