Yield Bearing Token Adapter

A unified interface for interacting with different types of yield-bearing tokens

Overview

The YieldBearingTokenAdapter is a library that provides a unified interface for interacting with different types of yield-bearing tokens in the YieldShield protocol. It abstracts the complexity of various yield token implementations, allowing the protocol to work seamlessly with different token standards and protocols.

Key Features

  • Multi-Token Support: Handles Mock, ERC4626, and Aave-style tokens
  • Unified Interface: Single function for all yield calculations
  • Time-Based Yield: Calculates yield based on elapsed time
  • Extensible Design: Easy to add support for new token types
  • Gas Efficient: Uses static calls for view operations

Supported Token Types

Type 0: Mock Tokens

  • Purpose: Testing and development
  • Implementation: Custom previewRedeem(uint256, uint64) function
  • Features: Time-based yield calculation with configurable APY
  • Usage: Used in test environments and simulations

Type 1: ERC4626 Tokens

  • Purpose: Standard vault tokens
  • Implementation: Standard previewRedeem(uint256) function
  • Features: Follows EIP-4626 standard for tokenized vaults
  • Usage: Most DeFi vault tokens (Yearn, Compound, etc.)

Type 2: Aave Tokens

  • Purpose: Aave protocol tokens (aTokens)
  • Implementation: getReserveNormalizedIncome(address) on pool contract
  • Features: Real-time yield calculation from Aave protocol
  • Usage: Aave lending protocol tokens

Core Functions

previewRedeem(address token, uint256 assets, uint64 timeElapsed, uint16 tokenType, address vault)

Calculates the current value of assets in a yield-bearing token, accounting for accrued yield over time.

Parameters:

  • token: Address of the yield-bearing token
  • assets: Amount of assets to preview redemption for
  • timeElapsed: Time elapsed since deposit (in days)
  • tokenType: Type of token (0: Mock, 1: ERC4626, 2: Aave)
  • vault: Vault/pool address for Aave tokens (can be address(0) for other types)

Returns:

  • uint256: Amount of assets that would be redeemed (including yield)

Behavior by Token Type:

Mock Tokens (Type 0)

// Calls: token.previewRedeem(uint256, uint64)
// Parameters: assets, timeElapsed
// Returns: assets + calculated yield based on time elapsed

ERC4626 Tokens (Type 1)

// Calls: token.previewRedeem(uint256)
// Parameters: assets
// Returns: Current value of assets in the vault

Aave Tokens (Type 2)

// Calls: vault.getReserveNormalizedIncome(address)
// Parameters: token address
// Returns: Normalized income for the reserve

Implementation Details

Error Handling

The library includes comprehensive error handling:

  • Unsupported Token Type: Reverts with "unsupported token type"
  • Missing Vault Address: Reverts for Aave tokens without vault address
  • Call Failure: Reverts if the external call fails
  • Invalid Data: Reverts if returned data is insufficient

Static Call Usage

All external calls use staticcall to ensure:

  • View-Only: No state changes during execution
  • Gas Efficiency: Lower gas costs for view functions
  • Safety: Prevents accidental state modifications

Data Validation

  • Success Check: Verifies call success before processing
  • Data Length: Ensures returned data is at least 32 bytes
  • Type Safety: Proper ABI decoding of return values

Error Messages

Common Errors

  • "previewRedeem failed: vault address required for Aave tokens"
  • "previewRedeem failed: unsupported token type"
  • "previewRedeem failed: call failed"