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

# Deposit Workflow

> Unified deposit API with automatic permit/approval detection

The unified deposit API simplifies deposits by automatically detecting the optimal authorization method. Use `prepareDepositAuthorization()` to determine the method, then `prepareDeposit()` to execute.

## Import

```ts theme={null}
import {
  prepareDepositAuthorization,
  prepareDeposit,
  isPermitAuth,
  isApprovalAuth,
  isAlreadyApprovedAuth,
} from "@paxoslabs/amplify-sdk";
```

## prepareDepositAuthorization()

Determines the optimal authorization method for a deposit operation.

### Function Signature

```ts theme={null}
async function prepareDepositAuthorization(
  params: PrepareDepositAuthorizationParams
): Promise<DepositAuthorizationResult>;
```

### Parameters

```ts theme={null}
interface PrepareDepositAuthorizationParams {
  /** Yield strategy type (e.g., YieldType.CORE) */
  yieldType: YieldType;

  /** Token contract address to deposit */
  depositAsset: Address;

  /** Amount of assets to deposit as decimal string (e.g., "100.25") */
  depositAmount: string;

  /** User's wallet address (permit owner / approval sender) */
  to: Address;

  /** Blockchain network ID */
  chainId: ChainId;

  /** Optional deadline for permit signature (defaults to 1 hour from now) */
  deadline?: bigint;

  /** Force specific authorization method (bypasses automatic detection) */
  forceMethod?: "permit" | "approval";
}
```

| Parameter       | Type                     | Required | Description                                  |
| --------------- | ------------------------ | -------- | -------------------------------------------- |
| `yieldType`     | `YieldType`              | Yes      | Yield strategy type (e.g., `YieldType.CORE`) |
| `depositAsset`  | `Address`                | Yes      | Token contract address to deposit            |
| `depositAmount` | `string`                 | Yes      | Amount as decimal string (e.g., "100.25")    |
| `to`            | `Address`                | Yes      | Recipient address for vault shares           |
| `chainId`       | `number`                 | Yes      | Blockchain network ID                        |
| `deadline`      | `bigint`                 | No       | Permit deadline (defaults to 1 hour)         |
| `forceMethod`   | `"permit" \| "approval"` | No       | Override automatic detection                 |

### Return Type (Discriminated Union)

<Tabs>
  <Tab title="Type Definition">
    ```ts theme={null}
    type DepositAuthorizationResult =
      | PermitAuthorizationResult
      | ApprovalAuthorizationResult
      | AlreadyApprovedAuthorizationResult;
    ```
  </Tab>

  <Tab title="Permit Result">
    ```ts theme={null}
    /** Returned when token supports EIP-2612 permit */
    interface PermitAuthorizationResult {
      method: "permit";
      /** EIP-712 typed data ready for wallet signing */
      permitData: PermitSignatureData;
    }
    ```
  </Tab>

  <Tab title="Approval Result">
    ```ts theme={null}
    /** Returned when an ERC20 approval transaction is needed */
    interface ApprovalAuthorizationResult {
      method: "approval";
      /** Transaction data for ERC20 approve() call */
      txData: ApproveDepositTokenTxData;
      /** Current allowance (human-readable) */
      currentAllowance: string;
      /** Current allowance as BigInt string */
      currentAllowanceAsBigInt: string;
    }
    ```
  </Tab>

  <Tab title="Already Approved Result">
    ```ts theme={null}
    /** Returned when existing allowance is sufficient */
    interface AlreadyApprovedAuthorizationResult {
      method: "already_approved";
      /** Current allowance (human-readable) */
      allowance: string;
      /** Current allowance as BigInt string */
      allowanceAsBigInt: string;
    }
    ```
  </Tab>
</Tabs>

### Decision Logic

The function follows this priority order:

1. **Check `forceMethod`** - If specified, use that method
2. **Check permit support** - If token is in permit allowlist, return permit data
3. **Check existing allowance** - If sufficient, return `ALREADY_APPROVED`
4. **Default to approval** - Return approval transaction data

***

## prepareDeposit()

Prepares transaction data for a deposit, automatically using the correct method based on provided parameters.

