Optional
environmentOptional
batch?: { multicall?: boolean | { batchSize?: number; wait?: number } }Flags for batch settings.
Optional
multicall?: boolean | { batchSize?: number; wait?: number }Toggle to enable eth_call
multicall aggregation.
Time (in ms) that cached data will remain in memory.
Optional
ccipRead?: CCIP Read configuration.
Chain for the client.
A key for the client.
A name for the client.
Frequency (in ms) for polling enabled actions & events. Defaults to 4_000 milliseconds.
Request function wrapped with friendly error handling
The RPC transport
The type of client.
A unique ID for the client.
Removes a transaction from the mempool.
Serializes the current state (including contracts code, contract's storage, accounts properties, etc.) into a savable data blob.
Returns the automatic mining status of the node.
Returns the details of all transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only.
Returns a summary of all the transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only.
Impersonate an account or contract address. This lets you send transactions from that account even if you don't have access to its private key.
Jump forward in time by the given amount of time, in seconds.
Returns a summary of all the transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only.
Adds state previously dumped with dumpState
to the current chain.
Mine a specified number of blocks.
Removes setBlockTimestampInterval
if it exists.
Resets fork back to its original state.
Revert the state of the blockchain at the current block.
Returns the details of all transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only.
import { createTestClient, http } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
mode: 'anvil',
chain: 'foundry',
transport: http(),
})
const hash = await client.sendUnsignedTransaction({
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
})
Enables or disables the automatic mining of new blocks with each new transaction submitted to the network.
Modifies the balance of an account.
Sets the block's gas limit.
Similar to increaseTime
, but sets a block timestamp interval
. The timestamp of future blocks will be computed as lastBlock_timestamp
+ interval
.
Modifies the bytecode stored at an account's address.
import { createTestClient, http } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
mode: 'anvil',
chain: 'foundry',
transport: http(),
})
await client.setCode({
address: '0xe846c6fcf817734ca4527b28ccb4aea2b6663c79',
bytecode: '0x60806040526000600355600019600955600c80546001600160a01b031916737a250d5630b4cf539739df…',
})
Sets the coinbase address to be used in new blocks.
Sets the automatic mining interval (in seconds) of blocks. Setting the interval to 0 will disable automatic mining.
Enable or disable logging on the test node network.
Change the minimum gas price accepted by the network (in wei).
Note: setMinGasPrice
can only be used on clients that do not have EIP-1559 enabled.
Sets the next block's base fee per gas.
Sets the next block's timestamp.
Modifies (overrides) the nonce of an account.
Sets the backend RPC URL.
Writes to a slot of an account's storage.
import { createTestClient, http } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
mode: 'anvil',
chain: 'foundry',
transport: http(),
})
await client.setStorageAt({
address: '0xe846c6fcf817734ca4527b28ccb4aea2b6663c79',
index: 2,
value: '0x0000000000000000000000000000000000000000000000000000000000000069',
})
Snapshot the state of the blockchain at the current block.
Stop impersonating an account after having previously used impersonateAccount
.
import { createTestClient, http } from 'viem'
import { foundry } from 'viem/chains'
import { stopImpersonatingAccount } from 'viem/test'
const client = createTestClient({
mode: 'anvil',
chain: 'foundry',
transport: http(),
})
await client.stopImpersonatingAccount({
address: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
})
Optional
walletThe Account of the Client.
Optional
batch?: { multicall?: boolean | { batchSize?: number; wait?: number } }Flags for batch settings.
Optional
multicall?: boolean | { batchSize?: number; wait?: number }Toggle to enable eth_call
multicall aggregation.
Time (in ms) that cached data will remain in memory.
Optional
ccipRead?: CCIP Read configuration.
Chain for the client.
A key for the client.
A name for the client.
Frequency (in ms) for polling enabled actions & events. Defaults to 4_000 milliseconds.
Request function wrapped with friendly error handling
The RPC transport
The type of client.
A unique ID for the client.
Adds an EVM chain to the wallet.
eth_addEthereumChain
Deploys a contract to the network, given bytecode and constructor arguments.
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
account: privateKeyToAccount('0x…'),
chain: mainnet,
transport: http(),
})
const hash = await client.deployContract({
abi: [],
account: '0x…,
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
Returns a list of account addresses owned by the wallet or client.
eth_accounts
Returns the chain ID associated with the current network.
eth_chainId
Gets the wallets current permissions.
wallet_getPermissions
Prepares a transaction request for signing.
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const request = await client.prepareTransactionRequest({
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
to: '0x0000000000000000000000000000000000000000',
value: 1n,
})
// Account Hoisting
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
account: privateKeyToAccount('0x…'),
chain: mainnet,
transport: custom(window.ethereum),
})
const request = await client.prepareTransactionRequest({
to: '0x0000000000000000000000000000000000000000',
value: 1n,
})
Requests a list of accounts managed by a wallet.
eth_requestAccounts
Sends a request to the wallet, asking for permission to access the user's accounts. After the user accepts the request, it will return a list of accounts (addresses).
This API can be useful for dapps that need to access the user's accounts in order to execute transactions or interact with smart contracts.
Requests permissions for a wallet.
wallet_requestPermissions
Sends a signed transaction to the network
eth_sendRawTransaction
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
import { sendRawTransaction } from 'viem/wallet'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const hash = await client.sendRawTransaction({
serializedTransaction: '0x02f850018203118080825208808080c080a04012522854168b27e5dc3d5839bab5e6b39e1a0ffd343901ce1622e3d64b48f1a04e00902ae0502c4728cbf12156290df99c3ed7de85b1dbfe20b5c36931733a33'
})
Creates, signs, and sends a new transaction to the network.
eth_sendTransaction
eth_sendRawTransaction
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const hash = await client.sendTransaction({
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
})
// Account Hoisting
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
account: privateKeyToAccount('0x…'),
chain: mainnet,
transport: http(),
})
const hash = await client.sendTransaction({
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
})
Calculates an Ethereum-specific signature in EIP-191 format: keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
.
personal_sign
With the calculated signature, you can:
verifyMessage
to verify the signature,recoverMessageAddress
to recover the signing address from a signature.import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const signature = await client.signMessage({
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
message: 'hello world',
})
// Account Hoisting
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
account: privateKeyToAccount('0x…'),
chain: mainnet,
transport: http(),
})
const signature = await client.signMessage({
message: 'hello world',
})
Signs a transaction.
eth_signTransaction
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const request = await client.prepareTransactionRequest({
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
to: '0x0000000000000000000000000000000000000000',
value: 1n,
})
const signature = await client.signTransaction(request)
// Account Hoisting
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
account: privateKeyToAccount('0x…'),
chain: mainnet,
transport: custom(window.ethereum),
})
const request = await client.prepareTransactionRequest({
to: '0x0000000000000000000000000000000000000000',
value: 1n,
})
const signature = await client.signTransaction(request)
Signs typed data and calculates an Ethereum-specific signature in EIP-191 format: keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
.
eth_signTypedData_v4
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const signature = await client.signTypedData({
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
types: {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
},
primaryType: 'Mail',
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
})
// Account Hoisting
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
account: privateKeyToAccount('0x…'),
chain: mainnet,
transport: http(),
})
const signature = await client.signTypedData({
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
types: {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
},
primaryType: 'Mail',
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
})
Switch the target chain in a wallet.
eth_switchEthereumChain
Adds an EVM chain to the wallet.
eth_switchEthereumChain
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const success = await client.watchAsset({
type: 'ERC20',
options: {
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
decimals: 18,
symbol: 'WETH',
},
})
Executes a write function on a contract.
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a Transaction is needed to be broadcast in order to change the state.
Internally, uses a Wallet Client to call the sendTransaction
action with ABI-encoded data
.
Warning: The write
internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with contract.simulate
before you execute it.
import { createWalletClient, custom, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const hash = await client.writeContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
functionName: 'mint',
args: [69420],
})
// With Validation
import { createWalletClient, custom, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const { request } = await client.simulateContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
functionName: 'mint',
args: [69420],
}
const hash = await client.writeContract(request)
The Account of the Client.