@orderly.network/hooks 2.1.1 → 2.1.2

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
@@ -8,7 +8,8 @@ import * as _orderly_network_core from '@orderly.network/core';
8
8
  import { AccountState, Account, EventEmitter, ConfigStore, ConfigKey, SimpleDI, OrderlyKeyStore, WalletAdapter, IContract, DefaultConfigStore } from '@orderly.network/core';
9
9
  export { WalletAdapter } from '@orderly.network/core';
10
10
  import * as _orderly_network_types from '@orderly.network/types';
11
- import { NetworkId, TrackerListenerKeyMap, API, Chain as Chain$1, ChainNamespace, WSMessage, OrderStatus, OrderSide, AlgoOrderRootType, OrderEntity, OrderType, OrderlyOrder, AlgoOrderEntity, AlgoOrderType, RequireKeys } from '@orderly.network/types';
11
+ import { NetworkId, TrackerEventName, API, Chain as Chain$1, ChainNamespace, WSMessage, OrderStatus, OrderSide, AlgoOrderRootType, OrderEntity, OrderType, OrderlyOrder, AlgoOrderEntity, AlgoOrderType, RequireKeys } from '@orderly.network/types';
12
+ import * as lodash from 'lodash';
12
13
  export { default as useConstant } from 'use-constant';
13
14
  import { WS } from '@orderly.network/net';
14
15
  import * as react from 'react';
@@ -28,7 +29,7 @@ declare global {
28
29
  };
29
30
  }
30
31
  }
31
- declare const _default: "2.1.1";
32
+ declare const _default: "2.1.2";
32
33
 
33
34
  type useQueryOptions<T> = SWRConfiguration & {
34
35
  formatter?: (data: any) => T;
@@ -113,8 +114,10 @@ type CurrentChain = {
113
114
  declare const useNetworkInfo: (networkId: NetworkId) => (chainId: number) => CurrentChain;
114
115
 
115
116
  declare const useTrack: () => {
116
- track: (eventName: keyof typeof TrackerListenerKeyMap, params: any) => void;
117
+ track: lodash.DebouncedFunc<(eventName: TrackerEventName, params: any) => void>;
118
+ tracking: (eventName: TrackerEventName, params: any) => void;
117
119
  setTrackUserId: (userId: string) => void;
120
+ setIdentify: (params: any) => void;
118
121
  };
119
122
 
120
123
  declare enum ENVType$1 {
@@ -132,7 +135,7 @@ declare class AmplitudeTracker {
132
135
  setUserId(userId: string): void;
133
136
  setSdkInfo(sdkInfo: any): void;
134
137
  identify(properties: any): void;
135
- track(eventName: keyof typeof TrackerListenerKeyMap, properties?: any): void;
138
+ track(eventName: TrackerEventName, properties?: any): void;
136
139
  private _bindEvents;
137
140
  }
138
141
 
@@ -1640,7 +1643,7 @@ declare const useOrderEntryNextInternal: (symbol: string, options?: {
1640
1643
  }) => Promise<{
1641
1644
  [x: string]: OrderValidationItem | undefined;
1642
1645
  }>;
1643
- readonly onMarkPriceChange: (markPrice: number, baseOn?: "total" | "order_quantity") => void;
1646
+ readonly onMarkPriceChange: (markPrice: number, baseOn?: string[]) => void;
1644
1647
  };
1645
1648
 
1646
1649
  type OrderEntryParameters = Parameters<typeof useOrderEntryNextInternal>;
package/dist/index.d.ts CHANGED
@@ -8,7 +8,8 @@ import * as _orderly_network_core from '@orderly.network/core';
8
8
  import { AccountState, Account, EventEmitter, ConfigStore, ConfigKey, SimpleDI, OrderlyKeyStore, WalletAdapter, IContract, DefaultConfigStore } from '@orderly.network/core';
9
9
  export { WalletAdapter } from '@orderly.network/core';
10
10
  import * as _orderly_network_types from '@orderly.network/types';
11
- import { NetworkId, TrackerListenerKeyMap, API, Chain as Chain$1, ChainNamespace, WSMessage, OrderStatus, OrderSide, AlgoOrderRootType, OrderEntity, OrderType, OrderlyOrder, AlgoOrderEntity, AlgoOrderType, RequireKeys } from '@orderly.network/types';
11
+ import { NetworkId, TrackerEventName, API, Chain as Chain$1, ChainNamespace, WSMessage, OrderStatus, OrderSide, AlgoOrderRootType, OrderEntity, OrderType, OrderlyOrder, AlgoOrderEntity, AlgoOrderType, RequireKeys } from '@orderly.network/types';
12
+ import * as lodash from 'lodash';
12
13
  export { default as useConstant } from 'use-constant';
13
14
  import { WS } from '@orderly.network/net';
14
15
  import * as react from 'react';
@@ -28,7 +29,7 @@ declare global {
28
29
  };
29
30
  }
30
31
  }