### Function Signature

```ts theme={null}
async function prepareDeposit(
  params: PrepareDepositParams
): Promise<PrepareDepositResult>;
```

### Parameters

```ts theme={null}
interface PrepareDepositParams {
  /** Yield strategy type (e.g., YieldType.CORE) */
  yieldType: YieldType;

  /** Token contract address to deposit */
  depositAsset: Address;

  /** Amount of assets to deposit as decimal string (e.g., "100.25") */
  depositAmount: string;

  /** Recipient address for vault shares */
  to: Address;

  /** Blockchain network ID */
  chainId: ChainId;

  /** Permit signature (required for permit flow) */
  signature?: Hex;

  /** Permit deadline (required for permit flow) */
  deadline?: bigint;

  /** Optional slippage in basis points (default: 50 = 0.5%) */
  slippage?: number;

  /** Optional partner code for fee attribution */
  partnerCode?: string;
}
```

### Return Type

```ts theme={null}
interface PrepareDepositResult {
  txData: {
    abi: Abi;
    address: Address;
    functionName: string;
    args: unknown[];
    chainId: number;
  };
}
```

***

## Complete Flow Example

```ts theme={null}
import { mainnet } from "viem/chains";
import {
  prepareDepositAuthorization,
  prepareDeposit,
  isPermitAuth,
  isApprovalAuth,
  isAlreadyApprovedAuth,
  YieldType,
} from "@paxoslabs/amplify-sdk";

const params = {
  yieldType: YieldType.CORE,
  depositAsset: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" as `0x${string}`, // USDC
  depositAmount: "1000",
  to: userAddress,
  chainId: mainnet.id,
};

// Step 1: Get authorization method
const auth = await prepareDepositAuthorization(params);

// Step 2: Handle based on method using type guards
if (isPermitAuth(auth)) {
  // Sign permit off-chain
  const signature = await walletClient.signTypedData({
    account: userAddress,
    ...auth.permitData,
  });

  // Prepare and execute deposit with permit
  const prepared = await prepareDeposit({
    ...params,
    signature,
    deadline: BigInt(auth.permitData.message.deadline),
  });

  await walletClient.writeContract(prepared.txData);
} else if (isApprovalAuth(auth)) {
  // Execute approval first
  await walletClient.writeContract(auth.txData);

  // Then deposit
  const prepared = await prepareDeposit(params);
  await walletClient.writeContract(prepared.txData);
} else if (isAlreadyApprovedAuth(auth)) {
  // Deposit directly
  const prepared = await prepareDeposit(params);
  await walletClient.writeContract(prepared.txData);
}
```

## Force Specific Method

Override automatic detection when needed:

```ts theme={null}
// Force permit even if automatic detection would choose approval
const authForcePermit = await prepareDepositAuthorization({
  ...params,
  forceMethod: "permit",
});

// Force approval even for permit-supporting tokens (useful for smart wallets)
const authForceApproval = await prepareDepositAuthorization({
  ...params,
  forceMethod: "approval",
});
```

## Slippage Configuration

```ts theme={null}
const prepared = await prepareDeposit({
  ...params,
  slippage: 100, // 1% instead of default 0.5%
});
```

## Error Handling

| Error Code               | Description                    | Resolution                       |
| ------------------------ | ------------------------------ | -------------------------------- |
| `SDK_NOT_INITIALIZED`    | SDK used before init           | Call `initAmplifySDK()` first    |
| `VAULT_NOT_FOUND`        | No vault matches params        | Verify yieldType, token, chainId |
| `PERMIT_NOT_SUPPORTED`   | Token doesn't support EIP-2612 | Use approval flow instead        |
| `INSUFFICIENT_ALLOWANCE` | Approval amount too low        | Increase approval or use permit  |

## Related

* [Deposits Guide](/v0.3.0/intro/products/earn/developers/guides/deposits) - Complete integration examples
* [DepositAuthMethod](/v0.3.0/intro/products/earn/developers/types/DepositAuthMethod) - Type definitions
* [prepareDepositTxData](./prepareDepositTxData) - Lower-level deposit function
