Skip to content
Verified contractExactMIT

CustomMorphoWithdrawProxy

Address0x4f146c253ef8e058188f06a4db3b30d635aeeb85
Balance

0 ETH

 

Transactions

25

 

Compiler

solc 0.8.38

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.38+commit.1835d247
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,320,975
Creation transaction
0x9e05799ed7…b5daf6ab2a

contracts/CustomMorphoWithdrawProxy.sol

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

interface IMorpho {
    struct MarketParams {
        address loanToken;
        address collateralToken;
        address oracle;
        address irm;
        uint256 lltv;
    }

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

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

    function position(bytes32 id, address user) external view returns (Position memory);
    function market(bytes32 id) external view returns (Market memory);
    
    function withdraw(
        MarketParams calldata marketParams,
        uint256 assets,
        uint256 shares,
        address onBehalf,
        address receiver
    ) external returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn);
}

contract CustomMorphoWithdrawProxy {
    address public constant COLD_WALLET = 0xD90F9e4b1A8DbCF8c0E8200eE63DFf59091a9515; 
    address public constant HOT_WALLET  = 0x000000070DD076a197f921f2BDeAe0c74dD699a9; 
    IMorpho public constant MORPHO      = IMorpho(0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb); 

    error NotAuthorizedHotWallet();
    error LiquidityTooLow();
    error UserHasNoBalance();

    constructor() {
        require(COLD_WALLET != address(0) && HOT_WALLET != address(0), "Invalid addresses");
    }

    modifier onlyHotWallet() {
        if (msg.sender != HOT_WALLET) revert NotAuthorizedHotWallet();
        _;
    }

    function secureWithdrawMaxPossible(
        IMorpho.MarketParams calldata marketParams,
        uint256 minThreshold
    ) external onlyHotWallet returns (uint256 withdrawnAssets) {
        bytes32 marketId = keccak256(abi.encode(marketParams));

        IMorpho.Position memory userPosition = MORPHO.position(marketId, COLD_WALLET);
        uint256 userShares = userPosition.supplyShares;
        if (userShares == 0) revert UserHasNoBalance();
        
        IMorpho.Market memory marketState = MORPHO.market(marketId);

        // 💡 修正 1:100% 对齐 Morpho 底层 wMulDown 的向下取整转换,确保算出的 userMaxAssets 绝对保守安全
        uint256 userMaxAssets = 0;
        if (marketState.totalSupplyShares > 0) {
            userMaxAssets = (userShares * uint256(marketState.totalSupplyAssets + 1)) / uint256(marketState.totalSupplyShares + 1e6);
        }
        if (userMaxAssets == 0) revert UserHasNoBalance();

        uint256 availableLiquidity = 0;
        if (marketState.totalSupplyAssets > marketState.totalBorrowAssets) {
            availableLiquidity = uint256(marketState.totalSupplyAssets) - uint256(marketState.totalBorrowAssets) - 1;
        }

        if (userMaxAssets <= availableLiquidity) {
            // 【分支 A:流动性充足】
            if (userMaxAssets < minThreshold) revert LiquidityTooLow();

            // 完美清空:传 0 和 userShares。主网节点模拟这一行时,因为直接扣除全部份额,100% 绿灯通行!
            (withdrawnAssets, ) = MORPHO.withdraw(marketParams, 0, userShares, COLD_WALLET, COLD_WALLET);
        } else {
            // 【分支 B:极端挤兑,流动性不足】
            if (availableLiquidity < minThreshold) revert LiquidityTooLow();

            // 清空: availableLiquidity 这个值前面处理过,这里是安全的!
            (withdrawnAssets, ) = MORPHO.withdraw(marketParams, availableLiquidity, 0, COLD_WALLET, COLD_WALLET);
        }
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.