@kodiak-finance/orderly-hooks 2.9.1 → 2.9.2-alpha.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
@@ -180,7 +180,7 @@ declare const noCacheConfig: SWRConfiguration;
180
180
 
181
181
  /**
182
182
  * useQuery
183
- * @description for public api
183
+ * @description for public api. Injects X-Orderly-Plugin-Id when inside PluginScopeProvider.
184
184
  * @param query
185
185
  * @param options
186
186
  */
@@ -1016,6 +1016,14 @@ declare const useIndexPricesStream: () => {
1016
1016
  getIndexPrice: (this: unknown, token: string) => number;
1017
1017
  };
1018
1018
 
1019
+ /**
1020
+ * Mark price from the in-memory store for `symbol`.
1021
+ *
1022
+ * @remarks
1023
+ * `data` reflects the store lookup and may be transiently unset before streams write in.
1024
+ * In TS strict JSX, avoid rendering `{data}` as a direct text child without narrowing
1025
+ * (`typeof data === "number"`) or formatting through a helper that supplies a fallback.
1026
+ */
1019
1027
  declare const useMarkPrice: (symbol: string) => {
1020
1028
  data: number;
1021
1029
  };
@@ -2893,6 +2901,10 @@ type OrderEntryReturn = {
2893
2901
  resetMetaState: () => void;
2894
2902
  formattedOrder: Partial<FullOrderState>;
2895
2903
  maxQty: number;
2904
+ maxQtys: {
2905
+ maxBuy: number;
2906
+ maxSell: number;
2907
+ };
2896
2908
  /**
2897
2909
  * The estimated liquidation price.
2898
2910
  */
package/dist/index.d.ts CHANGED
@@ -180,7 +180,7 @@ declare const noCacheConfig: SWRConfiguration;
180
180
 
181
181
  /**
182
182
  * useQuery
183
- * @description for public api
183
+ * @description for public api. Injects X-Orderly-Plugin-Id when inside PluginScopeProvider.
184
184
  * @param query
185
185
  * @param options
186
186
  */
@@ -1016,6 +1016,14 @@ declare const useIndexPricesStream: () => {
1016
1016
  getIndexPrice: (this: unknown, token: string) => number;
1017
1017
  };
1018
1018
 
1019
+ /**
1020
+ * Mark price from the in-memory store for `symbol`.
1021
+ *
1022
+ * @remarks
1023
+ * `data` reflects the store lookup and may be transiently unset before streams write in.
1024
+ * In TS strict JSX, avoid rendering `{data}` as a direct text child without narrowing
1025
+ * (`typeof data === "number"`) or formatting through a helper that supplies a fallback.
1026
+ */
1019
1027
  declare const useMarkPrice: (symbol: string) => {
1020
1028
  data: number;
1021
1029
  };
@@ -2893,6 +2901,10 @@ type OrderEntryReturn = {
2893
2901
  resetMetaState: () => void;
2894
2902
  formattedOrder: Partial<FullOrderState>;
2895
2903
  maxQty: number;
2904
+ maxQtys: {
2905
+ maxBuy: number;
2906
+ maxSell: number;
2907
+ };
2896
2908
  /**
2897
2909
  * The estimated liquidation price.
2898
2910
  */
package/dist/index.js CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  var React = require('react');
4
4
  var orderlyNet = require('@kodiak-finance/orderly-net');
5
- var useSWR5 = require('swr');
5
+ var orderlyPluginCore = require('@kodiak-finance/orderly-plugin-core');
6
6
  var orderlyTypes = require('@kodiak-finance/orderly-types');
7
+ var useSWR5 = require('swr');
7
8
  var orderlyUtils = require('@kodiak-finance/orderly-utils');
8
9
  var useSWRMutation = require('swr/mutation');
9
10
  var useConstant = require('use-constant');
@@ -110,19 +111,26 @@ function useConfig(key, defaultValue) {
110
111
  }
111
112
 
112
113
  // src/useQuery.ts
114
+ var PLUGIN_ID_HEADER = "X-Orderly-Plugin-Id";
113
115
  var useQuery = (query, options) => {
114
116
  const apiBaseUrl = useConfig("apiBaseUrl");
117
+ const pluginScope = orderlyPluginCore.usePluginScope();
115
118
  const { formatter, ...swrOptions } = options || {};
116
119
  if (typeof apiBaseUrl === "undefined") {
117
120
  throw new orderlyTypes.SDKError("please add OrderlyConfigProvider to your app");
118
121
  }
119
- return useSWR5__namespace.default(
120
- query,
121
- (url, init2) => fetcher(url.startsWith("http") ? url : `${apiBaseUrl}${url}`, init2, {
122
- formatter
123
- }),
124
- swrOptions
122
+ const fetcherFn = React.useCallback(
123
+ (url, init2) => {
124
+ const fullUrl = url.startsWith("http") ? url : `${apiBaseUrl}${url}`;
125
+ const headers = new Headers(init2?.headers);
126
+ if (pluginScope?.pluginId) {
127
+ headers.set(PLUGIN_ID_HEADER, pluginScope.pluginId);
128
+ }
129
+ return fetcher(fullUrl, { ...init2, headers }, { formatter });
130
+ },
131
+ [apiBaseUrl, pluginScope?.pluginId, formatter]
125
132
  );
133
+ return useSWR5__namespace.default(query, fetcherFn, swrOptions);
126
134
  };
127
135
  var timestampOffsetPromise = null;
128
136
  var timestampOffsetReady = false;
@@ -18727,12 +18735,21 @@ var initialOrderState = {
18727
18735
  order_type: orderlyTypes.OrderType.LIMIT,
18728
18736
  margin_mode: orderlyTypes.MarginMode.CROSS
18729
18737
  };
18738
+ var normalizeInitialOrder = (order) => {
18739
+ if (!order) {
18740
+ return void 0;
18741
+ }
18742
+ const rest = { ...order };
18743
+ delete rest.symbol;
18744
+ return rest;
18745
+ };
18730
18746
  var useOrderEntryNextInternal = (symbol, options = {}) => {
18731
18747
  const { symbolInfo, symbolLeverage } = options;
18748
+ const initialOrder = normalizeInitialOrder(options.initialOrder);
18732
18749
  const [orderEntity, setOrderEntity] = React.useState(
18733
18750
  () => ({
18734
18751
  ...initialOrderState,
18735
- ...options.initialOrder,
18752
+ ...initialOrder,
18736
18753
  symbol
18737
18754
  })
18738
18755
  );
@@ -18748,10 +18765,14 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18748
18765
  });
18749
18766
  },
18750
18767
  updateOrder: (order) => {
18751
- setOrderEntity((prev) => ({ ...prev, ...order }));
18768
+ setOrderEntity((prev) => ({ ...prev, ...order, symbol }));
18752
18769
  },
18753
18770
  updateOrderByKey: (key, value) => {
18754
- setOrderEntity((prev) => ({ ...prev, [key]: value }));
18771
+ setOrderEntity((prev) => ({
18772
+ ...prev,
18773
+ [key]: key === "symbol" ? symbol : value,
18774
+ symbol
18775
+ }));
18755
18776
  },
18756
18777
  resetOrder: (_order) => {
18757
18778
  setOrderEntity((prev) => ({
@@ -18779,12 +18800,15 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18779
18800
  return typeof order.tp_trigger_price !== "undefined" || typeof order.sl_trigger_price !== "undefined";
18780
18801
  }
18781
18802
  }),
18782
- [orderEntity]
18803
+ [orderEntity, symbol]
18783
18804
  );
18784
18805
  React.useEffect(() => {
18785
- actions.initOrder(symbol, options.initialOrder);
18786
- if (options.initialOrder) {
18787
- actions.updateOrder(options.initialOrder);
18806
+ actions.initOrder(symbol, initialOrder);
18807
+ if (initialOrder) {
18808
+ actions.updateOrder({
18809
+ ...initialOrder,
18810
+ symbol
18811
+ });
18788
18812
  }
18789
18813
  }, [symbol]);
18790
18814
  const calculate2 = React.useCallback(
@@ -19024,7 +19048,7 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
19024
19048
  }
19025
19049
  actions.updateOrder(newValues);
19026
19050
  },
19027
- [calculate2, options.symbolInfo, symbolLeverage, orderEntity]
19051
+ [actions, calculate2, options.symbolInfo, symbolLeverage, orderEntity]
19028
19052
  );
