Skip to main content
Prepare transaction data for canceling a pending withdrawal order on the WithdrawQueue contract. Only the order owner can cancel their order.
prepareCancelWithdrawOrderTxData() is a low-level transaction builder. Use getWithdrawalRequests to retrieve the orderIndex for a pending withdrawal before calling this function.

Import

import { prepareCancelWithdrawOrderTxData } from '@paxoslabs/amplify-sdk'

Usage

// First discover the vault
const [vault] = await getVaultsByConfig({
  yieldType: 'CORE',
  chainId: 1,
})

const txData = await prepareCancelWithdrawOrderTxData({
  vaultName: vault.name,
  wantAsset: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
  chainId: 1,
  orderIndex: 42n,
})

const hash = await walletClient.writeContract(txData)

Parameters

ParameterTypeRequiredDescription
vaultNamestringYesVault name from AmplifyVault.name (e.g. from getVaultsByConfig())
wantAssetAddressYesToken address to receive as a refund
chainIdnumberYesChain ID where the order exists
orderIndexbigintYesIndex of the order to cancel in the WithdrawQueue
interface PrepareCancelWithdrawOrderTxDataParams {
  vaultName: string
  wantAsset: Address
  chainId: number
  orderIndex: bigint
}

Return Type

interface CancelWithdrawOrderTxData {
  abi: typeof WithdrawQueueAbi;
  address: `0x${string}`;        // WithdrawQueue contract address
  functionName: "cancelOrder";
  args: [orderIndex: bigint];
  chainId: number;
}

Examples

import {
  getVaultsByConfig,
  getWithdrawalRequests,
  prepareCancelWithdrawOrderTxData,
  YieldType,
} from "@paxoslabs/amplify-sdk";
import { createWalletClient, createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

async function cancelWithdrawal(userAddress: `0x${string}`) {
  const walletClient = createWalletClient({
    account: userAddress,
    chain: mainnet,
    transport: http(),
  });
  const publicClient = createPublicClient({
    chain: mainnet,
    transport: http(),
  });

  const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";

  // Discover vault
  const [vault] = await getVaultsByConfig({
    yieldType: YieldType.CORE,
    chainId: 1,
  });

  // Step 1: Retrieve pending withdrawal requests
  const { withdrawalRequests } = await getWithdrawalRequests({
    userAddress,
    chainId: 1,
    status: "PENDING",
  });

  if (withdrawalRequests.length === 0) {
    console.log("No pending withdrawal requests to cancel");
    return;
  }

  const requestToCancel = withdrawalRequests[0];

  // Step 2: Prepare and submit cancellation
  const txData = await prepareCancelWithdrawOrderTxData({
    vaultName: vault.name,
    wantAsset: USDC,
    chainId: requestToCancel.chainId,
    orderIndex: BigInt(requestToCancel.orderIndex),
  });

  const hash = await walletClient.writeContract(txData);
  const receipt = await publicClient.waitForTransactionReceipt({ hash });

  console.log("Withdrawal canceled:", receipt.transactionHash);
  return hash;
}

Error Handling

ErrorDescriptionResolution
Vault not foundNo vault matches vaultNameVerify vault name via getVaultsByConfig()
Vault chain mismatchVault not on requested chainVerify chainId matches vault deployment
WithdrawQueue contract address not configuredMissing contract configCheck vault configuration
import {
  getVaultsByConfig,
  prepareCancelWithdrawOrderTxData,
  APIError,
} from '@paxoslabs/amplify-sdk'

const [vault] = await getVaultsByConfig({ yieldType: 'CORE', chainId: 1 })

try {
  const txData = await prepareCancelWithdrawOrderTxData({
    vaultName: vault.name,
    wantAsset: USDC_ADDRESS,
    chainId: 1,
    orderIndex: 42n,
  })
} catch (error) {
  if (error instanceof APIError) {
    console.error('SDK Error:', error.message)
  }
}