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

X-Chain Wallet Methods

Complete reference for X-Chain transaction preparation methods

Overview

The X-Chain Wallet Methods provide transaction preparation capabilities for the Exchange Chain. These methods allow you to create unsigned transactions for various operations including base transfers and cross-chain transfers.

Access: walletClient.xChain

prepareBaseTxn

Prepare a base X-Chain transaction for transferring AVAX or other assets.

Function Signature:

function prepareBaseTxn(
  params: PrepareBaseTxnParameters
): Promise<PrepareBaseTxnReturnType>;

interface PrepareBaseTxnParameters {
  outputs?: Output[];
  fromAddresses?: string[];
  changeAddresses?: string[];
  utxos?: Utxo[];
  memo?: string;
  minIssuanceTime?: bigint;
  context?: Context;
}

interface Output {
  addresses: string[];
  amount: bigint;
  assetId?: string;
  locktime?: bigint;
  threshold?: number;
}

interface PrepareBaseTxnReturnType {
  tx: UnsignedTx;
  baseTx: BaseTx;
  chainAlias: "X";
}

Parameters:

NameTypeRequiredDescription
outputsOutput[]NoArray of outputs to send funds to
fromAddressesstring[]NoAddresses to send funds from
changeAddressesstring[]NoAddresses to receive change
utxosUtxo[]NoUTXOs to use as inputs
memostringNoTransaction memo
minIssuanceTimebigintNoEarliest time this transaction can be issued
contextContextNoTransaction context (auto-fetched if omitted)

Output Object:

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

Returns:

TypeDescription
PrepareBaseTxnReturnTypeBase transaction object

Return Object:

PropertyTypeDescription
txUnsignedTxThe unsigned transaction
baseTxBaseTxThe base transaction instance
chainAlias"X"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" },
});

const unsignedTx = await walletClient.xChain.prepareBaseTxn({
  outputs: [
    {
      addresses: ["X-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5"],
      amount: avaxToNanoAvax(1),
    },
  ],
});

// Sign and send
const signedTx = await walletClient.signXPTransaction({
  tx: unsignedTx.tx,
  chainAlias: "X",
});

const { txHash } = await walletClient.sendXPTransaction({
  tx: signedTx.signedTxHex,
  chainAlias: "X",
});

console.log("Transaction hash:", txHash);

Related:


prepareExportTxn

Prepare a transaction to export AVAX or other assets from X-Chain to another chain.

Function Signature:

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

interface PrepareExportTxnParameters {
  destinationChain: "P" | "C";
  exportedOutputs: Output[];
  fromAddresses?: string[];
  changeAddresses?: string[];
  utxos?: Utxo[];
  memo?: string;
  minIssuanceTime?: bigint;
  context?: Context;
}

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

Parameters:

NameTypeRequiredDescription
destinationChain"P" | "C"YesChain alias to export funds to
exportedOutputsOutput[]YesOutputs to export
fromAddressesstring[]NoAddresses to send funds from
changeAddressesstring[]NoAddresses to receive change
utxosUtxo[]NoUTXOs to use as inputs
memostringNoTransaction memo
minIssuanceTimebigintNoEarliest time this transaction can be issued
contextContextNoTransaction context (auto-fetched if omitted)

Returns:

TypeDescription
PrepareExportTxnReturnTypeExport transaction object

Return Object:

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

Example:

const exportTx = await walletClient.xChain.prepareExportTxn({
  destinationChain: "C",
  exportedOutputs: [
    {
      addresses: ["0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6"],
      amount: avaxToNanoAvax(1),
    },
  ],
});

// Sign and send
const signedTx = await walletClient.signXPTransaction({
  tx: exportTx.tx,
  chainAlias: "X",
});

const { txHash } = await walletClient.sendXPTransaction({
  tx: signedTx.signedTxHex,
  chainAlias: "X",
});

Related:


prepareImportTxn

Prepare a transaction to import AVAX or other assets from another chain to X-Chain.

Function Signature:

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

interface PrepareImportTxnParameters {
  sourceChain: "P" | "C";
  importedOutput: ImportedOutput;
  fromAddresses?: string[];
  utxos?: Utxo[];
  memo?: string;
  minIssuanceTime?: bigint;
  context?: Context;
}

interface ImportedOutput {
  addresses: string[];
  locktime?: bigint;
  threshold?: number;
}

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

Parameters:

NameTypeRequiredDescription
sourceChain"P" | "C"YesChain alias to import funds from
importedOutputImportedOutputYesConsolidated imported output from atomic memory
fromAddressesstring[]NoAddresses to send funds from
utxosUtxo[]NoUTXOs to use as inputs
memostringNoTransaction memo
minIssuanceTimebigintNoEarliest time this transaction can be issued
contextContextNoTransaction context (auto-fetched if omitted)

Imported Output Object:

NameTypeRequiredDescription
addressesstring[]YesAddresses who can sign the consuming of this UTXO
locktimebigintNoTimestamp in seconds after which this UTXO can be consumed
thresholdnumberNoNumber of signatures required out of total addresses to spend the imported output

Returns:

TypeDescription
PrepareImportTxnReturnTypeImport transaction object

Return Object:

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

Example:

const importTx = await walletClient.xChain.prepareImportTxn({
  sourceChain: "C",
  importedOutput: {
    addresses: ["X-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5"],
    threshold: 1,
  },
});

// Sign and send
const signedTx = await walletClient.signXPTransaction({
  tx: importTx.tx,
  chainAlias: "X",
});

const { txHash } = await walletClient.sendXPTransaction({
  tx: signedTx.signedTxHex,
  chainAlias: "X",
});

Related:


Next Steps

Is this guide helpful?