@orderly.network/hooks 0.0.68 → 0.0.69

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
@@ -5,7 +5,7 @@ import * as _orderly_network_core from '@orderly.network/core';
5
5
  import { Account, AccountState, ConfigStore, OrderlyKeyStore, getWalletAdapterFunc, IContract, EventEmitter } from '@orderly.network/core';
6
6
  import * as react from 'react';
7
7
  import react__default, { FC, PropsWithChildren } from 'react';
8
- import { NetworkId, OrderSide, OrderEntity, API, WSMessage } from '@orderly.network/types';
8
+ import { NetworkId, OrderSide, OrderEntity, API, WSMessage, WS_WalletStatusEnum } from '@orderly.network/types';
9
9
  export { default as useConstant } from 'use-constant';
10
10
  import { WS } from '@orderly.network/net';
11
11
  import * as swr__internal from 'swr/_internal';
@@ -307,8 +307,11 @@ declare const useWithdraw: () => WithdrawReturns;
307
307
  type useDepositOptions = {
308
308
  address?: string;
309
309
  decimals?: number;
310
- vaultAddress?: string;
310
+ crossChainRouteAddress?: string;
311
+ depositorAddress?: string;
311
312
  networkId?: NetworkId;
313
+ srcChainId?: number;
314
+ srcToken?: string;
312
315
  };
313
316
  declare const useDeposit: (options?: useDepositOptions) => {
314
317
  dst: {
@@ -448,7 +451,8 @@ declare const useCrossSwap: () => {
448
451
  readonly value: bigint;
449
452
  } | undefined>;
450
453
  loading: boolean;
451
- status: MessageStatus;
454
+ bridgeStatus: MessageStatus;
455
+ status: WS_WalletStatusEnum;
452
456
  message: any;
453
457
  };
454
458
 
@@ -459,6 +463,9 @@ declare const useSwap: () => {
459
463
  toToken: string;
460
464
  minToAmount: string;
461
465
  orderlyNativeFees: bigint;
466
+ }, config: {
467
+ dst: any;
468
+ src: any;
462
469
  }) => Promise<{
463
470
  readonly hash: string;
464
471
  readonly to: string | null;
@@ -466,6 +473,7 @@ declare const useSwap: () => {
466
473
  readonly value: bigint;
467
474
  } | undefined>;
468
475
  loading: boolean;
476
+ status: WS_WalletStatusEnum;
469
477
  };
470
478
 
