Skip to content
Verified contractExactBSL

YechoMorphoMarketHelper

Address0x9c335a0dca89b4841be35dea9b622c910b4fbde8
Balance

0 ETH

 

Transactions

4

 

Compiler

solc 0.8.33

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.33+commit.64118f21
Language
solidity
Optimization
No
EVM version
prague
License
BSL
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,078,493
Creation transaction
0x3c475e6c19…6c2e7a7a1d

src/YechoMorphoMarketHelper.sol

// SPDX-License-Identifier: BSL 1.1
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/access/Ownable.sol";

/*
  ██    ██ ███████  ██████ ██   ██  ██████  
   ██  ██  ██      ██      ██   ██ ██    ██ 
    ████   █████   ██      ███████ ██    ██ 
     ██    ██      ██      ██   ██ ██    ██ 
     ██    ███████  ██████ ██   ██  ██████  
  Yecho — Know Your Yield
  Unified Morpho Market Helper

  https://www.yecho.app | https://x.com/YechoApp
  By Lasalad
*/


struct Position {
    uint256 supplyShares;
    uint128 borrowShares;
    uint128 collateral;
}

struct Market {
    uint128 totalSupplyAssets;
    uint128 totalSupplyShares;
    uint128 totalBorrowAssets;
    uint128 totalBorrowShares;
    uint128 lastUpdate;
    uint128 fee;
}

interface IMorpho {
    function position(bytes32 marketId, address user) external view returns (Position memory);
    function market(bytes32 marketId) external view returns (Market memory);
}

interface IERC4626 {
    function convertToAssets(uint256 shares) external view returns (uint256);
}

interface IGetRate {
    function getRate() external view returns (uint256);
}

interface IWstETH {
    function getStETHByWstETH(uint256 wstETHAmount) external view returns (uint256);
}

interface IKHypeStaking {
    function kHYPEToHYPE(uint256 kHYPEAmount) external view returns (uint256);
}

interface IWstHYPE {
    function balancePerShare() external view returns (uint256);
}

interface IPendleOracle {
    function getOracleState(address market, uint32 duration)
        external view returns (bool increaseCardinalityRequired, uint16 cardinality, bool oldestObservationSatisfied);
    function getPtToAssetRate(address market, uint32 duration) external view returns (uint256);
}

interface IPSM {
    function previewSwapExactIn(address tokenIn, address tokenOut, uint256 amountIn) external view returns (uint256);
}

interface IOracle {
    function lastAnswer() external view returns (int256);
}

interface IAccountant {
    function convertFromShares(address token, uint256 shares) external view returns (uint256);
}

interface ISharesToUnderlying {
    function underlyingBalanceFromShares(uint256 shares) external view returns (uint256);
}

interface IExchangeRate {
    function exchangeRate() external view returns (uint256);
}

interface IGetExchangeRate {
    function getExchangeRate() external view returns (uint256);
}

interface ISharePriceProvider {
    function getSharePrice() external view returns (uint256);
}

