WNLP

Description:

Multi-signature wallet contract requiring multiple confirmations for transaction execution.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "src/WNLP.sol": {
      "content": "// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {IERC20Metadata as IERC20} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import {NativeLPToken} from "./NativeLPToken.sol";
import {SafeERC20} from "./libraries/SafeERC20.sol";
import {ErrorsLib} from "./libraries/ErrorsLib.sol";
import {ReentrancyGuardTransient} from "./libraries/ReentrancyGuardTransient.sol";

/**
 * @title WNLP - Wrapped NativeLPToken
 * @dev Wrapper contract that converts rebasing NativeLPToken into a static-balance ERC20 token.
 * This makes NLP compatible with protocols that don't support rebasing tokens.
 * Users can wrap/unwrap at any time while preserving accumulated value.
 *
 * @notice For WNLP to function properly, it must be configured on the NLP contract:
 *         1. Set as trustedOperator
 *         2. Set as redeemCooldownExempt
 */
contract WNLP is ERC20, Ownable2Step, ReentrancyGuardTransient {
    using SafeERC20 for IERC20;

    /*//////////////////////////////////////////////////////////////////////////
                                    STATE VARIABLES
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice The Native LPToken address
    NativeLPToken public immutable nlp;

    /// @notice The underlying token of the NativeLPToken
    IERC20 public immutable underlying;

    /// @notice Address that receives early withdrawal fees
    address public earlyWithdrawFeeReceiver;

    /// @notice Mapping of trusted operators who can call depositFor and redeemTo functions
    mapping(address => bool) public trustedOperators;

    /// @notice Tracks the last deposit timestamp to enforce cooldown restrictions
    mapping(address => uint256) public lastDepositTimestamp;

    /*//////////////////////////////////////////////////////////////////////////
                                    EVENTS
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Event emitted when NLP tokens are wrapped into wNLP tokens
    event Wrapped(address indexed user, uint256 nlpAmount, uint256 wNLPAmount);

    /// @notice Event emitted when wNLP tokens are unwrapped back to NLP tokens
    event Unwrapped(address indexed user, uint256 wNLPAmount, uint256 nlpAmount);

    /// @notice Event emitted when underlying tokens are deposited and wrapped into wNLP tokens
    event DepositAndWrapped(
        address indexed from,
        address indexed to,
        address indexed beneficiary,
        uint256 underlyingAmount,
        uint256 wNLPAmount
    );

    /// @notice Event emitted when wNLP tokens are unwrapped and redeemed for underlying tokens
    event UnwrappedAndRedeemed(
        address indexed from,
        address indexed to,
        address indexed beneficiary,
        uint256 wNLPAmount,
        uint256 underlyingAmount,
        uint256 feeAmount
    );

    /// @notice Event emitted when a trusted operator status is updated
    event TrustedOperatorUpdated(address indexed account, bool status);

    /// @notice Event emitted when early withdraw fee is charged
    event EarlyWithdrawFeeCollected(address indexed beneficiary, uint256 feeAmount);

    /// @notice Event emitted when early withdraw fee receiver is updated
    event EarlyWithdrawFeeReceiverUpdated(address indexed oldReceiver, address indexed newReceiver);

    /// @notice Event emitted when deposit timestamp is updated
    event DepositTimestampUpdated(address indexed user, uint256 timestamp);

    constructor(string memory _name, string memory _symbol, NativeLPToken _nlp) ERC20(_name, _symbol) {
        nlp = _nlp;

        underlying = nlp.underlying();
        earlyWithdrawFeeReceiver = msg.sender;
    }

    /*//////////////////////////////////////////////////////////////////////////
                                    WRAP AND UNWRAP
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Wraps NativeLP tokens into wNLP tokens
    /// @param amount Amount of NativeLP tokens to deposit
    /// @return Amount of wNLP tokens received
    function wrap(uint256 amount) external nonReentrant returns (uint256) {
        require(amount > 0, ErrorsLib.ZeroAmount());

        uint256 wNLPAmount = nlp.getSharesByUnderlying(amount);

        IERC20(nlp).safeTransferFrom(msg.sender, address(this), amount);

        _mint(msg.sender, wNLPAmount);

        emit Wrapped(msg.sender, amount, wNLPAmount);

        return wNLPAmount;
    }

    /// @notice Deposit underlying tokens directly into NLP and receive wNLP tokens
    /// @param amount Amount of underlying tokens to deposit
    /// @return Amount of wNLP tokens received
    function depositAndWrap(uint256 amount) external nonReentrant returns (uint256) {
        require(amount > 0, ErrorsLib.ZeroAmount());

        return _depositAndWrap(msg.sender, msg.sender, msg.sender, amount);
    }

    /// @notice Deposit underlying tokens directly into NLP and receive wNLP tokens for a specified recipient
    /// @param to The address to receive the wNLP tokens
    /// @param beneficiary The address of the beneficiary for tracking purposes
    /// @param amount Amount of underlying tokens to deposit
    /// @return Amount of wNLP tokens received
    function depositAndWrapFor(
        address to,
        address beneficiary,
        uint256 amount
    ) external nonReentrant returns (uint256) {
        require(to != address(0), ErrorsLib.ZeroAddress());
        require(amount > 0, ErrorsLib.ZeroAmount());
        require(trustedOperators[msg.sender], ErrorsLib.NotTrustedOperator());

        return _depositAndWrap(msg.sender, to, beneficiary, amount);
    }

    /// @notice Unwraps wNLP tokens back to NativeLP tokens
    /// @param wNLPAmount Amount of wNLP tokens to unwrap
    /// @return Amount of NativeLP tokens received
    function unwrap(uint256 wNLPAmount) external nonReentrant returns (uint256) {
        require(wNLPAmount > 0, ErrorsLib.ZeroAmount());
        require(
            lastDepositTimestamp[msg.sender] + nlp.minRedeemInterval() <= block.timestamp
                || nlp.redeemCooldownExempt(msg.sender),
            ErrorsLib.TransferInCooldown()
        );

        uint256 nlpAmount = nlp.getUnderlyingByShares(wNLPAmount);
        _burn(msg.sender, wNLPAmount);
        IERC20(nlp).safeTransfer(msg.sender, nlpAmount);

        emit Unwrapped(msg.sender, wNLPAmount, nlpAmount);

        return nlpAmount;
    }

    /// @notice Unwrap wNLP tokens and redeem underlying tokens directly
    /// @param wNLPAmount Amount of wNLP tokens to unwrap and redeem
    /// @return Amount of underlying tokens received
    function unwrapAndRedeem(uint256 wNLPAmount) external nonReentrant returns (uint256) {
        require(wNLPAmount > 0, ErrorsLib.ZeroAmount());
        return _unwrapAndRedeem(wNLPAmount, msg.sender, msg.sender);
    }

    /// @notice Unwrap wNLP tokens and redeem underlying tokens to a specified address with beneficiary
    /// @param wNLPAmount Amount of wNLP tokens to unwrap and redeem
    /// @param to Address that will receive the underlying tokens
    /// @param beneficiary Address of the beneficiary for tracking purposes
    /// @return Amount of underlying tokens sent to the recipient
    function unwrapAndRedeemTo(
        uint256 wNLPAmount,
        address to,
        address beneficiary
    ) external nonReentrant returns (uint256) {
        require(wNLPAmount > 0, ErrorsLib.ZeroAmount());
        require(to != address(0), ErrorsLib.ZeroAddress());
        require(beneficiary != address(0), ErrorsLib.ZeroAddress());
        require(trustedOperators[msg.sender], ErrorsLib.NotTrustedOperator());

        return _unwrapAndRedeem(wNLPAmount, to, beneficiary);
    }

    /*//////////////////////////////////////////////////////////////////////////
                                    ADMIN FUNCTIONS
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Sets the trusted operator status for multiple addresses
    /// @param accounts The addresses to update
    /// @param statuses The new operator statuses corresponding to each account
    function setTrustedOperator(address[] calldata accounts, bool[] calldata statuses) external onlyOwner {
        for (uint256 i = 0; i < accounts.length; i++) {
            require(accounts[i] != address(0), ErrorsLib.ZeroAddress());

            trustedOperators[accounts[i]] = statuses[i];
            emit TrustedOperatorUpdated(accounts[i], statuses[i]);
        }
    }

    /// @notice Sets the address that receives early withdrawal fees
    /// @param _earlyWithdrawFeeReceiver The new early withdraw fee receiver address
    function setEarlyWithdrawFeeReceiver(address _earlyWithdrawFeeReceiver) external onlyOwner {
        require(_earlyWithdrawFeeReceiver != address(0), ErrorsLib.ZeroAddress());

        address oldReceiver = earlyWithdrawFeeReceiver;
        earlyWithdrawFeeReceiver = _earlyWithdrawFeeReceiver;

        emit EarlyWithdrawFeeReceiverUpdated(oldReceiver, _earlyWithdrawFeeReceiver);
    }

    /// @notice Update user's last deposit timestamp
    /// @dev This is a emergency function, should only be called when certain protocols
    ///      need to bypass cooldown restrictions in exceptional circumstances.
    /// @param user User address to update
    /// @param timestamp New timestamp for the user
    function updateLastDepositTimestamp(address user, uint256 timestamp) external {
        require(trustedOperators[msg.sender], ErrorsLib.NotTrustedOperator());
        require(timestamp <= block.timestamp, ErrorsLib.InvalidAmount());

        lastDepositTimestamp[user] = timestamp;

        emit DepositTimestampUpdated(user, timestamp);
    }

    /*//////////////////////////////////////////////////////////////////////////
                                    VIEW FUNCTIONS
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Gets the number of decimals for this token
    /// @return The number of decimals, matching the underlying token
    function decimals() public view override returns (uint8) {
        return nlp.decimals();
    }

    /// @notice Calculates the amount of wNLP tokens for a given amount of NLP tokens
    /// @param _nlpAmount Amount of NLP tokens
    /// @return Amount of wNLP tokens
    function getWnlpByNlp(uint256 _nlpAmount) external view returns (uint256) {
        return nlp.getSharesByUnderlying(_nlpAmount);
    }

    /// @notice Calculates the amount of NLP tokens for a given amount of wNLP tokens
    /// @param wNLPAmount Amount of wNLP tokens
    /// @return Amount of NLP tokens
    function getNlpByWnlp(uint256 wNLPAmount) external view returns (uint256) {
        return nlp.getUnderlyingByShares(wNLPAmount);
    }

    /// @notice Returns the amount of NLP tokens per 1 wNLP token
    /// @dev Used to calculate the exchange rate from wNLP to NLP
    /// @return Amount of NLP tokens equivalent to 1 wNLP token
    function nlpPerToken() external view returns (uint256) {
        return nlp.getUnderlyingByShares(10 ** decimals());
    }

    /// @notice Returns the amount of wNLP tokens per 1 NLP token
    /// @dev Used to calculate the exchange rate from NLP to wNLP
    /// @return Amount of wNLP tokens equivalent to 1 NLP token
    function tokensPerNlp() external view returns (uint256) {
        return nlp.getSharesByUnderlying(10 ** decimals());
    }

    /*//////////////////////////////////////////////////////////////////////////
                                INTER FUNCTIONS
    //////////////////////////////////////////////////////////////////////////*/

    function _depositAndWrap(
        address from,
        address to,
        address beneficiary,
        uint256 amount
    ) internal returns (uint256) {
        // Transfer underlying tokens from caller to this contract
        underlying.safeTransferFrom(from, address(this), amount);

        // Approve NLP to spend underlying tokens
        underlying.safeApprove(address(nlp), amount);

        uint256 wNLPAmount = nlp.deposit(amount);

        // Update the beneficiary's deposit timestamp for cooldown tracking
        lastDepositTimestamp[beneficiary] = block.timestamp;

        // Mint wNLP tokens to the specified recipient
        _mint(to, wNLPAmount);

        emit DepositAndWrapped(from, to, beneficiary, amount, wNLPAmount);

        return wNLPAmount;
    }

    function _unwrapAndRedeem(
        uint256 wNLPAmount,
        address to,
        address beneficiary
    ) internal returns (uint256 underlyingAmount) {
        _burn(msg.sender, wNLPAmount);

        // Get the gross underlying amount before fees
        uint256 grossUnderlyingAmount = nlp.getUnderlyingByShares(wNLPAmount);
        underlyingAmount = grossUnderlyingAmount;

        // Calculate fee only when potentially needed
        uint256 fee = 0;
        if (block.timestamp < lastDepositTimestamp[beneficiary] + nlp.minRedeemInterval()) {
            fee = (grossUnderlyingAmount * nlp.earlyWithdrawFeeBips()) / 10_000;
        }

        if (fee > 0) {
            underlyingAmount = grossUnderlyingAmount - fee;

            // Redeem to this contract first for fee collection
            nlp.redeemTo(wNLPAmount, address(this));

            // Transfer fee to earlyWithdrawFeeReceiver
            underlying.safeTransfer(earlyWithdrawFeeReceiver, fee);

            // Transfer remaining amount to the specified address
            underlying.safeTransfer(to, underlyingAmount);

            emit EarlyWithdrawFeeCollected(beneficiary, fee);
        } else {
            underlyingAmount = nlp.redeemTo(wNLPAmount, to);
        }

        emit UnwrappedAndRedeemed(msg.sender, to, beneficiary, wNLPAmount, underlyingAmount, fee);
    }

    /// @notice Override ERC20's _transfer to enforce cooldown periods
    function _transfer(address from, address to, uint256 amount) internal override {
        require(to != address(this), ErrorsLib.TransferToContract());

        // Unlike NLP's transfer validation, wNLP allows transfers if either the sender
        // or the recipient is in the redeemCooldownExempt, providing more flexibility
        require(
            lastDepositTimestamp[from] + nlp.minRedeemInterval() <= block.timestamp || nlp.redeemCooldownExempt(from)
                || nlp.redeemCooldownExempt(to),
            ErrorsLib.TransferInCooldown()
        );

        super._transfer(from, to, amount);
    }
}
"
    },
    "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol": {
      "content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/ERC20.sol)

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * The default value of {decimals} is 18. To change this, you should override
 * this function so it returns a different value.
 *
 * We have followed general OpenZeppelin Contracts guidelines: functions revert
 * instead returning `false` on failure. This behavior is nonetheless
 * conventional and does not conflict with the expectations of ERC20
 * applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20 is Context, IERC20, IERC20Metadata {
    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5.05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the default value returned by this function, unless
     * it's overridden.
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view virtual override returns (uint8) {
        return 18;
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     *
     * Requirements:
     *
     * - `from` and `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     * - the caller must have allowance for ``from``'s tokens of at least
     * `amount`.
     */
    function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        address owner = _msgSender();
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(owner, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `from` to `to`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     */
    function _transfer(address from, address to, uint256 amount) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
            // decrementing then incrementing.
            _balances[to] += amount;
        }

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        unchecked {
            // Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
            _balances[account] += amount;
        }
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
            // Overflow not possible: amount <= accountBalance <= totalSupply.
            _totalSupply -= amount;
        }

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner, address spender, uint256 amount) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    /**
     * @dev Updates `owner` s allowance for `spender` based on spent `amount`.
     *
     * Does not update the allowance amount in case of infinite allowance.
     * Revert if not enough allowance is available.
     *
     * Might emit an {Approval} event.
     */
    function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: insufficient allowance");
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * has been transferred to `to`.
     * - when `from` is zero, `amount` tokens have been minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
}
"
    },
    "lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol": {
      "content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable2Step.sol)

pragma solidity ^0.8.0;

import "./Ownable.sol";

/**
 * @dev Contract module which provides access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2Step is Ownable {
    address private _pendingOwner;

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

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

    /**
     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        _pendingOwner = newOwner;
        emit OwnershipTransferStarted(owner(), newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual override {
        delete _pendingOwner;
        super._transferOwnership(newOwner);
    }

    /**
     * @dev The new owner accepts the ownership transfer.
     */
    function acceptOwnership() public virtual {
        address sender = _msgSender();
        require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
        _transferOwnership(sender);
    }
}
"
    },
    "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);
}
"
    },
    "src/NativeLPToken.sol": {
      "content": "// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC20Metadata as IERC20} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import "./libraries/ConstantsLib.sol";
import {CreditVault} from "./CreditVault.sol";
import {ErrorsLib} from "./libraries/ErrorsLib.sol";
import {ReentrancyGuardTransient} from "./libraries/ReentrancyGuardTransient.sol";

/// @title NativeLPToken - Yield-bearing LP token contract
/// @notice A token contract that represents liquidity provider positions and distributes yield
/// @dev This contract manages LP shares and underlying assets, accruing yield based on protocol revenue
contract NativeLPToken is ERC20, Ownable2Step, ReentrancyGuardTransient {
    using SafeERC20 for IERC20;

    /*//////////////////////////////////////////////////////////////////////////
                                     STATE VARIABLES
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Whether deposit operations are paused
    bool public depositPaused;

    /// @notice Whether redeem operations are paused
    bool public redeemPaused;

    /// @notice The underlying token that this LP token represents
    IERC20 public underlying;

    /// @notice The address of the credit vault contract
    address public creditVault;

    /// @notice The number of decimals for this token, matching the underlying token's decimals
    uint8 private _decimals;

    /// @notice Total amount of underlying assets deposited by LPs
    uint256 public totalUnderlying;

    /// @notice Total number of shares issued
    uint256 public totalShares;

    /// @notice Early withdrawal fee in basis points (1 bip = 0.01%)
    /// @dev Applied to prevent front-running by users who deposit right before yield distribution and immediately redeem after
    uint256 public earlyWithdrawFeeBips;

    /// @notice Accumulated early withdrawal fee
    uint256 public accEarlyWithdrawFee;

    /// @notice Minimum time interval between deposit and redeem (in seconds)
    uint256 public minRedeemInterval;

    /// @notice Minimum amount required for deposits
    uint256 public minDeposit;

    /// @notice Mapping of user addresses to their share balances
    mapping(address => uint256) public shares;

    /// @notice Mapping of trusted operators who can call depositFor and redeemTo functions
    mapping(address => bool) public trustedOperators;

    /// @notice Mapping of user addresses to their last deposit timestamp
    mapping(address => uint256) public lastDepositTimestamp;

    /// @notice Mapping of addresses exempt from redeem cooldown period and early withdrawal fees
    mapping(address => bool) public redeemCooldownExempt;

    /*//////////////////////////////////////////////////////////////////////////
                                        EVENTS
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Event emitted when deposit operation is paused
    event DepositPaused();

    /// @notice Event emitted when deposit operation is unpaused
    event DepositUnpaused();

    /// @notice Event emitted when redeem operation is paused
    event RedeemPaused();

    /// @notice Event emitted when redeem operation is unpaused
    event RedeemUnpaused();

    /// @notice Event emitted when yield is distributed to LP holders
    event YieldDistributed(uint256 yieldAmount);

    /// @notice Event emitted when minimum redeem interval is updated
    event MinRedeemIntervalUpdated(uint256 newInterval);

    /// @notice Event emitted when shares are transferred between addresses
    event TransferShares(address indexed from, address indexed to, uint256 shares);

    /// @notice Event emitted when new shares are minted
    event SharesMinted(address indexed from, address indexed to, uint256 shares, uint256 underlyingAmount);

    /// @notice Event emitted when shares are burned
    event SharesBurned(address indexed from, address indexed to, uint256 shares, uint256 underlyingAmount);

    /// @notice Event emitted when minimum deposit amount is updated
    event MinDepositUpdated(uint256 oldAmount, uint256 newAmount);

    /// @notice Event emitted when early withdraw fee is updated
    event EarlyWithdrawFeeBipsUpdated(uint256 oldFeeBips, uint256 newFeeBips);

    /// @notice Event emitted when a trusted operator status is updated
    event TrustedOperatorUpdated(address indexed account, bool status);

    /// @notice Event emitted when an address's redeem cooldown exemption status is updated
    event RedeemCooldownExemptUpdated(address indexed account, bool status);

    /// @notice Event emitted when early withdrawal fees are withdrawn
    event EarlyWithdrawFeeWithdrawn(address indexed recipient, uint256 amount);

    /*//////////////////////////////////////////////////////////////////////////
                                     CONSTRUCTOR
    //////////////////////////////////////////////////////////////////////////*/

    constructor(
        string memory _name,
        string memory _symbol,
        address _underlying,
        address _creditVault
    ) ERC20(_name, _symbol) {
        underlying = IERC20(_underlying);
        creditVault = _creditVault;

        _decimals = IERC20(address(underlying)).decimals();
    }

    /// @notice Deposit underlying tokens to mint LP tokens
    /// @param amount Amount of underlying tokens to deposit
    /// @return sharesToMint Amount of LP tokens minted
    function deposit(uint256 amount)
        external
        nonReentrant
        whenNotPaused(depositPaused)
        returns (uint256 sharesToMint)
    {
        sharesToMint = _deposit(msg.sender, msg.sender, amount);
    }

    /// @notice Deposit underlying tokens on behalf of another address
    /// @param to The address to mint shares to
    /// @param amount Amount of underlying tokens to deposit
    /// @return sharesToMint Amount of LP tokens minted
    function depositFor(
        address to,
        uint256 amount
    ) external nonReentrant whenNotPaused(depositPaused) returns (uint256 sharesToMint) {
        require(to != address(0), ErrorsLib.ZeroAddress());
        require(trustedOperators[msg.sender], ErrorsLib.NotTrustedOperator());
        sharesToMint = _deposit(msg.sender, to, amount);
    }

    /// @notice Redeem LP tokens for underlying tokens
    /// @param sharesToBurn Amount of LP tokens to burn
    /// @return underlyingAmount Amount of underlying tokens received
    function redeem(uint256 sharesToBurn)
        external
        nonReentrant
        whenNotPaused(redeemPaused)
        returns (uint256 underlyingAmount)
    {
        underlyingAmount = _redeem(sharesToBurn, msg.sender);
    }

    /// @notice Redeem LP tokens and send underlying tokens to a specified address
    /// @param sharesToBurn Amount of LP tokens to burn from caller's balance
    /// @param to Address that will receive the underlying tokens
    /// @return underlyingAmount Amount of underlying tokens sent to the recipient
    function redeemTo(
        uint256 sharesToBurn,
        address to
    ) external nonReentrant whenNotPaused(redeemPaused) returns (uint256 underlyingAmount) {
        require(to != address(0), ErrorsLib.ZeroAddress());
        require(trustedOperators[msg.sender], ErrorsLib.NotTrustedOperator());
        underlyingAmount = _redeem(sharesToBurn, to);
    }

    /// @notice Transfers shares from sender to recipient
    /// @param recipient The address to transfer shares to
    /// @param sharesAmount The number of shares to transfer
    /// @return The amount of underlying tokens the shares represent
    function transferShares(address recipient, uint256 sharesAmount) external returns (uint256) {
        _transferShares(msg.sender, recipient, sharesAmount);
        uint256 tokensAmount = getUnderlyingByShares(sharesAmount);
        _emitTransferEvents(msg.sender, recipient, tokensAmount, sharesAmount);
        return tokensAmount;
    }

    /// @notice Distributes yield to LP token holders
    /// @param yieldAmount Amount of yield to distribute
    /// @dev Can only be called by the credit vault
    function distributeYield(uint256 yieldAmount) external {
        require(totalShares > 0, ErrorsLib.PoolNotInitialized());
        require(yieldAmount > 0, ErrorsLib.NoYieldToDistribute());
        require(msg.sender == creditVault, ErrorsLib.OnlyCreditVault());

        totalUnderlying += yieldAmount;

        emit YieldDistributed(yieldAmount);
    }

    /// @notice Gets the underlying token balance of an account
    /// @param account The address to check the balance for
    /// @return The amount of underlying tokens the account effectively owns
    function balanceOf(address account) public view override returns (uint256) {
        return getUnderlyingByShares(shares[account]);
    }

    /// @notice Gets the total supply of underlying tokens in the pool
    /// @return The total amount of underlying tokens managed by this contract
    function totalSupply() public view override returns (uint256) {
        return totalUnderlying;
    }

    /// @notice Gets the number of shares owned by an account
    /// @param account The address to check shares for
    /// @return The number of shares owned by the account
    function sharesOf(address account) public view returns (uint256) {
        return shares[account];
    }

    /// @notice Calculates the underlying token amount for a given number of shares
    /// @param sharesAmount The number of shares to convert
    /// @return The corresponding amount of underlying tokens
    function getUnderlyingByShares(uint256 sharesAmount) public view returns (uint256) {
        if (totalShares == 0) {
            return sharesAmount;
        }
        return (sharesAmount * totalUnderlying) / totalShares;
    }

    /// @notice Calculates the number of shares for a given amount of underlying tokens
    /// @param underlyingAmount The amount of underlying tokens to convert
    /// @return The corresponding number of shares
    function getSharesByUnderlying(uint256 underlyingAmount) public view returns (uint256) {
        if (totalShares == 0) {
            return underlyingAmount;
        }
        return (underlyingAmount * totalShares) / totalUnderlying;
    }

    /// @notice Gets the current exchange rate between shares and underlying tokens
    /// @return The exchange rate scaled by 1e18 (1:1 = 1e18)
    function exchangeRate() public view returns (uint256) {
        if (totalShares == 0) {
            return 1e18; // Initial exchange rate 1:1
        }
        return (totalUnderlying * 1e18) / totalShares;
    }

    /// @notice Gets the number of decimals for this token
    /// @return The number of decimals, matching the underlying token
    function decimals() public view override returns (uint8) {
        return _decimals;
    }

    /*//////////////////////////////////////////////////////////////////////////
                                    OWNER ONLY OPERATIONS
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Sets the minimum deposit amount
    /// @param _minDeposit New minimum deposit amount
    /// @dev Can only be called by the owner
    function setMinDeposit(uint256 _minDeposit) external onlyOwner {
        uint256 oldAmount = minDeposit;
        minDeposit = _minDeposit;
        emit MinDepositUpdated(oldAmount, _minDeposit);
    }

    /// @notice Sets the minimum time interval required between deposit and redeem
    /// @param _interval New minimum interval in seconds
    /// @dev Can only be called by the owner
    function setMinRedeemInterval(uint256 _interval) external onlyOwner {
        minRedeemInterval = _interval;

        emit MinRedeemIntervalUpdated(_interval);
    }

    /// @notice Sets the early withdrawal fee in basis points (BIPs)
    /// @param _earlyWithdrawFeeBips New early withdrawal fee in BIPs
    /// @dev Can only be called by the owner
    function setEarlyWithdrawFeeBips(uint256 _earlyWithdrawFeeBips) external onlyOwner {
        require(_earlyWithdrawFeeBips <= MAX_EARLY_WITHDRAW_FEE_BIPS, ErrorsLib.InvalidFeeBips());

        uint256 oldFeeBips = earlyWithdrawFeeBips;
        earlyWithdrawFeeBips = _earlyWithdrawFeeBips;

        emit EarlyWithdrawFeeBipsUpdated(oldFeeBips, _earlyWithdrawFeeBips);
    }

    /// @notice Sets the trusted operator status for multiple addresses
    /// @param accounts The addresses to update
    /// @param statuses The new operator statuses corresponding to each account
    function setTrustedOperator(address[] calldata accounts, bool[] calldata statuses) external onlyOwner {
        for (uint256 i = 0; i < accounts.length; i++) {
            require(accounts[i] != address(0), ErrorsLib.ZeroAddress());

            trustedOperators[accounts[i]] = statuses[i];
            emit TrustedOperatorUpdated(accounts[i], statuses[i]);
        }
    }

    /// @notice Sets the redeem cooldown exemption status for multiple addresses
    /// @param accounts The addresses to update
    /// @param statuses The new exemption statuses corresponding to each account
    /// @dev Can only be called by the owner
    function setRedeemCooldownExempt(address[] calldata accounts, bool[] calldata statuses) external onlyOwner {
        for (uint256 i = 0; i < accounts.length; i++) {
            require(accounts[i] != address(0), ErrorsLib.ZeroAddress());

            redeemCooldownExempt[accounts[i]] = statuses[i];
            emit RedeemCooldownExemptUpdated(accounts[i], statuses[i]);
        }
    }

    /// @notice Withdraws accumulated early withdrawal fees
    /// @param recipient The address to receive the withdrawn fees
    function withdrawEarlyFees(address recipient) external onlyOwner {
        require(recipient != address(0), ErrorsLib.ZeroAddress());

        // Transfer fees from credit vault to recipient
        CreditVault(creditVault).pay(recipient, accEarlyWithdrawFee);

        emit EarlyWithdrawFeeWithdrawn(recipient, accEarlyWithdrawFee);

        accEarlyWithdrawFee = 0;
    }

    /*//////////////////////////////////////////////////////////////////////////
                                     PAUSE OPERATIONS
    //////////////////////////////////////////////////////////////////////////*/

    /// @notice Function to pause deposit operation
    function pauseDeposit() external onlyOwner {
        depositPaused = true;
        emit DepositPaused();
    }

    /// @notice Function to unpause deposit operation
    function unpauseDeposit() external onlyOwner {
        depositPaused = false;
        emit DepositUnpaused();
    }

    /// @notice Function to pause redeem operation
    function pauseRedeem() external onlyOwner {
        redeemPaused = true;
        emit RedeemPaused();
    }

    /// @notice Function to unpause redeem operation
    function unpauseRedeem() external onlyOwner {
        redeemPaused = false;
        emit RedeemUnpaused();
    }

    modifier whenNotPaused(bool feature) {
        if (feature) {
            revert ErrorsLib.FeaturePaused();
        }
        _;
    }

    /*//////////////////////////////////////////////////////////////////////////
                                INTERNAL FUNCTIONS
    //////////////////////////////////////////////////////////////////////////*/

    function _deposit(address from, address to, uint256 amount) internal returns (uint256 sharesToMint) {
        require(amount >= minDeposit && amount > 0, ErrorsLib.BelowMinimumDeposit());

        // Transfer underlying to vault
        uint256 balanceBefore = underlying.balanceOf(creditVault);
        underlying.safeTransferFrom(from, creditVault, amount);
        amount = underlying.balanceOf(creditVault) - balanceBefore;

        // Calculate shares to mint
        if (totalShares == 0) {
            // Lock 1000 shares from first depositor to prevent inflation attack
            _mintShares(address(0), MINIMUM_SHARE);
            sharesToMint = amount - MINIMUM_SHARE;
        } else {
            sharesToMint = (amount * totalShares) / totalUnderlying;
        }

        // Mint shares
        _mintShares(to, sharesToMint);

        // Update total underlying
        totalUnderlying += amount;

        lastDepositTimestamp[to] = block.timestamp;

        emit SharesMinted(from, to, sharesToMint, amount);
    }

    function _redeem(uint256 sharesToBurn, address to) internal returns (uint256 underlyingAmount) {
        require(sharesToBurn > 0, ErrorsLib.ZeroAmount());
        require(shares[msg.sender] >= sharesToBurn, ErrorsLib.InsufficientShares());

        // Calculate underlying amount
        uint256 grossUnderlyingAmount = (sharesToBurn * totalUnderlying) / totalShares;
        underlyingAmount = grossUnderlyingAmount;

        if (
            block.timestamp < lastDepositTimestamp[msg.sender] + minRedeemInterval && earlyWithdrawFeeBips > 0
                && !redeemCooldownExempt[msg.sender]
        ) {
            uint256 fee = (underlyingAmount * earlyWithdrawFeeBips) / 10_000;

            accEarlyWithdrawFee += fee;
            underlyingAmount -= fee;
        }

        // Burn shares first
        _burnShares(msg.sender, sharesToBurn);

        // Transfer underlying from vault to msg.sender
        CreditVault(creditVault).pay(to, underlyingAmount);

        // Update total underlying
        totalUnderlying -= grossUnderlyingAmount;

        emit SharesBurned(msg.sender, to, sharesToBurn, grossUnderlyingAmount);
    }

    function _mintShares(address to, uint256 shareAmount) internal {
        require(shareAmount > 0, ErrorsLib.ZeroAmount());

        shares[to] += shareAmount;
        totalShares += shareAmount;
    }

    function _burnShares(address from, uint256 shareAmount) internal {
        require(shareAmount > 0, ErrorsLib.ZeroAmount());
        require(from != address(0), ErrorsLib.ZeroAddress());

        shares[from] -= shareAmount;
        totalShares -= shareAmount;
    }

    /// @notice Override ERC20's _transfer to handle yield-bearing LP token transfers
    /// @dev Since this is a yield-bearing token, the actual transfer is done by transferring shares
    ///      rather than token amounts directly. The shares represent the user's proportion of the
    ///      total underlying assets including yield.
    /// @param from The address to transfer from
    /// @param to The address to transfer to
    /// @param amount The underlying token amount to transfer
    function _transfer(address from, address to, uint256 amount) internal override {
        uint256 sharesToTransfer = getSharesByUnderlying(amount);
        _transferShares(from, to, sharesToTransfer);
        _emitTransferEvents(from, to, amount, sharesToTransfer);
    }

    function _transferShares(address from, address to, uint256 _shares) internal {
        require(from != address(0) && to != address(0), ErrorsLib.ZeroAddress());
        require(from != to, ErrorsLib.TransferSelf());
        require(to != address(this), ErrorsLib.TransferToContract());

        _validateTransferCooldown(from);

        shares[from] -= _shares;
        shares[to] += _shares;
    }

    function _validateTransferCooldown(address user) internal view {
        // During cooldown period, user can't transfer shares, but can still redeem
        require(
            lastDepositTimestamp[user] + minRedeemInterval <= block.timestamp || redeemCooldownExempt[user],
            ErrorsLib.TransferInCooldown()
        );
    }

    function _emitTransferEvents(address from, address to, uint256 tokenAmount, uint256 sharesAmount) internal {
        emit Transfer(from, to, tokenAmount);
        emit TransferShares(from, to, sharesAmount);
    }
}
"
    },
    "src/libraries/SafeERC20.sol": {
      "content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.28;

import "openzeppelin/token/ERC20/IERC20.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC-20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    /**
     * @dev An operation with an ERC-20 token failed.
     */
    error SafeERC20FailedOperation(address token);

    /**
     * @dev Indicates a failed `decreaseAllowance` request.
     */
    error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);

    /**
     * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
    }

    /**
     * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
     * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
     */
    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
    }

    /**
     * @dev Variant of {safeTransfer} that returns a bool instead of reverting if the operation is not successful.
     */
    function trySafeTransfer(IERC20 token, address to, uint256 value) internal returns (bool) {
        return _callOptionalReturnBool(token, abi.encodeCall(token.transfer, (to, value)));
    }

    /**
     * @dev Variant of {safeTransferFrom} that returns a bool instead of reverting if the operation is not successful.
     */
    function trySafeTransferFrom(IERC20 token, address from, address to, uint256 value) internal returns (bool) {
        return _callOptionalReturnBool(token, abi.encodeCall(token.transferFrom, (from, to, value)));
    }

    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        forceApprove(token, spender, value);
    }

    /**
     * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     *
     * IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
     * smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
     * this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
     * that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
     */
    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 oldAllowance = token.allowance(address(this), spender);
        forceApprove(token, spender, oldAllowance + value);
    }

    /**
     * @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
     * value, non-reverting calls are assumed to be successful.
     *
     * IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
     * smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
     * this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
     * that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
     */
    function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
        unchecked {
            uint256 currentAllowance = token.allowance(address(this), spender);
            if (currentAllowance < requestedDecrease) {
                revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
            }
            forceApprove(token, spender, currentAllowance - requestedDecrease);
        }
    }

    /**
     * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
     * to be set to zero before setting it to a non-zero value, such as USDT.
     *
     * NOTE: If the token implements ERC-7674, this function will not modify any temporary allowance. This function
     * only sets the "standard" allowance. Any temporary allowance will remain active, in addition to the value being
     * set here.
     */
    function forceApprove(IERC20 token, address spender, uint256 value) internal {
        bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));

        if (!_callOptionalReturnBool(token, approvalCall)) {
            _callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
            _callOptionalReturn(token, approvalCall);
        }
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturnBool} that reverts if call fails to meet the requirements.
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        uint256 returnSize;
        uint256 returnValue;
        assembly ("memory-safe") {
            let success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
            // bubble errors
            if iszero(success) {
                let ptr := mload(0x40)
                returndatacopy(ptr, 0, returndatasize())
                revert(ptr, returndatasize())
            }
            returnSize := returndatasize()
            returnValue := mload(0)
        }

        if (returnSize == 0 ? address(token).code.length == 0 : returnValue != 1) {
            revert SafeERC20FailedOperation(address(token));
        }
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturn} that silently catches all reverts and returns a bool instead.
     */
    function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
        bool success;
        uint256 returnSize;
        uint256 returnValue;
        assembly ("memory-safe") {
            success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
            returnSize := returndatasize()
            returnValue := mload(0)
        }
        return success && (returnSize == 0 ? address(token).code.length > 0 : returnValue == 1);
    }
}
"
    },
    "src/libraries/ErrorsLib.sol": {
      "content": "// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

/// @title Custom error definitions for the protocol
/// @notice This library contains all the error definitions used throughout the contract
/// @dev The errors are arranged in alphabetical order
library ErrorsLib {
    /// @notice Thrown when actual amount deviation exceeds 10%
    error AmountDeviationExceeds();

    /// @notice Thrown when deposit amount is below minimum required
    error BelowMinimumDeposit();

    /// @notice Thrown when epoch update is attempted before minimum interval
    error EpochUpdateInCoolDown();

    /// @notice Thrown when LP token exchange rate increases more than allowed
    error ExchangeRateIncreaseTooMuch();

    /// @notice Thrown when feature is paused
    error FeaturePaused();

    /// @notice Thrown when there are insufficient funding fees to withdraw
    error InsufficientFundingFees();

    /// @notice Thrown when LP token shares are insufficient
    error InsufficientShares();

    /// @notice Thrown when LP token underlying is insufficient
    error InsufficientUnderlying();

    /// @notice Thrown when there is insufficient WETH9 to unwrap
    error InsufficientWETH9();

    /// @notice Thrown when an amount parameter is invalid
    error InvalidAmount();

    /// @notice Thrown when confidence factor E is invalid
    error InvalidConfidenceFactorE();

    /// @notice Thrown when confidence factor N is invalid
    error InvalidConfidenceFactorN();

    /// @notice Thrown when fee rate in basis points exceeds maximum (10000)
    error InvalidFeeBips();

    /// @notice Thrown when LP token address is invalid
    error InvalidLPToken();

    /// @notice Thrown when underlying are not supported in the credit vault
    error InvalidUnderlying();

    /// @notice Thrown when market (LP token) is invalid
    error InvalidMarket();

    /// @notice Thrown when position update amount is invalid
    error InvalidPositionUpdateAmount();

    /// @notice Thrown when the pool address is invalid Native pool
    error InvalidNativePool();

    /// @notice Thrown when signature verification fails
    error InvalidSignature();

    /// @notice Thrown when signer is not authorized
    error InvalidSigner();

    /// @notice Thrown when WETH9 unwrap amount is zero or exceeds balance
    error InvalidWETH9Amount();

    /// @notice Thrown when widget fee rate is invalid
    error InvalidWidgetFeeRate();

    /// @notice Thrown when wrap token is invalid
    error InvalidWrapToken();

    /// @notice Thrown when liquidator and recipient are the same
    error LiquidatorRecipientConflict();

    /// @notice Thrown when nonce is used
    error NonceUsed();

    /// @notice Thrown when output amount is less than minimum required
    error NotEnoughAmountOut(uint256 amountOut, uint256 amountOutMinimum);

    /// @notice Thrown the address is not a trader or liquidator
    error NotTraderOrLiquidator();

    /// @notice Error when caller is not a trusted operator
    error NotTrustedOperator();

    /// @notice Thrown when there is no yield to distribute
    error NoYieldToDistribute();

    /// @notice Thrown when caller is not the credit pool
    error OnlyCreditPool();

    /// @notice Thrown when caller is not the credit vault
    error OnlyCreditVault();

    /// @notice Thrown when caller is not the epoch updater
    error OnlyEpochUpdater();

    /// @notice Thrown when caller is not the fee withdrawer
    error OnlyFeeWithdrawer();

    /// @notice Thrown when caller is not an authorized liquidator
    error OnlyLiquidator();

    /// @notice Thrown when caller is not an LP token
    error OnlyLpToken();

    /// @notice Thrown when caller is not the native router
    error OnlyNativeRouter();

    /// @notice Thrown when caller is not an authorized trader
    error OnlyTrader();

    /// @notice Thrown when caller is not the WETH9 contract
    error OnlyWETH9();

    /// @notice Thrown when arithmetic operation would overflow
    error Overflow();

    /// @notice Thrown when LP pool has no deposits yet
    error PoolNotInitialized();

    /// @notice Thrown when quote has expired
    error QuoteExpired();

    /// @notice Thrown when rebalance limit is exceeded
    error RebalanceLimitExceeded();

    /// @notice Thrown when request has expired
    error RequestExpired();

    /// @notice Thrown when token is already listed
    error TokenAlreadyListed();

    /// @notice Thrown when trader, settler and recipient are the same
    error TraderRecipientConflict();

    /// @notice Thrown when transfer is in cooldown period
    error TransferInCooldown();

    /// @notice Thrown when transfer to self
    error TransferSelf();

    /// @notice Thrown when transfer to current contract
    error TransferToContract();

    /// @notice Thrown when unexpected msg.value is sent
    error UnexpectedMsgValue();

    /// @notice Thrown when zero address is provided
    error ZeroAddress();

    /// @notice Thrown when amount is zero
    error ZeroAmount();

    /// @notice Thrown when input is zero or empty
    error ZeroInput();
}
"
    },
    "src/libraries/ReentrancyGuardTransient.sol": {
      "content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import {TStorage} from "./TStorage.sol";

// Refer from OpenZeppelin https://github.

Tags:
ERC20, Multisig, Swap, Liquidity, Yield, Upgradeable, Multi-Signature, Factory|addr:0x5593ddb6e5a1a0cf71a3e0bc7f0f936a06aa9f0b|verified:true|block:23394827|tx:0xeab5f448b0611817334dde99a6dd9b76ffcb54890bcab6669c93f32d9fa30780|first_check:1758277784

Submitted on: 2025-09-19 12:29:45

Comments

Log in to comment.

No comments yet.