Integrating Phantom (Solana) Wallet into dApps
Phantom is the de facto standard wallet in the Solana ecosystem with over 70% market share among active users. Its API injects into window.solana and follows the SolanaProvider specification. However, asynchronous injection causes 30% of "Provider not found" errors in new dApps. Over five years of work, we have integrated Phantom into 50+ projects—from simple NFT marketplaces to multimillion-dollar DeFi protocols. And each time we encountered the same pitfalls: provider overwritten by other wallets, state loss on account switch, unstable fees. Let's break them down with concrete code examples.
How to Properly Detect the Phantom Provider
The first mistake is checking window.solana immediately on page load. The extension injects asynchronously, and on fast machines it injects before your JS executes, but on slow ones it doesn't. In 30% of projects this led to "Provider not found" errors. A reliable pattern uses window.phantom.solana, avoiding conflicts:
const getProvider = (): PhantomProvider | undefined => { if ('phantom' in window) { const provider = (window as any).phantom?.solana; if (provider?.isPhantom) return provider; } return undefined; }; window.phantom.solana is preferred over window.solana because the latter can be intercepted by other wallets (Backpack, Solflare). If you need multiple wallet support, use the wallet-adapter from Solana Labs—@solana/wallet-adapter-react, which abstracts all providers via a unified interface. Additionally, we recommend delaying the connect() call by 100ms after DOMContentLoaded to guarantee injection.
Connection, Signing, and Transactions: Details
// Connection const response = await provider.connect(); const publicKey = response.publicKey.toString(); // Sign message (for authentication) const message = new TextEncoder().encode("Sign in to MyApp"); const { signature } = await provider.signMessage(message, "utf8"); // Send transaction const transaction = new Transaction().add(/* instruction */); transaction.feePayer = provider.publicKey; transaction.recentBlockhash = ( await connection.getLatestBlockhash() ).blockhash; const { signature: txSig } = await provider.signAndSendTransaction(transaction); Important: signAndSendTransaction sends the transaction through Phantom's own RPC. If you need to control the RPC endpoint (e.g., use Helius or QuickNode with priority fees), use signTransaction + connection.sendRawTransaction manually. This reduces latency by 40% under peak loads. In one project, using signAndSendTransaction caused three failed transactions during peak hours, and the user lost 0.5 SOL in fees. Switching to signTransaction with our own RPC eliminated the issue.
| Method | RPC Control | Latency | Fee Safety |
|---|---|---|---|
signAndSendTransaction |
No | Medium | Low |
signTransaction + sendRawTransaction |
Yes | Low | High |
Why Using signAndSendTransaction Is a Risk
The signAndSendTransaction method is convenient, but it takes away your control over fees. Phantom uses its own RPC, which may not handle load spikes. We always recommend using signTransaction and sending via your own RPC. This is especially critical for DeFi applications where every second counts. In practice, average gas savings amount to 15% by choosing the right RPC and batching transactions.
Handling State and Events
Phantom emits connect, disconnect, and accountChanged events. You must subscribe to accountChanged—the user may switch accounts inside the wallet without reconnecting, and your app won't know. In one project, this led to displaying someone else's balance for 10 minutes—a serious bug we caught during testing.
provider.on('accountChanged', (publicKey: PublicKey | null) => { if (publicKey) { // Update app state } else { // Wallet locked — logout user provider.connect().catch(() => {}); } }); For React apps, it's better to extract this layer into @solana/wallet-adapter-react—it handles lifecycle, memoization, and reconnection automatically.
Comparison: Manual Integration vs @solana/wallet-adapter-react
| Aspect | Manual Integration | wallet-adapter |
|---|---|---|
| Multiple wallet support | No, Phantom only | Yes (Phantom, Solflare, Backpack) |
| State management | DIY | Automatic |
| Connection lifecycle | Manual | Automatic |
| Reconnection | No | Built-in |
| Code volume | ~200 lines | ~30 lines |
What's Included in Phantom Integration?
- Documentation on connecting and configuring Phantom in your dApp
- Code examples for connection, signing, and sending transactions
- Handling of
accountChanged,connect,disconnectevents - Testing on real accounts (mainnet/testnet)
- Security checklist: reentrancy checks, flash loan attack protection
- Post-launch support—30 days of free consultations
- Gas optimization: average 15% savings via proper RPC selection and batch transactions
How We Ensure Integration Security
We use formal verification of smart contracts with Mythril and Slither. Each wallet interaction is tested for resilience against reentrancy and flash loan attacks. We also apply fuzz testing via Echidna—this uncovered 12 hidden bugs over the past six months. Our engineers hold blockchain security certifications, and every project undergoes code review before deployment.
Contact Us for a Scope Assessment
We are a team with 5 years of experience in blockchain development. With over 50 projects on Solana, Ethereum, and other chains, we guarantee timely integration without critical bugs. Get a consultation for your project—reach out to us for a scope and budget estimate.
Official Phantom documentation is available on GitHub for in-depth API study.







