Web3 Frontend Patterns

Production engineering guide for web3 frontend patterns in Web3 and blockchain systems.

Web3 frontends differ fundamentally from traditional web applications by managing user identity, state, and interactions across decentralized networks. These patterns emerge when a frontend must handle on-chain transactions, wallet connectivity, gas cost estimation, and state synchronization from blockchain events. The patterns are most critical during user onboarding, transaction execution, and real-time state updates across multiple chains.

Connecting to a Wallet

Web3 frontends begin with wallet connection, where users authenticate with a blockchain account. The process is not a simple OAuth login; it involves signing a message to prove ownership of a blockchain address.

The standard flow starts with the frontend generating a challenge message—typically a nonce or timestamp—then presenting it to the user through a signMessage RPC call via a JSON-RPC endpoint. The frontend must handle the wallet’s response, which includes a signature, public key, and the original message.

// Request to wallet
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "personal_sign",
  "params": [
    "0x8690c4c1c6032253a6a826f363411d2c1b8115f6c7111c9378143a4661c3e18d",
    "0x507b31a3608b9483f4853828b5c81d06664b5735333488a4d6e811994b5e5130"
  ]
}

The wallet returns a signature in r, s, and v components, often packed into a single 0x-prefixed hex string. The frontend must verify the signature using ecrecover in a smart contract or directly in Solidity.

A common failure mode is the double-signing attack: a user signs the same message twice, leading to wallet reuse across multiple dapps. To prevent this, the frontend includes a unique domainSeparator in the signTypedData call, which ensures signatures are scoped to a specific dapp and chain.

// Typed data for signTypedData
{
  "types": {
    "EIP712Domain": [
      { "name": "name", "type": "string" },
      { "name": "version", "type": "string" },
      { "name": "chainId", "type": "uint256" },
      { "name": "verifyingContract", "type": "address" }
    ],
    "Login": [
      { "name": "message", "type": "string" },
      { "name": "expiresAt", "type": "uint256" }
    ]
  },
  "primaryType": "Login",
  "domain": {
    "name": "MyDapp",
    "version": "1",
    "chainId": 1,
    "verifyingContract": "0x4f74e9a55f4f13d582d7a814d0d9a43c4f3341b8"
  },
  "message": {
    "message": "Sign in to MyDapp",
    "expiresAt": 1700000000
  }
}

The frontend must also support wallet discovery, where the user selects from a list of available wallets (e.g., MetaMask, WalletConnect, Coinbase Wallet). The protocol is WalletConnect v2, which uses a QR code to exchange a session key and establish a persistent connection. The frontend must manage session lifecycle, including reconnection after page reload and handling of session_request and session_update events.

Handling Transactions

Once connected, the frontend must support transaction execution. The core pattern involves transaction batching, gas estimation, and transaction lifecycle tracking.

A transaction is submitted via eth_sendTransaction, with from, to, value, data, and gas fields. The frontend must estimate gas using eth_estimateGas before sending, to avoid out-of-gas errors.

// Estimate gas for a transaction
{
  "jsonrpc": "2.0",
  "method": "eth_estimateGas",
  "params": [
    {
      "from": "0x742d35Cc66346515q77c9c5549315830e51a14b2",
      "to": "0x4f74e9a55f4f13d582d7a814d0d9a43c4f3341b8",
      "value": "0x12a05f200",
      "data": "0x4c6d34d54f34c5f2345234c2345234523452345234523452345234523452"
    }
  ],
  "id": 1
}

The response returns a hex-encoded uint256 value for the estimated gas. The frontend should display this to the user, optionally allowing them to adjust the gas price via eth_gasPrice.

After submission, the transaction is tracked via eth_getTransactionReceipt, which returns a receipt object with status, cumulativeGasUsed, and logs. The frontend must handle the "pending → confirmed → final" lifecycle, where "final" is defined as the block being finalized (e.g., after 32 confirmations in Ethereum).

A frequent failure is transaction reorgs: a block is replaced, causing a confirmed transaction to become pending again. The frontend must listen to newHeads events from the node and revalidate receipts on each new block.

// WebSocket subscription to new blocks
{
  "jsonrpc": "2.0",
  "method": "eth_subscribe",
  "params": [ "newHeads" ],
  "id": 1
}

The response is a subscriptionId, and each new block arrives as:

{
  "jsonrpc": "2.0",
  "method": "eth_subscription",
  "params": {
    "result": {
      "number": "0x1a3b",
      "hash": "0x34a4d2a3b2c1a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b",
      "parentHash": "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef",
      "timestamp": "0x640f312c"
    },
    "subscription": 12345
  }
}

The frontend updates the transaction status based on the block number and reorg detection.

Synchronizing State from Events

Frontends must reflect real-time state from blockchain events. The primary mechanism is event listening via eth_getLogs, which retrieves logs matching filters.

The frontend defines a filter object with address, topics, fromBlock, and toBlock to capture relevant events.

