wagmi

repository·main·Indexed 11 days ago

https://github.com/wevm/wagmi

A collection of tools and libraries for interacting with Ethereum and other EVM-compatible blockchains. It provides high-level hooks and utilities for developers, including @wagmi/core, @wagmi/connectors, and a React integration. The ecosystem includes @wagmi/cli for generating type-safe contract constants and hooks from ABIs, and create-wagmi for bootstrapping projects with Next.js, Vite React, or Vite Vue.

Tokens
334.6K
Snippets
1.1K
Records
1.5K
Agent score
90%

What's inside wagmi

  1. Core benefits of using Wagmi

    main

    Wagmi is a collection of modular and composable APIs designed to simplify Ethereum application development. It addresses common challenges such as wallet connectivity, multi-chain support, transaction signing, and state management.

    Key value propositions include:

    • Developer Experience: Provides strongly typed APIs with automatic type inference and autocomplete, especially when providing an ABI. It uses a documentation and test-driven development approach for predictable APIs.
    • Performance: Built by the team behind Viem, Wagmi is optimized for tree-shaking and dead-code elimination to minimize bundle sizes. It leverages TanStack Query for efficient data fetching, including caching, deduplication, and persistence.
    • Feature Coverage: Offers 40+ React Hooks for accounts, wallets, contracts, transactions, signing, and ENS. It supports EIP-6963 for multi-injected provider discovery and provides official connectors for most wallets.
    • Stability: Uses semver for predictable upgrades. From v2 onwards, new functionality is opt-in with deprecation paths for old features to ensure smooth migrations.
  2. Understand the core value propositions of Wagmi

    main

    Wagmi is designed to simplify Ethereum application development by abstracting complex tasks like wallet connection, multi-chain support, transaction signing, and state management. It focuses on four key pillars:

    • Developer Experience: Provides modular, composable APIs with strong TypeScript integration (autocomplete and type inference) and comprehensive documentation.
    • Performance: Built on top of Viem, Wagmi is optimized for tree-shaking and dead-code elimination to minimize bundle sizes. It also leverages TanStack Query for efficient data fetching, including caching, deduplication, and persistence.
    • Feature Coverage: Offers 40+ Vue Composables for common tasks (accounts, contracts, ENS, etc.) and supports a wide range of wallets via official connectors and EIP-6963.
    • Stability: Uses a test-driven approach with forked Ethereum nodes and follows semantic versioning (semver) to ensure predictable upgrades.
  3. Automate ABI management and code generation with Wagmi CLI

    main

    The Wagmi CLI automates the management of Application Binary Interfaces (ABIs) and generates type-safe code for your application. Instead of manually managing ABIs via npm packages, custom scripts, or copy-pasting, the CLI takes ABIs as inputs (from sources like Etherscan, Foundry, or Hardhat) and outputs both the ABIs and generated code.

    Key benefits include:

    • Automated ABI Fetching: Use plugins like the Etherscan plugin to fetch ABIs across multiple chains and deployments.
    • Code Generation: Use the React plugin to generate specialized Wagmi Hooks for your ABIs, significantly reducing boilerplate.
    • Watch Mode: The CLI supports a watch mode that automatically detects changes in your smart contracts (via Foundry or Hardhat) and re-runs plugins to update your generated code, shortening the development feedback loop.
    // Example of using generated hooks instead of generic Wagmi hooks
    import { useReadFroggyFriends, useWriteFroggyFriends } from './generated'
    
    function App() {
      // Using a generated hook reduces boilerplate by removing the need to pass
      // the abi and address manually every time.
      const { data } = useReadFroggyFriends({
        functionName: 'tokenURI',
        args: [123n],
      })
    
      const { write } = useWriteFroggyFriends()
      const onClick = React.useCallback(() => {
        write({
          functionName: 'mint',
          args: ['foo', 123n],
        })
      }, [write])
    }
  4. Core benefits of using Wagmi Core

    main

    Wagmi Core is designed to abstract the complexities of Ethereum application development. It provides a stable, high-performance foundation for handling:

    • Wallet Connectivity: Support for various wallets via official connectors and EIP-6963.
    • Multi-chain Support: Automatic management of multiple chains.
    • Blockchain Operations: Signing messages/data, sending transactions, and listening for events.
    • Data Management: Efficiently refreshing stale data and managing RPC usage.

    By using Wagmi Core, developers can focus on application logic rather than the intricacies of the Ethereum ecosystem.

  5. Compare Wagmi with other React Ethereum libraries

    main

    When choosing a React library for Ethereum development (managing wallet connections, utility hooks, etc.), Wagmi is compared against web3-react and useDApp.

    Wagmi Key Features

    • Framework Support: React and Vanilla JS.
    • EIP-6963: Fully compatible with EIP-6963 (multi-injected provider support).
    • TypeScript: High-fidelity type inference from ABIs and EIP-712 Typed Data.
    • Data Management: Uses TanStack Query for caching, request deduplication, and persistence.
    • Automation: Auto-refreshes data on wallet, block, and network changes.
    • Capabilities: Includes 20+ hooks for ENS, contracts, transactions, and signing, plus built-in support for Multicall.

    Comparison Summary

    FeatureWagmiweb3-reactuseDApp
    Supported FrameworksReact, Vanilla JSReactReact
    Documentation✅ Comprehensive🛑 None✅ Good
    TypeScript✅ High (ABI inference)🔶 Manual generics🔶 Manual generics
    EIP-6963 Support✅ Fully compatible🔴 Not compatible🔴 Not compatible
    Test Suite✅ Forked network tests🔶 Mocking only🔶 Mocking only
    Examples✅ Extensive🔶 Single example✅ Good
  6. Extend Wagmi functionality via Core, Viem, or the CLI

    main

    While Wagmi provides high-level composables, it offers multiple escape hatches for different levels of control:

    • Wagmi Core: Use this for lower-level control while staying within the Wagmi ecosystem.
    • Viem: Wagmi uses Viem internally; you can use Viem directly for the most granular blockchain operations.
    • Wagmi CLI: Use the CLI to manage your ABIs efficiently.
    • Connectors: Use official connectors or implement your own via the extensible API to support new wallets.
  7. Enable TypeScript type-safety for Wagmi config

    main

    To get strong type-safety across Solid Context (e.g., ensuring hooks only accept chains defined in your config), you can "register" your config by augmenting the @wagmi/solid module.

    import { type Config } from '@wagmi/solid'
    import { mainnet, sepolia } from '@wagmi/solid/chains'
    
    // 1. Your exported config
    export const config = createConfig({
      chains: [mainnet, sepolia],
      transports: {
        [mainnet.id]: http(),
        [sepolia.id]: http(),
      },
    })
    
    // 2. Register the config for type-safety
    declare module '@wagmi/solid' {
      interface Register {
        config: typeof config
      }
    }
    
    // Now hooks like useBlockNumber will be aware of your specific chains
    import { useBlockNumber } from '@wagmi/solid'
    useBlockNumber(() => ({ chainId: 123 })) // This will now be type-checked against your config
  8. Feature Coverage and Extensibility

    main

    Wagmi Core provides a wide range of out-of-the-box features and extension points:

    • VanillaJS Actions: Over 40 actions for accounts, wallets, contracts, transactions, signing, ENS, and more.
    • Wallet Support: Extensive support via official connectors and EIP-6963 multi-injected provider discovery.
    • Low-level Control: For developers needing more granular control, Wagmi Core uses Viem internally, allowing you to drop down to Viem directly.
    • ABI Management: Includes a CLI to manage ABIs.
    • Ecosystem: Integrates with third-party libraries like ConnectKit, Dynamic, and Privy.
  9. Understand the Connection object

    main

    A Connection object represents an active link to a provider via a specific connector. It contains:

    • accounts: A readonly array of Address associated with the connection.
    • chainId: The number representing the chain ID associated with the connection.
    • connector: The Connector instance associated with this connection.
  10. Understand Viem Public Client vs Wallet Client

    main

    When working with Viem through Wagmi, it is important to distinguish between the two primary client types:

    • Public Client: Dedicated to read-only operations. Use this for fetching block numbers, reading contract state, or querying historical data.
    • Wallet Client: Dedicated to write operations. Use this for actions that require a signature, such as sending transactions or signing messages.