@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.mjs CHANGED
@@ -1,10 +1,11 @@
1
1
  import { get, WS, mutate as mutate$1 } from '@orderly.network/net';
2
+ import React, { createContext, useContext, useCallback, useState, useEffect, useMemo, useRef, useId, useLayoutEffect } from 'react';
2
3
  import useSWR5__default, { mutate } from 'swr';
3
4
  import * as useSWR5 from 'swr';
4
5
  export { useSWR5 as swr };
5
6
  export { unstable_serialize, default as useSWR, useSWRConfig } from 'swr';
7
+ import { usePluginScope } from '@orderly.network/plugin-core';
6
8
  import { TesnetTokenFallback, ArbitrumSepoliaTokenInfo, SolanaDevnetTokenInfo, OrderType, OrderSide, MarginMode, SDKError, TrackerEventName, AccountStatusEnum, AlgoOrderType, AlgoOrderRootType, OrderStatus, ArbitrumSepoliaChainInfo, SolanaDevnetChainInfo, EMPTY_LIST, EMPTY_OBJECT, isNativeTokenChecker, nativeTokenAddress, ChainKey, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, ChainNamespace, MaxUint256, DEPOSIT_FEE_RATE, ETHEREUM_MAINNET_CHAINID, LedgerWalletKey, SOLANA_TESTNET_CHAINID, MONAD_TESTNET_CHAINID, ABSTRACT_TESTNET_CHAINID, BSC_TESTNET_CHAINID, SolanaChains, PositionType, DistributionType, TriggerPriceType } from '@orderly.network/types';
7
- import React, { createContext, useContext, useCallback, useState, useEffect, useMemo, useRef, useId, useLayoutEffect } from 'react';
8
9
  import { zero, windowGuard, getTimestamp, getGlobalObject, Decimal, timeConvertString, isTestnet, getPrecisionByNumber, getBBOType, camelCaseToUnderscoreCase, commify, todpIfNeed, getTPSLDirection } from '@orderly.network/utils';
9
10
  import useSWRMutation from 'swr/mutation';
10
11
  import useConstant from 'use-constant';
