useConnection()
Hook to access the Solana RPC connection. Supports both legacy @solana/web3.js Connection and modern @solana/kit Rpc.Return Values
DualConnection
Solana connection instance for RPC calls. Can be either:
- Legacy:
Connectionfrom @solana/web3.js - Kit:
Rpcfrom @solana/kit
ConnectionProvider or HermisProvider.
All dual architecture transaction methods work seamlessly with either type.WalletAdapterNetwork
Current network (mainnet-beta, devnet, testnet). Optional field.
Usage with web3.js Connection
Usage with Kit Rpc
Provider Setup
With Legacy Connection
With Kit Rpc (Custom Provider)
Note:ConnectionProvider creates a legacy Connection internally. To use Kit Rpc, create a custom provider:
With HermisProvider (Simplified)
Note:HermisProvider uses ConnectionProvider internally, which creates a legacy Connection.
HermisProvider is the recommended approach for most applications. It combines wallet and connection setup with sensible defaults.
DualConnection Type
TheDualConnection type is a union type that accepts both architectures:
Connectionfrom @solana/web3.js (legacy)Rpcfrom @solana/kit (modern)
sendTransaction, signAndSendTransaction) automatically handle both connection types.
Network Detection
When using auto-detected networks, the connection analyzes the RPC endpoint URL:- URLs containing “devnet” →
WalletAdapterNetwork.Devnet - URLs containing “testnet” →
WalletAdapterNetwork.Testnet - URLs containing “mainnet” →
WalletAdapterNetwork.Mainnet
Common RPC Methods
Both Connection and Kit Rpc support these common methods (with slight API differences):getBalance
getAccountInfo
getLatestBlockhash
Type Safety
TypeScript will correctly infer the connection type based on your provider setup:Migration Notes
If you’re upgrading from v1 and want to use Kit:- Stick with Connection if you have existing web3.js code
- Upgrade to Kit Rpc for modern features and better tree-shaking
Using Kit Rpc Without Providers
If you want to use Kit Rpc but don’t want to create a custom provider, you can pass the connection directly to transaction methods:sendTransaction, signAndSendTransaction, etc.) accept DualConnection and handle both connection types automatically.