Skip to content
Verified contractExactMIT

SkySavingsAdapter

Address0xa3a4f739873cebcc85f217e6916ebaab3103ccff
Balance

0 ETH

 

Transactions

2

 

Compiler

solc 0.8.20

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.20+commit.a1b79de6
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,089,865
Creation transaction
0xcc7c518ed2…8bc66af8e5

contracts/src/adapters/SkySavingsAdapter.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title SkySavingsAdapter
 * @notice ERC-4626 compatible adapter: accepts USDC, deposits into Sky sUSDS.
 *
 * Deposit flow (3 hops):
 *   USDC  → DAI   (Curve 3pool, hop 1 — ~0.01% slippage)
 *   DAI   → USDS  (DaiUsds migration contract, 1:1 exact, hop 2)
 *   USDS  → sUSDS (ERC-4626 deposit, hop 3)
 *
 * Redeem flow:
 *   sUSDS → USDS  (ERC-4626 redeem, hop 1)
 *   USDS  → DAI   (DaiUsds migration contract, 1:1 exact, hop 2)
 *   DAI   → USDC  (Curve 3pool, hop 3 — ~0.01% slippage)
 *
 * APY source: sUSDS.ssr() — Sky Savings Rate, per-second in RAY (1e27).
 *
 * Chain: Ethereum mainnet only.
 * Protocol ID: 11, InterfaceType: 3 (INTERFACE_ERC4626).
 * Eligible tiers: Conservative, Balanced, Aggressive (riskScore 90).
 * Max allocation: 40% per vault (maxAllocationBps 4000).
 *
 * Addresses (Ethereum mainnet):
 *   USDC:       0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
 *   DAI:        0x6B175474E89094C44Da98b954EedeAC495271d0F
 *   USDS:       0xdC035D45d973E3EC169d2276DDab16f1e407384F
 *   sUSDS:      0xa3931d71877C0E7a3148CB7Eb4463524FEc27fbD
 *   DaiUsds:    0x3225737a9Bbb6473CB4a45b7244ACa2BeFdB276A (1:1 migration)
 *   Curve 3pool:0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7 (DAI idx=0, USDC idx=1)
 */

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

interface IERC4626 {
    function deposit(uint256 assets, address receiver) external returns (uint256 shares);
    function redeem(uint256 shares, address receiver, address owner) external returns (uint256 assets);
    function convertToAssets(uint256 shares) external view returns (uint256);
    function convertToShares(uint256 assets) external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function totalAssets() external view returns (uint256);
    function totalSupply() external view returns (uint256);
    function ssr() external view returns (uint256);   // Sky-specific: per-second rate in RAY
}

// Curve V1 StableSwap (3pool: DAI/USDC/USDT)
interface ICurve3Pool {
    function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy) external returns (uint256);
    function get_dy(int128 i, int128 j, uint256 dx) external view returns (uint256);
}

// Sky DaiUsds migration — 1:1, no fees, no slippage
interface IDaiUsds {
    function daiToUsds(address usr, uint256 wad) external;  // burns wad DAI from caller, mints wad USDS to usr
    function usdsToDai(address usr, uint256 wad) external;  // burns wad USDS from caller, mints wad DAI to usr
}

