iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Cheatsheet

Web3 cheatsheet: Solidity, wallets, viem, common contract patterns.

Web3 — cheatsheet

EXAMPLE
// ===== Wallet + chain =====
import { createPublicClient, createWalletClient, custom, http, parseEther } from 'viem';
import { mainnet, sepolia, arbitrum, optimism, base } from 'viem/chains';

const pub = createPublicClient({ chain: mainnet, transport: http() });
const wallet = createWalletClient({ chain: mainnet, transport: custom(window.ethereum) });
const [address] = await wallet.requestAddresses();

// ===== Read =====
const block = await pub.getBlockNumber();
const balance = await pub.getBalance({ address });

// ===== Write =====
const hash = await wallet.sendTransaction({ account: address, to: '0x...', value: parseEther('0.01') });
await pub.waitForTransactionReceipt({ hash });

// ===== Contract read =====
const result = await pub.readContract({
  address: '0xContract',
  abi: ABI,
  functionName: 'balanceOf',
  args: [address],
});

// ===== Contract write =====
const txHash = await wallet.writeContract({
  address: '0xContract',
  abi: ABI,
  functionName: 'transfer',
  args: ['0xRecipient', BigInt(100)],
  account: address,
});

// ===== Solidity skeleton =====
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/access/Ownable.sol";

contract MyContract is Ownable {
    constructor(address initialOwner) Ownable(initialOwner) {}

    event Done(address indexed by, uint256 value);

    function action(uint256 v) external onlyOwner {
        // checks
        require(v > 0, "zero");
        // effects (state changes)
        // interactions (external calls)
        emit Done(msg.sender, v);
    }
}

// ===== Common ERC standards =====
// ERC-20    fungible tokens (USDC, DAI, your token)
// ERC-721   NFTs (one-of-a-kind)
// ERC-1155  multi-token (mix of fungible + NFT)
// ERC-2612  permit (gasless approvals)
// ERC-4626  vaults

// ===== Tools =====
// Foundry    Forge + Cast + Anvil + Chisel; fast TS-style testing in Solidity
// Hardhat    JS / TS-based; rich plugin ecosystem
// Remix      browser IDE; good for learning
// Tenderly   monitoring, debugging, simulation
// Etherscan  contract verification + explorer

// ===== Defensive patterns =====
// - Checks-Effects-Interactions (CEI)
// - ReentrancyGuard on external-call functions
// - Pull payments instead of push
// - SafeERC20 wrappers
// - Multisig + timelock on owner roles
// - Pause function for emergencies
// - Avoid block.timestamp for randomness
// - Avoid tx.origin

// ===== Storage cost =====
// Storage write: 20,000 gas (cold) / 5,000 (warm) / 100 (zero-value)
// Memory: cheap for short-lived data
// Calldata: cheapest for external function inputs

// ===== Layer 2 =====
// Optimistic: Arbitrum, Optimism, Base — EVM-compatible, 7-day exit
// ZK: zkSync, Starknet, Polygon zkEVM, Scroll, Linea — fast finality
// Default to L2 for user-facing dApps in 2026.

// ===== Front-end stack =====
// viem (or ethers v6) + wagmi + RainbowKit / ConnectKit
// SIWE for sign-in
// IPFS / Arweave for metadata
// The Graph for indexed queries

// ===== Patterns =====
// - OpenZeppelin contracts as audited base
// - Foundry for tests (forge test, forge fuzz)
// - Multisig + timelock on admin
// - Sign + verify on the front-end before relaying
// - L2 + EIP-2612 permit for gasless flows

// ===== Pitfalls =====
// - Reentrancy on payable functions
// - Unbounded loops -> gas DoS
// - Hard-coded chain IDs / addresses
// - Trusting front-end displays without on-chain verification

Why it matters

Web3 cheatsheet: viem read/write, Solidity + OpenZeppelin skeleton, ERC standards, CEI + ReentrancyGuard, L2 + permit. Foundry for tests, wagmi + RainbowKit for the front-end. The patterns that ship most production dApps in 2026.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// pragma | contract | function visibility (external/public/internal/private)
// state mutability (view/pure/payable) | modifier | event | error
Try it Yourself »

Discussion

Loading…