@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.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,9 +3135,7 @@ 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
- console.log("useDeposit options:", options);
3139
3139
  const { onlyTestnet } = useContext(OrderlyContext);
3140
3140
  const [balanceRevalidating, setBalanceRevalidating] = useState(false);
3141
3141
  const [allowanceRevalidating, setAllowanceRevalidating] = useState(false);
@@ -3145,6 +3145,7 @@ var useDeposit = (options) => {
3145
3145
  const [balance, setBalance] = useState("0");
3146
3146
  const [allowance, setAllowance] = useState("0");
3147
3147
  const { account: account5, state } = useAccount();
3148
+ const prevAddress = useRef();
3148
3149
  const dst = useMemo(() => {
3149
3150
  const chain = onlyTestnet ? findByChainId(ARBITRUM_TESTNET_CHAINID) : findByChainId(ARBITRUM_MAINNET_CHAINID);
3150
3151
  const USDC = chain?.token_infos.find((token) => token.symbol === "USDC");
@@ -3210,46 +3211,85 @@ var useDeposit = (options) => {
3210
3211
  const balances = await Promise.all(tasks);
3211
3212
  console.log("----- get balances from tokens -----", balances);
3212
3213
  }, []);
3213
- const getAllowance = useCallback(
3214
- async (address, vaultAddress) => {
3215
- console.log("getAllowance", address, vaultAddress);
3216
- if (!address || !vaultAddress)
3217
- return;
3218
- if (address && isNativeTokenChecker(address))
3219
- return;
3220
- if (allowanceRevalidating)
3221
- return;
3222
- setAllowanceRevalidating(true);
3223
- const allowance2 = await account5.assetsManager.getAllowance(
3224
- address,
3225
- vaultAddress
3226
- );
3227
- console.log("----- refresh allowance -----", allowance2);
3228
- setAllowance(() => allowance2);
3229
- setAllowanceRevalidating(false);
3230
- return allowance2;
3231
- },
3232
- [allowanceRevalidating]
3233
- );
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]);
3234
3256
  useEffect(() => {
3235
3257
  if (state.status < AccountStatusEnum.Connected)
3236
3258
  return;
3237
3259
  fetchBalance(options?.address);
3238
- getAllowance(options?.address, options?.vaultAddress);
3239
- }, [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
+ ]);
3240
3279
  const approve = useCallback(
3241
3280
  (amount) => {
3242
3281
  if (!options?.address) {
3243
3282
  throw new Error("address is required");
3244
3283
  }
3245
- 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) => {
3246
3286
  if (typeof amount !== "undefined") {
3247
3287
  setAllowance((value) => new Decimal(value).add(amount).toString());
3248
3288
  }
3249
3289
  return result;
3250
3290
  });
3251
3291
  },
3252
- [account5, getAllowance, options?.address, options?.vaultAddress]
3292
+ [account5, getAllowance, options?.address]
3253
3293
  );
3254
3294
  const deposit = useCallback(
3255
3295
  (amount) => {
@@ -3577,24 +3617,37 @@ var useCrossSwap = () => {
3577
3617
  "WAITTING" /* INITIALIZING */
3578
3618
  );
3579
3619
  const [bridgeMessage, setBridgeMessage] = useState();
3620
+ const [status, setStatus] = useState(
3621
+ WS_WalletStatusEnum.NO
3622
+ );
3623
+ const txHashFromBridge = useRef();
3580
3624
  const account5 = useAccountInstance();
3581
3625
  const { networkId, configStore } = useContext(OrderlyContext);
3582
3626
  const client = useRef(createClient(networkId)).current;
3583
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
+ });
3584
3636
  const checkLayerStatus = useCallback((txHash) => {
3585
3637
  const check = async (txHash2) => {
3586
3638
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3587
3639
  console.log("messages:", messages);
3588
3640
  if (messages.length > 0) {
3589
- const { status } = messages[0];
3590
- if (status === "INFLIGHT" /* INFLIGHT */) {
3641
+ const { status: status2 } = messages[0];
3642
+ if (status2 === "INFLIGHT" /* INFLIGHT */) {
3591
3643
  setTimeout(() => {
3592
3644
  check(txHash2);
3593
3645
  }, 1e3);
3594
3646
  }
3595
- setLayerStatus(status);
3596
- if (status === "DELIVERED" /* DELIVERED */) {
3647
+ setLayerStatus(status2);
3648
+ if (status2 === "DELIVERED" /* DELIVERED */) {
3597
3649
  setBridgeMessage(messages[0]);
3650
+ txHashFromBridge.current = messages[0].dstTxHash;
3598
3651
  }
3599
3652
  } else {
3600
3653
  setTimeout(() => {
@@ -3648,20 +3701,12 @@ var useCrossSwap = () => {
3648
3701
  from: account5.address,
3649
3702
  to: crossChainRouteAddress,
3650
3703
  data: [account5.address, src, dst, dstValutDeposit()],
3651
- value: quotoLZFee[0]
3704
+ value: isNativeTokenChecker(inputs.src.fromToken) ? BigInt(inputs.src.fromAmount) + quotoLZFee[0] : quotoLZFee[0]
3652
3705
  },
3653
3706
  {
3654
3707
  abi: woofiDexCrossChainRouterAbi
3655
3708
  }
3656
3709
  );
3657
- account5.walletClient.on(
3658
- {
3659
- address: crossChainRouteAddress
3660
- },
3661
- (log, event) => {
3662
- console.log("-------------", log, event);
3663
- }
3664
- );
3665
3710
  stop();
3666
3711
  checkLayerStatus(result.hash);
3667
3712
  return pick(["from", "to", "hash", "value"], result);
@@ -3674,7 +3719,8 @@ var useCrossSwap = () => {
3674
3719
  return {
3675
3720
  swap,
3676
3721
  loading,
3677
- status: layerStatus,
3722
+ bridgeStatus: layerStatus,
3723
+ status,
3678
3724
  message: bridgeMessage
3679
3725
  };
3680
3726
  };
@@ -3956,15 +4002,28 @@ var useSwap = () => {
3956
4002
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3957
4003
  const account5 = useAccountInstance();
3958
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
+ });
3959
4017
  const dstValutDeposit = useCallback(() => {
4018
+ const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
3960
4019
  return {
3961
4020
  accountId: account5.accountIdHashStr,
3962
- brokerHash: utils.parseBrokerHash(configStore.get("brokerId")),
4021
+ brokerHash: utils.parseBrokerHash(brokerId),
3963
4022
  tokenHash: utils.parseTokenHash("USDC")
3964
4023
  };
3965
4024
  }, [account5]);
3966
4025
  const swap = useCallback(
3967
- async (woofiDexDepositorAdress, inputs) => {
4026
+ async (woofiDexDepositorAdress, inputs, config) => {
3968
4027
  if (!account5.walletClient) {
3969
4028
  throw new Error("walletClient is undefined");
3970
4029
  }
@@ -3974,20 +4033,24 @@ var useSwap = () => {
3974
4033
  if (loading)
3975
4034
  return;
3976
4035
  start();
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