# Vault (V3)

The Vault contract is an ERC-4626 compliant vault token contract that represents shares in a YieldFi v3 vault.

### Connecting to Vault

#### Using Wagmi

```typescript
import { connectVault } from "yieldfi-sdk";
import { ethers } from "ethers";
import { useAccount, useWalletClient } from "wagmi";
import VaultABI from "yieldfi-sdk/abis/v3/Vault.json";

function VaultExample() {
  const { address } = useAccount();
  const { data: walletClient } = useWalletClient();

  const connectVaultContract = async (vaultAddress: string) => {
    if (!walletClient) {
      throw new Error("Wallet not connected");
    }

    const provider = new ethers.BrowserProvider(walletClient);
    const signer = await provider.getSigner();

    // For read-only operations, use provider
    const vaultReadOnly = connectVault(vaultAddress, VaultABI, provider);

    // For write operations, use signer
    const vault = connectVault(vaultAddress, VaultABI, signer);

    return { vault, vaultReadOnly };
  };
}
```

#### Using Browser Provider

```typescript
import { connectVault } from "yieldfi-sdk";
import { ethers } from "ethers";
import VaultABI from "yieldfi-sdk/abis/v3/Vault.json";

// For read-only operations
if (!window.ethereum) {
  throw new Error("No wallet found");
}

const provider = new ethers.BrowserProvider(window.ethereum);
const vaultAddress = "0x5bE91d34FeFbB7554497a74e25dC6df96bFef5DB";

// Connect to Vault contract (read-only)
const vault = connectVault(vaultAddress, VaultABI, provider);

// For write operations, get signer
const signer = await provider.getSigner();
const vaultWithSigner = connectVault(vaultAddress, VaultABI, signer);
```

### ERC-20 Operations

#### Get Balance

```typescript
const balance = await vault.balanceOf(userAddress);
console.log(`Shares: ${ethers.formatUnits(balance, 18)}`);
```

#### Transfer Shares

```typescript
await vault.transfer(recipient, shares);
```

#### Approve Spending

```typescript
await vault.approve(spender, shares);
```

#### Get Allowance

```typescript
const allowance = await vault.allowance(owner, spender);
```

### ERC-4626 Operations

#### Deposit Assets

Deposit assets directly to the vault (alternative to using Manager).

```typescript
// Approve asset spending
await assetToken.approve(vault.target, amount);

// Deposit assets
const shares = await vault.deposit(amount, receiver);
console.log(`Received ${ethers.formatUnits(shares, 18)} shares`);
```

#### Redeem Shares

Redeem shares for assets.

```typescript
const assets = await vault.redeem(shares, receiver, owner);
console.log(`Received ${ethers.formatUnits(assets, 6)} assets`);
```

#### Mint Shares

Mint shares by depositing assets.

```typescript
const assets = await vault.mint(shares, receiver);
console.log(`Deposited ${ethers.formatUnits(assets, 6)} assets`);
```

#### Withdraw Assets

Withdraw assets by redeeming shares.

```typescript
const shares = await vault.withdraw(assets, receiver, owner);
console.log(`Redeemed ${ethers.formatUnits(shares, 18)} shares`);
```

### Conversion Functions

#### Convert Shares to Assets

```typescript
const assets = await vault.convertToAssets(shares);
console.log(`Shares convert to: ${ethers.formatUnits(assets, 6)} assets`);
```

#### Convert Assets to Shares

```typescript
const shares = await vault.convertToShares(assets);
console.log(`Assets convert to: ${ethers.formatUnits(shares, 18)} shares`);
```

### Preview Functions

Preview the amount you'll receive before executing operations.

#### Preview Deposit

```typescript
const shares = await vault.previewDeposit(amount);
console.log(`Depositing ${ethers.formatUnits(amount, 6)} will give ${ethers.formatUnits(shares, 18)} shares`);
```

#### Preview Mint

```typescript
const assets = await vault.previewMint(shares);
console.log(`Minting ${ethers.formatUnits(shares, 18)} shares requires ${ethers.formatUnits(assets, 6)} assets`);
```

