Viem — Type-Safe Ethereum Interactions for TypeScript
You are an expert in Viem, the TypeScript interface for Ethereum that provides low-level, type-safe primitives for interacting with the blockchain. You help developers build dApps, scripts, and backends that read blockchain data, send transactions, interact with smart contracts, and handle wallet connections — with full type inference from ABIs, tree-shakeable modules, and zero dependencies beyond noble cryptography.
Best use case
Viem — Type-Safe Ethereum Interactions for TypeScript is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
You are an expert in Viem, the TypeScript interface for Ethereum that provides low-level, type-safe primitives for interacting with the blockchain. You help developers build dApps, scripts, and backends that read blockchain data, send transactions, interact with smart contracts, and handle wallet connections — with full type inference from ABIs, tree-shakeable modules, and zero dependencies beyond noble cryptography.
Teams using Viem — Type-Safe Ethereum Interactions for TypeScript should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/viem/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How Viem — Type-Safe Ethereum Interactions for TypeScript Compares
| Feature / Agent | Viem — Type-Safe Ethereum Interactions for TypeScript | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
You are an expert in Viem, the TypeScript interface for Ethereum that provides low-level, type-safe primitives for interacting with the blockchain. You help developers build dApps, scripts, and backends that read blockchain data, send transactions, interact with smart contracts, and handle wallet connections — with full type inference from ABIs, tree-shakeable modules, and zero dependencies beyond noble cryptography.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
Related Guides
SKILL.md Source
# Viem — Type-Safe Ethereum Interactions for TypeScript
You are an expert in Viem, the TypeScript interface for Ethereum that provides low-level, type-safe primitives for interacting with the blockchain. You help developers build dApps, scripts, and backends that read blockchain data, send transactions, interact with smart contracts, and handle wallet connections — with full type inference from ABIs, tree-shakeable modules, and zero dependencies beyond noble cryptography.
## Core Capabilities
### Client Setup
```typescript
import { createPublicClient, createWalletClient, http, parseEther } from "viem";
import { mainnet, sepolia } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
// Public client: read blockchain data (no wallet needed)
const publicClient = createPublicClient({
chain: mainnet,
transport: http("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY"),
});
// Wallet client: sign and send transactions
const account = privateKeyToAccount("0x...");
const walletClient = createWalletClient({
account,
chain: mainnet,
transport: http("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY"),
});
```
### Read Blockchain Data
```typescript
// Get ETH balance
const balance = await publicClient.getBalance({
address: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // vitalik.eth
});
console.log(`Balance: ${formatEther(balance)} ETH`);
// Get block
const block = await publicClient.getBlock({ blockTag: "latest" });
console.log(`Block #${block.number}: ${block.transactions.length} txs`);
// Read contract (type-safe from ABI)
const erc20Abi = [
{
name: "balanceOf",
type: "function",
stateMutability: "view",
inputs: [{ name: "account", type: "address" }],
outputs: [{ name: "balance", type: "uint256" }],
},
] as const;
const usdcBalance = await publicClient.readContract({
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
abi: erc20Abi,
functionName: "balanceOf", // Autocompleted from ABI
args: ["0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"],
});
// Return type automatically inferred as bigint
```
### Write Transactions
```typescript
// Send ETH
const hash = await walletClient.sendTransaction({
to: "0x...",
value: parseEther("0.1"),
});
const receipt = await publicClient.waitForTransactionReceipt({ hash });
// Write to contract
const hash = await walletClient.writeContract({
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
abi: erc20Abi,
functionName: "transfer",
args: ["0xrecipient...", 1000000n], // 1 USDC (6 decimals)
});
```
### Event Watching
```typescript
// Watch for ERC-20 Transfer events in real-time
const unwatch = publicClient.watchContractEvent({
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
abi: erc20Abi,
eventName: "Transfer",
onLogs: (logs) => {
for (const log of logs) {
console.log(`Transfer: ${log.args.from} → ${log.args.to}: ${log.args.value}`);
}
},
});
// Get historical events
const transferLogs = await publicClient.getContractEvents({
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
abi: erc20Abi,
eventName: "Transfer",
fromBlock: 18000000n,
toBlock: 18001000n,
});
```
### ENS Resolution
```typescript
// ENS name → address
const address = await publicClient.getEnsAddress({ name: "vitalik.eth" });
// Address → ENS name
const name = await publicClient.getEnsName({
address: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
});
```
## Installation
```bash
npm install viem
```
## Best Practices
1. **ABI as const** — Declare ABIs with `as const` for full type inference on function names, args, and return types
2. **Separate clients** — Use `publicClient` for reads (free), `walletClient` for writes (costs gas)
3. **waitForTransactionReceipt** — Always wait for receipt after sending; don't assume success from hash alone
4. **parseEther/formatEther** — Use viem's utilities for ETH conversions; never do manual decimal math with bigint
5. **Chain configuration** — Import chains from `viem/chains`; includes RPC URLs, block explorer, native currency
6. **Error handling** — Viem throws typed errors; catch `ContractFunctionRevertedError` for contract reverts
7. **Batch requests** — Use `multicall` to batch multiple contract reads into one RPC call; reduces latency
8. **Works with wagmi** — Viem is the core of wagmi (React hooks for Ethereum); same patterns, same typesRelated Skills
chart-type-recommender
Chart Type Recommender - Auto-activating skill for Data Analytics. Triggers on: chart type recommender, chart type recommender Part of the Data Analytics skill category.
microsoft-typescript
ALWAYS use when editing or working with *.ts, *.tsx, *.mts, *.cts files or code importing "typescript". Consult for debugging, best practices, or modifying typescript, TypeScript.
typespec-create-api-plugin
Generate a TypeSpec API plugin with REST operations, authentication, and Adaptive Cards for Microsoft 365 Copilot
typespec-create-agent
Generate a complete TypeSpec declarative agent with instructions, capabilities, and conversation starters for Microsoft 365 Copilot
typespec-api-operations
Add GET, POST, PATCH, and DELETE operations to a TypeSpec API plugin with proper routing, parameters, and adaptive cards
typescript-mcp-server-generator
Generate a complete MCP server project in TypeScript with tools, resources, and proper configuration
javascript-typescript-jest
Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.
repomix-safe-mixer
Safely package codebases with repomix by automatically detecting and removing hardcoded credentials before packing. Use when packaging code for distribution, creating reference packages, or when the user mentions security concerns about sharing code with repomix.
safe-file-reader
Read files from documents directory safely
safe-calculator
A safe calculator for mathematical expressions
typescript-pro
Master TypeScript with advanced types, generics, and strict type safety. Handles complex type systems, decorators, and enterprise-grade patterns. Use PROACTIVELY for TypeScript architecture, type inference optimization, or advanced typing patterns.
javascript-typescript-typescript-scaffold
You are a TypeScript project architecture expert specializing in scaffolding production-ready Node.js and frontend applications. Generate complete project structures with modern tooling (pnpm, Vite, N