Granite Upgrade Activates in08d:14h:57m:47s
MethodsWallet Methods

C-Chain Wallet Methods

Complete reference for C-Chain atomic transaction methods

Overview

The C-Chain Wallet Methods provide transaction preparation capabilities for atomic cross-chain transfers between the C-Chain and other Avalanche chains (P-Chain and X-Chain). These methods handle the export and import of native AVAX via atomic transactions.

Access: walletClient.cChain

prepareExportTxn

Prepare a transaction to export AVAX from C-Chain to another chain (P-Chain or X-Chain).

Function Signature:

function prepareExportTxn(
  params: PrepareExportTxnParameters
): Promise<PrepareExportTxnReturnType>;

interface PrepareExportTxnParameters {
  destinationChain: "P" | "X";
  fromAddress: string;
  exportedOutput: {
    addresses: string[];
    amount: bigint;
    locktime?: bigint;
    threshold?: number;
  };
  context?: Context;
}

interface PrepareExportTxnReturnType {
  tx: UnsignedTx;
  exportTx: ExportTx;
  chainAlias: "C";
}

Parameters:

NameTypeRequiredDescription
destinationChain"P" | "X"YesChain alias to export funds to (P-Chain or X-Chain)
fromAddressstringYesEVM address to export funds from
exportedOutputobjectYesConsolidated exported output (UTXO)
contextContextNoOptional context for the transaction

Exported Output Object:

NameTypeRequiredDescription
addressesstring[]YesAddresses who can sign the consuming of this UTXO
amountbigintYesAmount in nano AVAX held by this exported output
locktimebigintNoTimestamp in seconds after which this UTXO can be consumed
thresholdnumberNoThreshold of addresses' signatures required to consume this UTXO

Returns:

TypeDescription
PrepareExportTxnReturnTypeExport transaction object

Return Object:

PropertyTypeDescription
txUnsignedTxThe unsigned transaction
exportTxExportTxThe export transaction instance
chainAlias"C"The chain alias

Example:

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

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

const walletClient = createAvalancheWalletClient({
  account,
  chain: avalanche,
  transport: { type: "http" },
});

// Export from C-Chain to P-Chain
const exportTx = await walletClient.cChain.prepareExportTxn({
  destinationChain: "P",
  fromAddress: account.getEVMAddress(),
  exportedOutput: {
    addresses: [account.getXPAddress("P")],
    amount: avaxToNanoAvax(0.001),
  },
});

// Sign and send
const { txHash } = await walletClient.sendXPTransaction({
  txOrTxHex: exportTx,
  chainAlias: "C",
});

console.log("Export transaction hash:", txHash);

Related:


prepareImportTxn

Prepare a transaction to import AVAX from another chain (P-Chain or X-Chain) to C-Chain.

Function Signature:

function prepareImportTxn(
  params: PrepareImportTxnParameters
): Promise<PrepareImportTxnReturnType>;

interface PrepareImportTxnParameters {
  account?: AvalancheAccount | Address | undefined;
  sourceChain: "P" | "X";
  toAddress: string;
  fromAddresses?: string[];
  utxos?: Utxo[];
  context?: Context;
}

interface PrepareImportTxnReturnType {
  tx: UnsignedTx;
  importTx: ImportTx;
  chainAlias: "C";
}

Parameters:

NameTypeRequiredDescription
accountAvalancheAccount | AddressNoAccount to use for the transaction
sourceChain"P" | "X"YesChain alias to import funds from (P-Chain or X-Chain)
toAddressstringYesEVM address to import funds to
fromAddressesstring[]NoAddresses to import funds from (auto-fetched if not provided)
utxosUtxo[]NoUTXOs to use as inputs (must be in atomic memory, auto-fetched if not provided)
contextContextNoOptional context for the transaction

Returns:

TypeDescription
PrepareImportTxnReturnTypeImport transaction object

Return Object:

PropertyTypeDescription
txUnsignedTxThe unsigned transaction
importTxImportTxThe import transaction instance
chainAlias"C"The chain alias

Example:

const importTx = await walletClient.cChain.prepareImportTxn({
  sourceChain: "P",
  toAddress: account.getEVMAddress(),
  fromAddresses: [account.getXPAddress("P")],
});

// Sign and send
const { txHash } = await walletClient.sendXPTransaction({
  txOrTxHex: importTx,
  chainAlias: "C",
});

console.log("Import transaction hash:", txHash);

Related:


Complete Cross-Chain Transfer Workflow

Export from C-Chain to P-Chain

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

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

const walletClient = createAvalancheWalletClient({
  account,
  chain: avalanche,
  transport: { type: "http" },
});

// 1. Export from C-Chain
const exportTx = await walletClient.cChain.prepareExportTxn({
  destinationChain: "P",
  fromAddress: account.getEVMAddress(),
  exportedOutput: {
    addresses: [account.getXPAddress("P")],
    amount: avaxToNanoAvax(1.0),
  },
});

// 2. Sign and send export transaction
const { txHash: exportTxHash } = await walletClient.sendXPTransaction({
  txOrTxHex: exportTx,
  chainAlias: "C",
});

// 3. Wait for export to be committed
await walletClient.waitForTxn({
  txHash: exportTxHash,
  chainAlias: "C",
});

console.log("Export completed:", exportTxHash);

// 4. Import to P-Chain
const importTx = await walletClient.pChain.prepareImportTxn({
  sourceChain: "C",
  toAddresses: [account.getXPAddress("P")],
});

const { txHash: importTxHash } = await walletClient.sendXPTransaction({
  txOrTxHex: importTx,
  chainAlias: "P",
});

console.log("Import completed:", importTxHash);

Next Steps

Is this guide helpful?