contract SkySavingsAdapter {
    // ─── Constants ───────────────────────────────────────────────────────────
    address public constant USDC       = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address public constant DAI        = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address public constant USDS       = 0xdC035D45d973E3EC169d2276DDab16f1e407384F;
    address public constant SUSDS      = 0xa3931d71877C0E7a3148CB7Eb4463524FEc27fbD;
    address public constant DAI_USDS   = 0x3225737a9Bbb6473CB4a45b7244ACa2BeFdB276A;
    address public constant CURVE_3POOL = 0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7;

    // Curve 3pool coin indices
    int128 public constant DAI_IDX  = 0;
    int128 public constant USDC_IDX = 1;

    // Sky SSR math constants
    uint256 public constant RAY              = 1e27;
    uint256 public constant SECONDS_PER_YEAR = 31_557_600; // 365.25 days

    // ERC-4626 metadata
    string public constant name     = "StableOpt Sky sUSDS Adapter";
    string public constant symbol   = "so-sUSDS";
    uint8  public constant decimals = 6;  // USDC-denominated shares

    // ─── Errors ───────────────────────────────────────────────────────────────
    error OnlyOwner();
    error ZeroAmount();
    error SlippageTooHigh();
    error InsufficientShares();
    error Reentrancy();
    error InvalidAddress();
    error SlippageOutOfRange();

    // ─── Events ───────────────────────────────────────────────────────────────
    event Deposit(address indexed caller, address indexed receiver, uint256 usdcAssets, uint256 shares);
    event Withdraw(address indexed caller, address indexed receiver, uint256 usdcAssets, uint256 shares);
    event MaxSlippageSet(uint256 bps);

    // ─── State ───────────────────────────────────────────────────────────────
    address public owner;
    uint256 public maxSlippageBps = 30;  // 0.3% per Curve hop; 1:1 hops have no slippage

    // Wrapper share accounting (1:1 with sUSDS shares)
    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;

    uint256 private _locked = 1;

    constructor() {
        owner = msg.sender;

        // Pre-approve all token flows at deploy time (no per-tx approvals needed)
        IERC20(USDC).approve(CURVE_3POOL, type(uint256).max);  // USDC → 3pool
        IERC20(DAI).approve(DAI_USDS, type(uint256).max);      // DAI → DaiUsds
        IERC20(USDS).approve(SUSDS, type(uint256).max);        // USDS → sUSDS
        IERC20(USDS).approve(DAI_USDS, type(uint256).max);     // USDS → DaiUsds
        IERC20(DAI).approve(CURVE_3POOL, type(uint256).max);   // DAI → 3pool
    }

    modifier onlyOwner()    { if (msg.sender != owner) revert OnlyOwner(); _; }
    modifier nonReentrant() { if (_locked == 2) revert Reentrancy(); _locked = 2; _; _locked = 1; }

    // ─── Admin ────────────────────────────────────────────────────────────────

    function setMaxSlippage(uint256 bps) external onlyOwner {
        if (bps > 100) revert SlippageOutOfRange();
        maxSlippageBps = bps;
        emit MaxSlippageSet(bps);
    }

    function transferOwnership(address newOwner) external onlyOwner {
        if (newOwner == address(0)) revert InvalidAddress();
        owner = newOwner;
    }

    // ─── ERC-4626 Interface ───────────────────────────────────────────────────

    function asset() external pure returns (address) { return USDC; }

    /**
     * @notice Deposit USDC. Routes USDC → DAI (Curve) → USDS (DaiUsds) → sUSDS.
     * @param assets    USDC amount (6 decimals).
     * @param receiver  Address to receive wrapper shares.
     * @return shares   Wrapper shares minted (1:1 with sUSDS shares received).
     */
    function deposit(uint256 assets, address receiver)
        external nonReentrant returns (uint256 shares)
    {
        if (assets == 0) revert ZeroAmount();
        IERC20(USDC).transferFrom(msg.sender, address(this), assets);

        // Hop 1: USDC (6 dec) → DAI (18 dec) via Curve 3pool
        uint256 expectedDai = ICurve3Pool(CURVE_3POOL).get_dy(USDC_IDX, DAI_IDX, assets);
        uint256 minDai = expectedDai * (10000 - maxSlippageBps) / 10000;
        uint256 daiReceived = ICurve3Pool(CURVE_3POOL).exchange(USDC_IDX, DAI_IDX, assets, minDai);
        if (daiReceived < minDai) revert SlippageTooHigh();

        // Hop 2: DAI (18 dec) → USDS (18 dec) via DaiUsds migration (1:1, exact)
        IDaiUsds(DAI_USDS).daiToUsds(address(this), daiReceived);
        // After daiToUsds, we have daiReceived USDS (exact 1:1 conversion)

        // Hop 3: USDS (18 dec) → sUSDS via ERC-4626 deposit
        shares = IERC4626(SUSDS).deposit(daiReceived, address(this));
        if (shares == 0) revert SlippageTooHigh();

        // Mint wrapper shares 1:1 with sUSDS shares received
        totalSupply += shares;
        balanceOf[receiver] += shares;

        emit Deposit(msg.sender, receiver, assets, shares);
    }

    /**
     * @notice Redeem wrapper shares for USDC. Routes sUSDS → USDS → DAI (DaiUsds) → USDC (Curve).
     * @param shares    Wrapper shares to redeem.
     * @param receiver  Address to receive USDC.
     * @param owner_    Address whose shares are burned.
     * @return assets   USDC received (6 decimals).
     */
    function redeem(uint256 shares, address receiver, address owner_)
        external nonReentrant returns (uint256 assets)
    {
        if (shares == 0) revert ZeroAmount();
        if (balanceOf[owner_] < shares) revert InsufficientShares();

        balanceOf[owner_] -= shares;
        totalSupply -= shares;

        // Hop 1: sUSDS shares → USDS via ERC-4626 redeem
        uint256 usdsReceived = IERC4626(SUSDS).redeem(shares, address(this), address(this));
        if (usdsReceived == 0) revert SlippageTooHigh();

        // Hop 2: USDS (18 dec) → DAI (18 dec) via DaiUsds migration (1:1, exact)
        IDaiUsds(DAI_USDS).usdsToDai(address(this), usdsReceived);
        uint256 daiReceived = usdsReceived; // 1:1 exact

        // Hop 3: DAI (18 dec) → USDC (6 dec) via Curve 3pool
        uint256 expectedUsdc = ICurve3Pool(CURVE_3POOL).get_dy(DAI_IDX, USDC_IDX, daiReceived);
        uint256 minUsdc = expectedUsdc * (10000 - maxSlippageBps) / 10000;
        assets = ICurve3Pool(CURVE_3POOL).exchange(DAI_IDX, USDC_IDX, daiReceived, minUsdc);
        if (assets < minUsdc) revert SlippageTooHigh();

        IERC20(USDC).transfer(receiver, assets);
        emit Withdraw(msg.sender, receiver, assets, shares);
    }

    // ─── Pricing ──────────────────────────────────────────────────────────────

    /**
     * @notice Convert shares to approximate USDC value.
     *         sUSDS shares → USDS (via exchange rate) → USDC (near 1:1).
     */
    function convertToAssets(uint256 shares) public view returns (uint256) {
        if (totalSupply == 0 || shares == 0) return 0;
        // sUSDS shares → USDS value (18 decimals)
        uint256 usdsValue = IERC4626(SUSDS).convertToAssets(shares);
        // USDS (18 dec) → USDC (6 dec) at near-parity
        return usdsValue / 1e12;
    }

    /**
     * @notice Convert USDC amount to approximate shares.
     */
    function convertToShares(uint256 assets) public view returns (uint256) {
        if (assets == 0) return 0;
        // USDC (6 dec) → USDS (18 dec) near-parity, then to sUSDS shares
        uint256 usdsAmount = assets * 1e12;
        return IERC4626(SUSDS).convertToShares(usdsAmount);
    }

    /**
     * @notice Total USDC-equivalent value held by this adapter.
     *         Engine uses this to compute actual vs target allocation.
     */
    function totalAssets() external view returns (uint256) {
        uint256 susdsBal = IERC4626(SUSDS).balanceOf(address(this));
        return convertToAssets(susdsBal);
    }

    /**
     * @notice Current APY in basis points from Sky Savings Rate.
     *         Formula: (ssr - RAY) * SECONDS_PER_YEAR * 10000 / RAY
     *         Example: ssr=1.00000000114... → ~359 bps → ~3.59% APY
     */
    function getCurrentApyBps() external view returns (uint256) {
        uint256 ssr = IERC4626(SUSDS).ssr();
        if (ssr <= RAY) return 0;
        uint256 excess = ssr - RAY;
        // Overflow-safe: max excess ~2e18, * 3.2e7 * 1e4 = 6.4e29 < uint256.max
        return excess * SECONDS_PER_YEAR * 10000 / RAY;
    }

    function previewDeposit(uint256 assets) external view returns (uint256) {
        return convertToShares(assets);
    }

    function previewRedeem(uint256 shares) external view returns (uint256) {
        return convertToAssets(shares);
    }

    // ─── Admin rescue ─────────────────────────────────────────────────────────

    function rescueToken(address token, address to, uint256 amount) external onlyOwner {
        IERC20(token).transfer(to, amount);
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.