npm i viem
Overview
// 1. Import modules.
import { function createPublicClient<transport extends Transport, chain extends Chain | undefined = undefined>(parameters: PublicClientConfig<transport, chain>): PublicClient<transport, chain>Creates a Public Client with a given Transport configured for a Chain.
Docs: https://viem.sh/docs/clients/public
A Public Client is an interface to "public" JSON-RPC API methods such as retrieving block numbers, transactions, reading from smart contracts, etc through Public Actions.
createPublicClient, function http(url?: string, config?: HttpTransportConfig): HttpTransporthttp } from 'viem'
import { const mainnet: {
blockExplorers: {
readonly default: {
readonly name: "Etherscan";
readonly url: "https://etherscan.io";
readonly apiUrl: "https://api.etherscan.io/api";
};
};
contracts: {
readonly ensRegistry: {
readonly address: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
};
readonly ensUniversalResolver: {
readonly address: "0xc0497E381f536Be9ce14B0dD3817cBcAe57d2F62";
readonly blockCreated: 16966585;
};
readonly multicall3: {
readonly address: "0xca11bde05977b3631167028862be2a173976ca11";
readonly blockCreated: 14353601;
};
};
id: 1;
name: "Ethereum";
nativeCurrency: {
readonly name: "Ether";
readonly symbol: "ETH";
readonly decimals: 18;
};
rpcUrls: {
readonly default: {
readonly http: readonly ["https://cloudflare-eth.com"];
};
};
sourceId?: number | undefined;
testnet?: boolean | undefined;
formatters?: undefined;
serializers?: import("../../index.js").ChainSerializers<undefined> | undefined;
fees?: import("../../index.js").ChainFees<undefined> | undefined;
}mainnet } from 'viem/chains'
// 2. Set up your client with desired chain & transport.
const const client: {
account: undefined;
batch?: {
multicall?: boolean | {
batchSize?: number | undefined;
wait?: number | undefined;
} | undefined;
} | undefined;
cacheTime: number;
chain: {
blockExplorers: {
...;
};
... 9 more ...;
fees?: ChainFees<...> | undefined;
};
... 53 more ...;
extend: <const client extends {
...;
} & Partial<...>>(fn: (client: Client<...>) => client) => Client<...>;
}client = createPublicClient<HttpTransport, {
blockExplorers: {
readonly default: {
readonly name: "Etherscan";
readonly url: "https://etherscan.io";
readonly apiUrl: "https://api.etherscan.io/api";
};
};
... 9 more ...;
fees?: ChainFees<...> | undefined;
}>(parameters: {
...;
}): {
...;
}Creates a Public Client with a given Transport configured for a Chain.
Docs: https://viem.sh/docs/clients/public
A Public Client is an interface to "public" JSON-RPC API methods such as retrieving block numbers, transactions, reading from smart contracts, etc through Public Actions.
createPublicClient({
chain?: Chain | {
blockExplorers: {
...;
};
... 9 more ...;
fees?: ChainFees<...> | undefined;
} | undefinedChain for the client.
chain: const mainnet: {
blockExplorers: {
readonly default: {
readonly name: "Etherscan";
readonly url: "https://etherscan.io";
readonly apiUrl: "https://api.etherscan.io/api";
};
};
contracts: {
readonly ensRegistry: {
readonly address: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
};
readonly ensUniversalResolver: {
readonly address: "0xc0497E381f536Be9ce14B0dD3817cBcAe57d2F62";
readonly blockCreated: 16966585;
};
readonly multicall3: {
readonly address: "0xca11bde05977b3631167028862be2a173976ca11";
readonly blockCreated: 14353601;
};
};
id: 1;
name: "Ethereum";
nativeCurrency: {
readonly name: "Ether";
readonly symbol: "ETH";
readonly decimals: 18;
};
rpcUrls: {
readonly default: {
readonly http: readonly ["https://cloudflare-eth.com"];
};
};
sourceId?: number | undefined;
testnet?: boolean | undefined;
formatters?: undefined;
serializers?: import("../../index.js").ChainSerializers<undefined> | undefined;
fees?: import("../../index.js").ChainFees<undefined> | undefined;
}mainnet,
transport: HttpTransportThe RPC transport
transport: function http(url?: string | undefined, config?: HttpTransportConfig | undefined): HttpTransporthttp(),
})
// 3. Consume an action!
const const blockNumber: bigintblockNumber = await const client: {
account: undefined;
batch?: {
multicall?: boolean | {
batchSize?: number | undefined;
wait?: number | undefined;
} | undefined;
} | undefined;
cacheTime: number;
chain: {
blockExplorers: {
...;
};
... 9 more ...;
fees?: ChainFees<...> | undefined;
};
... 53 more ...;
extend: <const client extends {
...;
} & Partial<...>>(fn: (client: Client<...>) => client) => Client<...>;
}client.getBlockNumber: (args?: GetBlockNumberParameters | undefined) => Promise<bigint>Returns the number of the most recent block seen.
Docs: https://viem.sh/docs/actions/public/getBlockNumber
Examples: https://stackblitz.com/github/wevm/viem/tree/main/examples/blocks/fetching-blocks
JSON-RPC Methods: eth_blockNumber
getBlockNumber()
Features
viem supports all these features out-of-the-box:
- Abstractions over the JSON-RPC API to make your life easier
- First-class APIs for interacting with Smart Contracts
- Language closely aligned to official Ethereum terminology
- Import your Browser Extension, WalletConnect or Private Key Wallet
- Browser native BigInt, instead of large BigNumber libraries
- Utilities for working with ABIs (encoding/decoding/inspection)
- TypeScript ready (infer types from ABIs and EIP-712 Typed Data)
- First-class support for Anvil, Hardhat & Ganache
- Test suite running against forked Ethereum network
Community
Check out the following places for more wagmi-related content:
- Follow @wagmi_sh, @jakemoxey, and @awkweb on Twitter for project updates
- Join the discussions on GitHub
- Share your project/organization that uses viem
Support
Help support future development and make wagmi a sustainable open-source project: