Web3 Frontend Setup

Back

Loading concept...

๐ŸŒ‰ Web3 Frontend Setup: Your Bridge to the Blockchain

Imagine you want to enter a magical castle (the blockchain). You need a special key (your wallet), a translator (Web3 libraries), and a trusted guide (provider). Letโ€™s learn how to build this bridge!


๐ŸŽฏ What Youโ€™ll Learn

graph TD A["You"] -->|Connect| B["Wallet"] B -->|Through| C["Provider"] C -->|Using| D["Web3 Libraries"] D -->|Talk to| E["Blockchain"]

๐Ÿ” Web3 Authentication

The Bouncer at the Club

Think of Web3 authentication like showing your ID at a club entrance. But hereโ€™s the cool part: you own your ID card, not the club!

In traditional apps (like Facebook):

  • They hold your password
  • They can lock you out
  • They know everything about you

In Web3:

  • You hold your keys
  • You prove who you are
  • You control your data

How It Works

// Instead of username/password:
// "Hey, I'm user123, password: secret"

// Web3 says:
// "Sign this message to prove
// you own this wallet"
const message = "Login to CoolApp";
const signature = await wallet.sign(message);

Real Example: When you click โ€œConnect Walletโ€ on OpenSea or Uniswap, they ask your wallet to sign a message. That signature proves you own the wallet!


๐Ÿ‘› Wallet Connection

Your Digital Pocket

A wallet is like a magical keychain that:

  • Holds your digital money (tokens)
  • Proves your identity
  • Signs permission slips (transactions)

Popular Wallets

Wallet Type Best For
MetaMask Browser Beginners
Rainbow Mobile Pretty UI
Ledger Hardware Security

Connecting a Wallet

// Check if wallet exists
if (window.ethereum) {
  // Ask permission to connect
  const accounts = await window
    .ethereum
    .request({
      method: 'eth_requestAccounts'
    });

  // You got the user's address!
  console.log(accounts[0]);
}

What Happens:

  1. Your app asks: โ€œCan I see your wallet?โ€
  2. Wallet pops up: โ€œAllow this app?โ€
  3. User clicks โ€œConnectโ€
  4. App receives wallet address

๐Ÿ”— WalletConnect Protocol

The Universal Remote

Remember when every TV needed its own remote? WalletConnect is like a universal remote for wallets!

The Problem It Solves

Without WalletConnect:

  • Each wallet = different code
  • Phone wallet + Desktop app = ๐Ÿ˜ต
  • So many integrations!

With WalletConnect:

  • One protocol, all wallets
  • Scan QR, instant connect
  • Phone โ†” Desktop magic!

How It Works

graph TD A["Your dApp"] -->|Shows QR| B["WalletConnect"] B -->|User Scans| C["Phone Wallet"] C -->|Secure Bridge| A
// Initialize WalletConnect
import { WalletConnect } from
  "@walletconnect/client";

const connector = new WalletConnect({
  bridge: "https://bridge.wallet..."
});

// Generate QR code
if (!connector.connected) {
  await connector.createSession();
  // Show connector.uri as QR
}

Magic Moment: User scans QR with phone โ†’ Wallet connects to your desktop app instantly!


๐Ÿงฐ Wallet Connection Kits

The Easy Button

Building wallet connection from scratch is hard. Connection kits are like IKEA furniture โ€“ everything included!

Popular Kits

RainbowKit ๐ŸŒˆ

import { ConnectButton } from
  '@rainbow-me/rainbowkit';

// That's it! Beautiful button ready
<ConnectButton />

Web3Modal ๐ŸŽจ

import { Web3Modal } from
  '@web3modal/html';

const modal = new Web3Modal({
  projectId: 'your-id'
});

modal.openModal();

ConnectKit โšก

import { ConnectKitButton } from
  'connectkit';

<ConnectKitButton />

Why Use Kits?

DIY Connection Connection Kit
500+ lines 5 lines
Handle 10 wallets All wallets
Build UI Beautiful UI
Weeks Minutes

๐Ÿ“š Web3 JavaScript Libraries

Your Translation Dictionary

The blockchain speaks a weird language. Libraries are translators that help your app talk to it!

The Big Three

Ethers.js โ€“ The Friendly One

import { ethers } from 'ethers';

// Get balance (easy!)
const balance = await provider
  .getBalance(address);

// Convert to readable
ethers.formatEther(balance);
// "1.5" ETH