{
  "jsonrpc": "2.0",
  "method": "eth_getLogs",
  "params": [
    {
      "address": "0x4f74e9a55f4f13d582d7a814d0d9a43c4f3341b8",
      "topics": [
        "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef123456789abcdef"
      ],
      "fromBlock": "0x1a3b",
      "toBlock": "latest"
    }
  ],
  "id": 1
}

Each log entry includes blockHash, transactionHash, address, topics, and data. The frontend must map these to domain objects (e.g., Deposit, Transfer) and update UI state accordingly.

The pattern of event batching is essential: instead of polling every second, the frontend uses eth_subscribe with logs to receive real-time updates.

{
  "jsonrpc": "2.0",
  "method": "eth_subscribe",
  "params": [
    "logs",
    {
      "address": "0x4f74e9a55f4f13d582d7a814d0d9a43c4f3341b8",
      "topics": [
        "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef123456789abcdef"
      ]
    }
  ],
  "id": 1
}

The subscription returns a subscriptionId, and each new log arrives as:

{
  "jsonrpc": "2.0",
  "method": "eth_subscription",
  "params": {
    "result": {
      "removed": false,
      "logIndex": "0x1",
      "transactionIndex": "0x2",
      "transactionHash": "0x34a4d2a3b2c1a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b",
      "blockHash": "0x34a4d2a3b2c1a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b",
      "blockNumber": "0x1a3c",
      "address": "0x4f74e9a55f4f13d582d7a814d0d9a43c4f3341b8",
      "data": "0x4c6d34d54f34c5f2345234c2345234523452345234523452345234523452",
      "topics": [
        "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef123456789abcdef"
      ]
    },
    "subscription": 12345
  }
}

A subtle failure is event drift: logs are received out of order, or logs from multiple chains are interleaved. The frontend must deduplicate logs using a combination of transactionHash and logIndex, and use blockNumber to reconstruct a consistent timeline.

Managing Gas and Fees

Gas is not a single number—it is a dynamic, multi-layered cost. The frontend must present gas price tiers and priority fees to the user.

The frontend queries the current eth_gasPrice to set a baseline, then uses eth_feeHistory to fetch recent gas prices and base fee changes.

{
  "jsonrpc": "2.0",
  "method": "eth_feeHistory",
  "params": [ 10, "latest", [ 10, 25, 50, 75, 100 ] ],
  "id": 1
}

The response includes oldestBlock, reward, baseFeePerGas, and gasUsedRatio, allowing the frontend to compute priority fee (tip) and base fee separately.

The frontend uses this data to populate a gas slider, where the user selects a confidence level (e.g., 100%, 75%, 50%) and the frontend computes a maxFeePerGas and maxPriorityFeePerGas.

A common mistake is conflating maxFeePerGas and maxPriorityFeePerGas. The former is the total amount the user is willing to pay, while the latter is the tip above the base fee. The frontend must display both, and the user must understand that a higher maxPriorityFeePerGas increases the likelihood of inclusion in the next block.

Handling Cross-Chain Interactions

When a dapp spans multiple chains, the frontend must manage chain switching and cross-chain message delivery.

The frontend uses the wallet_switchEthereumChain and wallet_addEthereumChain RPC calls to prompt the user to switch networks.

{
  "jsonrpc": "2.0",
  "method": "wallet_switchEthereumChain",
  "params": [
    {
      "chainId": "0x1a4",
      "chainName": "Goerli Testnet",
      "nativeCurrency": {
        "name": "Goerli Ether",
        "symbol": "gorETH",
        "decimals": 18
      },
      "rpcUrls": [ "https://goerli.infura.io/v3/123456789" ],
      "blockExplorerUrls": [ "https://goerli.etherscan.io" ]
    }
  ],
  "id": 1
}

The user may receive a ProviderNotFoundError or ChainNotAddedError if the chain is not supported. The frontend must handle the chainChanged event, which fires when the user switches chains via the wallet UI.

The frontend also must track cross-chain message status, often using a message inbox pattern. Each message is represented by a messageId, sourceChain, destinationChain, and status (e.g., sent, received, executed). The frontend listens for MessageReceived events on the destination chain and updates the status in real time.

Caching and Local State

Finally, the frontend must maintain local state that persists across sessions and improves responsiveness. The pattern involves state hydration and client-side indexing.

The frontend caches blockchain state in a local database (e.g., SQLite, IndexedDB) and syncs it via eth_getBlockByNumber, eth_getTransactionReceipt, and eth_getLogs. On page load, the frontend hydrates from the cache and then updates via WebSocket.

A critical failure is stale state: the user interacts with data that is out of sync with the chain. The frontend must detect this by comparing blockNumber in the local state with the current eth_blockNumber. If the chain has advanced, the frontend triggers a re-sync.

The frontend also uses state versioning via stateRoot or merkleRoot hashes, allowing it to detect and resolve conflicts when multiple clients modify the same state.

This pattern is essential during network outages: the user can perform actions locally, and the frontend queues transactions to be sent when connectivity resumes.

This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.