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

Import

import { prepareWithdrawTxData } from "@paxoslabs/amplify-sdk";

Usage

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

Parameters

ParameterTypeRequiredDescription
yieldTypeYieldTypeYesYield strategy type (CORE, TREASURY, or FRONTIER)
offerAmountstringYesAmount of shares to burn as decimal string
wantAssetAddressAddressYesToken address the user wants to receive
chainIdnumberYesBlockchain network ID
slippagenumberNoSlippage tolerance in basis points (default: 50)
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

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;
}

Examples

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,
  }),
});

Slippage Configuration

Adjust slippage for withdrawal price tolerance:
// 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

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 CodeDescriptionResolution
SDK_NOT_INITIALIZEDSDK not initializedCall initAmplifySDK() first
VAULT_NOT_FOUNDNo vault matches parametersVerify yieldType, chainId
INSUFFICIENT_SHARESNot enough vault sharesCheck user’s share balance
NOT_APPROVEDWithdrawal not approvedCall prepareApproveWithdrawTxData first
SLIPPAGE_EXCEEDEDPrice moved beyond toleranceIncrease slippage or retry