@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.mjs CHANGED
@@ -2,7 +2,7 @@ import useSWR, { mutate } from 'swr';
2
2
  export { unstable_serialize, default as useSWR, useSWRConfig } from 'swr';
3
3
  import { get, WS, mutate as mutate$1 } from '@orderly.network/net';
4
4
  import React, { createContext, useContext, useCallback, useState, useEffect, useMemo, useRef, useId, useLayoutEffect } from 'react';
5
- import { TesntTokenFallback, ArbitrumSepoliaTokenInfo, SolanaDevnetTokenInfo, OrderType, OrderSide, SDKError, EnumTrackerKeys, AccountStatusEnum, AlgoOrderRootType, OrderStatus, ArbitrumSepoliaChainInfo, SolanaDevnetChainInfo, MONAD_TESTNET_CHAINID, nativeTokenAddress, ChainKey, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, isNativeTokenChecker, ChainNamespace, MaxUint256, DEPOSIT_FEE_RATE, LedgerWalletKey, SolanaChains, TrackerListenerKeyMap, AlgoOrderType, TriggerPriceType } from '@orderly.network/types';
5
+ import { TesntTokenFallback, ArbitrumSepoliaTokenInfo, SolanaDevnetTokenInfo, OrderType, OrderSide, SDKError, TrackerEventName, AccountStatusEnum, AlgoOrderRootType, OrderStatus, ArbitrumSepoliaChainInfo, SolanaDevnetChainInfo, MONAD_TESTNET_CHAINID, nativeTokenAddress, ChainKey, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, isNativeTokenChecker, ChainNamespace, MaxUint256, DEPOSIT_FEE_RATE, LedgerWalletKey, SolanaChains, AlgoOrderType, TriggerPriceType } from '@orderly.network/types';
6
6
  import useSWRMutation from 'swr/mutation';
7
7
  import useConstant from 'use-constant';
8
8
  export { default as useConstant } from 'use-constant';