contract YechoMorphoMarketHelper is Ownable {
    IMorpho public morpho;
    IPendleOracle public pendleOracle;
    uint32 public twap;
    address public susds;
    address public usds;
    address public usdc;
    address public usde;

    event MorphoUpdated(address indexed newMorpho);
    event PendleOracleUpdated(address indexed newOracle);
    event TwapUpdated(uint32 newTwap);
    event SUsdsConfigUpdated(address susds, address usds);

    constructor(
        address _morpho,
        address _pendleOracle,
        uint32 _twap
    ) Ownable(msg.sender) {
        require(_morpho != address(0), "Invalid Morpho");
        morpho = IMorpho(_morpho);
        if (_pendleOracle != address(0)) {
            pendleOracle = IPendleOracle(_pendleOracle);
        }
        twap = _twap;
    }

    function setMorpho(address _morpho) external onlyOwner {
        require(_morpho != address(0), "Invalid Morpho");
        morpho = IMorpho(_morpho);
        emit MorphoUpdated(_morpho);
    }

    function setPendleOracle(address _oracle) external onlyOwner {
        pendleOracle = IPendleOracle(_oracle);
        emit PendleOracleUpdated(_oracle);
    }

    function setTwap(uint32 _twap) external onlyOwner {
        twap = _twap;
        emit TwapUpdated(_twap);
    }

    function setSUsdsConfig(address _susds, address _usds) external onlyOwner {
        susds = _susds;
        usds = _usds;
        emit SUsdsConfigUpdated(_susds, _usds);
    }

    function setUsdcConfig(address _usdc) external onlyOwner {
        usdc = _usdc;
    }

    function setUsdeConfig(address _usde) external onlyOwner {
        usde = _usde;
    }

    function getMorphoLending(address user, bytes32 marketId) external view returns (uint256) {
        uint256 shares = morpho.position(marketId, user).supplyShares;
        if (shares == 0) return 0;
        Market memory mkt = morpho.market(marketId);
        return (shares * (uint256(mkt.totalSupplyAssets) + 1))
               / (uint256(mkt.totalSupplyShares) + 1e6);
    }

    function getMorphoDebt(address user, bytes32 marketId) external view returns (uint256) {
        Position memory pos = morpho.position(marketId, user);
        Market memory mkt = morpho.market(marketId);
        if (mkt.totalBorrowShares == 0) return 0;
        return (uint256(pos.borrowShares) * uint256(mkt.totalBorrowAssets))
               / uint256(mkt.totalBorrowShares);
    }

    function getMorphoCollat(address user, bytes32 marketId) external view returns (uint256) {
        return morpho.position(marketId, user).collateral;
    }

    function getMorphoCollatCvt(address user, bytes32 marketId, address vault) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return IERC4626(vault).convertToAssets(col);
    }

    function getMorphoCollatRate18(address user, bytes32 marketId, address rateSource) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        uint256 rate = IGetRate(rateSource).getRate();
        return (col * rate) / 1e18;
    }

    function getMorphoCollatRate8(address user, bytes32 marketId, address rateSource) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        uint256 rate = IGetRate(rateSource).getRate();
        return (col * rate) / 1e8;
    }

    function getMorphoCollatWst(address user, bytes32 marketId, address wstETH) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return IWstETH(wstETH).getStETHByWstETH(col);
    }

    function getMorphoCollatPT(address user, bytes32 marketId, address pendleMarket) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        (bool needCard, , bool oldestOk) = pendleOracle.getOracleState(pendleMarket, twap);
        if (needCard || !oldestOk) return 0;
        uint256 rate = pendleOracle.getPtToAssetRate(pendleMarket, twap);
        return (col * rate) / 1e18;
    }

    function getMorphoCollatKHype(address user, bytes32 marketId, address stakingManager) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return IKHypeStaking(stakingManager).kHYPEToHYPE(col);
    }

    function getMorphoCollatWstHype(address user, bytes32 marketId, address wstHYPE) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        uint256 bps = IWstHYPE(wstHYPE).balancePerShare();
        return (col * bps) / 1e18;
    }

    function getMorphoCollatPsm(address user, bytes32 marketId, address psm) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return IPSM(psm).previewSwapExactIn(susds, usds, col);
    }

    function getMorphoCollatOracle8(address user, bytes32 marketId, address oracle) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        int256 price = IOracle(oracle).lastAnswer();
        if (price <= 0) return 0;
        return (col * uint256(price)) / 1e8;
    }

    function getMorphoCollatOracle18(address user, bytes32 marketId, address oracle) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        int256 price = IOracle(oracle).lastAnswer();
        if (price <= 0) return 0;
        return (col * uint256(price)) / 1e18;
    }

    function getMorphoCollatAccountantUSDC(address user, bytes32 marketId, address accountant) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return IAccountant(accountant).convertFromShares(usdc, col);
    }

    function getMorphoCollatAccountantUSDe(address user, bytes32 marketId, address accountant) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return IAccountant(accountant).convertFromShares(usde, col); 
    }

    function getMorphoCollatSharesUnderlying(address user, bytes32 marketId, address token) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return ISharesToUnderlying(token).underlyingBalanceFromShares(col);
    }

    function getMorphoCollatSharePrice18(address user, bytes32 marketId, address src) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return (col * ISharePriceProvider(src).getSharePrice()) / 1e18;
    }

    function getMorphoCollatSharePrice6(address user, bytes32 marketId, address src) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return (col * ISharePriceProvider(src).getSharePrice()) / 1e6;
    }

    function getMorphoCollatExchangeRate18(address user, bytes32 marketId, address src) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return (col * IExchangeRate(src).exchangeRate()) / 1e18;
    }

    function getMorphoCollatGetExchangeRate18(address user, bytes32 marketId, address src) external view returns (uint256) {
        uint256 col = morpho.position(marketId, user).collateral;
        if (col == 0) return 0;
        return (col * IGetExchangeRate(src).getExchangeRate()) / 1e18;
    }
}

lib/openzeppelin-contracts/contracts/access/Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

lib/openzeppelin-contracts/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "evmVersion": "prague",
  "remappings": [
    "@openzeppelin/=lib/openzeppelin-contracts/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.