@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.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");
@@ -3172,21 +3173,21 @@ var useDeposit = (options) => {
3172
3173
  decimals
3173
3174
  });
3174
3175
  } else {
3175
- balance2 = await account5.assetsManager.getBalance(address);
3176
+ balance2 = await account5.assetsManager.getBalance(address, { decimals });
3176
3177
  }
3177
3178
  return balance2;
3178
3179
  },
3179
3180
  []
3180
3181
  );
3181
3182
  const fetchBalance = useCallback(
3182
- async (address) => {
3183
+ async (address, decimals) => {
3183
3184
  if (!address)
3184
3185
  return;
3185
3186
  try {
3186
3187
  if (balanceRevalidating)
3187
3188
  return;
3188
3189
  setBalanceRevalidating(true);
3189
- const balance2 = await fetchBalanceHandler(address);
3190
+ const balance2 = await fetchBalanceHandler(address, decimals);
3190
3191
  console.log("----- refresh balance -----", balance2);
3191
3192
  setBalance(() => balance2);
3192
3193
  setBalanceRevalidating(false);
@@ -3210,46 +3211,81 @@ 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
+ prevAddress.current = key;
3223
+ const allowance2 = await account5.assetsManager.getAllowance(
3224
+ address,
3225
+ vaultAddress
3226
+ );
3227
+ setAllowance(() => allowance2);
3228
+ return allowance2;
3229
+ };
3230
+ const getAllowanceByDefaultAddress = async (address) => {
3231
+ if (prevAddress.current === address)
3232
+ return;
3233
+ if (!address || isNativeTokenChecker(address))
3234
+ return;
3235
+ prevAddress.current = address;
3236
+ const allowance2 = await account5.assetsManager.getAllowance(address);
3237
+ console.log(
3238
+ "----- refresh allowance only orderly -----",
3239
+ { allowance: allowance2 }
3240
+ );
3241
+ setAllowance(() => allowance2);
3242
+ };
3243
+ const getVaultAddress = useCallback(() => {
3244
+ if (dst.chainId !== options?.srcChainId) {
3245
+ return options?.crossChainRouteAddress;
3246
+ } else {
3247
+ if (dst.symbol !== options?.srcToken) {
3248
+ return options?.depositorAddress;
3249
+ }
3250
+ }
3251
+ }, [options, dst]);
3234
3252
  useEffect(() => {
3235
3253
  if (state.status < AccountStatusEnum.Connected)
3236
3254
  return;
3237
- fetchBalance(options?.address);
3238
- getAllowance(options?.address, options?.vaultAddress);
3239
- }, [state.status, options?.address, options?.vaultAddress, account5.address]);
3255
+ fetchBalance(options?.address, options?.decimals);
3256
+ if (dst.chainId !== options?.srcChainId) {
3257
+ getAllowance(options?.address, options?.crossChainRouteAddress);
3258
+ } else {
3259
+ if (dst.symbol !== options?.srcToken) {
3260
+ getAllowance(options?.address, options?.depositorAddress);
3261
+ } else {
3262
+ getAllowanceByDefaultAddress(options?.address);
3263
+ }
3264
+ }
3265
+ }, [
3266
+ state.status,
3267
+ options?.address,
3268
+ options?.crossChainRouteAddress,
3269
+ options?.depositorAddress,
3270
+ options?.srcChainId,
3271
+ options?.srcToken,
3272
+ account5.address,
3273
+ dst
3274
+ ]);
3240
3275
  const approve = useCallback(
3241
3276
  (amount) => {
3242
3277
  if (!options?.address) {
3243
3278
  throw new Error("address is required");
3244
3279
  }
3245
- return account5.assetsManager.approve(options.address, amount, options?.vaultAddress).then((result) => {
3280
+ const vaultAddress = getVaultAddress();
3281
+ return account5.assetsManager.approve(options.address, amount, vaultAddress).then((result) => {
3246
3282
  if (typeof amount !== "undefined") {
3247
3283
  setAllowance((value) => new Decimal(value).add(amount).toString());
3248
3284
  }
3249
3285
  return result;
3250
3286
  });
3251
3287
  },
3252
- [account5, getAllowance, options?.address, options?.vaultAddress]
3288
+ [account5, getAllowance, options?.address]
3253
3289
  );
3254
3290
  const deposit = useCallback(
3255
3291
  (amount) => {
@@ -3577,24 +3613,37 @@ var useCrossSwap = () => {
3577
3613
  "WAITTING" /* INITIALIZING */
3578
3614
  );
3579
3615
  const [bridgeMessage, setBridgeMessage] = useState();
3616
+ const [status, setStatus] = useState(
3617
+ WS_WalletStatusEnum.NO
3618
+ );
3619
+ const txHashFromBridge = useRef();
3580
3620
  const account5 = useAccountInstance();
3581
3621
  const { networkId, configStore } = useContext(OrderlyContext);
3582
3622
  const client = useRef(createClient(networkId)).current;
3583
3623
  const timer = useRef();
3624
+ useWalletSubscription({
3625
+ onMessage: (message) => {
3626
+ const { side, transStatus, trxId } = message;
3627
+ if (side === "DEPOSIT" && trxId === txHashFromBridge.current) {
3628
+ setStatus(transStatus);
3629
+ }
3630
+ }
3631
+ });
3584
3632
  const checkLayerStatus = useCallback((txHash) => {
3585
3633
  const check = async (txHash2) => {
3586
3634
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3587
3635
  console.log("messages:", messages);
3588
3636
  if (messages.length > 0) {
3589
- const { status } = messages[0];
3590
- if (status === "INFLIGHT" /* INFLIGHT */) {
3637
+ const { status: status2 } = messages[0];
3638
+ if (status2 === "INFLIGHT" /* INFLIGHT */) {
3591
3639
  setTimeout(() => {
3592
3640
  check(txHash2);
3593
3641
  }, 1e3);
3594
3642
  }
3595
- setLayerStatus(status);
3596
- if (status === "DELIVERED" /* DELIVERED */) {
3643
+ setLayerStatus(status2);
3644
+ if (status2 === "DELIVERED" /* DELIVERED */) {
3597
3645
  setBridgeMessage(messages[0]);
3646
+ txHashFromBridge.current = messages[0].dstTxHash;
3598
3647
  }
3599
3648
  } else {
3600
3649
  setTimeout(() => {
@@ -3648,20 +3697,12 @@ var useCrossSwap = () => {
3648
3697
  from: account5.address,
3649
3698
  to: crossChainRouteAddress,
3650
3699
  data: [account5.address, src, dst, dstValutDeposit()],
3651
- value: quotoLZFee[0]
3700
+ value: isNativeTokenChecker(inputs.src.fromToken) ? BigInt(inputs.src.fromAmount) + quotoLZFee[0] : quotoLZFee[0]
3652
3701
  },
3653
3702
  {
3654
3703
  abi: woofiDexCrossChainRouterAbi
3655
3704
  }
3656
3705
  );
3657
- account5.walletClient.on(
3658
- {
3659
- address: crossChainRouteAddress
3660
- },
3661
- (log, event) => {
3662
- console.log("-------------", log, event);
3663
- }
3664
- );
3665
3706
  stop();
3666
3707
  checkLayerStatus(result.hash);
3667
3708
  return pick(["from", "to", "hash", "value"], result);
@@ -3674,7 +3715,8 @@ var useCrossSwap = () => {
3674
3715
  return {
3675
3716
  swap,
3676
3717
  loading,
3677
- status: layerStatus,
3718
+ bridgeStatus: layerStatus,
3719
+ status,
3678
3720
  message: bridgeMessage
3679
3721
  };
3680
3722
  };
@@ -3956,15 +3998,28 @@ var useSwap = () => {
3956
3998
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3957
3999
  const account5 = useAccountInstance();
3958
4000
  const { configStore } = useContext(OrderlyContext);
4001
+ const [status, setStatus] = useState(
4002
+ WS_WalletStatusEnum.NO
4003
+ );
4004
+ const txHash = useRef();
4005
+ useWalletSubscription({
4006
+ onMessage: (message) => {
4007
+ const { side, transStatus, trxId } = message;
4008
+ if (side === "DEPOSIT" && trxId === txHash.current) {
4009
+ setStatus(transStatus);
4010
+ }
4011
+ }
4012
+ });
3959
4013
  const dstValutDeposit = useCallback(() => {
4014
+ const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
3960
4015
  return {
3961
4016
  accountId: account5.accountIdHashStr,
3962
- brokerHash: utils.parseBrokerHash(configStore.get("brokerId")),
4017
+ brokerHash: utils.parseBrokerHash(brokerId),
3963
4018
  tokenHash: utils.parseTokenHash("USDC")
3964
4019
  };
3965
4020
  }, [account5]);
3966
4021
  const swap = useCallback(
3967
- async (woofiDexDepositorAdress, inputs) => {
4022
+ async (woofiDexDepositorAdress, inputs, config) => {
3968
4023
  if (!account5.walletClient) {
3969
4024
  throw new Error("walletClient is undefined");
3970
4025
  }
@@ -3974,20 +4029,24 @@ var useSwap = () => {
3974
4029
  if (loading)
3975
4030
  return;
3976
4031
  start();
4032
+ const txPayload = {
4033
+ from: account5.address,
4034
+ to: woofiDexDepositorAdress,
4035
+ data: [account5.address, inputs, dstValutDeposit()],
4036
+ value: isNativeTokenChecker(inputs.fromToken) ? BigInt(inputs.fromAmount) + inputs.orderlyNativeFees : inputs.orderlyNativeFees
4037
+ };
3977
4038
  try {
3978
4039
  const result = await account5.walletClient.sendTransaction(
3979
4040
  woofiDexDepositorAdress,
3980
4041
  "swap",
3981
- {
3982
- from: account5.address,
3983
- to: woofiDexDepositorAdress,
3984
- data: [account5.address, inputs, dstValutDeposit()]
3985
- },
4042
+ txPayload,
3986
4043
  {
3987
4044
  abi: woofiDexDepositorAbi
3988
4045
  }
3989
4046
  );
3990
4047
  stop();
4048
+ console.log("single swap result", result);
4049
+ txHash.current = result.hash;
3991
4050
  return pick(["from", "to", "hash", "value"], result);
3992
4051
  } catch (e) {
3993
4052
  console.log("\u8C03\u7528\u5408\u7EA6\u62A5\u9519\uFF1A", e);
@@ -3998,7 +4057,8 @@ var useSwap = () => {
3998
4057
  );
3999
4058
  return {
4000
4059
  swap,
4001
- loading
4060
+ loading,
4061
+ status
4002
4062
  };
4003
4063
  };
4004
4064