#### Preview Redeem

```typescript
const assets = await vault.previewRedeem(shares);
console.log(`Redeeming ${ethers.formatUnits(shares, 18)} shares will give ${ethers.formatUnits(assets, 6)} assets`);
```

#### Preview Withdraw

```typescript
const shares = await vault.previewWithdraw(assets);
console.log(`Withdrawing ${ethers.formatUnits(assets, 6)} assets requires redeeming ${ethers.formatUnits(shares, 18)} shares`);
```

### Vault-Specific Functions

#### Get Asset Address

```typescript
const assetAddress = await vault.asset();
console.log(`Vault Asset: ${assetAddress}`);
```

#### Get Total Assets

```typescript
const totalAssets = await vault.totalAssets();
console.log(`Total Assets: ${ethers.formatUnits(totalAssets, 6)}`);
```

#### Get Total Supply

```typescript
const totalSupply = await vault.totalSupply();
console.log(`Total Shares: ${ethers.formatUnits(totalSupply, 18)}`);
```

#### Get Rate

Get the current exchange rate (assets per share).

```typescript
const rate = await vault.getRate();
console.log(`Rate: ${ethers.formatUnits(rate, 18)}`);
```

#### Locked Shares

Get locked shares for a user (shares locked in redemption queue).

```typescript
const locked = await vault.lockedShares(userAddress);
console.log(`Locked Shares: ${ethers.formatUnits(locked, 18)}`);
```

#### Lock Shares (Internal)

Lock shares for redemption (typically called by Manager).

```typescript
// Usually called by Manager contract
await vault.lockShares(userAddress, shares);
```

#### Unlock Shares (Internal)

Unlock shares (typically called by Manager when cancelling redemption).

```typescript
// Usually called by Manager contract
await vault.unlockShares(userAddress, shares);
```

### Vault Information

#### Get Vault Details

```typescript
const name = await vault.name();
const symbol = await vault.symbol();
const decimals = await vault.decimals();
const manager = await vault.manager();

console.log(`Vault: ${name} (${symbol})`);
console.log(`Decimals: ${decimals}`);
console.log(`Manager: ${manager}`);
```

### Complete Example

#### Using Wagmi

```typescript
import { connectVault } from "yieldfi-sdk";
import { ethers } from "ethers";
import { useAccount, useWalletClient } from "wagmi";
import VaultABI from "yieldfi-sdk/abis/v3/Vault.json";

function VaultExample() {
  const { address } = useAccount();
  const { data: walletClient } = useWalletClient();

  const vaultInfo = async (vaultAddress: string) => {
    if (!walletClient || !address) {
      throw new Error("Wallet not connected");
    }

    const provider = new ethers.BrowserProvider(walletClient);
    const vault = connectVault(vaultAddress, VaultABI, provider);

    // Get vault info
    const name = await vault.name();
    const symbol = await vault.symbol();
    const assetAddress = await vault.asset();
    console.log(`Vault: ${name} (${symbol})`);
    console.log(`Asset: ${assetAddress}`);

    // Get balances
    const shares = await vault.balanceOf(address);
    const locked = await vault.lockedShares(address);
    const assets = await vault.convertToAssets(shares);

    console.log(`\nYour Vault Position:`);
    console.log(`  Active Shares: ${ethers.formatUnits(shares, 18)}`);
    console.log(`  Locked Shares: ${ethers.formatUnits(locked, 18)}`);
    console.log(`  Total Assets: ${ethers.formatUnits(assets, 6)}`);

    // Get vault totals
    const totalAssets = await vault.totalAssets();
    const totalSupply = await vault.totalSupply();
    const rate = await vault.getRate();

    console.log(`\nVault Totals:`);
    console.log(`  Total Assets: ${ethers.formatUnits(totalAssets, 6)}`);
    console.log(`  Total Shares: ${ethers.formatUnits(totalSupply, 18)}`);
    console.log(`  Rate: ${ethers.formatUnits(rate, 18)} assets/share`);

    // Preview operations
    const depositAmount = ethers.parseUnits("100", 6);
    const previewShares = await vault.previewDeposit(depositAmount);
    console.log(`\nPreview:`);
    console.log(`  Depositing ${ethers.formatUnits(depositAmount, 6)} assets`);
    console.log(`  Would receive: ${ethers.formatUnits(previewShares, 18)} shares`);
  };

  return (
    <button
      onClick={() =>
        vaultInfo("0x5bE91d34FeFbB7554497a74e25dC6df96bFef5DB")
      }
    >
      Get Vault Info
    </button>
  );
}
```

