> ## 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.

# prepareWithdrawTxData

> Prepare transaction data to withdraw vault shares for tokens

Prepares the transaction data for withdrawing vault shares and receiving the underlying tokens. Must be called after the withdrawal has been approved.

## Import

```ts theme={null}
import { prepareWithdrawTxData } from "@paxoslabs/amplify-sdk";
```

## Usage

```ts theme={null}
const txData = await prepareWithdrawTxData({
  yieldType: YieldType.CORE,
  offerAmount: "100.5",
  wantAssetAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  chainId: 1,
});
```

## Parameters

| Parameter          | Type        | Required | Description                                             |
| ------------------ | ----------- | -------- | ------------------------------------------------------- |
| `yieldType`        | `YieldType` | Yes      | Yield strategy type (`CORE`, `TREASURY`, or `FRONTIER`) |
| `offerAmount`      | `string`    | Yes      | Amount of shares to burn as decimal string              |
| `wantAssetAddress` | `Address`   | Yes      | Token address the user wants to receive                 |
| `chainId`          | `number`    | Yes      | Blockchain network ID                                   |
| `slippage`         | `number`    | No       | Slippage tolerance in basis points (default: 50)        |

```ts theme={null}
interface PrepareWithdrawParams {
  /** Yield strategy type (e.g., YieldType.CORE) */
  yieldType: YieldType;
  /** Amount of shares to burn as decimal string */
  offerAmount: string;
  /** Token address users want to receive */
  wantAssetAddress: Address;
  /** Blockchain network ID */
  chainId: number;
  /** Slippage in basis points (default: 50 = 0.5%) */
  slippage?: number;
}
```

## Return Type

<Tabs>
  <Tab title="Type Definition">
    ```ts theme={null}
    interface WithdrawTxData {
      /** Contract ABI for the withdraw function */
      abi: Abi;
      /** Atomic queue contract address */
      address: Address;
      /** Function name to call */
      functionName: string;
      /** Encoded function arguments */
      args: unknown[];
      /** Chain ID for the transaction */
      chainId: number;
    }
    ```
  </Tab>

  <Tab title="Example Response">
    ```ts theme={null}
    {
      abi: [...], // AtomicQueue ABI
      address: "0xAtomicQueue...Address",
      functionName: "updateAtomicRequest",
      args: [
        "0xVault...Address", // vault
        "0xWant...Asset", // wantAsset
        {
          deadline: 1234567890n,
          atomicPrice: 1000000n,
          offerAmount: 100500000n,
          inSolve: false,
        },
      ],
      chainId: 1,
    }
    ```
  </Tab>
</Tabs>

## Examples

<Tabs>
  <Tab title="Privy">
    ```tsx theme={null}
    import { encodeFunctionData } from "viem";
    import { usePrivy } from "@privy-io/react-auth";
    import {
      prepareApproveWithdrawTxData,
      prepareWithdrawTxData,
      YieldType,
    } from "@paxoslabs/amplify-sdk";

    const { sendTransaction } = usePrivy();
    const wantAsset = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; // USDC

    // Step 1: Approve (if not already approved)
    const approval = await prepareApproveWithdrawTxData({
      yieldType: YieldType.CORE,
      wantAssetAddress: wantAsset,
      chainId: 1,
    });

    await sendTransaction({
      chainId: approval.chainId,
      to: approval.address,
      data: encodeFunctionData({
        abi: approval.abi,
        functionName: approval.functionName,
        args: approval.args,
      }),
    });

    // Step 2: Execute withdrawal
    const withdraw = await prepareWithdrawTxData({
      yieldType: YieldType.CORE,
      offerAmount: "100.5",
      wantAssetAddress: wantAsset,
      chainId: 1,
    });

    await sendTransaction({
      chainId: withdraw.chainId,
      to: withdraw.address,
      data: encodeFunctionData({
        abi: withdraw.abi,
        functionName: withdraw.functionName,
        args: withdraw.args,
      }),
    });
    ```
  </Tab>

  <Tab title="Wagmi">
    ```tsx theme={null}
    import { useAccount, useWriteContract } from "wagmi";
    import {
      prepareApproveWithdrawTxData,
      prepareWithdrawTxData,
      YieldType,
    } from "@paxoslabs/amplify-sdk";

    const { address, chainId } = useAccount();
    const { writeContractAsync } = useWriteContract();

    // Step 1: Approve
    const approval = await prepareApproveWithdrawTxData({
      yieldType: YieldType.CORE,
      wantAssetAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      chainId: chainId!,
    });

    await writeContractAsync({ ...approval, account: address });

    // Step 2: Withdraw
    const withdraw = await prepareWithdrawTxData({
      yieldType: YieldType.CORE,
      offerAmount: "100.5",
      wantAssetAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      chainId: chainId!,
    });

    await writeContractAsync({ ...withdraw, account: address });
    ```
  </Tab>

  <Tab title="Viem">
    ```ts theme={null}
    import { createWalletClient, custom } from "viem";
    import { mainnet } from "viem/chains";
    import {
      prepareApproveWithdrawTxData,
      prepareWithdrawTxData,
      YieldType,
    } from "@paxoslabs/amplify-sdk";

    const client = createWalletClient({
      account,
      chain: mainnet,
      transport: custom(window.ethereum),
    });

    // Step 1: Approve
    const approval = await prepareApproveWithdrawTxData({
      yieldType: YieldType.CORE,
      wantAssetAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      chainId: mainnet.id,
    });

    await client.writeContract({ ...approval, account });

    // Step 2: Withdraw
    const withdraw = await prepareWithdrawTxData({
      yieldType: YieldType.CORE,
      offerAmount: "100.5",
      wantAssetAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      chainId: mainnet.id,
    });

    await client.writeContract({ ...withdraw, account });
    ```
  </Tab>
