@orderly.network/hooks 2.6.3 → 2.7.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.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.6.3";
64
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.7.0";
65
65
  }
66
- var version_default = "2.6.3";
66
+ var version_default = "2.7.0";
67
67
  var fetcher = (url, init2 = {}, queryOptions) => net.get(url, init2, queryOptions?.formatter);
68
68
  var noCacheConfig = {
69
69
  dedupingInterval: 0,
@@ -490,15 +490,17 @@ var useUpdatedRef = (val) => {
490
490
  return latestRef;
491
491
  };
492
492
  var useMemoizedFn = (fn) => {
493
- const fnRef = React2.useRef(fn);
494
- fnRef.current = React2.useMemo(() => fn, [fn]);
495
- const memoizedFn = React2.useRef(void 0);
496
- if (!memoizedFn.current) {
497
- memoizedFn.current = function(...args) {
493
+ const safeFn = typeof fn === "function" ? fn : () => {
494
+ };
495
+ const fnRef = React2.useRef(safeFn);
496
+ fnRef.current = React2.useMemo(() => safeFn, [safeFn]);
497
+ const wrapperRef = React2.useRef(null);
498
+ if (!wrapperRef.current) {
499
+ wrapperRef.current = function(...args) {
498
500
  return fnRef.current.apply(this, args);
499
501
  };
500
502
  }
501
- return memoizedFn.current;
503
+ return wrapperRef.current;
502
504
  };
503
505
  var useAudioPlayer = (src, options = {}) => {
504
506
  const { volume = 1, loop, autoPlay } = options;
@@ -2166,13 +2168,13 @@ var useFeeState = () => {
2166
2168
  const makerFeeBps = accountInfo?.futures_maker_fee_rate;
2167
2169
  const refereeRebate = referralData?.referee_info?.referee_rebate_rate;
2168
2170
  const takerFee = React2.useMemo(() => {
2169
- if (isAccountLoading || takerFeeBps == null) {
2171
+ if (isAccountLoading || takerFeeBps === null || takerFeeBps === void 0) {
2170
2172
  return "-";
2171
2173
  }
2172
2174
  return formatFractionAsPercent(bpsToFraction(takerFeeBps));
2173
2175
  }, [isAccountLoading, takerFeeBps]);
2174
2176
  const makerFee = React2.useMemo(() => {
2175
- if (isAccountLoading || makerFeeBps == null) {
2177
+ if (isAccountLoading || makerFeeBps === null || makerFeeBps === void 0) {
2176
2178
  return "-";
2177
2179
  }
2178
2180
  return formatFractionAsPercent(bpsToFraction(makerFeeBps));
@@ -2201,15 +2203,10 @@ var useFeeState = () => {
2201
2203
  }, [makerFeeBps, refereeRebate, isReferralLoading]);
2202
2204
  return {
2203
2205
  takerFee,
2204
- // 原始 taker 费率(百分比字符串)
2205
2206
  makerFee,
2206
- // 原始 maker 费率(百分比字符串)
2207
2207
  refereeRebate,
2208
- // 返佣(fraction),例如 0.2
2209
2208
  effectiveTakerFee,
2210
- // 有效 taker 费率(百分比字符串)
2211
2209
  effectiveMakerFee
2212
- // 有效 maker 费率(百分比字符串)
2213
2210
  };
2214
2211
  };
2215
2212
  var apiKeyMap = {
@@ -2624,6 +2621,7 @@ var useAppStore = zustand.create()(
2624
2621
  }))
2625
2622
  );
2626
2623
  var useAccountInfo2 = () => useAppStore((state) => state.accountInfo);
2624
+ var usePortfolio = () => useAppStore((state) => state.portfolio);
2627
2625
 
2628
2626
  // src/orderly/useSymbolsInfo.ts
2629
2627
  var useSymbolsInfo = () => {
@@ -3496,8 +3494,12 @@ var PositionCalculator = class extends BaseCalculator {
3496
3494
  markPrice: item.mark_price
3497
3495
  });
3498
3496
  let unrealPnl_index = 0, unrealPnlROI_index = 0;
3497
+ const maxLeverage = perp.account.maxLeverage({
3498
+ symbolLeverage: item.leverage,
3499
+ accountLeverage: accountInfo.max_leverage
3500
+ });
3499
3501
  const imr = perp.account.IMR({
3500
- maxLeverage: accountInfo.max_leverage,
3502
+ maxLeverage,
3501
3503
  baseIMR: info?.["base_imr"],
3502
3504
  IMR_Factor: accountInfo.imr_factor[item.symbol],
3503
3505
  positionNotional: notional,
@@ -4419,6 +4421,12 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
4419
4421
  }
4420
4422
  ];
4421
4423
  };
4424
+ var useSymbolInfo = (symbol) => {
4425
+ const infos = useSymbolsInfo();
4426
+ return React2.useMemo(() => {
4427
+ return !symbol || infos.isNil ? null : infos[symbol];
4428
+ }, [infos, symbol]);
4429
+ };
4422
4430
  var useFundingRates = () => {
4423
4431
  const data = useAppStore((state) => state.fundingRates);
4424
4432
  return createGetter({ ...data });
@@ -4842,6 +4850,13 @@ var useFundingRate = (symbol) => {
4842
4850
  }
4843
4851
  timerRef.current = setInterval(() => {
4844
4852
  const diff = new Date(next_funding_time).getTime() - utils.getTimestamp();
4853
+ if (diff <= 0) {
4854
+ setCountDown("00:00:00");
4855
+ if (timerRef.current) {
4856
+ clearInterval(timerRef.current);
4857
+ }
4858
+ return;
4859
+ }
4845
4860
  const result = utils.timeConvertString(diff);
4846
4861
  if (result.length === 3) {
4847
4862
  setCountDown(
@@ -4879,7 +4894,8 @@ var useFundingDetails = (symbol) => {
4879
4894
  throw new types.SDKError("symbol is required");
4880
4895
  }
4881
4896
  return useQuery(`/v1/public/info/${symbol}`, {
4882
- errorRetryCount: 3
4897
+ errorRetryCount: 3,
4898
+ revalidateOnFocus: false
4883
4899
  });
4884
4900
  };
4885
4901
  var calculatePositiveRate = (periodData, period) => {
@@ -5457,12 +5473,45 @@ var useCollateral = (options = { dp: 6 }) => {
5457
5473
  // positions: positionsPath(positions),
5458
5474
  };
5459
5475
  };
5476
+ var useLeverageBySymbol = (symbol) => {
5477
+ const { state } = useAccount();
5478
+ const ws = useWS();
5479
+ const [leverage, setLeverage] = React2.useState(void 0);
5480
+ const { data } = usePrivateQuery(
5481
+ symbol ? `/v1/client/leverage?symbol=${symbol}` : null,
5482
+ {
5483
+ revalidateOnFocus: false
5484
+ }
5485
+ );
5486
+ React2.useEffect(() => {
5487
+ if (data?.leverage) {
5488
+ setLeverage(data.leverage);
5489
+ }
5490
+ }, [data]);
5491
+ React2.useEffect(() => {
5492
+ if (!state.accountId || !symbol)
5493
+ return;
5494
+ const unsubscribe = ws.privateSubscribe("account", {
5495
+ onMessage: (data2) => {
5496
+ const res = data2?.accountDetail?.symbolLeverage || {};
5497
+ if (res.symbol === symbol) {
5498
+ setLeverage(res.leverage);
5499
+ }
5500
+ }
5501
+ });
5502
+ return () => unsubscribe?.();
5503
+ }, [symbol, state.accountId]);
5504
+ return leverage;
5505
+ };
5506
+
5507
+ // src/orderly/useMaxQty.ts
5460
5508
  var useMaxQty = (symbol, side, reduceOnly = false) => {
5461
5509
  const positions3 = usePositions();
5462
5510
  const accountInfo = useAccountInfo2();
5463
5511
  const symbolInfo = useSymbolsInfo();
5464
5512
  const { totalCollateral } = useCollateral();
5465
5513
  const { data: markPrices } = useMarkPricesStream();
5514
+ const symbolLeverage = useLeverageBySymbol(symbol);
5466
5515
  const maxQty = React2.useMemo(() => {
5467
5516
  if (!symbol)
5468
5517
  return 0;
@@ -5508,7 +5557,10 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
5508
5557
  symbol,
5509
5558
  baseMaxQty: getSymbolInfo("base_max"),
5510
5559
  totalCollateral,
5511
- maxLeverage: accountInfo.max_leverage,
5560
+ maxLeverage: perp.account.maxLeverage({
5561
+ symbolLeverage: symbolLeverage || currentSymbolPosition?.leverage,
5562
+ accountLeverage: accountInfo.max_leverage
5563
+ }),
5512
5564
  takerFeeRate: accountInfo.futures_taker_fee_rate,
5513
5565
  baseIMR: getSymbolInfo("base_imr"),
5514
5566
  otherIMs,
@@ -5592,6 +5644,11 @@ function useChains(networkId, options = {}) {
5592
5644
  const filterFun = React2.useRef(options?.filter);
5593
5645
  filterFun.current = options?.filter;
5594
5646
  const chainsMap = React2.useRef(/* @__PURE__ */ new Map());
5647
+ const brokerId = configStore.get("brokerId");
5648
+ const env = configStore.get("env");
5649
+ const brokerIdQuery = brokerId !== "orderly" ? `?broker_id=${brokerId}` : "";
5650
+ const urlPrefix = env === "prod" ? "https://testnet-api.orderly.org" : "";
5651
+ const needFetchFromAPI = options.forceAPI || !customChains;
5595
5652
  const commonSwrOpts = {
5596
5653
  revalidateIfStale: false,
5597
5654
  revalidateOnFocus: false,
@@ -5607,22 +5664,18 @@ function useChains(networkId, options = {}) {
5607
5664
  { ...commonSwrOpts }
5608
5665
  );
5609
5666
  const { data: testTokenChainsRes } = useQuery(
5610
- "https://testnet-api.orderly.org/v1/public/token",
5667
+ `${urlPrefix}/v1/public/token`,
5611
5668
  {
5612
5669
  ...commonSwrOpts,
5613
5670
  fallbackData: testnetTokenFallback
5614
5671
  }
5615
5672
  );
5616
- const brokerId = configStore.get("brokerId");
5617
- const env = configStore.get("env");
5618
- const brokerIdQuery = brokerId !== "orderly" ? `?broker_id=${brokerId}` : "";
5619
- const needFetchFromAPI = options.forceAPI || !customChains;
5620
5673
  const { data: chainInfos, error: chainInfoErr } = useQuery(
5621
5674
  needFetchFromAPI ? `https://api.orderly.org/v1/public/chain_info${brokerIdQuery}` : null,
5622
5675
  { ...commonSwrOpts }
5623
5676
  );
5624
5677
  const { data: testChainInfos, error: testChainInfoError } = useQuery(
5625
- needFetchFromAPI ? `https://testnet-api.orderly.org/v1/public/chain_info${brokerIdQuery}` : null,
5678
+ needFetchFromAPI ? `${urlPrefix}/v1/public/chain_info${brokerIdQuery}` : null,
5626
5679
  {
5627
5680
  ...commonSwrOpts,
5628
5681
  fallbackData: testnetChainFallback,
@@ -5630,10 +5683,6 @@ function useChains(networkId, options = {}) {
5630
5683
  }
5631
5684
  }
5632
5685
  );
5633
- const { data: envChainInfos, error: envChainInfoError } = useQuery(
5634
- needFetchFromAPI && env !== "prod" ? `/v1/public/chain_info${brokerIdQuery}` : null,
5635
- commonSwrOpts
5636
- );
5637
5686
  const { swapChains, swapChainsError } = useSwapChains();
5638
5687
  const chains = React2.useMemo(() => {
5639
5688
  const mainnetChains = formatChains({
@@ -5646,7 +5695,7 @@ function useChains(networkId, options = {}) {
5646
5695
  });
5647
5696
  const testnetChains = formatChains({
5648
5697
  tokenChains: testTokenChainsRes,
5649
- chainInfos: formatTestnetChainInfos(testChainInfos, envChainInfos),
5698
+ chainInfos: testChainInfos,
5650
5699
  swapChains,
5651
5700
  mainnet: false,
5652
5701
  chainTransformer
@@ -5688,8 +5737,7 @@ function useChains(networkId, options = {}) {
5688
5737
  pickField,
5689
5738
  allowedChains,
5690
5739
  swapChains,
5691
- chainTransformer,
5692
- envChainInfos
5740
+ chainTransformer
5693
5741
  ]);
5694
5742
  const findByChainId = React2.useCallback(
5695
5743
  (chainId, field) => {
@@ -5895,18 +5943,6 @@ function formatChains({
5895
5943
  }
5896
5944
  return chains;
5897
5945
  }
5898
- function formatTestnetChainInfos(chainInfos, envChainInfos) {
5899
- if (!chainInfos || !envChainInfos || !Array.isArray(chainInfos) || !Array.isArray(envChainInfos)) {
5900
- return chainInfos;
5901
- }
5902
- return chainInfos.map((chain) => {
5903
- const info = envChainInfos.find((item) => item.chain_id === chain.chain_id);
5904
- if (info) {
5905
- chain.vault_address = info.vault_address;
5906
- }
5907
- return chain;
5908
- });
5909
- }
5910
5946
  function useStorageChain() {
5911
5947
  const [chain, setChain] = useLocalStorage(types.ChainKey, null);
5912
5948
  const setStorageChain = (chainId) => {
@@ -5944,6 +5980,13 @@ var useChain = (token) => {
5944
5980
  }, [data, token]);
5945
5981
  return { chains, isLoading };
5946
5982
  };
5983
+
5984
+ // src/orderly/useChainInfo.ts
5985
+ var useChainInfo = () => {
5986
+ return useQuery("/v1/public/chain_info", {
5987
+ revalidateOnFocus: false
5988
+ });
5989
+ };
5947
5990
  var useHoldingStream = () => {
5948
5991
  const ws = useWS();
5949
5992
  const { data, isLoading, mutate: mutate5 } = usePrivateQuery(
@@ -8864,8 +8907,12 @@ function formatPositions(data, accountInfo, symbolsInfo, fundingRates) {
8864
8907
  markPrice: item.mark_price
8865
8908
  });
8866
8909
  let unrealPnl_index = 0, unrealPnlROI_index = 0;
8910
+ const maxLeverage = perp.account.maxLeverage({
8911
+ symbolLeverage: item.leverage,
8912
+ accountLeverage: accountInfo.max_leverage
8913
+ });
8867
8914
  const imr = perp.account.IMR({
8868
- maxLeverage: accountInfo.max_leverage,
8915
+ maxLeverage,
8869
8916
  baseIMR: info?.("base_imr"),
8870
8917
  IMR_Factor: accountInfo.imr_factor[item.symbol],
8871
8918
  positionNotional: notional,
@@ -9522,29 +9569,36 @@ var useTPSLOrder = (position, options) => {
9522
9569
  const result = useTaskProfitAndStopLossInternal(position, options);
9523
9570
  return result;
9524
9571
  };
9525
- var useSymbolLeverage = (symbol) => {
9526
- const { data: info } = useAccountInfo();
9527
- const maxAccountLeverage = info?.max_leverage;
9528
- const res = useQuery(`/v1/public/info/${symbol}`, {
9529
- dedupingInterval: 1e3 * 60 * 60 * 24,
9530
- // 24 hours
9531
- revalidateOnFocus: false,
9532
- errorRetryCount: 2,
9533
- errorRetryInterval: 200
9534
- });
9572
+ var useMaxLeverage = (symbol) => {
9573
+ const symbolsInfo = useSymbolsInfo();
9574
+ const accountInfo = useAccountInfo2();
9575
+ const maxAccountLeverage = accountInfo?.max_leverage;
9535
9576
  const maxSymbolLeverage = React2.useMemo(() => {
9536
- const base = res?.data?.base_imr;
9537
- if (base)
9538
- return 1 / base;
9539
- }, [res]);
9577
+ const symbolInfo = symbolsInfo[symbol];
9578
+ const baseIMR = symbolInfo("base_imr");
9579
+ return baseIMR ? 1 / baseIMR : 1;
9580
+ }, [symbolsInfo, symbol]);
9540
9581
  const maxLeverage = React2.useMemo(() => {
9541
9582
  if (!maxAccountLeverage || !maxSymbolLeverage) {
9542
- return "-";
9583
+ return 1;
9543
9584
  }
9544
9585
  return Math.min(maxAccountLeverage, maxSymbolLeverage);
9545
9586
  }, [maxAccountLeverage, maxSymbolLeverage]);
9546
9587
  return maxLeverage;
9547
9588
  };
9589
+ var useSymbolLeverage = (symbol) => {
9590
+ const symbolInfo = useSymbolInfo(symbol);
9591
+ const [update, { isMutating }] = useMutation("/v1/client/leverage");
9592
+ const maxLeverage = React2.useMemo(() => {
9593
+ const baseIMR = symbolInfo?.("base_imr");
9594
+ return baseIMR ? 1 / baseIMR : 1;
9595
+ }, [symbolInfo]);
9596
+ return {
9597
+ maxLeverage,
9598
+ update,
9599
+ isLoading: isMutating
9600
+ };
9601
+ };
9548
9602
  var useAssetsHistory = (options, config) => {
9549
9603
  const ee = useEventEmitter();
9550
9604
  const getKey = () => {
@@ -9821,12 +9875,12 @@ var MaintenanceStatus = /* @__PURE__ */ ((MaintenanceStatus2) => {
9821
9875
  MaintenanceStatus2[MaintenanceStatus2["Maintenance"] = 2] = "Maintenance";
9822
9876
  return MaintenanceStatus2;
9823
9877
  })(MaintenanceStatus || {});
9824
- function useMaintenanceStatus() {
9878
+ var useMaintenanceStatus = () => {
9825
9879
  const [status, setStatus] = React2.useState(0 /* None */);
9826
9880
  const [startTime, setStartTime] = React2.useState();
9827
9881
  const [endTime, setEndTime] = React2.useState();
9828
9882
  const [brokerName, setBrokerName] = React2.useState("Orderly network");
9829
- const { data: systemInfo, mutate: mutate5 } = useQuery(
9883
+ const { data: systemInfo } = useQuery(
9830
9884
  `/v1/public/system_info?source=maintenance`,
9831
9885
  {
9832
9886
  revalidateOnFocus: false,
@@ -9872,7 +9926,7 @@ function useMaintenanceStatus() {
9872
9926
  startTime,
9873
9927
  endTime
9874
9928
  };
9875
- }
9929
+ };
9876
9930
  var useStorageLedgerAddress = () => {
9877
9931
  const [ledgerWallet, setLedgerWallet] = useLocalStorage(types.LedgerWalletKey, []);
9878
9932
  const setLedgerAddress = (address) => {
@@ -10055,6 +10109,36 @@ var usePrivateDataObserver = (options) => {
10055
10109
  });
10056
10110
  return () => unsubscribe?.();
10057
10111
  }, [state.accountId, subOrder]);
10112
+ React2.useEffect(() => {
10113
+ if (!state.accountId)
10114
+ return;
10115
+ const key = ["/v1/positions", state.accountId];
10116
+ const unsubscribe = ws.privateSubscribe("account", {
10117
+ onMessage: (data) => {
10118
+ const { symbol, leverage } = data?.accountDetail?.symbolLeverage || {};
10119
+ if (symbol && leverage) {
10120
+ useSWR.mutate(
10121
+ key,
10122
+ (prevPositions) => {
10123
+ if (prevPositions?.rows?.length) {
10124
+ return {
10125
+ ...prevPositions,
10126
+ rows: prevPositions.rows.map((row) => {
10127
+ return row.symbol === symbol ? { ...row, leverage } : row;
10128
+ })
10129
+ };
10130
+ }
10131
+ return prevPositions;
10132
+ },
10133
+ {
10134
+ revalidate: false
10135
+ }
10136
+ );
10137
+ }
10138
+ }
10139
+ });
10140
+ return () => unsubscribe?.();
10141
+ }, [state.accountId]);
10058
10142
  React2.useEffect(() => {
10059
10143
  if (!state.accountId) {
10060
10144
  return;
@@ -17744,6 +17828,7 @@ exports.useBalanceSubscription = useBalanceSubscription;
17744
17828
  exports.useBalanceTopic = useBalanceTopic;
17745
17829
  exports.useBoolean = useBoolean;
17746
17830
  exports.useChain = useChain;
17831
+ exports.useChainInfo = useChainInfo;
17747
17832
  exports.useChains = useChains;
17748
17833
  exports.useCheckReferralCode = useCheckReferralCode;
17749
17834
  exports.useCollateral = useCollateral;
@@ -17776,6 +17861,7 @@ exports.useInternalTransfer = useInternalTransfer;
17776
17861
  exports.useKeyStore = useKeyStore;
17777
17862
  exports.useLazyQuery = useLazyQuery;
17778
17863
  exports.useLeverage = useLeverage;
17864
+ exports.useLeverageBySymbol = useLeverageBySymbol;
17779
17865
  exports.useLocalStorage = useLocalStorage;
17780
17866
  exports.useMaintenanceStatus = useMaintenanceStatus;
17781
17867
  exports.useMarginRatio = useMarginRatio;
@@ -17789,6 +17875,7 @@ exports.useMarketTradeStream = useMarketTradeStream;
17789
17875
  exports.useMarkets = useMarkets;
17790
17876
  exports.useMarketsStore = useMarketsStore;
17791
17877
  exports.useMarketsStream = useMarketsStream;
17878
+ exports.useMaxLeverage = useMaxLeverage;
17792
17879
  exports.useMaxQty = useMaxQty;
17793
17880
  exports.useMediaQuery = useMediaQuery;
17794
17881
  exports.useMemoizedFn = useMemoizedFn;
@@ -17802,6 +17889,7 @@ exports.useOrderStore = useOrderStore2;
17802
17889
  exports.useOrderStream = useOrderStream;
17803
17890
  exports.useOrderbookStream = useOrderbookStream;
17804
17891
  exports.useOrderlyContext = useOrderlyContext;
17892
+ exports.usePortfolio = usePortfolio;
17805
17893
  exports.usePositionActions = usePositionActions;
17806
17894
  exports.usePositionClose = usePositionClose;
17807
17895
  exports.usePositionStream = usePositionStream;
@@ -17828,6 +17916,7 @@ exports.useSubAccountMaxWithdrawal = useSubAccountMaxWithdrawal;
17828
17916
  exports.useSubAccountMutation = useSubAccountMutation;
17829
17917
  exports.useSubAccountQuery = useSubAccountQuery;
17830
17918
  exports.useSubAccountWS = useSubAccountWS;
17919
+ exports.useSymbolInfo = useSymbolInfo;
17831
17920
  exports.useSymbolLeverage = useSymbolLeverage;
17832
17921
  exports.useSymbolPriceRange = useSymbolPriceRange;
17833
17922
  exports.useSymbolsInfo = useSymbolsInfo;