@@ -38,9 +39,9 @@ var __export = (target, all) => {
38
39
  // src/version.ts
39
40
  if (typeof window !== "undefined") {
40
41
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
41
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.11.0";
42
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "3.0.0-beta.1";
42
43
  }
43
- var version_default = "2.11.0";
44
+ var version_default = "3.0.0-beta.1";
44
45
  var fetcher = (url, init2 = {}, queryOptions) => get(url, init2, queryOptions?.formatter);
45
46
  var noCacheConfig = {
46
47
  dedupingInterval: 0,
@@ -73,19 +74,26 @@ function useConfig(key, defaultValue) {
73
74
  }
74
75
 
75
76
  // src/useQuery.ts
77
+ var PLUGIN_ID_HEADER = "X-Orderly-Plugin-Id";
76
78
  var useQuery = (query, options) => {
77
79
  const apiBaseUrl = useConfig("apiBaseUrl");
80
+ const pluginScope = usePluginScope();
78
81
  const { formatter, ...swrOptions } = options || {};
79
82
  if (typeof apiBaseUrl === "undefined") {
80
83
  throw new SDKError("please add OrderlyConfigProvider to your app");
81
84
  }
82
- return useSWR5__default(
83
- query,
84
- (url, init2) => fetcher(url.startsWith("http") ? url : `${apiBaseUrl}${url}`, init2, {
85
- formatter
86
- }),
87
- swrOptions
85
+ const fetcherFn = useCallback(
86
+ (url, init2) => {
87
+ const fullUrl = url.startsWith("http") ? url : `${apiBaseUrl}${url}`;
88
+ const headers = new Headers(init2?.headers);
89
+ if (pluginScope?.pluginId) {
90
+ headers.set(PLUGIN_ID_HEADER, pluginScope.pluginId);
91
+ }
92
+ return fetcher(fullUrl, { ...init2, headers }, { formatter });
93
+ },
94
+ [apiBaseUrl, pluginScope?.pluginId, formatter]
88
95
  );
96
+ return useSWR5__default(query, fetcherFn, swrOptions);
89
97
  };
90
98
  var timestampOffsetPromise = null;
91
99
  var timestampOffsetReady = false;
@@ -5517,34 +5525,18 @@ var PositionCalculator = class extends BaseCalculator {
5517
5525
  rows = rows.map((item) => {
5518
5526
  const info = symbolsInfo[item.symbol];
5519
5527
  const totalCollateral = item.margin_mode === MarginMode.ISOLATED ? new Decimal(item.margin ?? 0).add(item.unsettlement_pnl ?? 0).toNumber() : crossMarginCollateral.toNumber();
5520
- let est_liq_price;
5521
- if (item.margin_mode === MarginMode.ISOLATED) {
5522
- est_liq_price = positions.liquidationPriceIsolated({
5523
- isolatedPositionMargin: item.margin ?? 0,
5524
- costPosition: item.cost_position ?? 0,
5525
- positionQty: item.position_qty ?? 0,
5526
- sumUnitaryFunding: fundingRates?.[item.symbol]?.["sum_unitary_funding"] ?? 0,
5527
- lastSumUnitaryFunding: item.last_sum_unitary_funding ?? 0,
5528
- baseMMR: info?.["base_mmr"] ?? 0,
5529
- baseIMR: info?.["base_imr"] ?? 0,
5530
- IMRFactor: accountInfo.imr_factor[item.symbol],
5531
- referencePrice: item.mark_price,
5532
- leverage: item.leverage ?? 0
5533
- });
5534
- } else {
5535
- est_liq_price = positions.liqPrice({
5536
- symbol: item.symbol,
5537
- markPrice: item.mark_price,
5538
- totalCollateral,
5539
- positionQty: item.position_qty,
5540
- positions: rows,
5541
- MMR: item.mmr,
5542
- baseMMR: info?.["base_mmr"],
5543
- baseIMR: info?.["base_imr"],
5544
- IMRFactor: accountInfo.imr_factor[item.symbol],
5545
- costPosition: item.cost_position
5546
- });
5547
- }
5528
+ const est_liq_price = positions.liqPrice({
5529
+ symbol: item.symbol,
5530
+ markPrice: item.mark_price,
5531
+ totalCollateral,
5532
+ positionQty: item.position_qty,
5533
+ positions: rows,
5534
+ MMR: item.mmr,
5535
+ baseMMR: info?.["base_mmr"],
5536
+ baseIMR: info?.["base_imr"],
5537
+ IMRFactor: accountInfo.imr_factor[item.symbol],
5538
+ costPosition: item.cost_position
5539
+ });
5548
5540
  return {
5549
5541
  ...item,
5550
5542
  est_liq_price
@@ -10959,7 +10951,7 @@ var calcEstLiqPrice = (order$1, askAndBid, inputs) => {
10959
10951
  totalCollateral,
10960
10952
  futures_taker_fee_rate,
10961
10953
  positions: positions3,
10962
- symbolLeverage,
10954
+ // leverage,
10963
10955
  sumUnitaryFunding
10964
10956
  } = inputs;
10965
10957
  const orderFee = order.orderFee({
@@ -10984,7 +10976,7 @@ var calcEstLiqPrice = (order$1, askAndBid, inputs) => {
10984
10976
  }
10985
10977
  });
10986
10978
  } else {
10987
- let isolatedPositionMargin = 0, costPosition = 0, positionQty = 0, lastSumUnitaryFunding = 0, leverage = symbolLeverage ?? 1;
10979
+ let isolatedPositionMargin = 0, costPosition = 0, positionQty = 0, lastSumUnitaryFunding = 0, leverage = 1;
10988
10980
  if (positions3) {
10989
10981
  const position = positions3.find(
10990
10982
  (p) => p.symbol === symbol && p.margin_mode === MarginMode.ISOLATED
@@ -18750,12 +18742,37 @@ var useOrderEntry2 = (symbol, options = {}) => {
18750
18742
  );
18751
18743
  const bestAskBid = askAndBid.current?.[0] || [];
18752
18744
  const referencePriceFromOrder = bestAskBid.length >= 2 && formattedOrder.order_type && formattedOrder.side ? getOrderReferencePriceFromOrder(formattedOrder, bestAskBid) : null;
18753
- const maxQtyValue = useMaxQty(symbol, formattedOrder.side, {
18745
+ const maxBuyQtyValue = useMaxQty(symbol, OrderSide.BUY, {
18754
18746
  reduceOnly: formattedOrder.reduce_only,
18755
18747
  marginMode: effectiveMarginMode,
18756
18748
  currentOrderReferencePrice: referencePriceFromOrder && referencePriceFromOrder > 0 ? referencePriceFromOrder : void 0
18757
18749
  });
18750
+ const maxSellQtyValue = useMaxQty(symbol, OrderSide.SELL, {
18751
+ reduceOnly: formattedOrder.reduce_only,
18752
+ marginMode: effectiveMarginMode,
18753
+ currentOrderReferencePrice: referencePriceFromOrder && referencePriceFromOrder > 0 ? referencePriceFromOrder : void 0
18754
+ });
18755
+ const maxQtyValue = formattedOrder.side === OrderSide.BUY ? maxBuyQtyValue : maxSellQtyValue;
18758
18756
  const maxQty = options.maxQty ?? maxQtyValue;
18757
+ const maxQtys = useMemo(
18758
+ () => ({
18759
+ maxBuy: formattedOrder.side === OrderSide.BUY ? (
18760
+ // @ts-ignore
18761
+ options.maxQty ?? maxBuyQtyValue
18762
+ ) : maxBuyQtyValue,
18763
+ maxSell: formattedOrder.side === OrderSide.SELL ? (
18764
+ // @ts-ignore
18765
+ options.maxQty ?? maxSellQtyValue
18766
+ ) : maxSellQtyValue
18767
+ }),
18768
+ [
18769
+ formattedOrder.side,
18770
+ maxBuyQtyValue,
18771
+ maxSellQtyValue,
18772
+ // @ts-ignore
18773
+ options.maxQty
18774
+ ]
18775
+ );
18759
18776
  const updateOrderPrice = () => {
18760
18777
  const order_type = formattedOrder.order_type;
18761
18778
  const order_type_ext = formattedOrder.order_type_ext ?? lastOrderTypeExt.current;
@@ -18991,8 +19008,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
18991
19008
  symbol,
18992
19009
  positions: positions3,
18993
19010
  symbolInfo,
18994
- sumUnitaryFunding,
18995
- symbolLeverage
19011
+ sumUnitaryFunding
18996
19012
  });
18997
19013
  return estLiqPrice2;
18998
19014
  }, [
@@ -19003,8 +19019,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
19003
19019
  symbol,
19004
19020
  maxQty,
19005
19021
  symbolInfo,
19006
- fundingRates,
19007
- symbolLeverage
19022
+ fundingRates
19008
19023
  ]);
19009
19024
  const estLiqPriceDistance = useMemo(() => {
19010
19025
  if (!estLiqPrice) {
@@ -19127,6 +19142,7 @@ var useOrderEntry2 = (symbol, options = {}) => {
19127
19142
  resetMetaState,
19128
19143
  formattedOrder,
19129
19144
  maxQty,
19145
+ maxQtys,
19130
19146
  estLiqPrice,
19131
19147
  estLiqPriceDistance,
19132
19148
  currentPosition,