MorphoReader

Description:

Decentralized Finance (DeFi) protocol contract providing Factory, Oracle functionality.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "contracts/utils/external/MorphoReader.sol": {
      "content": "// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

contract MorphoReader {
	struct Vars {
		bool success;
	    bytes execData;
	    bytes32 mid;
	}

	function getPositionValueInDebt(address account, address morpho, address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) external view returns (uint256) {
		Vars memory vars;

		vars.mid = keccak256(
			abi.encode(loanToken, collateralToken, oracle, irm, lltv)
		);


		(vars.success, vars.execData) = morpho.staticcall(
			abi.encodeWithSignature(
				"position(bytes32,address)",
				vars.mid,
				account
			)
		);
		require(vars.success == true, "fail position call");
		(, uint128 borrowShares, uint128 collateralAmount) = abi.decode(vars.execData, (uint256, uint128, uint128));

		(vars.success, vars.execData) = morpho.staticcall(
			abi.encodeWithSignature(
				"market(bytes32)",
				vars.mid
			)
		);
		require(vars.success == true, "fail market call");
		(,,uint256 totalBorrowAssets,uint256 totalBorrowShares,,) = abi.decode(vars.execData, (uint128, uint128, uint128, uint128, uint128, uint128));


		(vars.success, vars.execData) = oracle.staticcall(
			abi.encodeWithSignature(
				"price()"
			)
		);
		require(vars.success == true, "fail price call");
		uint256 price = abi.decode(vars.execData,(uint256));

		uint256 ctdeciamls = IERC20Metadata(collateralToken).decimals();
		uint256 debtValueInLoanToken = (uint256(borrowShares) * totalBorrowAssets) / totalBorrowShares;
		uint256 collateralValueInLoanToken = (uint256(collateralAmount) *  price) / (10** ctdeciamls) / (10** (36 - ctdeciamls)) ;

		return collateralValueInLoanToken - debtValueInLoanToken;
	}


	function getCollateralValueInDebt(address account, address morpho, address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) external view returns (uint256) {
		Vars memory vars;

		vars.mid = keccak256(
			abi.encode(loanToken, collateralToken, oracle, irm, lltv)
		);


		(vars.success, vars.execData) = morpho.staticcall(
			abi.encodeWithSignature(
				"position(bytes32,address)",
				vars.mid,
				account
			)
		);
		require(vars.success == true, "fail position call");
		(, , uint128 collateralAmount) = abi.decode(vars.execData, (uint256, uint128, uint128));

		(vars.success, vars.execData) = oracle.staticcall(
			abi.encodeWithSignature(
				"price()"
			)
		);
		require(vars.success == true, "fail price call");
		uint256 price = abi.decode(vars.execData,(uint256));

		uint256 ctdeciamls = IERC20Metadata(collateralToken).decimals();
		uint256 collateralValueInLoanToken = (uint256(collateralAmount) *  price) / (10** ctdeciamls) / (10** (36 - ctdeciamls)) ;

		return collateralValueInLoanToken;
	}

	function getDebtValue(address account, address morpho, address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) external view returns (uint256) {
		Vars memory vars;

		vars.mid = keccak256(
			abi.encode(loanToken, collateralToken, oracle, irm, lltv)
		);


		(vars.success, vars.execData) = morpho.staticcall(
			abi.encodeWithSignature(
				"position(bytes32,address)",
				vars.mid,
				account
			)
		);
		require(vars.success == true, "fail position call");
		(, uint128 borrowShares,) = abi.decode(vars.execData, (uint256, uint128, uint128));

		(vars.success, vars.execData) = morpho.staticcall(
			abi.encodeWithSignature(
				"market(bytes32)",
				vars.mid
			)
		);
		require(vars.success == true, "fail market call");
		(,,uint256 totalBorrowAssets,uint256 totalBorrowShares,,) = abi.decode(vars.execData, (uint128, uint128, uint128, uint128, uint128, uint128));
		uint256 debtValueInLoanToken = (uint256(borrowShares) * totalBorrowAssets) / totalBorrowShares;

		return  debtValueInLoanToken;
	}
}

"
    },
    "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol": {
      "content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}
"
    },
    "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol": {
      "content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
"
    }
  },
  "settings": {
    "remappings": [
      "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
      "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
      "truffle/=lib/truffle/src/",
      "@uniswap/v3-periphery/=lib/v3-periphery/",
      "@uniswap/v3-core/=lib/v3-core/",
      "@uniswap/v2-core/=node_modules/@uniswap/v2-core/",
      "@uniswap/v2-periphery/=node_modules/@uniswap/v2-periphery/",
      "@uniswap/lib/=node_modules/@uniswap/lib/",
      "@aerodrome-finance/slipstream/=lib/slipstream/",
      "@aerodrome-finance/slipstream/contracts/core/=lib/slipstream/contracts/core/",
      "@chainlink/=node_modules/@chainlink/",
      "@ensdomains/=node_modules/@ensdomains/",
      "@eth-optimism/=node_modules/@eth-optimism/",
      "@gnosis.pm/=node_modules/@gnosis.pm/",
      "@nomad-xyz/=lib/slipstream/lib/ExcessivelySafeCall/",
      "@pendle/=node_modules/@pendle/",
      "@solidity-parser/=lib/slipstream/node_modules/solhint/node_modules/@solidity-parser/",
      "ExcessivelySafeCall/=lib/slipstream/lib/ExcessivelySafeCall/src/",
      "base64-sol/=node_modules/base64-sol/",
      "base64/=lib/slipstream/lib/base64/",
      "ds-test/=lib/forge-std/lib/ds-test/src/",
      "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
      "forge-std/=lib/forge-std/src/",
      "hardhat/=node_modules/hardhat/",
      "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
      "openzeppelin-contracts/=lib/openzeppelin-contracts/",
      "openzeppelin-foundry-upgrades/=lib/openzeppelin-foundry-upgrades/src/",
      "openzeppelin/=lib/openzeppelin-contracts/contracts/",
      "safe-core-sdk/=lib/safe-core-sdk/",
      "slipstream/=lib/slipstream/",
      "solidity-lib/=lib/slipstream/lib/solidity-lib/contracts/",
      "solidity-stringutils/=lib/openzeppelin-foundry-upgrades/lib/solidity-stringutils/",
      "v2-core/=lib/v2-core/contracts/",
      "v2-periphery/=lib/v2-periphery/contracts/",
      "v3-core/=lib/v3-core/",
      "v3-periphery/=lib/v3-periphery/contracts/",
      "zodiac-modifier-roles-v1/=lib/zodiac-modifier-roles-v1/",
      "zodiac/=lib/zodiac/contracts/"
    ],
    "optimizer": {
      "enabled": true,
      "runs": 100
    },
    "metadata": {
      "useLiteralContent": false,
      "bytecodeHash": "ipfs",
      "appendCBOR": true
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "evmVersion": "london",
    "viaIR": false,
    "debug": {
      "revertStrings": "default"
    }
  }
}}

Tags:
ERC20, DeFi, Factory, Oracle|addr:0x919a59bce7c073972796e579d6e09788abc6b580|verified:true|block:23617586|tx:0x8e5d34b4a90c2eae5ec38457ec12e23bdd547171363ef10547140d7589422cd0|first_check:1760964776

Submitted on: 2025-10-20 14:52:57

Comments

Log in to comment.

No comments yet.