#### Using Browser Provider

```typescript
import { connectVault } from "yieldfi-sdk";
import { ethers } from "ethers";
import VaultABI from "yieldfi-sdk/abis/v3/Vault.json";

async function vaultExample() {
  if (!window.ethereum) {
    throw new Error("No wallet found");
  }

  const provider = new ethers.BrowserProvider(window.ethereum);
  const signer = await provider.getSigner();
  const userAddress = await signer.getAddress();

  const vaultAddress = "0x5bE91d34FeFbB7554497a74e25dC6df96bFef5DB";
  const vault = connectVault(vaultAddress, VaultABI, provider);

  // Get vault info
  const name = await vault.name();
  const symbol = await vault.symbol();
  const assetAddress = await vault.asset();
  console.log(`Vault: ${name} (${symbol})`);
  console.log(`Asset: ${assetAddress}`);

  // Get balances
  const shares = await vault.balanceOf(userAddress);
  const locked = await vault.lockedShares(userAddress);
  const assets = await vault.convertToAssets(shares);

  console.log(`\nYour Vault Position:`);
  console.log(`  Active Shares: ${ethers.formatUnits(shares, 18)}`);
  console.log(`  Locked Shares: ${ethers.formatUnits(locked, 18)}`);
  console.log(`  Total Assets: ${ethers.formatUnits(assets, 6)}`);

  // Get vault totals
  const totalAssets = await vault.totalAssets();
  const totalSupply = await vault.totalSupply();
  const rate = await vault.getRate();

  console.log(`\nVault Totals:`);
  console.log(`  Total Assets: ${ethers.formatUnits(totalAssets, 6)}`);
  console.log(`  Total Shares: ${ethers.formatUnits(totalSupply, 18)}`);
  console.log(`  Rate: ${ethers.formatUnits(rate, 18)} assets/share`);

  // Preview operations
  const depositAmount = ethers.parseUnits("100", 6);
  const previewShares = await vault.previewDeposit(depositAmount);
  console.log(`\nPreview:`);
  console.log(`  Depositing ${ethers.formatUnits(depositAmount, 6)} assets`);
  console.log(`  Would receive: ${ethers.formatUnits(previewShares, 18)} shares`);
}

vaultExample();
```

### Type Safety

The SDK provides TypeScript types for type-safe contract interactions:

#### Using Wagmi

```typescript
import type { VaultContract } from "yieldfi-sdk";
import { Contract } from "ethers";
import { useWalletClient } from "wagmi";

function TypedVaultExample() {
  const { data: walletClient } = useWalletClient();

  const useTypedVault = async (vaultAddress: string) => {
    if (!walletClient) return;

    const provider = new ethers.BrowserProvider(walletClient);
    const signer = await provider.getSigner();

    const vault = new Contract(
      vaultAddress,
      VaultABI,
      signer
    ) as unknown as VaultContract;

    // Full type safety and IntelliSense
    await vault.balanceOf(/* ... */);
    await vault.convertToAssets(/* ... */);
    await vault.deposit(/* ... */);
  };
}
```

#### Using Browser Provider

```typescript
import type { VaultContract } from "yieldfi-sdk";
import { Contract } from "ethers";

const provider = new ethers.BrowserProvider(window.ethereum);
const vault = new Contract(
  vaultAddress,
  VaultABI,
  provider
) as unknown as VaultContract;

// Full type safety and IntelliSense
await vault.balanceOf(/* ... */);
await vault.convertToAssets(/* ... */);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yield.fi/earn-with-yieldfi/integration-sdk/contracts/vault-v3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
