@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.mjs CHANGED
@@ -6,7 +6,7 @@ import useSWRMutation from 'swr/mutation';
6
6
  import useConstant from 'use-constant';
7
7
  export { default as useConstant } from 'use-constant';
8
8
  import { SimpleDI, Account, EventEmitter, utils } from '@orderly.network/core';
9
- import { AccountStatusEnum, OrderSide, chainsMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, OrderStatus as OrderStatus$1, OrderType } from '@orderly.network/types';
9
+ import { AccountStatusEnum, OrderSide, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, OrderStatus as OrderStatus$1, WS_WalletStatusEnum, OrderType } from '@orderly.network/types';
10
10
  import useSWRSubscription from 'swr/subscription';
11
11
  import { Decimal, zero, getPrecisionByNumber, timeConvertString } from '@orderly.network/utils';
12
12
  import { pathOr, propOr, compose, head, prop, mergeDeepRight, pick } from 'ramda';
@@ -2923,6 +2923,7 @@ var woofiDexCrossChainRouterAbi = [
2923
2923
  }
2924
2924
  ];
2925
2925
  var nativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
2926
+ var isNativeTokenChecker = (address) => address === nativeTokenAddress;
2926
2927
 
2927
2928
  // src/orderly/useChains.ts
2928
2929
  var useChains = (networkId, options = {}) => {
@@ -2966,9 +2967,10 @@ var useChains = (networkId, options = {}) => {
2966
2967
  item.chain_details.forEach((chain) => {
2967
2968
  const chainId = Number(chain.chain_id);
2968
2969
  orderlyChainIds.add(chainId);
2970
+ const chainInfo = chainsInfoMap.get(chainId);
2969
2971
  const _chain = {
2970
2972
  network_infos: {
2971
- name: chain.chain_name ?? "--",
2973
+ name: chain.chain_name ?? chainInfo?.chainName ?? "--",
2972
2974
  // "public_rpc_url": "https://arb1.arbitrum.io/rpc",
2973
2975
  chain_id: chainId,
2974
2976
  withdrawal_fee: chain.withdrawal_fee,
@@ -3099,7 +3101,7 @@ var useChain = (token) => {
3099
3101
  let item = data.find((chain) => chain.token === token);
3100
3102
  if (item) {
3101
3103
  item.chain_details = item.chain_details.map((d) => {
3102
- const chain = chainsMap.get(Number(d.chain_id));
3104
+ const chain = chainsInfoMap.get(Number(d.chain_id));
3103
3105
  return {
3104
3106
  ...d,
3105
3107
  chain_name: chain?.chainName ?? "--"
@@ -3133,7 +3135,6 @@ var useWithdraw = () => {
3133
3135
  }, [usdc, unsettledPnL]);
3134
3136
  return { withdraw, isLoading, maxAmount, availableBalance, unsettledPnL };
3135
3137
  };
3136
- var isNativeTokenChecker = (address) => address === nativeTokenAddress;
3137
3138
  var useDeposit = (options) => {
3138
3139
  const { onlyTestnet } = useContext(OrderlyContext);
3139
3140
  const [balanceRevalidating, setBalanceRevalidating] = useState(false);
@@ -3144,6 +3145,7 @@ var useDeposit = (options) => {
3144
3145
  const [balance, setBalance] = useState("0");
3145
3146
  const [allowance, setAllowance] = useState("0");
3146
3147
  const { account: account5, state } = useAccount();
3148
+ const prevAddress = useRef();
3147
3149
  const dst = useMemo(() => {
3148
3150
  const chain = onlyTestnet ? findByChainId(ARBITRUM_TESTNET_CHAINID) : findByChainId(ARBITRUM_MAINNET_CHAINID);
3149
3151
  const USDC = chain?.token_infos.find((token) => token.symbol === "USDC");
@@ -3209,46 +3211,85 @@ var useDeposit = (options) => {
3209
3211
  const balances = await Promise.all(tasks);
3210
3212
  console.log("----- get balances from tokens -----", balances);
3211
3213
  }, []);
3212
- const getAllowance = useCallback(
3213
- async (address, vaultAddress) => {
3214
- console.log("getAllowance", address, vaultAddress);
3215
- if (!address)
3216
- return;
3217
- if (address && isNativeTokenChecker(address))
3218
- return;
3219
- if (allowanceRevalidating)
3220
- return;
3221
- setAllowanceRevalidating(true);
3222
- const allowance2 = await account5.assetsManager.getAllowance(
3223
- address,
3224
- vaultAddress
3225
- );
3226
- console.log("----- refresh allowance -----", allowance2);
3227
- setAllowance(() => allowance2);
3228
- setAllowanceRevalidating(false);
3229
- return allowance2;
3230
- },
3231
- [allowanceRevalidating]
3232
- );
3214
+ const getAllowance = async (address, vaultAddress) => {
3215
+ const key = `${address}-${vaultAddress}`;
3216
+ if (prevAddress.current === key)
3217
+ return;
3218
+ if (!address || !vaultAddress)
3219
+ return;
3220
+ if (address && isNativeTokenChecker(address))
3221
+ return;
3222
+ console.log("getAllowance", address, vaultAddress);
3223
+ prevAddress.current = key;
3224
+ const allowance2 = await account5.assetsManager.getAllowance(
3225
+ address,
3226
+ vaultAddress
3227
+ );
3228
+ console.log("----- refresh allowance -----", {
3229
+ allowance: allowance2
3230
+ });
3231
+ setAllowance(() => allowance2);
3232
+ return allowance2;
3233
+ };
3234
+ const getAllowanceByDefaultAddress = async (address) => {
3235
+ if (prevAddress.current === address)
3236
+ return;
3237
+ if (!address || isNativeTokenChecker(address))
3238
+ return;
3239
+ prevAddress.current = address;
3240
+ const allowance2 = await account5.assetsManager.getAllowance(address);
3241
+ console.log(
3242
+ "----- refresh allowance only orderly -----",
3243
+ { allowance: allowance2 }
3244
+ );
3245
+ setAllowance(() => allowance2);
3246
+ };
3247
+ const getVaultAddress = useCallback(() => {
3248
+ if (dst.chainId !== options?.srcChainId) {
3249
+ return options?.crossChainRouteAddress;
3250
+ } else {
3251
+ if (dst.symbol !== options?.srcToken) {
3252
+ return options?.depositorAddress;
3253
+ }
3254
+ }
3255
+ }, [options, dst]);
3233
3256
  useEffect(() => {
3234
3257
  if (state.status < AccountStatusEnum.Connected)
3235
3258
  return;
3236
3259
  fetchBalance(options?.address);
3237
- getAllowance(options?.address, options?.vaultAddress);
3238
- }, [state.status, options?.address, options?.vaultAddress, account5.address]);
3260
+ if (dst.chainId !== options?.srcChainId) {
3261
+ getAllowance(options?.address, options?.crossChainRouteAddress);
3262
+ } else {
3263
+ if (dst.symbol !== options?.srcToken) {
3264
+ getAllowance(options?.address, options?.depositorAddress);
3265
+ } else {
3266
+ getAllowanceByDefaultAddress(options?.address);
3267
+ }
3268
+ }
3269
+ }, [
3270
+ state.status,
3271
+ options?.address,
3272
+ options?.crossChainRouteAddress,
3273
+ options?.depositorAddress,
3274
+ options?.srcChainId,
3275
+ options?.srcToken,
3276
+ account5.address,
3277
+ dst
3278
+ ]);
3239
3279
  const approve = useCallback(
3240
3280
  (amount) => {
3241
3281
  if (!options?.address) {
3242
3282
  throw new Error("address is required");
3243
3283
  }
3244
- return account5.assetsManager.approve(options.address, amount, options?.vaultAddress).then((result) => {
3284
+ const vaultAddress = getVaultAddress();
3285
+ return account5.assetsManager.approve(options.address, amount, vaultAddress).then((result) => {
3245
3286
  if (typeof amount !== "undefined") {
3246
3287
  setAllowance((value) => new Decimal(value).add(amount).toString());
3247
3288
  }
3248
3289
  return result;
3249
3290
  });
3250
3291
  },
3251
- [account5, getAllowance, options?.address, options?.vaultAddress]
3292
+ [account5, getAllowance, options?.address]
3252
3293
  );
3253
3294
  const deposit = useCallback(
3254
3295
  (amount) => {
@@ -3576,24 +3617,37 @@ var useCrossSwap = () => {
3576
3617
  "WAITTING" /* INITIALIZING */
3577
3618
  );
3578
3619
  const [bridgeMessage, setBridgeMessage] = useState();
3620
+ const [status, setStatus] = useState(
3621
+ WS_WalletStatusEnum.NO
3622
+ );
3623
+ const txHashFromBridge = useRef();
3579
3624
  const account5 = useAccountInstance();
3580
3625
  const { networkId, configStore } = useContext(OrderlyContext);
3581
3626
  const client = useRef(createClient(networkId)).current;
3582
3627
  const timer = useRef();
3628
+ useWalletSubscription({
3629
+ onMessage: (message) => {
3630
+ const { side, transStatus, trxId } = message;
3631
+ if (side === "DEPOSIT" && trxId === txHashFromBridge.current) {
3632
+ setStatus(transStatus);
3633
+ }
3634
+ }
3635
+ });
3583
3636
  const checkLayerStatus = useCallback((txHash) => {
3584
3637
  const check = async (txHash2) => {
3585
3638
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3586
3639
  console.log("messages:", messages);
3587
3640
  if (messages.length > 0) {
3588
- const { status } = messages[0];
3589
- if (status === "INFLIGHT" /* INFLIGHT */) {
3641
+ const { status: status2 } = messages[0];
3642
+ if (status2 === "INFLIGHT" /* INFLIGHT */) {
3590
3643
  setTimeout(() => {
3591
3644
  check(txHash2);
3592
3645
  }, 1e3);
3593
3646
  }
3594
- setLayerStatus(status);
3595
- if (status === "DELIVERED" /* DELIVERED */) {
3647
+ setLayerStatus(status2);
3648
+ if (status2 === "DELIVERED" /* DELIVERED */) {
3596
3649
  setBridgeMessage(messages[0]);
3650
+ txHashFromBridge.current = messages[0].dstTxHash;
3597
3651
  }
3598
3652
  } else {
3599
3653
  setTimeout(() => {
@@ -3647,20 +3701,12 @@ var useCrossSwap = () => {
3647
3701
  from: account5.address,
3648
3702
  to: crossChainRouteAddress,
3649
3703
  data: [account5.address, src, dst, dstValutDeposit()],
3650
- value: quotoLZFee[0]
3704
+ value: isNativeTokenChecker(inputs.src.fromToken) ? BigInt(inputs.src.fromAmount) + quotoLZFee[0] : quotoLZFee[0]
3651
3705
  },
3652
3706
  {
3653
3707
  abi: woofiDexCrossChainRouterAbi
3654
3708
  }
3655
3709
  );
3656
- account5.walletClient.on(
3657
- {
3658
- address: crossChainRouteAddress
3659
- },
3660
- (log, event) => {
3661
- console.log("-------------", log, event);
3662
- }
3663
- );
3664
3710
  stop();
3665
3711
  checkLayerStatus(result.hash);
3666
3712
  return pick(["from", "to", "hash", "value"], result);
@@ -3673,7 +3719,8 @@ var useCrossSwap = () => {
3673
3719
  return {
3674
3720
  swap,
3675
3721
  loading,
3676
- status: layerStatus,
3722
+ bridgeStatus: layerStatus,
3723
+ status,
3677
3724
  message: bridgeMessage
3678
3725
  };
3679
3726
  };
@@ -3955,15 +4002,28 @@ var useSwap = () => {
3955
4002
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3956
4003
  const account5 = useAccountInstance();
3957
4004
  const { configStore } = useContext(OrderlyContext);
4005
+ const [status, setStatus] = useState(
4006
+ WS_WalletStatusEnum.NO
4007
+ );
4008
+ const txHash = useRef();
4009
+ useWalletSubscription({
4010
+ onMessage: (message) => {
4011
+ const { side, transStatus, trxId } = message;
4012
+ if (side === "DEPOSIT" && trxId === txHash.current) {
4013
+ setStatus(transStatus);
4014
+ }
4015
+ }
4016
+ });
3958
4017
  const dstValutDeposit = useCallback(() => {
4018
+ const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
3959
4019
  return {
3960
4020
  accountId: account5.accountIdHashStr,
3961
- brokerHash: utils.parseBrokerHash(configStore.get("brokerId")),
4021
+ brokerHash: utils.parseBrokerHash(brokerId),
3962
4022
  tokenHash: utils.parseTokenHash("USDC")
3963
4023
  };
3964
4024
  }, [account5]);
3965
4025
  const swap = useCallback(
3966
- async (woofiDexDepositorAdress, inputs) => {
4026
+ async (woofiDexDepositorAdress, inputs, config) => {
3967
4027
  if (!account5.walletClient) {
3968
4028
  throw new Error("walletClient is undefined");
3969
4029
  }
@@ -3973,21 +4033,24 @@ var useSwap = () => {
3973
4033
  if (loading)
3974
4034
  return;
3975
4035
  start();
3976
- console.log("---------", inputs);
4036
+ const txPayload = {
4037
+ from: account5.address,
4038
+ to: woofiDexDepositorAdress,
4039
+ data: [account5.address, inputs, dstValutDeposit()],
4040
+ value: isNativeTokenChecker(inputs.fromToken) ? BigInt(inputs.fromAmount) + inputs.orderlyNativeFees : inputs.orderlyNativeFees
4041
+ };
3977
4042
  try {
3978
4043
  const result = await account5.walletClient.sendTransaction(
3979
4044
  woofiDexDepositorAdress,
3980
4045
  "swap",
3981
- {
3982
- from: account5.address,
3983
- to: woofiDexDepositorAdress,
3984
- data: [account5.address, inputs, dstValutDeposit()]
3985
- },
4046
+ txPayload,
3986
4047
  {
3987
4048
  abi: woofiDexDepositorAbi
3988
4049
  }
3989
4050
  );
3990
4051
  stop();
4052
+ console.log("single swap result", result);
4053
+ txHash.current = result.hash;
3991
4054
  return pick(["from", "to", "hash", "value"], result);
3992
4055
  } catch (e) {
3993
4056
  console.log("\u8C03\u7528\u5408\u7EA6\u62A5\u9519\uFF1A", e);
@@ -3998,7 +4061,8 @@ var useSwap = () => {
3998
4061
  );
3999
4062
  return {
4000
4063
  swap,
4001
- loading
4064
+ loading,
4065
+ status
4002
4066
  };
4003
4067
  };
4004
4068