Granite Upgrade Activates in06d:02h:04m:26s
Account ManagementLocal Accounts

Using Accounts with Clients

Overview

Accounts work with both public clients (read-only) and wallet clients (transactions). You can hoist the account into the client or pass it to each method.

Public Client (Read-Only)

import { createAvalancheClient } from "@avalanche-sdk/client";
import { avalanche } from "@avalanche-sdk/client/chains";
import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts";

const client = createAvalancheClient({
  chain: avalanche,
  transport: { type: "http" },
});

const account = privateKeyToAvalancheAccount("0x...");

// Read operations
const balance = await client.getBalance({ address: account.getEVMAddress() });
const height = await client.pChain.getHeight();

Wallet Client (Transactions)

import { createAvalancheWalletClient } from "@avalanche-sdk/client";
import { avalanche } from "@avalanche-sdk/client/chains";
import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts";
import { avaxToWei } from "@avalanche-sdk/client/utils";

const account = privateKeyToAvalancheAccount("0x...");

// Hoist account (recommended)
const walletClient = createAvalancheWalletClient({
  account, // Account is hoisted
  chain: avalanche,
  transport: { type: "http" },
});

// C-Chain transaction
const txHash = await walletClient.send({
  to: "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6",
  amount: avaxToWei(0.001),
});

// X/P-Chain transaction
const xpTx = await walletClient.xChain.prepareBaseTxn({
  outputs: [{ addresses: [account.getXPAddress("X")], amount: 1 }],
});
await walletClient.sendXPTransaction(xpTx);

Account Hoisting

You can hoist the account into the client (recommended) or pass it to each method:

// Hoisted (recommended)
const walletClient = createAvalancheWalletClient({
  account, // No need to pass to each method
  chain: avalanche,
  transport: { type: "http" },
});
await walletClient.send({ to: "0x...", amount: 0.001 });

// Or pass per method
const walletClient = createAvalancheWalletClient({
  chain: avalanche,
  transport: { type: "http" },
});
await walletClient.send({ account, to: "0x...", amount: 0.001 });

Cross-Chain Operations

Cross-chain transfers use the export/import pattern. Export from the source chain, wait for confirmation, then import to the destination chain.

Learn more about cross-chain transfers →

Next Steps

Is this guide helpful?