forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWallet.ts
More file actions
65 lines (58 loc) · 1.68 KB
/
Copy pathuseWallet.ts
File metadata and controls
65 lines (58 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { useWalletContext } from '@/context/WalletContext'
import type { WalletState } from '@/types'
/**
* Primary hook for interacting with the Freighter wallet.
*
* Returns all wallet state plus connect / disconnect / sign helpers.
*/
export function useWallet() {
const { wallet, connect, disconnect, refreshAccount, setNetwork, signTransaction } =
useWalletContext()
const isConnected = wallet.status === 'connected'
const isConnecting = wallet.status === 'connecting'
const hasError = wallet.status === 'error'
const xlmBalance = wallet.account?.balances.find(
(b) => b.asset.code === 'XLM',
)?.balance ?? null
const usdcBalance = wallet.account?.balances.find(
(b) => b.asset.code === 'USDC',
)?.balance ?? null
return {
// State
wallet,
publicKey: wallet.publicKey,
network: wallet.network,
account: wallet.account,
isConnected,
isConnecting,
hasError,
error: wallet.error,
isFreighterInstalled: wallet.isFreighterInstalled,
// Derived balances
xlmBalance,
usdcBalance,
// Actions
connect,
disconnect,
refreshAccount,
setNetwork,
signTransaction,
} satisfies {
wallet: WalletState
publicKey: string | null
network: 'testnet' | 'mainnet'
account: WalletState['account']
isConnected: boolean
isConnecting: boolean
hasError: boolean
error: string | null
isFreighterInstalled: boolean
xlmBalance: string | null
usdcBalance: string | null
connect: () => Promise<void>
disconnect: () => void
refreshAccount: () => Promise<void>
setNetwork: (n: 'testnet' | 'mainnet') => void
signTransaction: (xdr: string) => Promise<string>
}
}