Web3.js โ€“ The Original

import Web3 from 'web3';

const web3 = new Web3(provider);
const balance = await web3.eth
  .getBalance(address);

Viem โ€“ The New Kid

import { createPublicClient }
  from 'viem';

const client = createPublicClient({
  transport: http()
});

Comparison

Feature Ethers.js Web3.js Viem
Size Small Large Tiny
Speed Fast Medium Fastest
Learning Easy Medium Medium
TypeScript โœ… Great โš ๏ธ OK โœ… Best

๐Ÿ”Œ Provider Pattern

The Power Outlet

A provider is like an electrical outlet for blockchain power. Your app plugs into it to get data!

Types of Providers

graph TD A["Your App"] -->|Plugs into| B{Provider} B -->|Browser| C["MetaMask"] B -->|API| D["Infura/Alchemy"] B -->|Direct| E["Your Node"]

Browser Provider (Free!)

// MetaMask injects this
const provider = window.ethereum;

API Provider (Reliable)

// Connect to Infura
const provider = new ethers
  .JsonRpcProvider(
    'https://mainnet.infura.io/v3/KEY'
  );

What Providers Do

Action Provider Does
Read balance โœ… Yes
Get transactions โœ… Yes
View contracts โœ… Yes
Send money โŒ No (need signer)

Key Point: Providers are read-only. Theyโ€™re like a window into the blockchain. To DO things, you need a signer!


โœ๏ธ Signer Abstraction

The Permission Slip

Remember signing permission slips for school trips? A signer does that for blockchain transactions!

Provider vs Signer

Provider Signer
๐Ÿ‘€ Looks โœ๏ธ Acts
Read data Write data
Free Costs gas
No key needed Needs private key

Getting a Signer

// From browser wallet
const provider = new ethers
  .BrowserProvider(window.ethereum);

// Request permission + get signer
const signer = await provider
  .getSigner();

// Now you can sign things!
const address = await signer
  .getAddress();

What Signers Do

// Send money
await signer.sendTransaction({
  to: "0xfriend...",
  value: ethers.parseEther("0.1")
});

// Sign a message (prove identity)
const signature = await signer
  .signMessage("Hello Web3!");

// Interact with contracts
const tx = await contract
  .connect(signer)
  .mint();

Signer Abstraction Magic

Modern tools hide the complexity:

// Old way (scary!)
const wallet = new Wallet(privateKey);
const tx = {
  nonce: await provider.getNonce(),
  gasLimit: 21000,
  gasPrice: await provider.getGasPrice(),
  // ... so much more
};

// New way (easy!)
await signer.sendTransaction({
  to: address,
  value: amount
});
// Library handles everything!

๐ŸŽฏ Putting It All Together

Hereโ€™s how all pieces connect:

graph TD A["User"] -->|Has| B["Wallet"] C["Your App"] -->|Uses| D["Connection Kit"] D -->|Via| E["WalletConnect"] E -->|Connects| B C -->|Uses| F["Web3 Library"] F -->|Creates| G["Provider"] G -->|Read Only| H["Blockchain"] B -->|Becomes| I["Signer"] I -->|Write Transactions| H

Complete Example

// 1. Setup (with RainbowKit)
import { ConnectButton } from
  '@rainbow-me/rainbowkit';

// 2. Get provider & signer
const provider = new ethers
  .BrowserProvider(window.ethereum);
const signer = await provider
  .getSigner();

// 3. Read (provider)
const balance = await provider
  .getBalance(address);

// 4. Write (signer)
await signer.sendTransaction({
  to: friend,
  value: ethers.parseEther("1.0")
});

๐Ÿš€ Quick Summary

Concept Analogy Purpose
Authentication Club ID Prove identity
Wallet Magic keychain Hold keys & sign
WalletConnect Universal remote Connect any wallet
Connection Kit IKEA furniture Easy setup
Web3 Library Translator Talk to blockchain
Provider Power outlet Read blockchain
Signer Permission slip Write to blockchain

๐Ÿ’ก Remember

  1. Wallets hold your keys, not websites
  2. WalletConnect = scan QR, connect anywhere
  3. Connection kits save weeks of work
  4. Providers read, Signers write
  5. Libraries make blockchain easy to talk to

You now have the keys to build the bridge! ๐ŸŒ‰

Next up: Try connecting a wallet in the Interactive Lab!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.