viem.sh Open in urlscan Pro
76.76.21.164  Public Scan

URL: https://viem.sh/docs/clients/wallet
Submission: On February 11 via api from US — Scanned from DE

Form analysis 0 forms found in the DOM

Text Content

Skip to content
Introduction
Why viemGetting StartedMigration GuideEthers v5 → viemTypeScriptError
HandlingPlatform CompatibilityFAQ
Clients & Transports
IntroductionPublic ClientWallet ClientTest ClientBuild your own Client
Transports
HTTPWebSocketCustom (EIP-1193)IPCFallback
Public Actions
Chevron Right
Wallet Actions
Chevron Right
Test Actions
Chevron Right
Accounts
Chevron Right
Chains
Chevron Right
Contract
Chevron Right
ENS
Chevron Right
ABI
Chevron Right
Utilities
Chevron Right
Third Party
Chevron Right
Glossary
Chevron Right
Search



   Docs
 * Extensions
   Examples
 * 2.7.6


GitHub
Discord
X

Sun
Moon
   Docs
 * Extensions
   Examples
 * 2.7.6

Docs
Chevron Down
GitHub
Discord
X
Menu
Wallet Client
On this page
Chevron Right


ON THIS PAGE

 * Import
 * JSON-RPC Accounts
 * Local Accounts (Private Key, Mnemonic, etc)
 * Parameters
    * account (optional)
    * chain (optional)
    * cacheTime (optional)
    * key (optional)
    * name (optional)
    * pollingInterval (optional)
      


WALLET CLIENT


A function to create a Wallet Client.

A Wallet Client is an interface to interact with Ethereum Account(s) and
provides the ability to retrieve accounts, execute transactions, sign messages,
etc through Wallet Actions.

The createWalletClient function sets up a Wallet Client with a given Transport.

The Wallet Client supports signing over:

 * JSON-RPC Accounts (e.g. Browser Extension Wallets, WalletConnect, etc.).
 * Local Accounts (e.g. private key/mnemonic wallets).


IMPORT


Copyimport { createWalletClient } from 'viem'


JSON-RPC ACCOUNTS


A JSON-RPC Account defers signing of transactions & messages to the target
Wallet over JSON-RPC. An example could be sending a transaction via a Browser
Extension Wallet (e.g. MetaMask) with the window.ethereum Provider.

Below is an example of how you can set up a JSON-RPC Account.


1: INITIALIZE A WALLET CLIENT


Before we set up our Account and start consuming Wallet Actions, we will need to
set up our Wallet Client with the custom Transport, where we will pass in the
window.ethereum Provider:

Copyimport { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum!)
})


2: SET UP YOUR JSON-RPC ACCOUNT


We will want to retrieve an address that we can access in our Wallet (e.g.
MetaMask).

Copyimport { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum!)
})
 
const [address] = await client.getAddresses() 
// or: const [address] = await client.requestAddresses()

> Note: Some Wallets (like MetaMask) may require you to request access to
> Account addresses via client.requestAddresses first.


3: CONSUME WALLET ACTIONS


Now you can use that address within Wallet Actions that require a signature from
the user:

Copyimport { createWalletClient, custom, parseEther } from 'viem'
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum!)
})
 
const [address] = await client.getAddresses()
 
const hash = await client.sendTransaction({ 
  account: address,
  to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
  value: parseEther('0.001')
})


OPTIONAL: HOIST THE ACCOUNT


If you do not wish to pass an account around to every Action that requires an
account, you can also hoist the account into the Wallet Client.

Copyimport { createWalletClient, http, parseEther } from 'viem'
import { mainnet } from 'viem/chains'
 
const [account] = await window.ethereum!.request({ method: 'eth_requestAccounts' })
 
const client = createWalletClient({ 
  account, 
  chain: mainnet,
  transport: http()
})
 
const hash = await client.sendTransaction({
  account, 
  to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
  value: parseEther('0.001')
})


LOCAL ACCOUNTS (PRIVATE KEY, MNEMONIC, ETC)


A Local Account performs signing of transactions & messages with a private key
before executing a method over JSON-RPC.

There are three types of Local Accounts in viem:

 * Private Key Account
 * Mnemonic Account
 * Hierarchical Deterministic (HD) Account

