Configs2026年5月14日·1 分钟阅读

Viem — TypeScript Interface for Ethereum

Viem is a modern TypeScript library for interacting with the Ethereum blockchain. It provides composable, type-safe modules for public client reads, wallet actions, contract interactions, ABI encoding, and ENS resolution. Viem is the low-level transport layer that powers Wagmi and has become the successor to ethers.js for many TypeScript-first teams.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Viem
通用 CLI 安装命令
npx tokrepo install b5e57160-4f90-11f1-9bc6-00163e2b0d79

Introduction

Viem is a TypeScript-first Ethereum library built by the wevm team (the creators of Wagmi). It takes a composable, tree-shakeable approach where each action is an independent function rather than a method on a large class. Viem replaces ethers.js and web3.js for developers who want strict TypeScript safety and minimal bundle sizes.

What Viem Does

  • Reads chain state through a Public Client with actions like getBalance, getBlock, and readContract
  • Signs and sends transactions via a Wallet Client connected to a local key or browser wallet
  • Encodes and decodes ABI data, function selectors, event logs, and EIP-712 typed data
  • Resolves ENS names and supports reverse resolution
  • Provides chain definitions for 100+ EVM networks out of the box

Architecture Overview

Viem is organized around three client types: Public Client (read-only RPC calls), Wallet Client (signing and sending transactions), and Test Client (Anvil/Hardhat node manipulation). Each client accepts a chain definition and a transport (HTTP, WebSocket, IPC, or custom). Actions are standalone functions that take a client as the first argument, making them composable and tree-shakeable. ABI types are fully inferred at the TypeScript level, so contract method names, argument types, and return types are checked at compile time.

Self-Hosting & Configuration

  • Install with npm install viem and import only the modules you need
  • Create clients with createPublicClient or createWalletClient specifying chain and transport
  • Use viem/chains to import pre-configured chain objects or define custom ones
  • For browser DApps, use custom(window.ethereum) as the transport with a Wallet Client
  • Configure batch JSON-RPC via the batch option on the HTTP transport for reduced latency

Key Features

  • Full TypeScript inference on contract reads and writes based on ABI literals
  • Tree-shakeable architecture keeps production bundles small
  • Built-in multicall batching for efficient on-chain reads
  • Human-readable ABI support alongside standard JSON ABIs
  • Zero dependencies beyond TypeScript types

Comparison with Similar Tools

  • ethers.js — Class-based API with a larger footprint; Viem is functional, tree-shakeable, and has stricter types
  • web3.js — Plugin-oriented legacy library; Viem offers modern TypeScript ergonomics
  • Wagmi — React hooks layer built on Viem; Viem is the framework-agnostic core
  • Alloy — Rust equivalent by the same ecosystem; Viem serves the TypeScript/JavaScript side

FAQ

Q: Is Viem a replacement for ethers.js? A: Many teams adopt Viem as a modern alternative. It offers better TypeScript inference and smaller bundles, though ethers.js remains widely used.

Q: Does Viem work in the browser? A: Yes. Viem works in browsers, Node.js, Deno, and Bun with no environment-specific dependencies.

Q: How does ABI type inference work? A: When you pass an ABI as a const assertion, Viem infers function names, argument types, and return types at the TypeScript level, catching errors before runtime.

Q: Can I use Viem without Wagmi? A: Yes. Viem is a standalone library. Wagmi is an optional React/Vue layer built on top of it.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产