471
479
  export { AppStateErrors, DataSourceProvider, OrderStatus, OrderlyAppConfig, OrderlyContext, OrderlyContextState, OrderlyProvider, index as apis, useAccount, useAccountInfo, useAccountInstance, useAppState, useBalance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useExecutionReport, useFetures, useFundingRate, useHoldingStream, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateQuery, useQuery, useRunOnce, useSessionStorage, useSwap, useSymbolsInfo, useTickerStream, useTokenInfo, useTopicObserve, useTradingView, useWS, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ import * as _orderly_network_core from '@orderly.network/core';
5
5
  import { Account, AccountState, ConfigStore, OrderlyKeyStore, getWalletAdapterFunc, IContract, EventEmitter } from '@orderly.network/core';
6
6
  import * as react from 'react';
7
7
  import react__default, { FC, PropsWithChildren } from 'react';
8
- import { NetworkId, OrderSide, OrderEntity, API, WSMessage } from '@orderly.network/types';
8
+ import { NetworkId, OrderSide, OrderEntity, API, WSMessage, WS_WalletStatusEnum } from '@orderly.network/types';
9
9
  export { default as useConstant } from 'use-constant';
10
10
  import { WS } from '@orderly.network/net';
11
11
  import * as swr__internal from 'swr/_internal';
@@ -307,8 +307,11 @@ declare const useWithdraw: () => WithdrawReturns;
307
307
  type useDepositOptions = {
308
308
  address?: string;
309
309
  decimals?: number;
310
- vaultAddress?: string;
310
+ crossChainRouteAddress?: string;
311
+ depositorAddress?: string;
311
312
  networkId?: NetworkId;
313
+ srcChainId?: number;
314
+ srcToken?: string;
312
315
  };
313
316
  declare const useDeposit: (options?: useDepositOptions) => {
314
317
  dst: {
@@ -448,7 +451,8 @@ declare const useCrossSwap: () => {
448
451
  readonly value: bigint;
449
452
  } | undefined>;
450
453
  loading: boolean;
451
- status: MessageStatus;
454
+ bridgeStatus: MessageStatus;
455
+ status: WS_WalletStatusEnum;
452
456
  message: any;
453
457
  };
454
458
 
@@ -459,6 +463,9 @@ declare const useSwap: () => {
459
463
  toToken: string;
460
464
  minToAmount: string;
461
465
  orderlyNativeFees: bigint;
466
+ }, config: {
467
+ dst: any;
468
+ src: any;
462
469
  }) => Promise<{
463
470
  readonly hash: string;
464
471
  readonly to: string | null;
@@ -466,6 +473,7 @@ declare const useSwap: () => {
466
473
  readonly value: bigint;
467
474
  } | undefined>;
468
475
  loading: boolean;
476
+ status: WS_WalletStatusEnum;
469
477
  };
470
478
 
471
479
  export { AppStateErrors, DataSourceProvider, OrderStatus, OrderlyAppConfig, OrderlyContext, OrderlyContextState, OrderlyProvider, index as apis, useAccount, useAccountInfo, useAccountInstance, useAppState, useBalance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useExecutionReport, useFetures, useFundingRate, useHoldingStream, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateQuery, useQuery, useRunOnce, useSessionStorage, useSwap, useSymbolsInfo, useTickerStream, useTokenInfo, useTopicObserve, useTradingView, useWS, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
package/dist/index.js CHANGED
@@ -2932,6 +2932,7 @@ var woofiDexCrossChainRouterAbi = [
2932
2932
  }
2933
2933
  ];
2934
2934
  var nativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
2935
+ var isNativeTokenChecker = (address) => address === nativeTokenAddress;
2935
2936
 
2936
2937
  // src/orderly/useChains.ts
2937
2938
  var useChains = (networkId, options = {}) => {
@@ -2975,9 +2976,10 @@ var useChains = (networkId, options = {}) => {
2975
2976
  item.chain_details.forEach((chain) => {
2976
2977
  const chainId = Number(chain.chain_id);
2977
2978
  orderlyChainIds.add(chainId);
2979
+ const chainInfo = types.chainsInfoMap.get(chainId);
2978
2980
  const _chain = {
2979
2981
  network_infos: {
2980
- name: chain.chain_name ?? "--",
2982
+ name: chain.chain_name ?? chainInfo?.chainName ?? "--",
2981
2983
  // "public_rpc_url": "https://arb1.arbitrum.io/rpc",
2982
2984
  chain_id: chainId,
2983
2985
  withdrawal_fee: chain.withdrawal_fee,
@@ -3108,7 +3110,7 @@ var useChain = (token) => {
3108
3110
  let item = data.find((chain) => chain.token === token);
3109
3111
  if (item) {
3110
3112
  item.chain_details = item.chain_details.map((d) => {
3111
- const chain = types.chainsMap.get(Number(d.chain_id));
3113
+ const chain = types.chainsInfoMap.get(Number(d.chain_id));
3112
3114
  return {
3113
3115
  ...d,
3114
3116
  chain_name: chain?.chainName ?? "--"
@@ -3142,9 +3144,7 @@ var useWithdraw = () => {
3142
3144
  }, [usdc, unsettledPnL]);
3143
3145
  return { withdraw, isLoading, maxAmount, availableBalance, unsettledPnL };
3144
3146
  };
3145
- var isNativeTokenChecker = (address) => address === nativeTokenAddress;
3146
3147
  var useDeposit = (options) => {
3147
- console.log("useDeposit options:", options);
3148
3148
  const { onlyTestnet } = React2.useContext(OrderlyContext);
3149
3149
  const [balanceRevalidating, setBalanceRevalidating] = React2.useState(false);
3150
3150
  const [allowanceRevalidating, setAllowanceRevalidating] = React2.useState(false);
@@ -3154,6 +3154,7 @@ var useDeposit = (options) => {
3154
3154
  const [balance, setBalance] = React2.useState("0");
3155
3155
  const [allowance, setAllowance] = React2.useState("0");
3156
3156
  const { account: account5, state } = useAccount();
3157
+ const prevAddress = React2.useRef();
3157
3158
  const dst = React2.useMemo(() => {
3158
3159
  const chain = onlyTestnet ? findByChainId(types.ARBITRUM_TESTNET_CHAINID) : findByChainId(types.ARBITRUM_MAINNET_CHAINID);
3159
3160
  const USDC = chain?.token_infos.find((token) => token.symbol === "USDC");
@@ -3219,46 +3220,85 @@ var useDeposit = (options) => {
3219
3220
  const balances = await Promise.all(tasks);
3220
3221
  console.log("----- get balances from tokens -----", balances);
3221
3222
  }, []);
3222
- const getAllowance = React2.useCallback(
3223
- async (address, vaultAddress) => {
3224
- console.log("getAllowance", address, vaultAddress);
3225
- if (!address || !vaultAddress)
3226
- return;
3227
- if (address && isNativeTokenChecker(address))
3228
- return;
3229
- if (allowanceRevalidating)
3230
- return;
3231
- setAllowanceRevalidating(true);
3232
- const allowance2 = await account5.assetsManager.getAllowance(
3233
- address,
3234
- vaultAddress
3235
- );
3236
- console.log("----- refresh allowance -----", allowance2);
3237
- setAllowance(() => allowance2);
3238
- setAllowanceRevalidating(false);
3239
- return allowance2;
3240
- },
3241
- [allowanceRevalidating]
3242
- );
3223
+ const getAllowance = async (address, vaultAddress) => {
3224
+ const key = `${address}-${vaultAddress}`;
3225
+ if (prevAddress.current === key)
3226
+ return;
3227
+ if (!address || !vaultAddress)
3228
+ return;
3229
+ if (address && isNativeTokenChecker(address))
3230
+ return;
3231
+ console.log("getAllowance", address, vaultAddress);
3232
+ prevAddress.current = key;
3233
+ const allowance2 = await account5.assetsManager.getAllowance(
3234
+ address,
3235
+ vaultAddress
3236
+ );
3237
+ console.log("----- refresh allowance -----", {
3238
+ allowance: allowance2
3239
+ });
3240
+ setAllowance(() => allowance2);
3241
+ return allowance2;
3242
+ };
3243
+ const getAllowanceByDefaultAddress = async (address) => {
3244
+ if (prevAddress.current === address)
3245
+ return;
3246
+ if (!address || isNativeTokenChecker(address))
3247
+ return;
3248
+ prevAddress.current = address;
3249
+ const allowance2 = await account5.assetsManager.getAllowance(address);
3250
+ console.log(
3251
+ "----- refresh allowance only orderly -----",
3252
+ { allowance: allowance2 }
3253
+ );
3254
+ setAllowance(() => allowance2);
3255
+ };
3256
+ const getVaultAddress = React2.useCallback(() => {
3257
+ if (dst.chainId !== options?.srcChainId) {
3258
+ return options?.crossChainRouteAddress;
3259
+ } else {
3260
+ if (dst.symbol !== options?.srcToken) {
3261
+ return options?.depositorAddress;
3262
+ }
3263
+ }
3264
+ }, [options, dst]);
3243
3265
  React2.useEffect(() => {
3244
3266
  if (state.status < types.AccountStatusEnum.Connected)
3245
3267
  return;
3246
3268
  fetchBalance(options?.address);
3247
- getAllowance(options?.address, options?.vaultAddress);
3248
- }, [state.status, options?.address, options?.vaultAddress, account5.address]);
3269
+ if (dst.chainId !== options?.srcChainId) {
3270
+ getAllowance(options?.address, options?.crossChainRouteAddress);
3271
+ } else {
3272
+ if (dst.symbol !== options?.srcToken) {
3273
+ getAllowance(options?.address, options?.depositorAddress);
3274
+ } else {
3275
+ getAllowanceByDefaultAddress(options?.address);
3276
+ }
3277
+ }
3278
+ }, [
3279
+ state.status,
3280
+ options?.address,
3281
+ options?.crossChainRouteAddress,
3282
+ options?.depositorAddress,
3283
+ options?.srcChainId,
3284
+ options?.srcToken,
3285
+ account5.address,
3286
+ dst
3287
+ ]);
3249
3288
  const approve = React2.useCallback(
3250
3289
  (amount) => {
3251
3290
  if (!options?.address) {
3252
3291
  throw new Error("address is required");
3253
3292
  }
3254
- return account5.assetsManager.approve(options.address, amount, options?.vaultAddress).then((result) => {
3293
+ const vaultAddress = getVaultAddress();
3294
+ return account5.assetsManager.approve(options.address, amount, vaultAddress).then((result) => {
3255
3295
  if (typeof amount !== "undefined") {
3256
3296
  setAllowance((value) => new utils.Decimal(value).add(amount).toString());
3257
3297
  }
3258
3298
  return result;
3259
3299
  });
3260
3300
  },
3261
- [account5, getAllowance, options?.address, options?.vaultAddress]
3301
+ [account5, getAllowance, options?.address]
3262
3302
  );
3263
3303
  const deposit = React2.useCallback(
3264
3304
  (amount) => {
@@ -3586,24 +3626,37 @@ var useCrossSwap = () => {
3586
3626
  "WAITTING" /* INITIALIZING */
3587
3627
  );
3588
3628
  const [bridgeMessage, setBridgeMessage] = React2.useState();
3629
+ const [status, setStatus] = React2.useState(
3630
+ types.WS_WalletStatusEnum.NO
3631
+ );
3632
+ const txHashFromBridge = React2.useRef();
3589
3633
  const account5 = useAccountInstance();
3590
3634
  const { networkId, configStore } = React2.useContext(OrderlyContext);
3591
3635
  const client = React2.useRef(scanClient.createClient(networkId)).current;
3592
3636
  const timer = React2.useRef();
3637
+ useWalletSubscription({
3638
+ onMessage: (message) => {
3639
+ const { side, transStatus, trxId } = message;
3640
+ if (side === "DEPOSIT" && trxId === txHashFromBridge.current) {
3641
+ setStatus(transStatus);
3642
+ }
3643
+ }
3644
+ });
3593
3645
  const checkLayerStatus = React2.useCallback((txHash) => {
3594
3646
  const check = async (txHash2) => {
3595
3647
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3596
3648
  console.log("messages:", messages);
3597
3649
  if (messages.length > 0) {
3598
- const { status } = messages[0];
3599
- if (status === "INFLIGHT" /* INFLIGHT */) {
3650
+ const { status: status2 } = messages[0];
3651
+ if (status2 === "INFLIGHT" /* INFLIGHT */) {
3600
3652
  setTimeout(() => {
3601
3653
  check(txHash2);
3602
3654
  }, 1e3);
3603
3655
  }
3604
- setLayerStatus(status);
3605
- if (status === "DELIVERED" /* DELIVERED */) {
3656
+ setLayerStatus(status2);
3657
+ if (status2 === "DELIVERED" /* DELIVERED */) {
3606
3658
  setBridgeMessage(messages[0]);
3659
+ txHashFromBridge.current = messages[0].dstTxHash;
3607
3660
  }
3608
3661
  } else {
3609
3662
  setTimeout(() => {
@@ -3657,20 +3710,12 @@ var useCrossSwap = () => {
3657
3710
  from: account5.address,
3658
3711
  to: crossChainRouteAddress,
3659
3712
  data: [account5.address, src, dst, dstValutDeposit()],
3660
- value: quotoLZFee[0]
3713
+ value: isNativeTokenChecker(inputs.src.fromToken) ? BigInt(inputs.src.fromAmount) + quotoLZFee[0] : quotoLZFee[0]
3661
3714
  },
3662
3715
  {
3663
3716
  abi: woofiDexCrossChainRouterAbi
3664
3717
  }
3665
3718
  );
3666
- account5.walletClient.on(
3667
- {
3668
- address: crossChainRouteAddress
3669
- },
3670
- (log, event) => {
3671
- console.log("-------------", log, event);
3672
- }
3673
- );
3674
3719
  stop();
3675
3720
  checkLayerStatus(result.hash);
3676
3721
  return ramda.pick(["from", "to", "hash", "value"], result);
@@ -3683,7 +3728,8 @@ var useCrossSwap = () => {
3683
3728
  return {
3684
3729
  swap,
3685
3730
  loading,
3686
- status: layerStatus,
3731
+ bridgeStatus: layerStatus,
3732
+ status,
3687
3733
  message: bridgeMessage
3688
3734
  };
3689
3735
  };
@@ -3965,15 +4011,28 @@ var useSwap = () => {
3965
4011
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3966
4012
  const account5 = useAccountInstance();
3967
4013
  const { configStore } = React2.useContext(OrderlyContext);
4014
+ const [status, setStatus] = React2.useState(
4015
+ types.WS_WalletStatusEnum.NO
4016
+ );
4017
+ const txHash = React2.useRef();
4018
+ useWalletSubscription({
4019
+ onMessage: (message) => {
4020
+ const { side, transStatus, trxId } = message;
4021
+ if (side === "DEPOSIT" && trxId === txHash.current) {
4022
+ setStatus(transStatus);
4023
+ }
4024
+ }
4025
+ });
3968
4026
  const dstValutDeposit = React2.useCallback(() => {
4027
+ const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
3969
4028
  return {
3970
4029
  accountId: account5.accountIdHashStr,
3971
- brokerHash: core.utils.parseBrokerHash(configStore.get("brokerId")),
4030
+ brokerHash: core.utils.parseBrokerHash(brokerId),
3972
4031
  tokenHash: core.utils.parseTokenHash("USDC")
3973
4032
  };
3974
4033
  }, [account5]);
3975
4034
  const swap = React2.useCallback(
3976
- async (woofiDexDepositorAdress, inputs) => {
4035
+ async (woofiDexDepositorAdress, inputs, config) => {
3977
4036
  if (!account5.walletClient) {
3978
4037
  throw new Error("walletClient is undefined");
3979
4038
  }
@@ -3983,20 +4042,24 @@ var useSwap = () => {
3983
4042
  if (loading)
3984
4043
  return;
3985
4044
  start();
4045
+ const txPayload = {
4046
+ from: account5.address,
4047
+ to: woofiDexDepositorAdress,
4048
+ data: [account5.address, inputs, dstValutDeposit()],
4049
+ value: isNativeTokenChecker(inputs.fromToken) ? BigInt(inputs.fromAmount) + inputs.orderlyNativeFees : inputs.orderlyNativeFees
4050
+ };
3986
4051
  try {
3987
4052
  const result = await account5.walletClient.sendTransaction(
3988
4053
  woofiDexDepositorAdress,
3989
4054
  "swap",
3990
- {
3991
- from: account5.address,
3992
- to: woofiDexDepositorAdress,
3993
- data: [account5.address, inputs, dstValutDeposit()]
3994
- },
4055
+ txPayload,
3995
4056
  {
3996
4057
  abi: woofiDexDepositorAbi
3997
4058
  }
3998
4059
  );
3999
4060
  stop();
4061
+ console.log("single swap result", result);
4062
+ txHash.current = result.hash;
4000
4063
  return ramda.pick(["from", "to", "hash", "value"], result);
4001
4064
  } catch (e) {
4002
4065
  console.log("\u8C03\u7528\u5408\u7EA6\u62A5\u9519\uFF1A", e);
@@ -4007,7 +4070,8 @@ var useSwap = () => {
4007
4070
  );
4008
4071
  return {
4009
4072
  swap,
4010
- loading
4073
+ loading,
4074
+ status
4011
4075
  };
4012
4076
  };
4013
4077