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

const txData = await prepareCancelWithdrawOrderTxData({
  yieldType: "CORE",
  wantAsset: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
  chainId: 1,
  orderIndex: 42n,
});

const hash = await walletClient.writeContract(txData);

Parameters

ParameterTypeRequiredDescription
yieldTypeYieldTypeYesYield strategy (CORE, TREASURY, FRONTIER)
wantAssetAddressYesToken address to receive as a refund
chainIdChainIdYesChain ID where the order exists
orderIndexbigintYesIndex of the order to cancel in the WithdrawQueue
interface PrepareCancelWithdrawOrderTxDataParams {
  yieldType: YieldType;
  wantAsset: Address;
  chainId: ChainId;
  orderIndex: bigint;
}

Return Type

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

Examples

import {
  getWithdrawalRequests,
  prepareCancelWithdrawOrderTxData,
} 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";

  // 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({
    yieldType: "CORE",
    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 chain mismatchVault not on requested chainVerify chainId matches vault deployment
WithdrawQueue contract address not configuredMissing contract configCheck vault exists for yieldType/chain
No vault found for assetInvalid vault lookupVerify wantAsset and yieldType combination
import { prepareCancelWithdrawOrderTxData, APIError } from "@paxoslabs/amplify-sdk";

try {
  const txData = await prepareCancelWithdrawOrderTxData({
    yieldType: "CORE",
    wantAsset: USDC_ADDRESS,
    chainId: 1,
    orderIndex: 42n,
  });
} catch (error) {
  if (error instanceof APIError) {
    console.error("SDK Error:", error.message);
  }
}