> ## Documentation Index
> Fetch the complete documentation index at: https://l2docs.whitechain.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Use viem with Whitechain

> Set up a viem client for Whitechain Sepolia and run common operations: read a balance, send WBT, and call a contract.

Whitechain Sepolia ships as `whitechainSepolia` in `viem/chains`. No custom chain definition is needed. This page shows how to install viem, create a client, and run the most common read and write operations.

## Install

```bash Terminal theme={null}
npm install viem
npm install --save-dev @types/node
```

The code snippets on this page use top-level `await` and `process.env`. Add `"type": "module"` to `package.json` and declare the `node` types in `tsconfig.json`:

```json package.json theme={null}
{
  "type": "module"
}
```

```json tsconfig.json theme={null}
{
  "compilerOptions": {
    "types": ["node"]
  }
}
```

## 1. Create a client

Use `createPublicClient` for read operations and `createWalletClient` for transactions and contract writes.

Read the private key from an environment variable and exclude `.env` from version control.

```ts client.ts theme={null}
import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { whitechainSepolia } from 'viem/chains'

export const publicClient = createPublicClient({
  chain: whitechainSepolia,
  transport: http(),
})

const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`)

export const walletClient = createWalletClient({
  account,
  chain: whitechainSepolia,
  transport: http(),
})
```

<Note>
  `http()` with no argument uses the RPC endpoint built into `whitechainSepolia` (`https://rpc.testnet.whitechain.io`). Pass a URL string to `http('https://...')` to override it.
</Note>

## 2. Read a balance

`getBalance` returns the WBT balance of an address in wei. Use `formatEther` to convert it to a decimal string.

```ts theme={null}
import { formatEther } from 'viem'
import { publicClient, walletClient } from './client'

const address = walletClient.account.address
const balance = await publicClient.getBalance({ address })

console.log(`Address: ${address}`)
console.log(`Balance: ${formatEther(balance)} WBT`) // e.g. "1.5"
```

## 3. Send WBT

`sendTransaction` sends WBT to a recipient address. Use `parseEther` to express the value in WBT rather than wei.

```ts theme={null}
import { parseEther } from 'viem'
import { walletClient } from './client'

const hash = await walletClient.sendTransaction({
  to: '0xRecipientAddress',
  value: parseEther('0.01'),
})

console.log(hash) // transaction hash
```

## 4. Call a contract

Define the contract address and ABI once and share them across read and write operations.

```ts contract.ts theme={null}
export const CONTRACT_ADDRESS = '0xYourContractAddress' as const

export const storageAbi = [
  {
    type: 'function',
    name: 'retrieve',
    inputs: [],
    outputs: [{ name: '', type: 'uint256' }],
    stateMutability: 'view',
  },
  {
    type: 'function',
    name: 'store',
    inputs: [{ name: 'num', type: 'uint256' }],
    outputs: [],
    stateMutability: 'nonpayable',
  },
] as const
```

<Note>
  Keep `as const` on the ABI. Without it, TypeScript cannot infer function names or argument types from the array.
</Note>

### Read from a contract

`readContract` calls a `view` or `pure` function. No transaction is sent and no gas is spent.

```ts theme={null}
import { publicClient } from './client'
import { CONTRACT_ADDRESS, storageAbi } from './contract'

const value = await publicClient.readContract({
  address: CONTRACT_ADDRESS,
  abi: storageAbi,
  functionName: 'retrieve',
})

console.log(value) // bigint
```

### Write to a contract

`writeContract` sends a transaction that calls a state-changing function.

```ts theme={null}
import { walletClient } from './client'
import { CONTRACT_ADDRESS, storageAbi } from './contract'

const hash = await walletClient.writeContract({
  address: CONTRACT_ADDRESS,
  abi: storageAbi,
  functionName: 'store',
  args: [42n],
})

console.log(hash) // transaction hash
```

## Related

* [Deploy with Hardhat](/build/deploy/deploy-with-hardhat)
* [Deploy with Foundry](/build/deploy/deploy-with-foundry)
* [Next dapp template](/build/dapps/dapp-with-nextjs)
* [Network reference](/learn/network/reference)
