@orderly.network/hooks 0.0.68 → 0.0.70

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");
@@ -3181,21 +3182,21 @@ var useDeposit = (options) => {
3181
3182
  decimals
3182
3183
  });
3183
3184
  } else {
3184
- balance2 = await account5.assetsManager.getBalance(address);
3185
+ balance2 = await account5.assetsManager.getBalance(address, { decimals });
3185
3186
  }
3186
3187
  return balance2;
3187
3188
  },
3188
3189
  []
3189
3190
  );
3190
3191
  const fetchBalance = React2.useCallback(
3191
- async (address) => {
3192
+ async (address, decimals) => {
3192
3193
  if (!address)
3193
3194
  return;
3194
3195
  try {
3195
3196
  if (balanceRevalidating)
3196
3197
  return;
3197
3198
  setBalanceRevalidating(true);
3198
- const balance2 = await fetchBalanceHandler(address);
3199
+ const balance2 = await fetchBalanceHandler(address, decimals);
3199
3200
  console.log("----- refresh balance -----", balance2);
3200
3201
  setBalance(() => balance2);
3201
3202
  setBalanceRevalidating(false);
@@ -3219,46 +3220,81 @@ 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
+ prevAddress.current = key;
3232
+ const allowance2 = await account5.assetsManager.getAllowance(
3233
+ address,
3234
+ vaultAddress
3235
+ );
3236
+ setAllowance(() => allowance2);
3237
+ return allowance2;
3238
+ };
3239
+ const getAllowanceByDefaultAddress = async (address) => {
3240
+ if (prevAddress.current === address)
3241
+ return;
3242
+ if (!address || isNativeTokenChecker(address))
3243
+ return;
3244
+ prevAddress.current = address;
3245
+ const allowance2 = await account5.assetsManager.getAllowance(address);
3246
+ console.log(
3247
+ "----- refresh allowance only orderly -----",
3248
+ { allowance: allowance2 }
3249
+ );
3250
+ setAllowance(() => allowance2);
3251
+ };
3252
+ const getVaultAddress = React2.useCallback(() => {
3253
+ if (dst.chainId !== options?.srcChainId) {
3254
+ return options?.crossChainRouteAddress;
3255
+ } else {
3256
+ if (dst.symbol !== options?.srcToken) {
3257
+ return options?.depositorAddress;
3258
+ }
3259
+ }
3260
+ }, [options, dst]);
3243
3261
  React2.useEffect(() => {
3244
3262
  if (state.status < types.AccountStatusEnum.Connected)
3245
3263
  return;
3246
- fetchBalance(options?.address);
3247
- getAllowance(options?.address, options?.vaultAddress);
3248
- }, [state.status, options?.address, options?.vaultAddress, account5.address]);
3264
+ fetchBalance(options?.address, options?.decimals);
3265
+ if (dst.chainId !== options?.srcChainId) {
3266
+ getAllowance(options?.address, options?.crossChainRouteAddress);
3267
+ } else {
3268
+ if (dst.symbol !== options?.srcToken) {
3269
+ getAllowance(options?.address, options?.depositorAddress);
3270
+ } else {
3271
+ getAllowanceByDefaultAddress(options?.address);
3272
+ }
3273
+ }
3274
+ }, [
3275
+ state.status,
3276
+ options?.address,
3277
+ options?.crossChainRouteAddress,
3278
+ options?.depositorAddress,
3279
+ options?.srcChainId,
3280
+ options?.srcToken,
3281
+ account5.address,
3282
+ dst
3283
+ ]);
3249
3284
  const approve = React2.useCallback(
3250
3285
  (amount) => {
3251
3286
  if (!options?.address) {
3252
3287
  throw new Error("address is required");
3253
3288
  }
3254
- return account5.assetsManager.approve(options.address, amount, options?.vaultAddress).then((result) => {
3289
+ const vaultAddress = getVaultAddress();
3290
+ return account5.assetsManager.approve(options.address, amount, vaultAddress).then((result) => {
3255
3291
  if (typeof amount !== "undefined") {
3256
3292
  setAllowance((value) => new utils.Decimal(value).add(amount).toString());
3257
3293
  }
3258
3294
  return result;
3259
3295
  });
3260
3296
  },
3261
- [account5, getAllowance, options?.address, options?.vaultAddress]
3297
+ [account5, getAllowance, options?.address]
3262
3298
  );
3263
3299
  const deposit = React2.useCallback(
3264
3300
  (amount) => {
@@ -3586,24 +3622,37 @@ var useCrossSwap = () => {
3586
3622
  "WAITTING" /* INITIALIZING */
3587
3623
  );
3588
3624
  const [bridgeMessage, setBridgeMessage] = React2.useState();
3625
+ const [status, setStatus] = React2.useState(
3626
+ types.WS_WalletStatusEnum.NO
3627
+ );
3628
+ const txHashFromBridge = React2.useRef();
3589
3629
  const account5 = useAccountInstance();
3590
3630
  const { networkId, configStore } = React2.useContext(OrderlyContext);
3591
3631
  const client = React2.useRef(scanClient.createClient(networkId)).current;
3592
3632
  const timer = React2.useRef();
3633
+ useWalletSubscription({
3634
+ onMessage: (message) => {
3635
+ const { side, transStatus, trxId } = message;
3636
+ if (side === "DEPOSIT" && trxId === txHashFromBridge.current) {
3637
+ setStatus(transStatus);
3638
+ }
3639
+ }
3640
+ });
3593
3641
  const checkLayerStatus = React2.useCallback((txHash) => {
3594
3642
  const check = async (txHash2) => {
3595
3643
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3596
3644
  console.log("messages:", messages);
3597
3645
  if (messages.length > 0) {
3598
- const { status } = messages[0];
3599
- if (status === "INFLIGHT" /* INFLIGHT */) {
3646
+ const { status: status2 } = messages[0];
3647
+ if (status2 === "INFLIGHT" /* INFLIGHT */) {
3600
3648
  setTimeout(() => {
3601
3649
  check(txHash2);
3602
3650
  }, 1e3);
3603
3651
  }
3604
- setLayerStatus(status);
3605
- if (status === "DELIVERED" /* DELIVERED */) {
3652
+ setLayerStatus(status2);
3653
+ if (status2 === "DELIVERED" /* DELIVERED */) {
3606
3654
  setBridgeMessage(messages[0]);
3655
+ txHashFromBridge.current = messages[0].dstTxHash;
3607
3656
  }
3608
3657
  } else {
3609
3658
  setTimeout(() => {
@@ -3657,20 +3706,12 @@ var useCrossSwap = () => {
3657
3706
  from: account5.address,
3658
3707
  to: crossChainRouteAddress,
3659
3708
  data: [account5.address, src, dst, dstValutDeposit()],
3660
- value: quotoLZFee[0]
3709
+ value: isNativeTokenChecker(inputs.src.fromToken) ? BigInt(inputs.src.fromAmount) + quotoLZFee[0] : quotoLZFee[0]
3661
3710
  },
3662
3711
  {
3663
3712
  abi: woofiDexCrossChainRouterAbi
3664
3713
  }
3665
3714
  );
3666
- account5.walletClient.on(
3667
- {
3668
- address: crossChainRouteAddress
3669
- },
3670
- (log, event) => {
3671
- console.log("-------------", log, event);
3672
- }
3673
- );
3674
3715
  stop();
3675
3716
  checkLayerStatus(result.hash);
3676
3717
  return ramda.pick(["from", "to", "hash", "value"], result);
@@ -3683,7 +3724,8 @@ var useCrossSwap = () => {
3683
3724
  return {
3684
3725
  swap,
3685
3726
  loading,
3686
- status: layerStatus,
3727
+ bridgeStatus: layerStatus,
3728
+ status,
3687
3729
  message: bridgeMessage
3688
3730
  };
3689
3731
  };
@@ -3965,15 +4007,28 @@ var useSwap = () => {
3965
4007
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3966
4008
  const account5 = useAccountInstance();
3967
4009
  const { configStore } = React2.useContext(OrderlyContext);
4010
+ const [status, setStatus] = React2.useState(
4011
+ types.WS_WalletStatusEnum.NO
4012
+ );
4013
+ const txHash = React2.useRef();
4014
+ useWalletSubscription({
4015
+ onMessage: (message) => {
4016
+ const { side, transStatus, trxId } = message;
4017
+ if (side === "DEPOSIT" && trxId === txHash.current) {
4018
+ setStatus(transStatus);
4019
+ }
4020
+ }
4021
+ });
3968
4022
  const dstValutDeposit = React2.useCallback(() => {
4023
+ const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
3969
4024
  return {
3970
4025
  accountId: account5.accountIdHashStr,
3971
- brokerHash: core.utils.parseBrokerHash(configStore.get("brokerId")),
4026
+ brokerHash: core.utils.parseBrokerHash(brokerId),
3972
4027
  tokenHash: core.utils.parseTokenHash("USDC")
3973
4028
  };
3974
4029
  }, [account5]);
3975
4030
  const swap = React2.useCallback(
3976
- async (woofiDexDepositorAdress, inputs) => {
4031
+ async (woofiDexDepositorAdress, inputs, config) => {
3977
4032
  if (!account5.walletClient) {
3978
4033
  throw new Error("walletClient is undefined");
3979
4034
  }
@@ -3983,20 +4038,24 @@ var useSwap = () => {
3983
4038
  if (loading)
3984
4039
  return;
3985
4040
  start();
4041
+ const txPayload = {
4042
+ from: account5.address,
4043
+ to: woofiDexDepositorAdress,
4044
+ data: [account5.address, inputs, dstValutDeposit()],
4045
+ value: isNativeTokenChecker(inputs.fromToken) ? BigInt(inputs.fromAmount) + inputs.orderlyNativeFees : inputs.orderlyNativeFees
4046
+ };
3986
4047
  try {
3987
4048
  const result = await account5.walletClient.sendTransaction(
3988
4049
  woofiDexDepositorAdress,
3989
4050
  "swap",
3990
- {
3991
- from: account5.address,
3992
- to: woofiDexDepositorAdress,
3993
- data: [account5.address, inputs, dstValutDeposit()]
3994
- },
4051
+ txPayload,
3995
4052
  {
3996
4053
  abi: woofiDexDepositorAbi
3997
4054
  }
3998
4055
  );
3999
4056
  stop();
4057
+ console.log("single swap result", result);
4058
+ txHash.current = result.hash;
4000
4059
  return ramda.pick(["from", "to", "hash", "value"], result);
4001
4060
  } catch (e) {
4002
4061
  console.log("\u8C03\u7528\u5408\u7EA6\u62A5\u9519\uFF1A", e);
@@ -4007,7 +4066,8 @@ var useSwap = () => {
4007
4066
  );
4008
4067
  return {
4009
4068
  swap,
4010
- loading
4069
+ loading,
4070
+ status
4011
4071
  };
4012
4072
  };
4013
4073