@orderly.network/hooks 0.0.53 → 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.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,25 +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
- var usePreLoadData = (onSuccess) => {
330
+ var usePreLoadData = () => {
331
331
  const { configStore } = React2.useContext(OrderlyContext);
332
- useSWR__default.default(
332
+ const { error: swapSupportError, data: swapSupportData } = useSWR__default.default(
333
333
  `${configStore.get("swapSupportApiUrl")}/swap_support`,
334
334
  (url) => fetch(url).then((res) => res.json()),
335
335
  {
336
- revalidateOnFocus: false,
337
- // suspense: true,
338
- onSuccess: (data, key, config) => {
339
- onSuccess("chains_fetch");
340
- }
336
+ revalidateOnFocus: false
341
337
  }
342
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
+ };
343
349
  };
344
350
  var useEventEmitter = (channel) => {
345
351
  return useConstant__default.default(() => {
@@ -1907,7 +1913,6 @@ var useMarketTradeStream = (symbol, options = {}) => {
1907
1913
  unsubscript == null ? void 0 : unsubscript();
1908
1914
  };
1909
1915
  }, [symbol]);
1910
- console.log("trades", trades);
1911
1916
  return { data: trades, isLoading };
1912
1917
  };
1913
1918
  var useMarginRatio = () => {
@@ -1934,56 +1939,106 @@ var useChains = (networkId, options = {}) => {
1934
1939
  const _a = options, { filter, pick, crossEnabled, wooSwapEnabled } = _a, swrOptions = __objRest(_a, ["filter", "pick", "crossEnabled", "wooSwapEnabled"]);
1935
1940
  const { configStore } = React2.useContext(OrderlyContext);
1936
1941
  const field = options == null ? void 0 : options.pick;
1937
- const { data } = useSWR__default.default(
1938
- // () =>
1939
- // wooSwapEnabled
1940
- // ? `${configStore.get("swapSupportApiUrl")}/swap_support`
1941
- // : null,
1942
- `${configStore.get("swapSupportApiUrl")}/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`,
1943
1946
  (url) => fetch(url).then((res) => res.json()),
1944
1947
  __spreadValues({
1945
1948
  revalidateOnFocus: false,
1946
1949
  revalidateOnReconnect: false
1947
1950
  }, swrOptions)
1948
1951
  );
1949
- const { data: orderlyChains } = useQuery("/v1/public/token");
1952
+ const { data: orderlyChains, error: tokenError } = useQuery("/v1/public/token");
1950
1953
  const chains = React2.useMemo(() => {
1951
- if (!data || !data.data || !orderlyChains)
1952
- return data;
1953
- let testnetArr = [];
1954
- let mainnetArr = [];
1955
- Object.keys(data.data).forEach((key) => {
1956
- const item = __spreadProps(__spreadValues({}, data.data[key]), { name: key, priority: 1 });
1957
- if (typeof (options == null ? void 0 : options.filter) === "function") {
1958
- if (!options.filter(item))
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
+ });
1988
+ });
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))
1959
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
+ }
2015
+ });
2016
+ if (orderlyChainIds.size > 0) {
2017
+ testnetArr = [...orderlyChainsArr, ...testnetArr];
1960
2018
  }
1961
- if (item.network_infos.mainnet) {
1962
- mainnetArr.push(field ? item[field] : item);
1963
- } else {
1964
- testnetArr.push(field ? item[field] : item);
2019
+ if (networkId === "mainnet") {
2020
+ return mainnetArr;
1965
2021
  }
1966
- });
1967
- if (networkId === "mainnet") {
1968
- return mainnetArr;
1969
- }
1970
- if (networkId === "testnet") {
1971
- return testnetArr;
2022
+ if (networkId === "testnet") {
2023
+ return testnetArr;
2024
+ }
2025
+ return {
2026
+ testnet: testnetArr,
2027
+ mainnet: mainnetArr
2028
+ };
1972
2029
  }
1973
- return {
1974
- testnet: testnetArr,
1975
- mainnet: mainnetArr
1976
- };
1977
2030
  }, [data, networkId, field, options, orderlyChains, wooSwapEnabled]);
1978
2031
  const findByChainId = React2.useCallback(
1979
- (chainId) => {
1980
- if (!data || !data.data)
1981
- return void 0;
1982
- 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;
1983
2038
  },
1984
- [data]
2039
+ [chains, map.current]
1985
2040
  );
1986
- return [chains, { findByChainId }];
2041
+ return [chains, { findByChainId, error: swapSupportError || tokenError }];
1987
2042
  };
1988
2043
  var useChain = (token) => {
1989
2044
  const { data, isLoading } = useQuery("/v1/public/token");
@@ -2028,25 +2083,78 @@ var useWithdraw = () => {
2028
2083
  }, [usdc, unsettledPnL]);
2029
2084
  return { withdraw, isLoading, maxAmount, availableBalance, unsettledPnL };
2030
2085
  };
2031
- 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);
2032
2091
  const [balance, setBalance] = React2.useState("0");
2033
2092
  const [allowance, setAllowance] = React2.useState("0");
2034
2093
  const { account: account5, state } = useAccount();
2035
- const fetchBalance = React2.useCallback(() => __async(void 0, null, function* () {
2036
- const balance2 = yield account5.assetsManager.getBalance();
2037
- setBalance(() => balance2);
2038
- }), [state]);
2039
- const fetchAllowance = React2.useCallback(() => __async(void 0, null, function* () {
2040
- const allowance2 = yield account5.assetsManager.getAllowance();
2041
- setAllowance(() => allowance2);
2042
- 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;
2043
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
+ );
2044
2152
  React2.useEffect(() => {
2045
2153
  if (state.status < types.AccountStatusEnum.EnableTrading)
2046
2154
  return;
2047
- fetchBalance();
2048
- fetchAllowance();
2049
- }, [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]);
2050
2158
  const approve = React2.useCallback(
2051
2159
  (amount) => {
2052
2160
  return account5.assetsManager.approve(amount).then((result) => {
@@ -2061,7 +2169,6 @@ var useDeposit = () => {
2061
2169
  const deposit = React2.useCallback(
2062
2170
  (amount) => {
2063
2171
  return account5.assetsManager.deposit(amount).then((res) => {
2064
- console.log("----- deposit -----", res);
2065
2172
  setAllowance((value) => new utils.Decimal(value).sub(amount).toString());
2066
2173
  setBalance((value) => new utils.Decimal(value).sub(amount).toString());
2067
2174
  return res;
@@ -2072,8 +2179,12 @@ var useDeposit = () => {
2072
2179
  return {
2073
2180
  balance,
2074
2181
  allowance,
2182
+ balanceRevalidating,
2183
+ allowanceRevalidating,
2075
2184
  approve,
2076
- deposit
2185
+ deposit,
2186
+ fetchBalances,
2187
+ fetchBalance: fetchBalanceHandler
2077
2188
  };
2078
2189
  };
2079
2190
  var useWalletSubscription = (options) => {