> ## Documentation Index
> Fetch the complete documentation index at: https://developers.paxoslabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# prepareCancelWithdrawOrderTxData

> Prepare a transaction to cancel a pending withdrawal order

Prepare transaction data for canceling a pending withdrawal order on the WithdrawQueue contract. Only the order owner can cancel their order.

> [!WARNING]
>
> `prepareCancelWithdrawOrderTxData()` is a low-level transaction builder. Use
> [getWithdrawalRequests](https://developers.paxoslabs.com/v0.5.3/intro/products/earn/developers/api/display/getWithdrawalRequests.md)
> to retrieve the `orderIndex` for a pending withdrawal before calling this
> function.

## Import

```typescript theme={null}
import { prepareCancelWithdrawOrderTxData } from '@paxoslabs/amplify-sdk'
```

## Usage

```typescript theme={null}
// 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

| Parameter | Type | Required | Description |
| - | - | - | - |
| `vaultName` | `string` | Yes | Account name from `AmplifyVault.name` (e.g. from `getVaultsByConfig()`) |
| `wantAsset` | `Address` | Yes | Token address to receive as a refund |
| `chainId` | `number` | Yes | Chain ID where the order exists |
| `orderIndex` | `bigint` | Yes | Index of the order to cancel in the WithdrawQueue |

```typescript theme={null}
interface PrepareCancelWithdrawOrderTxDataParams {
  vaultName: string
  wantAsset: Address
  chainId: number
  orderIndex: bigint
}
```

***

## Return Type

> [tab] Type Definition
>
> ```typescript theme={null}
> interface CancelWithdrawOrderTxData {
>   abi: typeof WithdrawQueueAbi;
>   address: `0x${string}`;        // WithdrawQueue contract address
>   functionName: "cancelOrder";
>   args: [orderIndex: bigint];
>   chainId: number;
> }
> ```

> [tab] Example Response
>
> ```typescript theme={null}
> {
>   abi: [/* WithdrawQueue ABI */],
>   address: "0x1234567890abcdef...", // WithdrawQueue address
>   functionName: "cancelOrder",
>   args: [42n],                      // Order index
>   chainId: 1,
> }
> ```

***

## Examples

> [tab] Viem
>
> ```typescript theme={null}
> 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;
> }
> ```

> [tab] Wagmi
>
> ```tsx theme={null}
> import {
>   getVaultsByConfig,
>   getWithdrawalRequests,
>   prepareCancelWithdrawOrderTxData,
>   YieldType,
> } from "@paxoslabs/amplify-sdk";
> import {
>   useAccount,
>   useWriteContract,
>   usePublicClient,
> } from "wagmi";
> import { useState } from "react";
>
> function CancelWithdrawalButton() {
>   const { address } = useAccount();
>   const publicClient = usePublicClient();
>   const { writeContractAsync } = useWriteContract();
>   const [status, setStatus] = useState("idle");
>
>   const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
>
>   async function handleCancel() {
>     if (!address) return;
>
>     try {
>       const [vault] = await getVaultsByConfig({
>         yieldType: YieldType.CORE,
>         chainId: 1,
>       });
>
>       setStatus("Fetching pending requests...");
>       const { withdrawalRequests } = await getWithdrawalRequests({
>         userAddress: address,
>         chainId: 1,
>         status: "PENDING",
>       });
>
>       if (withdrawalRequests.length === 0) {
>         setStatus("No pending requests to cancel");
>         return;
>       }
>
>       const requestToCancel = withdrawalRequests[0];
>
>       setStatus("Submitting cancellation...");
>       const txData = await prepareCancelWithdrawOrderTxData({
>         vaultName: vault.name,
>         wantAsset: USDC,
>         chainId: requestToCancel.chainId,
>         orderIndex: BigInt(requestToCancel.orderIndex),
>       });
>
>       const hash = await writeContractAsync(txData);
>       await publicClient.waitForTransactionReceipt({ hash });
>
>       setStatus("Withdrawal canceled!");
>     } catch (error) {
>       setStatus(`Error: ${(error as Error).message}`);
>     }
>   }
>
>   return (
>     <div>
>       <button onClick={handleCancel}>Cancel Withdrawal</button>
>       <p>{status}</p>
>     </div>
>   );
> }
> ```

***

## Error Handling

| Error | Description | Resolution |
| - | - | - |
| `Vault not found` | No account matches `vaultName` | Verify account name via `getVaultsByConfig()` |
| `Vault chain mismatch` | Account not on requested chain | Verify chainId matches account deployment |
| `WithdrawQueue contract address not configured` | Missing contract config | Check account configuration |

```typescript theme={null}
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)
  }
}
```

***

## Related

* [getVaultsByConfig](https://developers.paxoslabs.com/v0.5.3/intro/products/earn/developers/api/getVaultsByConfig.md) - Discover account names
* [Withdrawals Guide](https://developers.paxoslabs.com/v0.5.3/intro/products/earn/developers/guides/withdrawals.md) — Complete withdrawal integration guide including cancellation flow
* [getWithdrawalRequests](https://developers.paxoslabs.com/v0.5.3/intro/products/earn/developers/api/display/getWithdrawalRequests.md) — Retrieve withdrawal requests to find the orderIndex
* [prepareWithdrawOrderTxData](https://developers.paxoslabs.com/v0.5.3/intro/products/earn/developers/api/prepareWithdrawOrderTxData.md) — Submit withdrawal orders
* [prepareApproveWithdrawOrderTxData](https://developers.paxoslabs.com/v0.5.3/intro/products/earn/developers/api/prepareApproveWithdrawOrderTxData.md) — Approve account shares for withdrawal


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.