@agg-build/hooks 1.0.2 → 1.2.11

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/deposit.mjs CHANGED
@@ -1,16 +1,15 @@
1
1
  import {
2
2
  useRampQuotes,
3
3
  useRampSession
4
- } from "./chunk-EB64HHYM.mjs";
4
+ } from "./chunk-JWPZNCGY.mjs";
5
5
  import {
6
6
  __async,
7
- getWalletAddressFromUserProfile,
8
7
  useAggAuthState,
9
8
  useAggBalanceState,
10
9
  useAggUiConfig,
11
10
  useDepositAddresses,
12
11
  useSyncBalances
13
- } from "./chunk-V6VNA7MX.mjs";
12
+ } from "./chunk-553OI6M2.mjs";
14
13
 
15
14
  // src/deposit/normalize-wallet-error.ts
16
15
  function normalizeWalletError(error, supportedChains) {
@@ -34,16 +33,43 @@ function normalizeWalletError(error, supportedChains) {
34
33
  }
35
34
 
36
35
  // src/deposit/use-wallet-token-balance.ts
37
- import { useEffect, useMemo, useState } from "react";
36
+ import { Connection, PublicKey } from "@solana/web3.js";
38
37
  import { useQuery } from "@tanstack/react-query";
39
38
  import { formatUnits } from "viem";
40
39
  import { useAccount, useReadContract } from "wagmi";
41
- import { useWallet } from "@solana/wallet-adapter-react";
42
- import { Connection, PublicKey } from "@solana/web3.js";
43
40
 
44
41
  // src/deposit/constants.ts
45
42
  var DEFAULT_SOLANA_RPC_ENDPOINT = "https://solana-rpc.publicnode.com";
46
43
  var SVM_CHAIN_IDS = /* @__PURE__ */ new Set([792703809]);
44
+ var MULTI_CHAIN_SOLANA_WALLET_NAMES = /* @__PURE__ */ new Set(["Phantom"]);
45
+
46
+ // src/deposit/use-svm-address.ts
47
+ import { useEffect, useState } from "react";
48
+ import { useWallet } from "@solana/wallet-adapter-react";
49
+ function getPhantomProvider() {
50
+ var _a, _b;
51
+ if (typeof window === "undefined") return void 0;
52
+ const w = window;
53
+ return (_b = (_a = w.phantom) == null ? void 0 : _a.solana) != null ? _b : w.solana;
54
+ }
55
+ function useSvmAddress() {
56
+ var _a;
57
+ const { publicKey: adapterPublicKey } = useWallet();
58
+ const [phantomAddress, setPhantomAddress] = useState(void 0);
59
+ useEffect(() => {
60
+ var _a2;
61
+ const provider = getPhantomProvider();
62
+ if (!provider) return;
63
+ const existing = (_a2 = provider.publicKey) == null ? void 0 : _a2.toBase58();
64
+ if (existing) {
65
+ setPhantomAddress(existing);
66
+ return;
67
+ }
68
+ provider.connect({ onlyIfTrusted: true }).then((res) => setPhantomAddress(res.publicKey.toString())).catch(() => {
69
+ });
70
+ }, []);
71
+ return (_a = adapterPublicKey == null ? void 0 : adapterPublicKey.toBase58()) != null ? _a : phantomAddress;
72
+ }
47
73
 
48
74
  // src/deposit/use-wallet-token-balance.ts
49
75
  var ERC20_BALANCE_OF_ABI = [
@@ -55,11 +81,46 @@ var ERC20_BALANCE_OF_ABI = [
55
81
  outputs: [{ name: "", type: "uint256" }]
56
82
  }
57
83
  ];
58
- function getPhantomProvider() {
59
- var _a, _b;
60
- if (typeof window === "undefined") return void 0;
61
- const w = window;
62
- return (_b = (_a = w.phantom) == null ? void 0 : _a.solana) != null ? _b : w.solana;
84
+ var SVM_BALANCE_RPC_TIMEOUT_MS = 2e3;
85
+ var SVM_BALANCE_STALE_TIME_MS = 3e4;
86
+ var SvmBalanceTimeoutError = class extends Error {
87
+ constructor() {
88
+ super("Solana balance request timed out");
89
+ this.name = "SvmBalanceTimeoutError";
90
+ }
91
+ };
92
+ function withTimeout(promise, timeoutMs) {
93
+ return new Promise((resolve, reject) => {
94
+ const timer = setTimeout(() => reject(new SvmBalanceTimeoutError()), timeoutMs);
95
+ promise.then(
96
+ (value) => {
97
+ clearTimeout(timer);
98
+ resolve(value);
99
+ },
100
+ (err) => {
101
+ clearTimeout(timer);
102
+ reject(err);
103
+ }
104
+ );
105
+ });
106
+ }
107
+ function fetchSvmTokenBalance(rpcUrl, owner, mint) {
108
+ return __async(this, null, function* () {
109
+ var _a;
110
+ const connection = new Connection(rpcUrl);
111
+ const accounts = yield withTimeout(
112
+ connection.getTokenAccountsByOwner(owner, { mint }),
113
+ SVM_BALANCE_RPC_TIMEOUT_MS
114
+ );
115
+ if (accounts.value.length === 0) return 0;
116
+ const first = accounts.value[0];
117
+ if (!first) return 0;
118
+ const balance = yield withTimeout(
119
+ connection.getTokenAccountBalance(first.pubkey),
120
+ SVM_BALANCE_RPC_TIMEOUT_MS
121
+ );
122
+ return (_a = balance.value.uiAmount) != null ? _a : 0;
123
+ });
63
124
  }
64
125
  function useWalletTokenBalance({
65
126
  isOpen = true,
@@ -68,7 +129,7 @@ function useWalletTokenBalance({
68
129
  decimals,
69
130
  svmAddress
70
131
  }) {
71
- var _a, _b, _c;
132
+ var _a;
72
133
  const { solanaRpcUrl } = useAggUiConfig();
73
134
  const rpcEndpoint = solanaRpcUrl != null ? solanaRpcUrl : DEFAULT_SOLANA_RPC_ENDPOINT;
74
135
  const isSvm = chainId !== void 0 && SVM_CHAIN_IDS.has(chainId);
@@ -82,45 +143,27 @@ function useWalletTokenBalance({
82
143
  args: evmAddress ? [evmAddress] : void 0,
83
144
  query: { enabled: evmEnabled, staleTime: 0, refetchOnMount: true }
84
145
  });
85
- const { publicKey: adapterPublicKey } = useWallet();
86
- const [phantomAddress, setPhantomAddress] = useState(void 0);
87
- useEffect(() => {
88
- var _a2;
89
- if (!isSvm) return;
90
- const provider = getPhantomProvider();
91
- if (!provider) return;
92
- const existing = (_a2 = provider.publicKey) == null ? void 0 : _a2.toBase58();
93
- if (existing) {
94
- setPhantomAddress(existing);
95
- return;
96
- }
97
- provider.connect({ onlyIfTrusted: true }).then((res) => setPhantomAddress(res.publicKey.toString())).catch(() => {
98
- });
99
- }, [isSvm]);
100
- const resolvedSvmAddress = (_b = (_a = adapterPublicKey == null ? void 0 : adapterPublicKey.toBase58()) != null ? _a : phantomAddress) != null ? _b : svmAddress;
101
- const connection = useMemo(() => new Connection(rpcEndpoint), [rpcEndpoint]);
146
+ const detectedSvmAddress = useSvmAddress();
147
+ const resolvedSvmAddress = detectedSvmAddress != null ? detectedSvmAddress : svmAddress;
102
148
  const svmEnabled = isOpen && isSvm && !!resolvedSvmAddress && !!tokenAddress;
103
149
  const svmQuery = useQuery({
104
150
  queryKey: ["svm-token-balance", resolvedSvmAddress != null ? resolvedSvmAddress : null, tokenAddress != null ? tokenAddress : null, isOpen],
105
151
  enabled: svmEnabled,
106
- staleTime: 0,
152
+ staleTime: SVM_BALANCE_STALE_TIME_MS,
153
+ retry: false,
154
+ refetchOnWindowFocus: "always",
155
+ refetchOnReconnect: "always",
107
156
  queryFn: () => __async(null, null, function* () {
108
- var _a2;
109
157
  if (!resolvedSvmAddress || !tokenAddress) return 0;
110
158
  const owner = new PublicKey(resolvedSvmAddress);
111
159
  const mint = new PublicKey(tokenAddress);
112
- const accounts = yield connection.getTokenAccountsByOwner(owner, { mint });
113
- if (accounts.value.length === 0) return 0;
114
- const first = accounts.value[0];
115
- if (!first) return 0;
116
- const balance = yield connection.getTokenAccountBalance(first.pubkey);
117
- return (_a2 = balance.value.uiAmount) != null ? _a2 : 0;
160
+ return yield fetchSvmTokenBalance(rpcEndpoint, owner, mint);
118
161
  })
119
162
  });
120
163
  if (isSvm) {
121
164
  const awaitingSelection2 = isOpen && !!chainId && !tokenAddress;
122
165
  return {
123
- balance: (_c = svmQuery.data) != null ? _c : 0,
166
+ balance: (_a = svmQuery.data) != null ? _a : 0,
124
167
  isLoading: awaitingSelection2 || svmQuery.isLoading || svmQuery.isFetching
125
168
  };
126
169
  }
@@ -327,7 +370,7 @@ function useWalletSendToken() {
327
370
  }
328
371
 
329
372
  // src/deposit/use-wallet-transaction-status.ts
330
- import { useMemo as useMemo2 } from "react";
373
+ import { useMemo } from "react";
331
374
  import { useQuery as useQuery2 } from "@tanstack/react-query";
332
375
  import { formatEther } from "viem";
333
376
  import { Connection as Connection3 } from "@solana/web3.js";
@@ -349,7 +392,7 @@ function useWalletTransactionStatus({
349
392
  refetchInterval: 2e3
350
393
  }
351
394
  });
352
- const connection = useMemo2(() => new Connection3(rpcEndpoint, "confirmed"), [rpcEndpoint]);
395
+ const connection = useMemo(() => new Connection3(rpcEndpoint, "confirmed"), [rpcEndpoint]);
353
396
  const svmQuery = useQuery2({
354
397
  queryKey: ["wallet-transaction-status", chainId != null ? chainId : null, txId != null ? txId : null],
355
398
  enabled: isSvm && !!txId,
@@ -385,11 +428,13 @@ function useWalletTransactionStatus({
385
428
  }
386
429
 
387
430
  // src/deposit/use-deposit-flow.ts
388
- import { useCallback as useCallback2, useEffect as useEffect2, useMemo as useMemo3, useRef, useState as useState2 } from "react";
431
+ import { useCallback as useCallback2, useEffect as useEffect2, useMemo as useMemo2, useRef, useState as useState2 } from "react";
389
432
  import { useAccount as useAccount2, useSwitchChain } from "wagmi";
390
433
  import { useWallet as useSolanaWallet } from "@solana/wallet-adapter-react";
434
+ var DEFAULT_WALLET_CHAIN_ID = 1;
435
+ var DEFAULT_WALLET_TOKEN_SYMBOL = "USDC";
391
436
  function useDepositFlow(options) {
392
- var _a, _b;
437
+ var _a, _b, _c;
393
438
  const { open, onOpenChange } = options;
394
439
  const { user } = useAggAuthState();
395
440
  const { walletActions } = useAggUiConfig();
@@ -433,45 +478,46 @@ function useDepositFlow(options) {
433
478
  }, []);
434
479
  const { chainId: connectedChainId, address: connectedAddress } = useAccount2();
435
480
  const { switchChainAsync } = useSwitchChain();
436
- const { publicKey: solanaPublicKey } = useSolanaWallet();
437
- const balanceChainId = walletModalChainId ? Number(walletModalChainId) : connectedChainId;
438
- const selectedChainTokens = useMemo3(() => {
481
+ const { wallet: solanaWallet } = useSolanaWallet();
482
+ const resolvedSvmAddress = useSvmAddress();
483
+ const solanaAdapterName = (_b = solanaWallet == null ? void 0 : solanaWallet.adapter) == null ? void 0 : _b.name;
484
+ const isMultiChainSolanaWallet = Boolean(
485
+ solanaAdapterName && MULTI_CHAIN_SOLANA_WALLET_NAMES.has(solanaAdapterName)
486
+ );
487
+ const hasSolanaCapability = Boolean(resolvedSvmAddress || (solanaWallet == null ? void 0 : solanaWallet.adapter));
488
+ const hasEvmCapability = Boolean(connectedAddress) || isMultiChainSolanaWallet;
489
+ const defaultBalanceChainId = hasEvmCapability && (supportedChains == null ? void 0 : supportedChains.some((chain) => chain.chainId === DEFAULT_WALLET_CHAIN_ID)) ? DEFAULT_WALLET_CHAIN_ID : connectedChainId;
490
+ const balanceChainId = walletModalChainId ? Number(walletModalChainId) : defaultBalanceChainId;
491
+ const selectedChainTokens = useMemo2(() => {
439
492
  var _a2;
440
493
  if (!supportedChains || !balanceChainId) return [];
441
494
  const chain = supportedChains.find((c) => c.chainId === balanceChainId);
442
495
  return (_a2 = chain == null ? void 0 : chain.tokens) != null ? _a2 : [];
443
496
  }, [supportedChains, balanceChainId]);
444
- const selectedTokenMeta = useMemo3(() => {
445
- if (!walletModalTokenSymbol) return void 0;
446
- const match = selectedChainTokens.find((t) => t.symbol === walletModalTokenSymbol);
497
+ const selectedTokenMeta = useMemo2(() => {
498
+ const preferredTokenSymbol = walletModalTokenSymbol != null ? walletModalTokenSymbol : DEFAULT_WALLET_TOKEN_SYMBOL;
499
+ const match = selectedChainTokens.find((t) => t.symbol === preferredTokenSymbol);
447
500
  return match ? { address: match.address, decimals: match.decimals } : void 0;
448
501
  }, [selectedChainTokens, walletModalTokenSymbol]);
449
- const authSvmAddress = useMemo3(() => {
450
- var _a2;
451
- const wallets = user == null ? void 0 : user.wallets;
452
- return (_a2 = wallets == null ? void 0 : wallets.find((w) => w.chain === "solana" || w.chain === "svm")) == null ? void 0 : _a2.address;
453
- }, [user]);
454
502
  const isSvmBalanceChain = balanceChainId !== void 0 && SVM_CHAIN_IDS.has(balanceChainId);
455
- const walletAddress = useMemo3(() => {
456
- var _a2, _b2, _c;
503
+ const walletAddress = useMemo2(() => {
457
504
  if (isSvmBalanceChain) {
458
- return (_b2 = (_a2 = solanaPublicKey == null ? void 0 : solanaPublicKey.toBase58()) != null ? _a2 : authSvmAddress) != null ? _b2 : "";
505
+ return resolvedSvmAddress != null ? resolvedSvmAddress : "";
459
506
  }
460
- return (_c = connectedAddress != null ? connectedAddress : getWalletAddressFromUserProfile(user)) != null ? _c : "";
461
- }, [authSvmAddress, connectedAddress, isSvmBalanceChain, solanaPublicKey, user]);
462
- const walletLabel = useMemo3(() => {
507
+ return connectedAddress != null ? connectedAddress : "";
508
+ }, [connectedAddress, isSvmBalanceChain, resolvedSvmAddress]);
509
+ const walletLabel = useMemo2(() => {
463
510
  return walletAddress ? `${walletAddress.slice(0, 6)}...${walletAddress.slice(-4)}` : "\u2014";
464
511
  }, [walletAddress]);
465
512
  const { balance: walletBalance, isLoading: isWalletBalanceLoading } = useWalletTokenBalance({
466
513
  isOpen: open,
467
514
  chainId: balanceChainId,
468
515
  tokenAddress: selectedTokenMeta == null ? void 0 : selectedTokenMeta.address,
469
- decimals: selectedTokenMeta == null ? void 0 : selectedTokenMeta.decimals,
470
- svmAddress: authSvmAddress
516
+ decimals: selectedTokenMeta == null ? void 0 : selectedTokenMeta.decimals
471
517
  });
472
518
  const { status: observedWalletTransactionStatus, gasFee: walletTransactionGasFee } = useWalletTransactionStatus({
473
519
  chainId: walletTransaction == null ? void 0 : walletTransaction.chainId,
474
- txId: (_b = walletTransaction == null ? void 0 : walletTransaction.txId) != null ? _b : null
520
+ txId: (_c = walletTransaction == null ? void 0 : walletTransaction.txId) != null ? _c : null
475
521
  });
476
522
  const walletTransactionStatus = walletTransactionError ? "error" : walletTransaction ? observedWalletTransactionStatus != null ? observedWalletTransactionStatus : "submitted" : void 0;
477
523
  const syncedDepositTxIdRef = useRef(null);
@@ -499,9 +545,15 @@ function useDepositFlow(options) {
499
545
  },
500
546
  [onOpenChange]
501
547
  );
548
+ const handleCancelWalletDeposit = useCallback2(() => {
549
+ setWalletTransaction(null);
550
+ setWalletTransactionError(null);
551
+ setWalletTransactionErrorTone("error");
552
+ syncedDepositTxIdRef.current = null;
553
+ }, []);
502
554
  const handleConfirmWalletDeposit = useCallback2(
503
555
  (params) => __async(null, null, function* () {
504
- var _a2, _b2, _c;
556
+ var _a2, _b2, _c2;
505
557
  const { address, amount, chainId, token } = params;
506
558
  const chain = (_a2 = supportedChains == null ? void 0 : supportedChains.find((c) => c.chainId === chainId)) != null ? _a2 : null;
507
559
  const tokenMeta = (_b2 = chain == null ? void 0 : chain.tokens.find((t) => t.symbol === token)) != null ? _b2 : null;
@@ -517,7 +569,7 @@ function useDepositFlow(options) {
517
569
  token: {
518
570
  symbol: token,
519
571
  address: tokenMeta == null ? void 0 : tokenMeta.address,
520
- decimals: (_c = tokenMeta == null ? void 0 : tokenMeta.decimals) != null ? _c : 6
572
+ decimals: (_c2 = tokenMeta == null ? void 0 : tokenMeta.decimals) != null ? _c2 : 6
521
573
  },
522
574
  to: address,
523
575
  amount
@@ -585,11 +637,12 @@ function useDepositFlow(options) {
585
637
  onWalletAmountChange: setDepositAmount,
586
638
  onWalletMax: useCallback2(() => setDepositAmount(String(walletBalance)), [walletBalance]),
587
639
  onConfirmWalletDeposit: handleConfirmWalletDeposit,
640
+ onCancelWalletDeposit: handleCancelWalletDeposit,
588
641
  onWalletNetworkChange: setWalletModalChainId,
589
642
  onWalletTokenChange: setWalletModalTokenSymbol,
590
643
  initialWalletChainId: connectedChainId ? String(connectedChainId) : void 0,
591
- connectedWalletKind: connectedChainId && !solanaPublicKey ? "evm" : solanaPublicKey && !connectedChainId ? "solana" : void 0,
592
- sendCryptoConfig: { minDeposit: "$1", feeEstimate: "~$0.01", eta: "~30s" },
644
+ connectedWalletKind: hasEvmCapability && !hasSolanaCapability ? "evm" : hasSolanaCapability && !hasEvmCapability ? "solana" : void 0,
645
+ sendCryptoConfig: { minDeposit: "$1", eta: "~30s" },
593
646
  onDoneSendCrypto: useCallback2(
594
647
  () => handleDepositModalOpenChange(false),
595
648
  [handleDepositModalOpenChange]