@orderly.network/hooks 0.0.67 → 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,7 +3144,6 @@ 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
3148
  const { onlyTestnet } = React2.useContext(OrderlyContext);
3148
3149
  const [balanceRevalidating, setBalanceRevalidating] = React2.useState(false);
@@ -3153,6 +3154,7 @@ var useDeposit = (options) => {
3153
3154
  const [balance, setBalance] = React2.useState("0");
3154
3155
  const [allowance, setAllowance] = React2.useState("0");
3155
3156
  const { account: account5, state } = useAccount();
3157
+ const prevAddress = React2.useRef();
3156
3158
  const dst = React2.useMemo(() => {
3157
3159
  const chain = onlyTestnet ? findByChainId(types.ARBITRUM_TESTNET_CHAINID) : findByChainId(types.ARBITRUM_MAINNET_CHAINID);
3158
3160
  const USDC = chain?.token_infos.find((token) => token.symbol === "USDC");
@@ -3218,46 +3220,85 @@ var useDeposit = (options) => {
3218
3220
  const balances = await Promise.all(tasks);
3219
3221
  console.log("----- get balances from tokens -----", balances);
3220
3222
  }, []);
3221
- const getAllowance = React2.useCallback(
3222
- async (address, vaultAddress) => {
3223
- console.log("getAllowance", address, vaultAddress);
3224
- if (!address)
3225
- return;
3226
- if (address && isNativeTokenChecker(address))
3227
- return;
3228
- if (allowanceRevalidating)
3229
- return;
3230
- setAllowanceRevalidating(true);
3231
- const allowance2 = await account5.assetsManager.getAllowance(
3232
- address,
3233
- vaultAddress
3234
- );
3235
- console.log("----- refresh allowance -----", allowance2);
3236
- setAllowance(() => allowance2);
3237
- setAllowanceRevalidating(false);
3238
- return allowance2;
3239
- },
3240
- [allowanceRevalidating]
3241
- );
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]);
3242
3265
  React2.useEffect(() => {
3243
3266
  if (state.status < types.AccountStatusEnum.Connected)
3244
3267
  return;
3245
3268
  fetchBalance(options?.address);
3246
- getAllowance(options?.address, options?.vaultAddress);
3247
- }, [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
+ ]);
3248
3288
  const approve = React2.useCallback(
3249
3289
  (amount) => {
3250
3290
  if (!options?.address) {
3251
3291
  throw new Error("address is required");
3252
3292
  }
3253
- 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) => {
3254
3295
  if (typeof amount !== "undefined") {
3255
3296
  setAllowance((value) => new utils.Decimal(value).add(amount).toString());
3256
3297
  }
3257
3298
  return result;
3258
3299
  });
3259
3300
  },
3260
- [account5, getAllowance, options?.address, options?.vaultAddress]
3301
+ [account5, getAllowance, options?.address]
3261
3302
  );
3262
3303
  const deposit = React2.useCallback(
3263
3304
  (amount) => {
@@ -3585,24 +3626,37 @@ var useCrossSwap = () => {
3585
3626
  "WAITTING" /* INITIALIZING */
3586
3627
  );
3587
3628
  const [bridgeMessage, setBridgeMessage] = React2.useState();
3629
+ const [status, setStatus] = React2.useState(
3630
+ types.WS_WalletStatusEnum.NO
3631
+ );
3632
+ const txHashFromBridge = React2.useRef();
3588
3633
  const account5 = useAccountInstance();
3589
3634
  const { networkId, configStore } = React2.useContext(OrderlyContext);
3590
3635
  const client = React2.useRef(scanClient.createClient(networkId)).current;
3591
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
+ });
3592
3645
  const checkLayerStatus = React2.useCallback((txHash) => {
3593
3646
  const check = async (txHash2) => {
3594
3647
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3595
3648
  console.log("messages:", messages);
3596
3649
  if (messages.length > 0) {
3597
- const { status } = messages[0];
3598
- if (status === "INFLIGHT" /* INFLIGHT */) {
3650
+ const { status: status2 } = messages[0];
3651
+ if (status2 === "INFLIGHT" /* INFLIGHT */) {
3599
3652
  setTimeout(() => {
3600
3653
  check(txHash2);
3601
3654
  }, 1e3);
3602
3655
  }
3603
- setLayerStatus(status);
3604
- if (status === "DELIVERED" /* DELIVERED */) {
3656
+ setLayerStatus(status2);
3657
+ if (status2 === "DELIVERED" /* DELIVERED */) {
3605
3658
  setBridgeMessage(messages[0]);
3659
+ txHashFromBridge.current = messages[0].dstTxHash;
3606
3660
  }
3607
3661
  } else {
3608
3662
  setTimeout(() => {
@@ -3656,20 +3710,12 @@ var useCrossSwap = () => {
3656
3710
  from: account5.address,
3657
3711
  to: crossChainRouteAddress,
3658
3712
  data: [account5.address, src, dst, dstValutDeposit()],
3659
- value: quotoLZFee[0]
3713
+ value: isNativeTokenChecker(inputs.src.fromToken) ? BigInt(inputs.src.fromAmount) + quotoLZFee[0] : quotoLZFee[0]
3660
3714
  },
3661
3715
  {
3662
3716
  abi: woofiDexCrossChainRouterAbi
3663
3717
  }
3664
3718
  );
3665
- account5.walletClient.on(
3666
- {
3667
- address: crossChainRouteAddress
3668
- },
3669
- (log, event) => {
3670
- console.log("-------------", log, event);
3671
- }
3672
- );
3673
3719
  stop();
3674
3720
  checkLayerStatus(result.hash);
3675
3721
  return ramda.pick(["from", "to", "hash", "value"], result);
@@ -3682,7 +3728,8 @@ var useCrossSwap = () => {
3682
3728
  return {
3683
3729
  swap,
3684
3730
  loading,
3685
- status: layerStatus,
3731
+ bridgeStatus: layerStatus,
3732
+ status,
3686
3733
  message: bridgeMessage
3687
3734
  };
3688
3735
  };
@@ -3964,15 +4011,28 @@ var useSwap = () => {
3964
4011
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3965
4012
  const account5 = useAccountInstance();
3966
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
+ });
3967
4026
  const dstValutDeposit = React2.useCallback(() => {
4027
+ const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
3968
4028
  return {
3969
4029
  accountId: account5.accountIdHashStr,
3970
- brokerHash: core.utils.parseBrokerHash(configStore.get("brokerId")),
4030
+ brokerHash: core.utils.parseBrokerHash(brokerId),
3971
4031
  tokenHash: core.utils.parseTokenHash("USDC")
3972
4032
  };
3973
4033
  }, [account5]);
3974
4034
  const swap = React2.useCallback(
3975
- async (woofiDexDepositorAdress, inputs) => {
4035
+ async (woofiDexDepositorAdress, inputs, config) => {
3976
4036
  if (!account5.walletClient) {
3977
4037
  throw new Error("walletClient is undefined");
3978
4038
  }
@@ -3982,21 +4042,24 @@ var useSwap = () => {
3982
4042
  if (loading)
3983
4043
  return;
3984
4044
  start();
3985
- console.log("---------", inputs);
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