βœ… Master WalletConnect Integration

Understand QR sessions, message signing, and transaction approval

Integrate Web3 wallets into your dApp

βœ“ Key Takeaways

🎯 Core Concepts

  • πŸ”Œ
    Wallet = Web3 Passport: Your crypto wallet (MetaMask, Coinbase Wallet) is your universal identity across all dApps. One connection works everywhereβ€”no accounts, no passwords, no 2FA. Private keys stay in your wallet, never exposed to websites.
  • πŸ”
    Connection Detection: Check for window.ethereum to detect browser wallets. Call eth_requestAccounts to request connection. User must approve in wallet popup. Handle rejection (error 4001) gracefully.
  • ✍️
    Message Signing (Free): Used for authentication, voting (Snapshot), permits (EIP-2612). Zero gas cost. Signature proves wallet ownership without revealing private key. Verify with ecrecover or ethers.verifyMessage().
  • πŸ“
    Transaction Signing (Costs Gas): Modifies blockchain state. Users review: to address, value, data, gas cost. Wallet signs with private key, broadcasts to network. Irreversible once confirmed. Total cost = (gasLimit Γ— gasPrice) + value.
  • β›½
    Gas = Transaction Fee: Paid in ETH to miners/validators. Simple transfer: 21,000 gas. Token swap: ~180,000 gas. Higher gas price = faster confirmation. EIP-1559: base fee (burned) + priority fee (to miner). Wallets estimate for users.
  • ⚠️
    Security: Trust but Verify: Always check: contract address (official?), token approvals (unlimited = dangerous), transaction simulation (unexpected transfers?). Mistakes are irreversible. Use tools like revoke.cash, Tenderly, Etherscan verification.

πŸ› οΈ Best Practices for Developers

Use Wallet Libraries

Don't build from scratch. Use RainbowKit, ConnectKit, or web3modal. They handle: multi-wallet support, network switching, error handling, mobile (WalletConnect), and state management. Battle-tested by thousands of dApps.

Show Transaction Previews

Display human-readable summaries: "Swap 1 ETH β†’ 2000 USDC" instead of raw hex data. Show expected outcomes. Use transaction simulation APIs. Clear previews prevent user errors and build trust.

Handle All States

Loading, connecting, connected, error, wrong network, rejected, account switched, disconnected. Show clear feedback for each state. Test with slow networks, mobile wallets, and user rejections.

Listen for Events

accountsChanged, chainChanged, disconnect. Update UI immediately when user switches accounts or networks. Clean up listeners on unmount to prevent memory leaks.

πŸ“š Real-World Examples

UniswapDEX

Uses RainbowKit for wallet connection. Shows transaction preview: "Swap 1 ETH for 2,041 USDC (slippage 0.5%)". Simulates transaction before signing. Displays gas cost in USD. One-click approval + swap bundling.

OpenSeaNFT Marketplace

Uses message signing for authentication: "Sign this message to prove you own this wallet". No password needed. Gasless login. Signs off-chain orders (Seaport protocol) to list NFTs without immediate gas cost. Only buyer pays gas.

Snapshot.orgDAO Voting

100% gasless voting via message signing. Users sign vote (FOR/AGAINST), signature stored off-chain. Votes aggregated and displayed. Optional on-chain execution via SafeSnap if proposal passes. Enables participation without ETH for gas.

πŸŽ“ Test Your Knowledge

Take a 5-question quiz to verify your understanding of wallet connection, message signing, transaction signing, and gas mechanics.