Skip to content
Verified contractPartialBUSL-1.1

PlasmaVaultBase

Address0x80f451017066494de93943c36ce453fb98ae0b29
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.30

 

Same code

2 more contracts

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
BUSL-1.1
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,255,248
Creation transaction
0xd6f9fd4c91…cfcc4bd924
Main copy (most transactions)
0x233074f6…a1a18c7b

79 files

The source code is large: one file is shown at a time. Choose a file in the list.

lib/ipor-fusion/contracts/managers/withdraw/WithdrawManager.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.30;

import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {AccessManagedUpgradeable} from "../access/AccessManagedUpgradeable.sol";
import {WithdrawManagerStorageLib} from "./WithdrawManagerStorageLib.sol";
import {WithdrawRequest} from "./WithdrawManagerStorageLib.sol";
import {ContextClient} from "../context/ContextClient.sol";
import {IPlasmaVaultBase} from "../../interfaces/IPlasmaVaultBase.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

struct WithdrawRequestInfo {
    uint256 shares;
    uint256 endWithdrawWindowTimestamp;
    bool canWithdraw;
    uint256 withdrawWindowInSeconds;
}
/**
 * @title WithdrawManager
 * @notice Manages withdrawal requests and their processing for the IPOR Fusion protocol
 * @dev This contract handles the scheduling and execution of withdrawals with specific time windows
 *
 * Access Control:
 * - TECH_PLASMA_VAULT_ROLE: Required for canWithdrawAndUpdate
 * - ALPHA_ROLE: Required for releaseFunds
 * - ATOMIST_ROLE: Required for updateWithdrawWindow
 * - PUBLIC_ROLE: Can call request, getLastReleaseFundsTimestamp, getWithdrawWindow, and requestInfo
 */
