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

# prepareApproveWithdrawTxData

> Prepare the approval transaction required before withdrawing

Prepares the approval transaction that must be executed before withdrawing vault shares. This grants the atomic queue contract permission to transfer shares on behalf of the user.

## Import

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

## Usage

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

## Parameters

| Parameter          | Type        | Required | Description                                             |
| ------------------ | ----------- | -------- | ------------------------------------------------------- |
| `yieldType`        | `YieldType` | Yes      | Yield strategy type (`CORE`, `TREASURY`, or `FRONTIER`) |
| `wantAssetAddress` | `Address`   | Yes      | Token address the user wants to receive                 |
| `chainId`          | `number`    | Yes      | Blockchain network ID                                   |

```ts theme={null}
interface PrepareApproveWithdrawParams {
  /** Yield strategy type (e.g., YieldType.CORE) */
  yieldType: YieldType;
  /** Token address users want to receive */
  wantAssetAddress: Address;
  /** Blockchain network ID */
  chainId: number;
}
```

## Return Type

<Tabs>
  <Tab title="Type Definition">
    ```ts theme={null}
    interface ApproveWithdrawTxData {
      /** Contract ABI for the approve function */
      abi: Abi;
      /** Vault share token address */
      address: Address;
      /** Function name to call (always "approve") */
      functionName: string;
      /** Encoded function arguments */
      args: unknown[];
      /** Chain ID for the transaction */
      chainId: number;
    }
    ```
  </Tab>

  <Tab title="Example Response">
    ```ts theme={null}
    {
      abi: [...], // ERC20 ABI
      address: "0xVault...ShareToken",
      functionName: "approve",
      args: [
        "0xAtomicQueue...Address", // spender
        115792089237316195423570985008687907853269984665640564039457584007913129639935n, // max uint256
      ],
      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 withdrawal
    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 (see prepareWithdrawTxData)
    ```
  </Tab>

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

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

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

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

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

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

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

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

## Withdrawal Flow

The complete withdrawal process requires two transactions:

```
1. Approve   ->  2. Withdraw
   |                 |
   v                 v
   Grant queue       Burn shares,
   permission        receive tokens
```

<Info>
  The approval is typically only needed once per vault. After approval, subsequent withdrawals only require the `prepareWithdrawTxData` step.
</Info>

## Smart Wallet Batching

For smart wallets, you can batch both transactions into a single user confirmation:

```tsx theme={null}
// Privy Smart Wallet
const { client: smartWalletClient } = useSmartWallets();

const approval = await prepareApproveWithdrawTxData({...});
const withdraw = await prepareWithdrawTxData({...});

await smartWalletClient.sendTransaction({
  calls: [
    {
      to: approval.address,
      data: encodeFunctionData({
        abi: approval.abi,
        functionName: approval.functionName,
        args: approval.args,
      }),
    },
    {
      to: withdraw.address,
      data: encodeFunctionData({
        abi: withdraw.abi,
        functionName: withdraw.functionName,
        args: withdraw.args,
      }),
    },
  ],
}, { sponsor: true });
```

## 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         |
| `INVALID_WANT_ASSET`  | Want asset not supported    | Check supported withdrawal assets |

## Related

* [prepareWithdrawTxData](/v0.3.0/intro/products/earn/developers/api/prepareWithdrawTxData) - Execute withdrawal
* [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