@@ -34,9 +34,9 @@ var __export = (target, all) => {
34
34
  // src/version.ts
35
35
  if (typeof window !== "undefined") {
36
36
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
37
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.1.1";
37
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.1.2";
38
38
  }
39
- var version_default = "2.1.1";
39
+ var version_default = "2.1.2";
40
40
  var fetcher = (url, init2 = {}, queryOptions) => get(url, init2, queryOptions?.formatter);
41
41
  var OrderlyContext = createContext({
42
42
  // configStore: new MemoryConfigStore(),
@@ -231,8 +231,8 @@ var useWalletConnector = () => {
231
231
  var useTrack = () => {
232
232
  const ee = useEventEmitter();
233
233
  const { wallet } = useWalletConnector();
234
- const debouncedTrackFn = useCallback(
235
- debounce((eventName, params) => {
234
+ const tracking = useCallback(
235
+ (eventName, params) => {
236
236
  windowGuard(() => {
237
237
  const location = window.location;
238
238
  const origin = location.origin;
@@ -245,23 +245,29 @@ var useTrack = () => {
245
245
  page_domain: origin,
246
246
  user_agent: userAgent
247
247
  });
248
- if (eventName === EnumTrackerKeys.placeorderSuccess) {
248
+ if (eventName === TrackerEventName.placeOrderSuccess) {
249
249
  Object.assign(params, {
250
250
  wallet: wallet?.label || "QR code"
251
251
  });
252
252
  }
253
253
  ee.emit(eventName, params);
254
254
  });
255
- }, 500),
256
- []
255
+ },
256
+ [wallet]
257
257
  );
258
- const track2 = useCallback((eventName, params) => {
259
- debouncedTrackFn(eventName, params);
260
- }, [debouncedTrackFn]);
258
+ const track2 = useCallback(debounce(tracking, 500), [tracking]);
261
259
  const setTrackUserId = useCallback((userId) => {
262
- ee.emit(EnumTrackerKeys.trackIdentifyUserId, userId);
260
+ ee.emit(TrackerEventName.trackIdentifyUserId, userId);
263
261
  }, []);
264
- return { track: track2, setTrackUserId };
262
+ const setIdentify = useCallback((params) => {
263
+ ee.emit(TrackerEventName.trackIdentify, params);
264
+ }, []);
265
+ return {
266
+ track: track2,
267
+ tracking,
268
+ setTrackUserId,
269
+ setIdentify
270
+ };
265
271
  };
266
272
 
267
273
  // src/useAccount.ts
@@ -296,7 +302,7 @@ var useAccount = () => {
296
302
  }, []);
297
303
  const createOrderlyKey = useCallback(
298
304
  async (remember) => {
299
- track2(EnumTrackerKeys.signinSuccess, {
305
+ track2(TrackerEventName.signinSuccess, {
300
306
  network: account5.chainId,
301
307
  wallet: state.connectWallet?.name
302
308
  });
@@ -572,14 +578,22 @@ var AmplitudeTracker = class {
572
578
  amplitude.identify(identify2);
573
579
  }
574
580
  track(eventName, properties) {
575
- amplitude.track(TrackerListenerKeyMap[eventName], properties);
581
+ amplitude.track(eventName, properties);
576
582
  }
577
583
  _bindEvents() {
578
- const listenKeys = Object.keys(TrackerListenerKeyMap);
584
+ const listenKeys = Object.values(TrackerEventName);
579
585
  listenKeys.forEach((key) => {
580
586
  this._ee.addListener(key, (params = {}) => {
581
- if (key === EnumTrackerKeys.trackIdentifyUserId) {
587
+ if (key === TrackerEventName.trackIdentifyUserId) {
582
588
  this.setUserId(params);
589
+ } else if (key === TrackerEventName.trackIdentify) {
590
+ this.identify(params);
591
+ } else if (key === TrackerEventName.trackCustomEvent) {
592
+ const { eventName, ...rest } = params;
593
+ if (!eventName) {
594
+ return;
595
+ }
596
+ this.track(eventName, rest);
583
597
  } else {
584
598
  this.track(key, params);
585
599
  }
@@ -4164,6 +4178,8 @@ var useHoldingStream = () => {
4164
4178
  isLoading
4165
4179
  };
4166
4180
  };
4181
+
4182
+ // src/orderly/useWithdraw.ts
4167
4183
  var useWithdraw = (options) => {
4168
4184
  const { account: account5, state } = useAccount();
4169
4185
  const [isLoading, setIsLoading] = useState(false);
@@ -4213,7 +4229,7 @@ var useWithdraw = (options) => {
4213
4229
  (inputs) => {
4214
4230
  return account5.assetsManager.withdraw(inputs).then((res) => {
4215
4231
  if (res.success) {
4216
- track2(EnumTrackerKeys.withdrawSuccess, {
4232
+ track2(TrackerEventName.withdrawSuccess, {
4217
4233
  wallet: state?.connectWallet?.name,
4218
4234
  network: targetChain?.network_infos.name,
4219
4235
  quantity: inputs.amount
@@ -4221,7 +4237,7 @@ var useWithdraw = (options) => {
4221
4237
  }
4222
4238
  return res;
4223
4239
  }).catch((err) => {
4224
- track2(EnumTrackerKeys.withdrawFailed, {
4240
+ track2(TrackerEventName.withdrawFailed, {
4225
4241
  wallet: state?.connectWallet?.name,
4226
4242
  network: targetChain?.network_infos.name,
4227
4243
  msg: JSON.stringify(err)
@@ -4443,7 +4459,7 @@ var useDeposit = (options) => {
4443
4459
  throw new SDKError("Insufficient allowance");
4444
4460
  }
4445
4461
  return account5.assetsManager.deposit(quantity, depositFee).then((res) => {
4446
- track2(EnumTrackerKeys.depositSuccess, {
4462
+ track2(TrackerEventName.depositSuccess, {
4447
4463
  wallet: state?.connectWallet?.name,
4448
4464
  network: targetChain?.network_infos.name,
4449
4465
  quantity
@@ -4452,7 +4468,7 @@ var useDeposit = (options) => {
4452
4468
  setBalance((value) => new Decimal(value).sub(quantity).toString());
4453
4469
  return res;
4454
4470
  }).catch((e) => {
4455
- track2(EnumTrackerKeys.depositFailed, {
4471
+ track2(TrackerEventName.depositFailed, {
4456
4472
  wallet: state?.connectWallet?.name,
4457
4473
  network: targetChain?.network_infos?.name,
4458
4474
  msg: JSON.stringify(e)
@@ -13892,6 +13908,10 @@ var calcEstLeverage = (order$1, askAndBid, inputs) => {
13892
13908
  }
13893
13909
  });
13894
13910
  };
13911
+ function isBBOOrder(options) {
13912
+ const { order_type, order_type_ext } = options;
13913
+ return order_type === OrderType.LIMIT && [OrderType.ASK, OrderType.BID].includes(order_type_ext);
13914
+ }
13895
13915
  var initialOrderState = {
13896
13916
  order_price: "",
13897
13917
  order_quantity: "",
@@ -13977,7 +13997,7 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
13977
13997
  const calculate = useCallback(
13978
13998
  (values2, fieldName, value, markPrice, config) => {
13979
13999
  const fieldHandler = getCalculateHandler(fieldName);
13980
- let newValues = compose(
14000
+ const newValues = compose(
13981
14001
  head,
13982
14002
  // orderEntityFormatHandle(baseDP, quoteDP),
13983
14003
  fieldHandler,
@@ -14097,11 +14117,11 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14097
14117
  return newValues;
14098
14118
  };
14099
14119
  const onMarkPriceUpdated = useCallback(
14100
- (markPrice, baseOn) => {
14120
+ (markPrice, baseOn = []) => {
14101
14121
  if (!options.symbolInfo)
14102
14122
  return;
14103
- let newValues;
14104
- if (typeof baseOn === "undefined") {
14123
+ let newValues = { ...orderEntity };
14124
+ if (baseOn.length === 0) {
14105
14125
  newValues = calculate(
14106
14126
  { ...orderEntity },
14107
14127
  "order_price",
@@ -14110,13 +14130,15 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14110
14130
  options.symbolInfo
14111
14131
  );
14112
14132
  } else {
14113
- newValues = calculate(
14114
- { ...orderEntity },
14115
- baseOn,
14116
- orderEntity[baseOn],
14117
- markPrice,
14118
- options.symbolInfo
14119
- );
14133
+ baseOn.forEach((key) => {
14134
+ newValues = calculate(
14135
+ { ...newValues },
14136
+ key,
14137
+ orderEntity[key],
14138
+ markPrice,
14139
+ options.symbolInfo
14140
+ );
14141
+ });
14120
14142
  }
14121
14143
  if (hasTPSL(newValues)) {
14122
14144
  if (newValues.tp_pnl && newValues.order_quantity) {
@@ -14169,6 +14191,8 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
14169
14191
  onMarkPriceChange: onMarkPriceUpdated
14170
14192
  };
14171
14193
  };
14194
+
14195
+ // src/next/useOrderEntry/useOrderEntry.ts
14172
14196
  var useOrderEntry2 = (symbol, options = {}) => {
14173
14197
  if (!symbol) {
14174
14198
  throw new SDKError("Symbol is required");
@@ -14185,6 +14209,10 @@ var useOrderEntry2 = (symbol, options = {}) => {
14185
14209
  const lastChangedField = useRef();
14186
14210
  const lastOrderTypeExt = useRef();
14187
14211
  const lastLevel = useRef();
14212
+ const calculateTPSL_baseOn = useRef({
14213
+ tp: "",
14214
+ sl: ""
14215
+ });
14188
14216
  const actions = useMarkPriceActions();
14189
14217
  const symbolConfig = useSymbolsInfo();
14190
14218
  const accountInfo = useAccountInfo();
@@ -14213,24 +14241,26 @@ var useOrderEntry2 = (symbol, options = {}) => {
14213
14241
  formattedOrder.reduce_only
14214
14242
  );
14215
14243
  const updateOrderPrice = () => {
14244
+ const order_type = formattedOrder.order_type;
14216
14245
  const order_type_ext = formattedOrder.order_type_ext ?? lastOrderTypeExt.current;
14217
14246
  const level = formattedOrder.level ?? lastLevel.current;
14218
- if (![OrderType.ASK, OrderType.BID].includes(order_type_ext) || level === void 0) {
14247
+ const isBBO = isBBOOrder({ order_type, order_type_ext });
14248
+ if (!isBBO || level === void 0) {
14219
14249
  return;
14220
14250
  }
14221
14251
  lastOrderTypeExt.current = order_type_ext;
14222
14252
  lastLevel.current = level;
14223
14253
  const index = order_type_ext === OrderType.ASK ? 0 : 1;
14224
14254
  const price = askAndBid.current?.[level]?.[index];
14225
- if (price && !isNaN(price)) {
14255
+ if (price) {
14226
14256
  setValue("order_price", price, {
14227
14257
  shouldUpdateLastChangedField: false
14228
14258
  });
14229
14259
  }
14230
14260
  };
14231
14261
  const updateOrderPriceByOrderBook = () => {
14232
- const { order_type, order_type_ext, order_quantity } = formattedOrder;
14233
- const isBBO = order_type === OrderType.LIMIT && [OrderType.ASK, OrderType.BID].includes(order_type_ext);
14262
+ const { order_type, order_type_ext } = formattedOrder;
14263
+ const isBBO = isBBOOrder({ order_type, order_type_ext });
14234
14264
  if (lastChangedField.current !== "total" && isBBO) {
14235
14265
  updateOrderPrice();
14236
14266
  }
@@ -14250,10 +14280,17 @@ var useOrderEntry2 = (symbol, options = {}) => {
14250
14280
  }, []);
14251
14281
  useEffect(() => {
14252
14282
  if ((formattedOrder.order_type === OrderType.MARKET || formattedOrder.order_type === OrderType.STOP_MARKET) && markPrice) {
14253
- orderEntryActions.onMarkPriceChange(
14254
- markPrice,
14255
- lastChangedField.current
14256
- );
14283
+ const baseOn = /* @__PURE__ */ new Set();
14284
+ if (lastChangedField.current) {
14285
+ baseOn.add(lastChangedField.current);
14286
+ }
14287
+ if (calculateTPSL_baseOn.current.tp) {
14288
+ baseOn.add(calculateTPSL_baseOn.current.tp);
14289
+ }
14290
+ if (calculateTPSL_baseOn.current.sl) {
14291
+ baseOn.add(calculateTPSL_baseOn.current.sl);
14292
+ }
14293
+ orderEntryActions.onMarkPriceChange(markPrice, Array.from(baseOn));
14257
14294
  }
14258
14295
  }, [markPrice, formattedOrder.order_type]);
14259
14296
  const prepareData = useCallback(() => {
@@ -14303,6 +14340,11 @@ var useOrderEntry2 = (symbol, options = {}) => {
14303
14340
  interactiveValidate(values2);
14304
14341
  }
14305
14342
  if (shouldUpdateLastChangedField) {
14343
+ if (key.startsWith("tp_")) {
14344
+ calculateTPSL_baseOn.current.tp = key;
14345
+ } else if (key.startsWith("sl_")) {
14346
+ calculateTPSL_baseOn.current.sl = key;
14347
+ }
14306
14348
  lastChangedField.current = key;
14307
14349
  }
14308
14350
  };
@@ -14421,7 +14463,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
14421
14463
  const order = generateOrder(creator, prepareData());
14422
14464
  const result = await doCreateOrder(order);
14423
14465
  if (result.success) {
14424
- track2(EnumTrackerKeys.placeorderSuccess, {
14466
+ track2(TrackerEventName.placeOrderSuccess, {
14425
14467
  side: order.side,
14426
14468
  order_type: order.order_type,
14427
14469
  tp_sl: hasTPSL(formattedOrder),