contract WithdrawManager is Initializable, AccessManagedUpgradeable, ContextClient {
    error WithdrawManagerInvalidTimestamp(uint256 timestamp);
    error WithdrawManagerInvalidSharesToRelease(
        uint256 sharesToRelease,
        uint256 shares,
        uint256 plasmaVaultBalanceOfUnallocatedShares
    );
    error WithdrawManagerZeroShares();
    error WithdrawManagerInvalidFee(uint256 fee);

    /// @notice Emitted when an unallocated withdrawal is validated
    /// @param caller Address initiating the withdrawal (PlasmaVault)
    /// @param shares Amount of shares being withdrawn
    /// @param sharesToRelease Current sharesToRelease (reserved for scheduled withdrawals)
    /// @param availableUnallocatedShares Available unallocated shares before this withdrawal
    /// @param feeSharesToBurn Fee shares to be burned (0 if no fee)
    event UnallocatedWithdrawalValidated(
        address indexed caller,
        uint256 shares,
        uint256 sharesToRelease,
        uint256 availableUnallocatedShares,
        uint256 feeSharesToBurn
    );

    /// @notice Constructor that initializes the WithdrawManager with access control
    /// @dev Used when deploying directly without proxy
    /// @param accessManager_ The address of the access control manager that will manage roles and permissions
    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor(address accessManager_) initializer {
        __AccessManaged_init(accessManager_);
    }

    /// @notice Initializes the WithdrawManager with access manager (for cloning)
    /// @param accessManager_ The address of the access control manager
    /// @dev This method is called after cloning to initialize the contract
    function proxyInitialize(address accessManager_) external initializer {
        __AccessManaged_init(accessManager_);
    }

    /**
     * @notice Creates a new withdrawal request
     * @dev Publicly accessible function
     * @param shares_ The amount requested for redeem, amount of shares to redeem
     * @custom:access Public
     */
    function requestShares(uint256 shares_) external {
        if (shares_ == 0) {
            revert WithdrawManagerZeroShares();
        }

        uint256 feeRate = WithdrawManagerStorageLib.getRequestFee();
        if (feeRate > 0) {
            //@dev 1e18 is the precision of the fee rate
            uint256 feeAmount = Math.mulDiv(shares_, feeRate, 1e18);
            WithdrawManagerStorageLib.updateWithdrawRequest(_msgSender(), shares_ - feeAmount);
            IPlasmaVaultBase(getPlasmaVaultAddress()).transferRequestSharesFee(_msgSender(), address(this), feeAmount);
        } else {
            WithdrawManagerStorageLib.updateWithdrawRequest(_msgSender(), shares_);
        }
    }

    /**
     * @notice Checks if the account can withdraw the specified amount from a request
     * @dev Only callable by PlasmaVault contract (TECH_PLASMA_VAULT_ROLE)
     * @param account_ The address of the account to check
     * @param shares_ The amount to check for withdrawal
     * @return bool True if the account can withdraw the specified amount, false otherwise
     * @custom:access TECH_PLASMA_VAULT_ROLE
     */
    function canWithdrawFromRequest(address account_, uint256 shares_) external restricted returns (bool) {
        uint256 releaseFundsTimestamp = WithdrawManagerStorageLib.getLastReleaseFundsTimestamp();
        WithdrawRequest memory request = WithdrawManagerStorageLib.getWithdrawRequest(account_);

        if (
            _canWithdrawFromRequest(
                request.endWithdrawWindowTimestamp,
                WithdrawManagerStorageLib.getWithdrawWindowInSeconds(),
                releaseFundsTimestamp
            ) && request.shares >= shares_
        ) {
            WithdrawManagerStorageLib.decreaseSharesFromWithdrawRequest(account_, shares_);
            WithdrawManagerStorageLib.decreaseSharesToRelease(shares_);
            return true;
        }
        return false;
    }

    /**
     * @notice Validates and calculates withdrawal fee for unallocated balance withdrawals
     * @dev Only callable by PlasmaVault contract (TECH_PLASMA_VAULT_ROLE)
     *
     * Unallocated Balance:
     * - Represents the portion of vault's assets available for immediate withdrawal
     * - Calculated as: vault's total balance - sharesToRelease (shares explicitly released by governance)
     * - Does NOT subtract the sum of all pending withdrawal requests (only sharesToRelease is reserved)
     * - Available for immediate withdrawals without scheduling
     * - Subject to different fee structure than scheduled withdrawals
     * - Can be accessed through standard withdraw/redeem operations
     *
     * Validation Flow:
     * 1. Balance Verification
     *    - Checks PlasmaVault's total underlying token balance
     *    - Subtracts only sharesToRelease (shares explicitly released via releaseFunds)
     *    - Ensures withdrawal amount + sharesToRelease <= total balance
     *    - Prevents double-allocation of released shares
     *
     * 2. Fee Calculation
     *    - Retrieves current withdraw fee rate for unallocated withdrawals
     *    - Calculates fee amount in shares
     *    - Uses WAD precision (18 decimals)
     *    - Returns 0 if no fee configured
     *
     * Security Features:
     * - Role-based access control
     * - Balance sufficiency checks
     * - Share conversion safety
     * - Withdrawal limit enforcement
     * - Protection against over-allocation
     *
     * Integration Points:
     * - PlasmaVault: Main caller and balance source
     * - ERC4626: Share/asset conversion
     * - Storage: Fee rate and sharesToRelease
     * - BurnRequestFeeFuse: Fee burning mechanism
     *
     * Important Notes:
     * - Different from scheduled withdrawal system
     * - Immediate withdrawal pathway
     * - Separate fee structure
     * - Only reserves sharesToRelease, NOT all pending withdrawal requests
     * - Pending requests that have not been released by governance do NOT reduce unallocated balance
     * - Must maintain withdrawal request safety
     * - Critical for vault liquidity management
     *
     * Error Cases:
     * - Insufficient unallocated balance
     * - Invalid share calculations
     * - Unauthorized caller
     * - Balance allocation conflicts
     *
     * @param shares_ Amount of shares attempting to withdraw from unallocated balance
     * @return feeSharesToBurn Amount of shares to be burned as fee (0 if no fee)
     * @custom:access TECH_PLASMA_VAULT_ROLE
     */
    function canWithdrawFromUnallocated(uint256 shares_) external restricted returns (uint256) {
        address plasmaVaultAddress = msg.sender;
        uint256 feeRate = WithdrawManagerStorageLib.getWithdrawFee();
        uint256 balanceOfPlasmaVault = ERC4626(ERC4626(plasmaVaultAddress).asset()).balanceOf(plasmaVaultAddress);
        uint256 plasmaVaultBalanceOfUnallocatedShares = ERC4626(plasmaVaultAddress).convertToShares(
            balanceOfPlasmaVault
        );
        uint256 sharesToRelease = WithdrawManagerStorageLib.getSharesToRelease();

        if (sharesToRelease > 0 && plasmaVaultBalanceOfUnallocatedShares < sharesToRelease + shares_) {
            revert WithdrawManagerInvalidSharesToRelease(
                sharesToRelease,
                shares_,
                plasmaVaultBalanceOfUnallocatedShares
            );
        }

        uint256 feeSharesToBurn;
        if (feeRate > 0) {
            //@dev 1e18 is the precision of the fee rate
            feeSharesToBurn = Math.mulDiv(shares_, feeRate, 1e18);
        }

        emit UnallocatedWithdrawalValidated(
            plasmaVaultAddress,
            shares_,
            sharesToRelease,
            plasmaVaultBalanceOfUnallocatedShares,
            feeSharesToBurn
        );

        return feeSharesToBurn;
    }

    /**
     * @notice Updates the release funds timestamp to allow withdrawals after this point
     * @dev Only callable by accounts with ALPHA_ROLE
     * @param timestamp_ The timestamp to set as the release funds timestamp
     * @param sharesToRelease_ Amount of shares released
     * @dev Reverts if the provided timestamp is in the future
     * @custom:access ALPHA_ROLE
     */
    function releaseFunds(uint256 timestamp_, uint256 sharesToRelease_) external restricted {
        if (timestamp_ < block.timestamp) {
            WithdrawManagerStorageLib.releaseFunds(timestamp_, sharesToRelease_);
        } else {
            revert WithdrawManagerInvalidTimestamp(timestamp_);
        }
    }

    /**
     * @notice Gets the last timestamp when funds were released for withdrawals
     * @dev Publicly accessible function
     * @return uint256 The timestamp of the last funds release
     * @custom:access Public
     */
    function getLastReleaseFundsTimestamp() external view returns (uint256) {
        return WithdrawManagerStorageLib.getLastReleaseFundsTimestamp();
    }

    function getSharesToRelease() external view returns (uint256) {
        return WithdrawManagerStorageLib.getSharesToRelease();
    }

    /**
     * @notice Updates the withdrawal window duration
     * @dev Only callable by accounts with ATOMIST_ROLE
     * @param window_ The new withdrawal window duration in seconds
     * @custom:access ATOMIST_ROLE
     */
    function updateWithdrawWindow(uint256 window_) external restricted {
        WithdrawManagerStorageLib.updateWithdrawWindowLength(window_);
    }

    /**
     * @notice Updates the fee rate for withdrawals from unallocated balance
     * @dev Only callable by accounts with ATOMIST_ROLE
     *
     * Fee System:
     * - Fee rate is specified in WAD (18 decimals)
     * - 1e18 represents 100% fee
     * - Fee is calculated as: amount * feeRate / 1e18
     * - Collected fees are burned through BurnRequestFeeFuse
     *
     * Access Control:
     * - Restricted to ATOMIST_ROLE
     * - Critical protocol parameter
     * - Part of fee management system
     *
     * Integration Points:
     * - Used in canWithdrawFromUnallocated
     * - Affects withdrawal costs
     * - Impacts protocol revenue
     * - Connected to burn mechanism
     *
     * Security Considerations:
     * - Maximum fee rate capped at 100%
     * - State updates through storage library
     * - Event emission for tracking
     * - Access controlled operation
     *
     * Use Cases:
     * - Protocol fee adjustment
     * - Revenue model updates
     * - Market condition responses
     * - Economic parameter tuning
     *
     * @param fee_ The new fee rate in WAD (18 decimals precision, 1e18 = 100%)
     * @custom:access WITHDRAW_MANAGER_WITHDRAW_FEE_ROLE
     */
    function updateWithdrawFee(uint256 fee_) external restricted {
        //@dev 1e18 is the 100% of the fee rate
        if (fee_ > 1e18) {
            revert WithdrawManagerInvalidFee(fee_);
        }
        WithdrawManagerStorageLib.setWithdrawFee(fee_);
    }

    function getWithdrawFee() external view returns (uint256) {
        return WithdrawManagerStorageLib.getWithdrawFee();
    }

    /**
     * @notice Updates the fee rate for withdrawal requests
     * @dev Only callable by accounts with ATOMIST_ROLE
     *
     * Fee System:
     * - Fee rate is specified in WAD (18 decimals)
     * - 1e18 represents 100% fee
     * - Fee is calculated as: shares * feeRate / 1e18
     * - Fees are transferred to WithdrawManager during requestShares
     *
     * Access Control:
     * - Restricted to ATOMIST_ROLE
     * - Critical protocol parameter
     * - Part of request fee management system
     *
     * Integration Points:
     * - Used in requestShares function
     * - Affects request costs
     * - Impacts protocol revenue
     * - Integrates with transferRequestSharesFee
     *
     * Security Considerations:
     * - Maximum fee rate capped at 100%
     * - State updates through storage library
     * - Event emission for tracking
     * - Access controlled operation
     *
     * Use Cases:
     * - Request fee adjustment
     * - Withdrawal request cost management
     * - Protocol revenue optimization
     * - Market condition adaptation
     *
     * Related Components:
     * - WithdrawManagerStorageLib
     * - PlasmaVaultBase (for fee transfers)
     * - BurnRequestFeeFuse (eventual fee burning)
     * - Access control system
     *
     * @param fee_ The new request fee rate in WAD (18 decimals precision, 1e18 = 100%)
     * @custom:access WITHDRAW_MANAGER_REQUEST_FEE_ROLE
     */
    function updateRequestFee(uint256 fee_) external restricted {
        /// @dev 1e18 is the 100% of the fee rate
        if (fee_ > 1e18) {
            revert WithdrawManagerInvalidFee(fee_);
        }
        WithdrawManagerStorageLib.setRequestFee(fee_);
    }

    function getRequestFee() external view returns (uint256) {
        return WithdrawManagerStorageLib.getRequestFee();
    }

    function updatePlasmaVaultAddress(address plasmaVaultAddress_) external restricted {
        WithdrawManagerStorageLib.setPlasmaVaultAddress(plasmaVaultAddress_);
    }

    function getPlasmaVaultAddress() public view returns (address) {
        return WithdrawManagerStorageLib.getPlasmaVaultAddress();
    }

    /**
     * @notice Gets the current withdrawal window duration
     * @dev Publicly accessible function
     * @return uint256 The withdrawal window duration in seconds
     * @custom:access Public
     */
    function getWithdrawWindow() external view returns (uint256) {
        return WithdrawManagerStorageLib.getWithdrawWindowInSeconds();
    }

    /**
     * @notice Gets detailed information about a withdrawal request
     * @dev Publicly accessible function
     * @param account_ The address to get withdrawal request information for
     * @return WithdrawRequestInfo Struct containing withdrawal request details
     * @custom:access Public
     */
    function requestInfo(address account_) external view returns (WithdrawRequestInfo memory) {
        uint256 withdrawWindow = WithdrawManagerStorageLib.getWithdrawWindowInSeconds();
        uint256 releaseFundsTimestamp = WithdrawManagerStorageLib.getLastReleaseFundsTimestamp();
        WithdrawRequest memory request = WithdrawManagerStorageLib.getWithdrawRequest(account_);
        return
            WithdrawRequestInfo({
                shares: request.shares,
                endWithdrawWindowTimestamp: request.endWithdrawWindowTimestamp,
                canWithdraw: _canWithdrawFromRequest(
                    request.endWithdrawWindowTimestamp,
                    withdrawWindow,
                    releaseFundsTimestamp
                ),
                withdrawWindowInSeconds: withdrawWindow
            });
    }

    function _canWithdrawFromRequest(
        uint256 endWithdrawWindowTimestamp_,
        uint256 withdrawWindow_,
        uint256 releaseFundsTimestamp_
    ) private view returns (bool) {
        /// @dev User who never requested a withdrawal can withdraw immediately, but can't withdraw from request
        if (endWithdrawWindowTimestamp_ < withdrawWindow_) {
            return false;
        }

        uint256 requestTimestamp_ = endWithdrawWindowTimestamp_ - withdrawWindow_;

        return
            block.timestamp >= requestTimestamp_ &&
            block.timestamp <= endWithdrawWindowTimestamp_ &&
            requestTimestamp_ < releaseFundsTimestamp_;
    }

    /// @notice Internal function to get the message sender from context
    /// @return The address of the message sender
    function _msgSender() internal view override returns (address) {
        return _getSenderFromContext();
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/=lib/ipor-fusion/node_modules/@openzeppelin/",
    "@chainlink/=lib/ipor-fusion/node_modules/@chainlink/",
    "@uniswap/v4-core/=lib/ipor-fusion/node_modules/@uniswap/v4-core/",
    "eth-gas-reporter/=node_modules/eth-gas-reporter/",
    "ds-test/=lib/forge-std/lib/ds-test/src/",
    "forge-std/=lib/forge-std/src/",
    "@ipor-protocol-deploy/=contracts/",
    "@ipor-fusion/=lib/ipor-fusion/",
    "@ipor-protocol/=lib/ipor-protocol/",
    "scripts/=scripts/",
    "@eth-optimism/=lib/ipor-fusion/node_modules/@eth-optimism/",
    "@ethereum-vault-connector/=lib/ipor-fusion/node_modules/ethereum-vault-connector/",
    "@morpho-org/=lib/ipor-fusion/node_modules/@morpho-org/",
    "@pendle/=lib/ipor-fusion/node_modules/@pendle/",
    "@prb/test/=lib/ipor-fusion/lib/foundry-random/lib/prb-test/src/",
    "@scroll-tech/=lib/ipor-fusion/node_modules/@scroll-tech/",
    "@uniswap/v3-core/=lib/ipor-fusion/node_modules/@uniswap/v3-core/",
    "CramBit/=lib/ipor-fusion/lib/foundry-random/lib/CramBit/",
    "abdk-libraries-solidity/=node_modules/abdk-libraries-solidity/",
    "base64-sol/=lib/ipor-fusion/node_modules/base64-sol/",
    "crambit/=lib/ipor-fusion/lib/foundry-random/lib/CramBit/src/",
    "erc4626-tests/=lib/ipor-fusion/node_modules/@uniswap/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "ethereum-vault-connector/=lib/ipor-fusion/node_modules/ethereum-vault-connector/",
    "foundry-random/=lib/ipor-fusion/lib/foundry-random/src/",
    "hardhat-deploy/=node_modules/hardhat-deploy/",
    "hardhat/=node_modules/hardhat/",
    "ipor-fusion/=lib/ipor-fusion/contracts/",
    "openzeppelin-contracts/=lib/ipor-fusion/node_modules/@uniswap/v4-core/lib/openzeppelin-contracts/",
    "openzeppelin/=lib/ipor-fusion/node_modules/ethereum-vault-connector/lib/openzeppelin-contracts/contracts/",
    "prb-test/=lib/ipor-fusion/lib/foundry-random/lib/prb-test/src/",
    "solidity-bytes-utils/=lib/ipor-fusion/lib/foundry-random/lib/solidity-bytes-utils/contracts/",
    "solmate/=lib/ipor-fusion/node_modules/@uniswap/v4-core/lib/solmate/",
    "src/=lib/ipor-fusion/lib/foundry-random/src/",
    "test/=lib/ipor-fusion/test/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.