31
- declare const _default: "2.1.1";
32
+ declare const _default: "2.1.2";
32
33
 
33
34
  type useQueryOptions<T> = SWRConfiguration & {
34
35
  formatter?: (data: any) => T;
@@ -113,8 +114,10 @@ type CurrentChain = {
113
114
  declare const useNetworkInfo: (networkId: NetworkId) => (chainId: number) => CurrentChain;
114
115
 
115
116
  declare const useTrack: () => {
116
- track: (eventName: keyof typeof TrackerListenerKeyMap, params: any) => void;
117
+ track: lodash.DebouncedFunc<(eventName: TrackerEventName, params: any) => void>;
118
+ tracking: (eventName: TrackerEventName, params: any) => void;
117
119
  setTrackUserId: (userId: string) => void;
120
+ setIdentify: (params: any) => void;
118
121
  };
119
122
 
120
123
  declare enum ENVType$1 {
@@ -132,7 +135,7 @@ declare class AmplitudeTracker {
132
135
  setUserId(userId: string): void;
133
136
  setSdkInfo(sdkInfo: any): void;
134
137
  identify(properties: any): void;
135
- track(eventName: keyof typeof TrackerListenerKeyMap, properties?: any): void;
138
+ track(eventName: TrackerEventName, properties?: any): void;
136
139
  private _bindEvents;
137
140
  }
138
141
 
@@ -1640,7 +1643,7 @@ declare const useOrderEntryNextInternal: (symbol: string, options?: {
1640
1643
  }) => Promise<{
1641
1644
  [x: string]: OrderValidationItem | undefined;
1642
1645
  }>;
1643
- readonly onMarkPriceChange: (markPrice: number, baseOn?: "total" | "order_quantity") => void;
1646
+ readonly onMarkPriceChange: (markPrice: number, baseOn?: string[]) => void;
1644
1647
  };
1645
1648
 
1646
1649
  type OrderEntryParameters = Parameters<typeof useOrderEntryNextInternal>;
package/dist/index.js CHANGED
@@ -61,9 +61,9 @@ var __export = (target, all) => {
61
61
  // src/version.ts
62
62
  if (typeof window !== "undefined") {
63
63
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
64
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.1.1";
64
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.1.2";
65
65
  }
66
- var version_default = "2.1.1";
66
+ var version_default = "2.1.2";
67
67
  var fetcher = (url, init2 = {}, queryOptions) => net.get(url, init2, queryOptions?.formatter);
68
68
  var OrderlyContext = React.createContext({
69
69
  // configStore: new MemoryConfigStore(),
@@ -258,8 +258,8 @@ var useWalletConnector = () => {
258
258
  var useTrack = () => {
259
259
  const ee = useEventEmitter();
260
260
  const { wallet } = useWalletConnector();
261
- const debouncedTrackFn = React.useCallback(
262
- lodash.debounce((eventName, params) => {
261
+ const tracking = React.useCallback(
262
+ (eventName, params) => {
263
263
  utils.windowGuard(() => {
264
264
  const location = window.location;
265
265
  const origin = location.origin;
@@ -272,23 +272,29 @@ var useTrack = () => {
272
272
  page_domain: origin,
273
273
  user_agent: userAgent
274
274
  });
275
- if (eventName === types.EnumTrackerKeys.placeorderSuccess) {
275
+ if (eventName === types.TrackerEventName.placeOrderSuccess) {
276
276
  Object.assign(params, {
277
277
  wallet: wallet?.label || "QR code"
278
278
  });
279
279
  }
280
280
  ee.emit(eventName, params);
281
281
  });
282
- }, 500),
283
- []
282
+ },
283
+ [wallet]
284
284
  );
285
- const track2 = React.useCallback((eventName, params) => {
286
- debouncedTrackFn(eventName, params);
287
- }, [debouncedTrackFn]);
285
+ const track2 = React.useCallback(lodash.debounce(tracking, 500), [tracking]);
288
286
  const setTrackUserId = React.useCallback((userId) => {
289
- ee.emit(types.EnumTrackerKeys.trackIdentifyUserId, userId);
287
+ ee.emit(types.TrackerEventName.trackIdentifyUserId, userId);
290
288
  }, []);
291
- return { track: track2, setTrackUserId };
289
+ const setIdentify = React.useCallback((params) => {
290
+ ee.emit(types.TrackerEventName.trackIdentify, params);
291
+ }, []);
292
+ return {
293
+ track: track2,
294
+ tracking,
295
+ setTrackUserId,
296
+ setIdentify
297
+ };
292
298
  };
293
299
 
294
300
  // src/useAccount.ts
@@ -323,7 +329,7 @@ var useAccount = () => {
323
329
  }, []);
324
330
  const createOrderlyKey = React.useCallback(
325
331
  async (remember) => {
326
- track2(types.EnumTrackerKeys.signinSuccess, {
332
+ track2(types.TrackerEventName.signinSuccess, {
327
333
  network: account5.chainId,
328
334
  wallet: state.connectWallet?.name
329
335
  });
@@ -599,14 +605,22 @@ var AmplitudeTracker = class {
599
605
  amplitude__namespace.identify(identify2);
600
606
  }
601
607
  track(eventName, properties) {
602
- amplitude__namespace.track(types.TrackerListenerKeyMap[eventName], properties);
608
+ amplitude__namespace.track(eventName, properties);
603
609
  }
604
610
  _bindEvents() {
605
- const listenKeys = Object.keys(types.TrackerListenerKeyMap);
611
+ const listenKeys = Object.values(types.TrackerEventName);
606
612
  listenKeys.forEach((key) => {
607
613
  this._ee.addListener(key, (params = {}) => {
608
- if (key === types.EnumTrackerKeys.trackIdentifyUserId) {
614
+ if (key === types.TrackerEventName.trackIdentifyUserId) {
609
615
  this.setUserId(params);
616
+ } else if (key === types.TrackerEventName.trackIdentify) {
617
+ this.identify(params);
618
+ } else if (key === types.TrackerEventName.trackCustomEvent) {
619
+ const { eventName, ...rest } = params;
620
+ if (!eventName) {
621
+ return;
622
+ }
623
+ this.track(eventName, rest);
610
624
  } else {
611
625
  this.track(key, params);
612
626
  }
@@ -4191,6 +4205,8 @@ var useHoldingStream = () => {
4191
4205
  isLoading
4192
4206
  };
4193
4207
  };
4208
+
4209
+ // src/orderly/useWithdraw.ts
4194
4210
  var useWithdraw = (options) => {
4195
4211
  const { account: account5, state } = useAccount();
4196
4212
  const [isLoading, setIsLoading] = React.useState(false);
@@ -4240,7 +4256,7 @@ var useWithdraw = (options) => {
4240
4256
  (inputs) => {
4241
4257
  return account5.assetsManager.withdraw(inputs).then((res) => {
4242
4258
  if (res.success) {
4243
- track2(types.EnumTrackerKeys.withdrawSuccess, {
4259
+ track2(types.TrackerEventName.withdrawSuccess, {
4244
4260
  wallet: state?.connectWallet?.name,
4245
4261
  network: targetChain?.network_infos.name,
4246
4262
  quantity: inputs.amount
@@ -4248,7 +4264,7 @@ var useWithdraw = (options) => {
4248
4264
  }
4249
4265
  return res;
4250
4266
  }).catch((err) => {
4251
- track2(types.EnumTrackerKeys.withdrawFailed, {
4267
+ track2(types.TrackerEventName.withdrawFailed, {
4252
4268
  wallet: state?.connectWallet?.name,
4253
4269
  network: targetChain?.network_infos.name,
4254
4270
  msg: JSON.stringify(err)
@@ -4470,7 +4486,7 @@ var useDeposit = (options) => {
4470
4486
  throw new types.SDKError("Insufficient allowance");
4471
4487
  }
4472
4488
  return account5.assetsManager.deposit(quantity, depositFee).then((res) => {
4473
- track2(types.EnumTrackerKeys.depositSuccess, {
4489
+ track2(types.TrackerEventName.depositSuccess, {
4474
4490
  wallet: state?.connectWallet?.name,
4475
4491
  network: targetChain?.network_infos.name,
4476
4492
  quantity
@@ -4479,7 +4495,7 @@ var useDeposit = (options) => {
4479
4495
  setBalance((value) => new utils.Decimal(value).sub(quantity).toString());
4480
4496
  return res;
4481
4497
  }).catch((e) => {
4482
- track2(types.EnumTrackerKeys.depositFailed, {
4498
+ track2(types.TrackerEventName.depositFailed, {
4483
4499
  wallet: state?.connectWallet?.name,
4484
4500
  network: targetChain?.network_infos?.name,
4485
4501
  msg: JSON.stringify(e)
@@ -13919,6 +13935,10 @@ var calcEstLeverage = (order, askAndBid, inputs) => {
13919
13935
  }
13920
13936
  });
13921
13937
  };
13938
+ function isBBOOrder(options) {
13939
+ const { order_type, order_type_ext } = options;
13940
+ return order_type === types.OrderType.LIMIT && [types.OrderType.ASK, types.OrderType.BID].includes(order_type_ext);
13941
+ }
13922
13942
  var initialOrderState = {
13923
13943
  order_price: "",
13924
13944
  order_quantity: "",
@@ -14004,7 +14024,7 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14004
14024
  const calculate = React.useCallback(
14005
14025
  (values2, fieldName, value, markPrice, config) => {
14006
14026
  const fieldHandler = getCalculateHandler(fieldName);
14007
- let newValues = ramda.compose(
14027
+ const newValues = ramda.compose(
14008
14028
  ramda.head,
14009
14029
  // orderEntityFormatHandle(baseDP, quoteDP),
14010
14030
  fieldHandler,
@@ -14124,11 +14144,11 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14124
14144
  return newValues;
14125
14145
  };
14126
14146
  const onMarkPriceUpdated = React.useCallback(
14127
- (markPrice, baseOn) => {
14147
+ (markPrice, baseOn = []) => {
14128
14148
  if (!options.symbolInfo)
14129
14149
  return;
14130
- let newValues;
14131
- if (typeof baseOn === "undefined") {
14150
+ let newValues = { ...orderEntity };
14151
+ if (baseOn.length === 0) {
14132
14152
  newValues = calculate(
14133
14153
  { ...orderEntity },
14134
14154
  "order_price",
@@ -14137,13 +14157,15 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14137
14157
  options.symbolInfo
14138
14158
  );
14139
14159
  } else {
14140
- newValues = calculate(
14141
- { ...orderEntity },
14142
- baseOn,
14143
- orderEntity[baseOn],
14144
- markPrice,
14145
- options.symbolInfo
14146
- );
14160
+ baseOn.forEach((key) => {
14161
+ newValues = calculate(
14162
+ { ...newValues },
14163
+ key,
14164
+ orderEntity[key],
14165
+ markPrice,
14166
+ options.symbolInfo
14167
+ );
14168
+ });
14147
14169
  }
14148
14170
  if (hasTPSL(newValues)) {
14149
14171
  if (newValues.tp_pnl && newValues.order_quantity) {
@@ -14196,6 +14218,8 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14196
14218
  onMarkPriceChange: onMarkPriceUpdated
14197
14219
  };
14198
14220
  };
14221
+
14222
+ // src/next/useOrderEntry/useOrderEntry.ts
14199
14223
  var useOrderEntry2 = (symbol, options = {}) => {
14200
14224
  if (!symbol) {
14201
14225
  throw new types.SDKError("Symbol is required");
@@ -14212,6 +14236,10 @@ var useOrderEntry2 = (symbol, options = {}) => {
14212
14236
  const lastChangedField = React.useRef();
14213
14237
  const lastOrderTypeExt = React.useRef();
14214
14238
  const lastLevel = React.useRef();
14239
+ const calculateTPSL_baseOn = React.useRef({
14240
+ tp: "",
14241
+ sl: ""
14242
+ });
14215
14243
  const actions = useMarkPriceActions();
14216
14244
  const symbolConfig = useSymbolsInfo();
14217
14245
  const accountInfo = useAccountInfo();
@@ -14240,24 +14268,26 @@ var useOrderEntry2 = (symbol, options = {}) => {
14240
14268
  formattedOrder.reduce_only
14241
14269
  );
14242
14270
  const updateOrderPrice = () => {
14271
+ const order_type = formattedOrder.order_type;
14243
14272
  const order_type_ext = formattedOrder.order_type_ext ?? lastOrderTypeExt.current;
14244
14273
  const level = formattedOrder.level ?? lastLevel.current;
14245
- if (![types.OrderType.ASK, types.OrderType.BID].includes(order_type_ext) || level === void 0) {
14274
+ const isBBO = isBBOOrder({ order_type, order_type_ext });
14275
+ if (!isBBO || level === void 0) {
14246
14276
  return;
14247
14277
  }
14248
14278
  lastOrderTypeExt.current = order_type_ext;
14249
14279
  lastLevel.current = level;
14250
14280
  const index = order_type_ext === types.OrderType.ASK ? 0 : 1;
14251
14281
  const price = askAndBid.current?.[level]?.[index];
14252
- if (price && !isNaN(price)) {
14282
+ if (price) {
14253
14283
  setValue("order_price", price, {
14254
14284
  shouldUpdateLastChangedField: false
14255
14285
  });
14256
14286
  }
14257
14287
  };
14258
14288
  const updateOrderPriceByOrderBook = () => {
14259
- const { order_type, order_type_ext, order_quantity } = formattedOrder;
14260
- const isBBO = order_type === types.OrderType.LIMIT && [types.OrderType.ASK, types.OrderType.BID].includes(order_type_ext);
14289
+ const { order_type, order_type_ext } = formattedOrder;
14290
+ const isBBO = isBBOOrder({ order_type, order_type_ext });
14261
14291
  if (lastChangedField.current !== "total" && isBBO) {
14262
14292
  updateOrderPrice();
14263
14293
  }
@@ -14277,10 +14307,17 @@ var useOrderEntry2 = (symbol, options = {}) => {
14277
14307
  }, []);
14278
14308
  React.useEffect(() => {
14279
14309
  if ((formattedOrder.order_type === types.OrderType.MARKET || formattedOrder.order_type === types.OrderType.STOP_MARKET) && markPrice) {
14280
- orderEntryActions.onMarkPriceChange(
14281
- markPrice,
14282
- lastChangedField.current
14283
- );
14310
+ const baseOn = /* @__PURE__ */ new Set();
14311
+ if (lastChangedField.current) {
14312
+ baseOn.add(lastChangedField.current);
14313
+ }
14314
+ if (calculateTPSL_baseOn.current.tp) {
14315
+ baseOn.add(calculateTPSL_baseOn.current.tp);
14316
+ }
14317
+ if (calculateTPSL_baseOn.current.sl) {
14318
+ baseOn.add(calculateTPSL_baseOn.current.sl);
14319
+ }
14320
+ orderEntryActions.onMarkPriceChange(markPrice, Array.from(baseOn));
14284
14321
  }
14285
14322
  }, [markPrice, formattedOrder.order_type]);
14286
14323
  const prepareData = React.useCallback(() => {
@@ -14330,6 +14367,11 @@ var useOrderEntry2 = (symbol, options = {}) => {
14330
14367
  interactiveValidate(values2);
14331
14368
  }
14332
14369
  if (shouldUpdateLastChangedField) {
14370
+ if (key.startsWith("tp_")) {
14371
+ calculateTPSL_baseOn.current.tp = key;
14372
+ } else if (key.startsWith("sl_")) {
14373
+ calculateTPSL_baseOn.current.sl = key;
14374
+ }
14333
14375
  lastChangedField.current = key;
14334
14376
  }
14335
14377
  };
@@ -14448,7 +14490,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
14448
14490
  const order = generateOrder(creator, prepareData());
14449
14491
  const result = await doCreateOrder(order);
14450
14492
  if (result.success) {
14451
- track2(types.EnumTrackerKeys.placeorderSuccess, {
14493
+ track2(types.TrackerEventName.placeOrderSuccess, {
14452
14494
  side: order.side,
14453
14495
  order_type: order.order_type,
14454
14496
  tp_sl: hasTPSL(formattedOrder),