</Tabs>

## Slippage Configuration

Adjust slippage for withdrawal price tolerance:

```ts theme={null}
// 1% slippage for volatile conditions
const withdraw = await prepareWithdrawTxData({
  ...params,
  slippage: 100,
});

// 0.1% slippage for tighter tolerance
const withdraw = await prepareWithdrawTxData({
  ...params,
  slippage: 10,
});
```

## Complete Withdrawal Hook

```tsx theme={null}
import { useState, useCallback } from "react";
import { encodeFunctionData } from "viem";
import { usePrivy } from "@privy-io/react-auth";
import {
  prepareApproveWithdrawTxData,
  prepareWithdrawTxData,
  YieldType,
} from "@paxoslabs/amplify-sdk";

export function useWithdraw() {
  const { sendTransaction } = usePrivy();
  const [status, setStatus] = useState("");
  const [isLoading, setIsLoading] = useState(false);

  const withdraw = useCallback(
    async (params: {
      yieldType: YieldType;
      offerAmount: string;
      wantAssetAddress: `0x${string}`;
      chainId: number;
    }) => {
      setIsLoading(true);

      try {
        // Step 1: Approve
        setStatus("Approving withdrawal...");
        const approval = await prepareApproveWithdrawTxData(params);
        await sendTransaction({
          chainId: approval.chainId,
          to: approval.address,
          data: encodeFunctionData({
            abi: approval.abi,
            functionName: approval.functionName,
            args: approval.args,
          }),
        });

        // Step 2: Withdraw
        setStatus("Executing withdrawal...");
        const withdraw = await prepareWithdrawTxData(params);
        const hash = await sendTransaction({
          chainId: withdraw.chainId,
          to: withdraw.address,
          data: encodeFunctionData({
            abi: withdraw.abi,
            functionName: withdraw.functionName,
            args: withdraw.args,
          }),
        });

        setStatus("Withdrawal complete!");
        return hash;
      } catch (error) {
        setStatus(`Error: ${error instanceof Error ? error.message : "Unknown"}`);
        throw error;
      } finally {
        setIsLoading(false);
      }
    },
    [sendTransaction]
  );

  return { withdraw, status, isLoading };
}
```

## Error Handling

| Error Code            | Description                  | Resolution                                |
| --------------------- | ---------------------------- | ----------------------------------------- |
| `SDK_NOT_INITIALIZED` | SDK not initialized          | Call `initAmplifySDK()` first             |
| `VAULT_NOT_FOUND`     | No vault matches parameters  | Verify yieldType, chainId                 |
| `INSUFFICIENT_SHARES` | Not enough vault shares      | Check user's share balance                |
| `NOT_APPROVED`        | Withdrawal not approved      | Call `prepareApproveWithdrawTxData` first |
| `SLIPPAGE_EXCEEDED`   | Price moved beyond tolerance | Increase slippage or retry                |

## Related

* [prepareApproveWithdrawTxData](/v0.3.0/intro/products/earn/developers/api/prepareApproveWithdrawTxData) - Approve withdrawals
* [Withdrawals Guide](/v0.3.0/intro/products/earn/developers/guides/withdrawals) - Complete examples
* [Smart Wallets Guide](/v0.3.0/intro/products/earn/developers/guides/smart-wallets) - Batched withdrawals
