# YToken (Legacy)

> **Note**: This page documents legacy YToken contracts. For v3 contracts, see Vault Contract.

YToken contracts are yield-bearing ERC-4626 vault tokens (yUSD, yBTC, yETH, etc.) from earlier versions.

### Connecting to YToken

#### Using Wagmi

```typescript
import { connectYToken, getContractAddresses, Chain } from "yieldfi-sdk";
import { ethers } from "ethers";
import { useAccount, useWalletClient } from "wagmi";

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

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

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

    const contracts = getContractAddresses(Chain.ETHEREUM);
    const yUSD = connectYToken(
      contracts.yUSD,
      yTokenAbi, // Your YToken ABI
      signer
    );

    return yUSD;
  };
}
```

#### Using Browser Provider

```typescript
import { connectYToken, getContractAddresses, Chain } from "yieldfi-sdk";
import { ethers } from "ethers";

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

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

const contracts = getContractAddresses(Chain.ETHEREUM);

const yUSD = connectYToken(
  contracts.yUSD,
  yTokenAbi, // Your YToken ABI
  signer
);
```

### ERC-20 Operations

```typescript
// Get balance
const balance = await yUSD.balanceOf(userAddress);

// Transfer
await yUSD.transfer(recipient, amount);

// Approve
await yUSD.approve(spender, amount);
```

### ERC-4626 Operations

```typescript
// Deposit assets
const shares = await yUSD.deposit(amount, receiver);

// Redeem shares
await yUSD.redeem(shares, receiver, owner);

// Get conversion rates
const assetsPerShare = await yUSD.convertToAssets(shares);
const sharesPerAsset = await yUSD.convertToShares(amount);
```

### View Methods

```typescript
// Get total assets
const totalAssets = await yUSD.totalAssets();

// Get total supply
const totalSupply = await yUSD.totalSupply();

// Get asset address
const asset = await yUSD.asset();
```

### Migration to V3

For new integrations, use v3 contracts:

* **Manager V3** - See Manager Contract
* **Vault Contract** - See Vault Contract


---

# 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/ytoken-legacy.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.