19029
19053
  const validate = (order, creator, options2) => {
19030
19054
  return creator?.validate(order, {
@@ -19033,10 +19057,16 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
19033
19057
  });
19034
19058
  };
19035
19059
  const generateOrder = (creator, options2) => {
19036
- const order = creator.create(orderEntity, {
19037
- ...options2,
19038
- symbol: symbolInfo
19039
- });
19060
+ const order = creator.create(
19061
+ {
19062
+ ...orderEntity,
19063
+ symbol
19064
+ },
19065
+ {
19066
+ ...options2,
19067
+ symbol: symbolInfo
19068
+ }
19069
+ );
19040
19070
  return order;
19041
19071
  };
19042
19072
  const submitOrder = React.useCallback(() => {
@@ -19111,12 +19141,37 @@ var useOrderEntry2 = (symbol, options = {}) => {
19111
19141
  );
19112
19142
  const bestAskBid = askAndBid.current?.[0] || [];
19113
19143
  const referencePriceFromOrder = bestAskBid.length >= 2 && formattedOrder.order_type && formattedOrder.side ? getOrderReferencePriceFromOrder(formattedOrder, bestAskBid) : null;
19114
- const maxQtyValue = useMaxQty(symbol, formattedOrder.side, {
19144
+ const maxBuyQtyValue = useMaxQty(symbol, orderlyTypes.OrderSide.BUY, {
19145
+ reduceOnly: formattedOrder.reduce_only,
19146
+ marginMode: effectiveMarginMode,
19147
+ currentOrderReferencePrice: referencePriceFromOrder && referencePriceFromOrder > 0 ? referencePriceFromOrder : void 0
19148
+ });
19149
+ const maxSellQtyValue = useMaxQty(symbol, orderlyTypes.OrderSide.SELL, {
19115
19150
  reduceOnly: formattedOrder.reduce_only,
19116
19151
  marginMode: effectiveMarginMode,
19117
19152
  currentOrderReferencePrice: referencePriceFromOrder && referencePriceFromOrder > 0 ? referencePriceFromOrder : void 0
19118
19153
  });
19154
+ const maxQtyValue = formattedOrder.side === orderlyTypes.OrderSide.BUY ? maxBuyQtyValue : maxSellQtyValue;
19119
19155
  const maxQty = options.maxQty ?? maxQtyValue;
19156
+ const maxQtys = React.useMemo(
19157
+ () => ({
19158
+ maxBuy: formattedOrder.side === orderlyTypes.OrderSide.BUY ? (
19159
+ // @ts-ignore
19160
+ options.maxQty ?? maxBuyQtyValue
19161
+ ) : maxBuyQtyValue,
19162
+ maxSell: formattedOrder.side === orderlyTypes.OrderSide.SELL ? (
19163
+ // @ts-ignore
19164
+ options.maxQty ?? maxSellQtyValue
19165
+ ) : maxSellQtyValue
19166
+ }),
19167
+ [
19168
+ formattedOrder.side,
19169
+ maxBuyQtyValue,
19170
+ maxSellQtyValue,
19171
+ // @ts-ignore
19172
+ options.maxQty
19173
+ ]
19174
+ );
19120
19175
  const updateOrderPrice = () => {
19121
19176
  const order_type = formattedOrder.order_type;
19122
19177
  const order_type_ext = formattedOrder.order_type_ext ?? lastOrderTypeExt.current;
@@ -19506,6 +19561,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
19506
19561
  resetMetaState,
19507
19562
  formattedOrder,
19508
19563
  maxQty,
19564
+ maxQtys,
19509
19565
  estLiqPrice,
19510
19566
  estLiqPriceDistance,
19511
19567
  currentPosition,