@orderly.network/hooks 0.0.52 → 0.0.54

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
@@ -79,8 +79,6 @@ interface OrderlyContextState extends OrderlyAppConfig {
79
79
  onWalletConnect?: () => Promise<any>;
80
80
  onWalletDisconnect?: () => Promise<any>;
81
81
  onSetChain?: (chainId: number) => Promise<any>;
82
- ready: boolean;
83
- onAppTestChange?: (name: string) => void;
84
82
  errors: AppStateErrors;
85
83
  }
86
84
  declare const OrderlyContext: react.Context<OrderlyContextState>;
@@ -88,10 +86,12 @@ declare const OrderlyProvider: react.Provider<OrderlyContextState>;
88
86
 
89
87
  declare const useAppState: () => {
90
88
  errors: AppStateErrors;
91
- ready: boolean;
92
89
  };
93
90
 
94
- declare const usePreLoadData: (onSuccess: (name: string) => void) => void;
91
+ declare const usePreLoadData: () => {
92
+ error: any;
93
+ done: boolean;
94
+ };
95
95
 
96
96
  declare const useEventEmitter: (channel?: string) => EventEmitter<string | symbol, any>;
97
97
 
@@ -264,6 +264,8 @@ declare const useMarginRatio: () => {
264
264
  type inputOptions = {
265
265
  filter?: (item: API.Chain) => boolean;
266
266
  pick?: "dexs" | "network_infos" | "token_infos";
267
+ crossEnabled?: boolean;
268
+ wooSwapEnabled?: boolean;
267
269
  };
268
270
  declare const useChains: (networkId?: "testnet" | "mainnet", options?: inputOptions & SWRConfiguration) => any[];
269
271
 
@@ -284,11 +286,18 @@ declare const useWithdraw: () => {
284
286
  unsettledPnL: number;
285
287
  };
286
288
 
287
- declare const useDeposit: () => {
289
+ type useDepositOptions = {
290
+ address?: string;
291
+ };
292
+ declare const useDeposit: (options?: useDepositOptions) => {
288
293
  balance: string;
289
294
  allowance: string;
295
+ balanceRevalidating: boolean;
296
+ allowanceRevalidating: boolean;
290
297
  approve: (amount: string | undefined) => Promise<any>;
291
298
  deposit: (amount: string) => Promise<any>;
299
+ fetchBalances: (tokens: API.TokenInfo[]) => Promise<void>;
300
+ fetchBalance: (address: string) => Promise<string>;
292
301
  };
293
302
 
294
303
  declare const useHoldingStream: () => {
package/dist/index.d.ts CHANGED
@@ -79,8 +79,6 @@ interface OrderlyContextState extends OrderlyAppConfig {
79
79
  onWalletConnect?: () => Promise<any>;
80
80
  onWalletDisconnect?: () => Promise<any>;
81
81
  onSetChain?: (chainId: number) => Promise<any>;
82
- ready: boolean;
83
- onAppTestChange?: (name: string) => void;
84
82
  errors: AppStateErrors;
85
83
  }
86
84
  declare const OrderlyContext: react.Context<OrderlyContextState>;
@@ -88,10 +86,12 @@ declare const OrderlyProvider: react.Provider<OrderlyContextState>;
88
86
 
89
87
  declare const useAppState: () => {
90
88
  errors: AppStateErrors;
91
- ready: boolean;
92
89
  };
93
90
 
94
- declare const usePreLoadData: (onSuccess: (name: string) => void) => void;
91
+ declare const usePreLoadData: () => {
92
+ error: any;
93
+ done: boolean;
94
+ };
95
95
 
96
96
  declare const useEventEmitter: (channel?: string) => EventEmitter<string | symbol, any>;
97
97
 
@@ -264,6 +264,8 @@ declare const useMarginRatio: () => {
264
264
  type inputOptions = {
265
265
  filter?: (item: API.Chain) => boolean;
266
266
  pick?: "dexs" | "network_infos" | "token_infos";
267
+ crossEnabled?: boolean;
268
+ wooSwapEnabled?: boolean;
267
269
  };
268
270
  declare const useChains: (networkId?: "testnet" | "mainnet", options?: inputOptions & SWRConfiguration) => any[];
269
271
 
@@ -284,11 +286,18 @@ declare const useWithdraw: () => {
284
286
  unsettledPnL: number;
285
287
  };
286
288
 
287
- declare const useDeposit: () => {
289
+ type useDepositOptions = {
290
+ address?: string;
291
+ };
292
+ declare const useDeposit: (options?: useDepositOptions) => {
288
293
  balance: string;
289
294
  allowance: string;
295
+ balanceRevalidating: boolean;
296
+ allowanceRevalidating: boolean;
290
297
  approve: (amount: string | undefined) => Promise<any>;
291
298
  deposit: (amount: string) => Promise<any>;
299
+ fetchBalances: (tokens: API.TokenInfo[]) => Promise<void>;
300
+ fetchBalance: (address: string) => Promise<string>;
292
301
  };
293
302
 
294
303
  declare const useHoldingStream: () => {
package/dist/index.js CHANGED
@@ -30,26 +30,26 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
30
30
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
31
31
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
32
32
  var __spreadValues = (a, b) => {
33
- for (var prop2 in b || (b = {}))
34
- if (__hasOwnProp.call(b, prop2))
35
- __defNormalProp(a, prop2, b[prop2]);
33
+ for (var prop3 in b || (b = {}))
34
+ if (__hasOwnProp.call(b, prop3))
35
+ __defNormalProp(a, prop3, b[prop3]);
36
36
  if (__getOwnPropSymbols)
37
- for (var prop2 of __getOwnPropSymbols(b)) {
38
- if (__propIsEnum.call(b, prop2))
39
- __defNormalProp(a, prop2, b[prop2]);
37
+ for (var prop3 of __getOwnPropSymbols(b)) {
38
+ if (__propIsEnum.call(b, prop3))
39
+ __defNormalProp(a, prop3, b[prop3]);
40
40
  }
41
41
  return a;
42
42
  };
43
43
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
44
44
  var __objRest = (source, exclude) => {
45
45
  var target = {};
46
- for (var prop2 in source)
47
- if (__hasOwnProp.call(source, prop2) && exclude.indexOf(prop2) < 0)
48
- target[prop2] = source[prop2];
46
+ for (var prop3 in source)
47
+ if (__hasOwnProp.call(source, prop3) && exclude.indexOf(prop3) < 0)
48
+ target[prop3] = source[prop3];
49
49
  if (source != null && __getOwnPropSymbols)
50
- for (var prop2 of __getOwnPropSymbols(source)) {
51
- if (exclude.indexOf(prop2) < 0 && __propIsEnum.call(source, prop2))
52
- target[prop2] = source[prop2];
50
+ for (var prop3 of __getOwnPropSymbols(source)) {
51
+ if (exclude.indexOf(prop3) < 0 && __propIsEnum.call(source, prop3))
52
+ target[prop3] = source[prop3];
53
53
  }
54
54
  return target;
55
55
  };
@@ -321,26 +321,31 @@ var useTopicObserve = (topic) => {
321
321
  };
322
322
  };
323
323
  var useAppState = () => {
324
- const { errors, ready } = React2.useContext(OrderlyContext);
324
+ const { errors } = React2.useContext(OrderlyContext);
325
325
  return {
326
- errors,
327
- ready
326
+ errors
327
+ // ready,
328
328
  };
329
329
  };
330
-
331
- // src/usePreloadData.ts
332
- var usePreLoadData = (onSuccess) => {
333
- useSWR__default.default(
334
- "https://fi-api.woo.org/swap_support",
330
+ var usePreLoadData = () => {
331
+ const { configStore } = React2.useContext(OrderlyContext);
332
+ const { error: swapSupportError, data: swapSupportData } = useSWR__default.default(
333
+ `${configStore.get("swapSupportApiUrl")}/swap_support`,
335
334
  (url) => fetch(url).then((res) => res.json()),
336
335
  {
337
- revalidateOnFocus: false,
338
- // suspense: true,
339
- onSuccess: (data, key, config) => {
340
- onSuccess("chains_fetch");
341
- }
336
+ revalidateOnFocus: false
342
337
  }
343
338
  );
339
+ const { error: tokenError, data: tokenData } = useQuery("/v1/public/token", {
340
+ revalidateOnFocus: false
341
+ });
342
+ const isDone = React2.useMemo(() => {
343
+ return !!swapSupportData && !!tokenData;
344
+ }, [swapSupportData, tokenData]);
345
+ return {
346
+ error: swapSupportError || tokenError,
347
+ done: isDone
348
+ };
344
349
  };
345
350
  var useEventEmitter = (channel) => {
346
351
  return useConstant__default.default(() => {
@@ -1123,7 +1128,7 @@ var usePositionStream = (symbol, options) => {
1123
1128
  const MMR = futures.positions.MMR({
1124
1129
  baseMMR: info("base_mmr"),
1125
1130
  baseIMR: info("base_imr"),
1126
- IMRFactor: accountInfo.imr_factor[info("base")],
1131
+ IMRFactor: accountInfo.imr_factor[item.symbol],
1127
1132
  positionNotional: item.notional,
1128
1133
  IMR_factor_power: 4 / 5
1129
1134
  });
@@ -1343,7 +1348,7 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
1343
1348
  positionQty,
1344
1349
  buyOrdersQty,
1345
1350
  sellOrdersQty,
1346
- IMR_Factor: accountInfo.imr_factor[getSymbolInfo("base")]
1351
+ IMR_Factor: accountInfo.imr_factor[symbol]
1347
1352
  });
1348
1353
  }, [
1349
1354
  orders,
@@ -1908,7 +1913,6 @@ var useMarketTradeStream = (symbol, options = {}) => {
1908
1913
  unsubscript == null ? void 0 : unsubscript();
1909
1914
  };
1910
1915
  }, [symbol]);
1911
- console.log("trades", trades);
1912
1916
  return { data: trades, isLoading };
1913
1917
  };
1914
1918
  var useMarginRatio = () => {
@@ -1932,51 +1936,109 @@ var useMarginRatio = () => {
1932
1936
  return { marginRatio, currentLeverage };
1933
1937
  };
1934
1938
  var useChains = (networkId, options = {}) => {
1935
- const _a = options, swrOptions = __objRest(_a, ["filter", "pick"]);
1939
+ const _a = options, { filter, pick, crossEnabled, wooSwapEnabled } = _a, swrOptions = __objRest(_a, ["filter", "pick", "crossEnabled", "wooSwapEnabled"]);
1940
+ const { configStore } = React2.useContext(OrderlyContext);
1936
1941
  const field = options == null ? void 0 : options.pick;
1937
- const { data } = useSWR__default.default(
1938
- "https://fi-api.woo.org/swap_support",
1942
+ const map = React2.useRef(/* @__PURE__ */ new Map());
1943
+ const { data, error: swapSupportError } = useSWR__default.default(
1944
+ () => wooSwapEnabled ? `${configStore.get("swapSupportApiUrl")}/swap_support` : null,
1945
+ // `${configStore.get("swapSupportApiUrl")}/swap_support`,
1939
1946
  (url) => fetch(url).then((res) => res.json()),
1940
1947
  __spreadValues({
1941
1948
  revalidateOnFocus: false,
1942
1949
  revalidateOnReconnect: false
1943
1950
  }, swrOptions)
1944
1951
  );
1952
+ const { data: orderlyChains, error: tokenError } = useQuery("/v1/public/token");
1945
1953
  const chains = React2.useMemo(() => {
1946
- if (!data || !data.data)
1947
- return data;
1948
- let arr = [];
1949
- Object.keys(data.data).forEach((key) => {
1950
- const item = data.data[key];
1951
- arr.push(__spreadProps(__spreadValues({}, item), {
1952
- name: key
1953
- }));
1954
+ if (!orderlyChains)
1955
+ return void 0;
1956
+ const orderlyChainsArr = [];
1957
+ const orderlyChainIds = /* @__PURE__ */ new Set();
1958
+ orderlyChains.forEach((item) => {
1959
+ item.chain_details.forEach((chain) => {
1960
+ var _a2;
1961
+ const chainId = Number(chain.chain_id);
1962
+ orderlyChainIds.add(chainId);
1963
+ const chainInfo = types.chainsMap.get(chainId);
1964
+ const _chain = {
1965
+ network_infos: {
1966
+ name: (_a2 = chainInfo == null ? void 0 : chainInfo.chainName) != null ? _a2 : "--",
1967
+ // "public_rpc_url": "https://arb1.arbitrum.io/rpc",
1968
+ chain_id: chainId,
1969
+ // decimals: chain.decimals,
1970
+ // contract_address: chain.contract_address,
1971
+ bridgeless: true
1972
+ },
1973
+ token_infos: [
1974
+ {
1975
+ symbol: item.token,
1976
+ address: chain.contract_address,
1977
+ decimals: chain.decimals
1978
+ }
1979
+ ]
1980
+ };
1981
+ if (typeof (options == null ? void 0 : options.filter) === "function") {
1982
+ if (!options.filter(_chain))
1983
+ return;
1984
+ }
1985
+ map.current.set(chainId, _chain);
1986
+ orderlyChainsArr.push(field ? _chain[field] : _chain);
1987
+ });
1954
1988
  });
1955
- if (networkId === "mainnet") {
1956
- arr = arr.filter((item) => item.network_infos.mainnet);
1957
- }
1958
- if (networkId === "testnet") {
1959
- arr = arr.filter((item) => !item.network_infos.mainnet);
1960
- }
1961
- if (typeof (options == null ? void 0 : options.filter) === "function") {
1962
- arr = arr.filter(options.filter);
1963
- }
1964
- if (typeof field !== "undefined") {
1965
- return arr.map((item) => {
1966
- return item[field];
1989
+ if (!wooSwapEnabled) {
1990
+ return orderlyChainsArr;
1991
+ } else {
1992
+ if (!data || !data.data)
1993
+ return data;
1994
+ let testnetArr = [];
1995
+ let mainnetArr = [];
1996
+ Object.keys(data.data).forEach((key) => {
1997
+ var _a2;
1998
+ if (orderlyChainIds.has(data.data[key].network_infos.chain_id))
1999
+ return;
2000
+ const item = __spreadProps(__spreadValues({}, data.data[key]), {
2001
+ name: key
2002
+ });
2003
+ if (((_a2 = item.token_infos) == null ? void 0 : _a2.length) === 0)
2004
+ return;
2005
+ map.current.set(item.network_infos.chain_id, item);
2006
+ if (typeof (options == null ? void 0 : options.filter) === "function") {
2007
+ if (!options.filter(item))
2008
+ return;
2009
+ }
2010
+ if (item.network_infos.mainnet) {
2011
+ mainnetArr.push(field ? item[field] : item);
2012
+ } else {
2013
+ testnetArr.push(field ? item[field] : item);
2014
+ }
1967
2015
  });
2016
+ if (orderlyChainIds.size > 0) {
2017
+ testnetArr = [...orderlyChainsArr, ...testnetArr];
2018
+ }
2019
+ if (networkId === "mainnet") {
2020
+ return mainnetArr;
2021
+ }
2022
+ if (networkId === "testnet") {
2023
+ return testnetArr;
2024
+ }
2025
+ return {
2026
+ testnet: testnetArr,
2027
+ mainnet: mainnetArr
2028
+ };
1968
2029
  }
1969
- return arr;
1970
- }, [data, networkId, field, options]);
2030
+ }, [data, networkId, field, options, orderlyChains, wooSwapEnabled]);
1971
2031
  const findByChainId = React2.useCallback(
1972
- (chainId) => {
1973
- if (!data || !data.data)
1974
- return void 0;
1975
- return data.data[chainId];
2032
+ (chainId, field2) => {
2033
+ const chain = map.current.get(chainId);
2034
+ if (typeof field2 === "string") {
2035
+ return ramda.prop(field2, chain);
2036
+ }
2037
+ return chain;
1976
2038
  },
1977
- [data]
2039
+ [chains, map.current]
1978
2040
  );
1979
- return [chains, { findByChainId }];
2041
+ return [chains, { findByChainId, error: swapSupportError || tokenError }];
1980
2042
  };
1981
2043
  var useChain = (token) => {
1982
2044
  const { data, isLoading } = useQuery("/v1/public/token");
@@ -2021,25 +2083,78 @@ var useWithdraw = () => {
2021
2083
  }, [usdc, unsettledPnL]);
2022
2084
  return { withdraw, isLoading, maxAmount, availableBalance, unsettledPnL };
2023
2085
  };
2024
- var useDeposit = () => {
2086
+ var nativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
2087
+ var isNativeToken = (address) => address === nativeTokenAddress;
2088
+ var useDeposit = (options) => {
2089
+ const [balanceRevalidating, setBalanceRevalidating] = React2.useState(false);
2090
+ const [allowanceRevalidating, setAllowanceRevalidating] = React2.useState(false);
2025
2091
  const [balance, setBalance] = React2.useState("0");
2026
2092
  const [allowance, setAllowance] = React2.useState("0");
2027
2093
  const { account: account5, state } = useAccount();
2028
- const fetchBalance = React2.useCallback(() => __async(void 0, null, function* () {
2029
- const balance2 = yield account5.assetsManager.getBalance();
2030
- setBalance(() => balance2);
2031
- }), [state]);
2032
- const fetchAllowance = React2.useCallback(() => __async(void 0, null, function* () {
2033
- const allowance2 = yield account5.assetsManager.getAllowance();
2034
- setAllowance(() => allowance2);
2035
- return allowance2;
2094
+ const fetchBalanceHandler = React2.useCallback((address) => __async(void 0, null, function* () {
2095
+ let balance2;
2096
+ if (!!address && isNativeToken(address)) {
2097
+ balance2 = yield account5.assetsManager.getNativeBalance();
2098
+ } else {
2099
+ balance2 = yield account5.assetsManager.getBalance(address);
2100
+ }
2101
+ return balance2;
2036
2102
  }), []);
2103
+ const fetchBalance = React2.useCallback(
2104
+ (address) => __async(void 0, null, function* () {
2105
+ if (!address)
2106
+ return;
2107
+ try {
2108
+ if (balanceRevalidating)
2109
+ return;
2110
+ setBalanceRevalidating(true);
2111
+ const balance2 = yield fetchBalanceHandler(address);
2112
+ console.log("----- refresh balance -----", balance2);
2113
+ setBalance(() => balance2);
2114
+ setBalanceRevalidating(false);
2115
+ } catch (e) {
2116
+ console.warn("----- refresh balance error -----", e);
2117
+ setBalanceRevalidating(false);
2118
+ setBalance(() => "0");
2119
+ }
2120
+ }),
2121
+ [state, balanceRevalidating]
2122
+ );
2123
+ const fetchBalances = React2.useCallback((tokens) => __async(void 0, null, function* () {
2124
+ const tasks = [];
2125
+ console.log("fetch balances ---->>>>", tokens);
2126
+ for (const token of tokens) {
2127
+ if (isNativeToken(token.address)) {
2128
+ continue;
2129
+ }
2130
+ tasks.push(account5.assetsManager.getBalanceByAddress(token.address));
2131
+ }
2132
+ const balances = yield Promise.all(tasks);
2133
+ console.log("----- get balances from tokens -----", balances);
2134
+ }), []);
2135
+ const fetchAllowance = React2.useCallback(
2136
+ (address) => __async(void 0, null, function* () {
2137
+ if (!address)
2138
+ return;
2139
+ if (address && isNativeToken(address))
2140
+ return;
2141
+ if (allowanceRevalidating)
2142
+ return;
2143
+ setAllowanceRevalidating(true);
2144
+ const allowance2 = yield account5.assetsManager.getAllowance(address);
2145
+ console.log("----- refresh allowance -----", allowance2);
2146
+ setAllowance(() => allowance2);
2147
+ setAllowanceRevalidating(false);
2148
+ return allowance2;
2149
+ }),
2150
+ [allowanceRevalidating]
2151
+ );
2037
2152
  React2.useEffect(() => {
2038
2153
  if (state.status < types.AccountStatusEnum.EnableTrading)
2039
2154
  return;
2040
- fetchBalance();
2041
- fetchAllowance();
2042
- }, [state]);
2155
+ fetchBalance(options == null ? void 0 : options.address);
2156
+ fetchAllowance(options == null ? void 0 : options.address);
2157
+ }, [state.status, options == null ? void 0 : options.address]);
2043
2158
  const approve = React2.useCallback(
2044
2159
  (amount) => {
2045
2160
  return account5.assetsManager.approve(amount).then((result) => {
@@ -2054,7 +2169,6 @@ var useDeposit = () => {
2054
2169
  const deposit = React2.useCallback(
2055
2170
  (amount) => {
2056
2171
  return account5.assetsManager.deposit(amount).then((res) => {
2057
- console.log("----- deposit -----", res);
2058
2172
  setAllowance((value) => new utils.Decimal(value).sub(amount).toString());
2059
2173
  setBalance((value) => new utils.Decimal(value).sub(amount).toString());
2060
2174
  return res;
@@ -2065,8 +2179,12 @@ var useDeposit = () => {
2065
2179
  return {
2066
2180
  balance,
2067
2181
  allowance,
2182
+ balanceRevalidating,
2183
+ allowanceRevalidating,
2068
2184
  approve,
2069
- deposit
2185
+ deposit,
2186
+ fetchBalances,
2187
+ fetchBalance: fetchBalanceHandler
2070
2188
  };
2071
2189
  };
2072
2190
  var useWalletSubscription = (options) => {