Below are the steps to integrate a Private Key Account, but the same steps can
be applied to Mnemonic & HD Accounts.


1: INITIALIZE A WALLET CLIENT


Before we set up our Account and start consuming Wallet Actions, we will need to
set up our Wallet Client with the http Transport:

Copyimport { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  chain: mainnet,
  transport: http()
})


2: SET UP YOUR LOCAL ACCOUNT


Next, we will instantiate a Private Key Account using privateKeyToAccount:

Copyimport { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts' 
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  chain: mainnet,
  transport: http()
})
 
const account = privateKeyToAccount('0x...') 


3: CONSUME WALLET ACTIONS


Now you can use that Account within Wallet Actions that need a signature from
the user:

Copyimport { createWalletClient, http, parseEther } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  chain: mainnet,
  transport: http()
})
 
const account = privateKeyToAccount('0x...')
 
const hash = await client.sendTransaction({ 
  account,
  to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
  value: parseEther('0.001')
})


OPTIONAL: HOIST THE ACCOUNT


If you do not wish to pass an account around to every Action that requires an
account, you can also hoist the account into the Wallet Client.

Copyimport { createWalletClient, http, parseEther } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
 
const account = privateKeyToAccount('0x...')
 
const client = createWalletClient({ 
  account, 
  chain: mainnet,
  transport: http()
})
 
const hash = await client.sendTransaction({
  account, 
  to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
  value: parseEther('0.001')
})


OPTIONAL: EXTEND WITH PUBLIC ACTIONS


When using a Local Account, you may be finding yourself using a Public Client
instantiated with the same parameters (transport, chain, etc) as your Wallet
Client.

In this case, you can extend your Wallet Client with Public Actions to avoid
having to handle multiple Clients.

Copyimport { createWalletClient, http, publicActions } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
 
const account = privateKeyToAccount('0x...')
 
const client = createWalletClient({ 
  account,
  chain: mainnet,
  transport: http()
}).extend(publicActions) 
 
const { request } = await client.simulateContract({ ... }) // Public Action
const hash = await client.writeContract(request) // Wallet Action


PARAMETERS



ACCOUNT (OPTIONAL)


 * Type: Account | Address

The Account to use for the Wallet Client. This will be used for Actions that
require an account as an argument.

Accepts a JSON-RPC Account or Local Account (Private Key, etc).

Copyimport { createWalletClient, custom, parseEther } from 'viem'
import { mainnet } from 'viem/chains'
 
const client = createWalletClient({
  account: '0x...', 
  chain: mainnet,
  transport: custom(window.ethereum!)
})
 
const hash = await client.sendTransaction({
  to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
  value: parseEther('0.001')
})


CHAIN (OPTIONAL)


 * Type: Chain

The Chain of the Wallet Client.

Used in the sendTransaction & writeContract Actions to assert that the chain
matches the wallet's active chain.

Copyconst client = createWalletClient({
  chain: mainnet, 
  transport: custom(window.ethereum!)
})


CACHETIME (OPTIONAL)


 * Type: number
 * Default: client.pollingInterval

Time (in ms) that cached data will remain in memory.

Copyconst client = createWalletClient({
  cacheTime: 10_000, 
  chain: mainnet,
  transport: custom(window.ethereum!)
})


KEY (OPTIONAL)


 * Type: string
 * Default: "wallet"

A key for the Client.

Copyconst client = createWalletClient({
  key: 'foo', 
  transport: custom(window.ethereum!)
})


NAME (OPTIONAL)


 * Type: string
 * Default: "Wallet Client"

A name for the Client.

Copyconst client = createWalletClient({
  name: 'Foo Wallet Client', 
  transport: custom(window.ethereum!)
})


POLLINGINTERVAL (OPTIONAL)


 * Type: number
 * Default: 4_000

Frequency (in ms) for polling enabled Actions.

Copyconst client = createWalletClient({
  pollingInterval: 10_000, 
  transport: custom(window.ethereum!)
})

Last updated: 2/4/24, 6:18 AM
Arrow Left
Public Client
Previousshift←
Test Client
Arrow Right
Nextshift→