viem
Version:
118 lines (111 loc) • 3.87 kB
text/typescript
import type { Address } from 'abitype'
import type { Account } from '../accounts/types.js'
import type { ErrorType } from '../errors/utils.js'
import type { ParseAccount } from '../types/account.js'
import type { Chain } from '../types/chain.js'
import type { RpcSchema, WalletRpcSchema } from '../types/eip1193.js'
import type { Prettify } from '../types/utils.js'
import {
type Client,
type ClientConfig,
type CreateClientErrorType,
createClient,
} from './createClient.js'
import { type WalletActions, walletActions } from './decorators/wallet.js'
import type { Transport } from './transports/createTransport.js'
export type WalletClientConfig<
transport extends Transport = Transport,
chain extends Chain | undefined = Chain | undefined,
accountOrAddress extends Account | Address | undefined =
| Account
| Address
| undefined,
rpcSchema extends RpcSchema | undefined = undefined,
> = Prettify<
Pick<
ClientConfig<transport, chain, accountOrAddress, rpcSchema>,
| 'account'
| 'cacheTime'
| 'ccipRead'
| 'chain'
| 'key'
| 'name'
| 'pollingInterval'
| 'rpcSchema'
| 'transport'
>
>
export type WalletClient<
transport extends Transport = Transport,
chain extends Chain | undefined = Chain | undefined,
account extends Account | undefined = Account | undefined,
rpcSchema extends RpcSchema | undefined = undefined,
> = Prettify<
Client<
transport,
chain,
account,
rpcSchema extends RpcSchema
? [...WalletRpcSchema, ...rpcSchema]
: WalletRpcSchema,
WalletActions<chain, account>
>
>
export type CreateWalletClientErrorType = CreateClientErrorType | ErrorType
/**
* Creates a Wallet Client with a given [Transport](https://viem.sh/docs/clients/intro) configured for a [Chain](https://viem.sh/docs/clients/chains).
*
* - Docs: https://viem.sh/docs/clients/wallet
*
* A Wallet Client is an interface to interact with [Ethereum Account(s)](https://ethereum.org/en/glossary/#account) and provides the ability to retrieve accounts, execute transactions, sign messages, etc. through [Wallet Actions](https://viem.sh/docs/actions/wallet/introduction).
*
* The Wallet Client supports signing over:
* - [JSON-RPC Accounts](https://viem.sh/docs/clients/wallet#json-rpc-accounts) (e.g. Browser Extension Wallets, WalletConnect, etc).
* - [Local Accounts](https://viem.sh/docs/clients/wallet#local-accounts-private-key-mnemonic-etc) (e.g. private key/mnemonic wallets).
*
* @param config - {@link WalletClientConfig}
* @returns A Wallet Client. {@link WalletClient}
*
* @example
* // JSON-RPC Account
* import { createWalletClient, custom } from 'viem'
* import { mainnet } from 'viem/chains'
*
* const client = createWalletClient({
* chain: mainnet,
* transport: custom(window.ethereum),
* })
*
* @example
* // Local Account
* import { createWalletClient, custom } from 'viem'
* import { privateKeyToAccount } from 'viem/accounts'
* import { mainnet } from 'viem/chains'
*
* const client = createWalletClient({
* account: privateKeyToAccount('0x…')
* chain: mainnet,
* transport: http(),
* })
*/
export function createWalletClient<
transport extends Transport,
chain extends Chain | undefined = undefined,
accountOrAddress extends Account | Address | undefined = undefined,
rpcSchema extends RpcSchema | undefined = undefined,
>(
parameters: WalletClientConfig<transport, chain, accountOrAddress, rpcSchema>,
): WalletClient<transport, chain, ParseAccount<accountOrAddress>, rpcSchema>
export function createWalletClient(
parameters: WalletClientConfig,
): WalletClient {
const { key = 'wallet', name = 'Wallet Client', transport } = parameters
const client = createClient({
...parameters,
key,
name,
transport,
type: 'walletClient',
})
return client.extend(walletActions)
}