TPlusNPortfolio

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/portfolios/TPlusNPortfolio.sol": {
      "content": "//SPDX-License-Identifier: BSD 3-Clause
pragma solidity 0.8.13;

import {EntityBaseTokenTransferor} from "../EntityBaseTokenTransferor.sol";
import {TPlusNAsset} from "./TPlusNAsset.sol";
import {Registry} from "../Registry.sol";
import {Entity} from "../Entity.sol";
import {Portfolio} from "../Portfolio.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {Math} from "../lib/Math.sol";
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";

/// ENUMS
enum ConsolidationOperation {
    Deposit,
    Redeem
}

/// STRUCTS
/**
 * @notice Arguments for constructor. Using a struct to avoid stack too deep error.
 * @param _registry Endaoment registry.
 * @param _receiptAsset Address of the receipt asset. Should normally be a `TPlusNAsset` contract.
 * @param _shareTokenName Name of ERC20 portfolio share token.
 * @param _shareTokenSymbol Symbol of ERC20 portfolio share token.
 * @param _ebtt Address of the EBTT contract.
 * @param _processor Address to automatically route deposit base token to.
 * @param _minDeposit Minimum base token amount allowed for a valid deposit.
 * @param _cap Maximum amount of assets this portfolio can hold.
 * @param _feeTreasury Address of treasury that should receive fees.
 * @param _depositFee Percentage fee as ZOC that should go to treasury on deposit. (100 = 1%).
 * @param _redemptionFee Percentage fee as ZOC that should go to treasury on redemption. (100 = 1%).
 * @param _aumRate Percentage fee per second (as WAD) that should accrue to treasury as AUM fee. (1e16 = 1%).
 */
struct ConstructorArgs {
    Registry registry;
    address receiptAsset;
    string shareTokenName;
    string shareTokenSymbol;
    EntityBaseTokenTransferor ebtt;
    address processor;
    uint256 minDeposit;
    uint256 cap;
    address feeTreasury;
    uint256 depositFee;
    uint256 redemptionFee;
    uint256 aumRate;
}

/**
 * @notice Struct representing a single consolidation for a deposit/purchase or redeem/sale operation.
 * @param operation The type of consolidation operation - deposit/purchase or redeem/sale.
 * @param entity The entity whose pending balance should be consolidated.
 * @param amountBaseToken The amount of base token to consolidate. For purchases, this is the amount of base token used. For sales, this is the amount of base token received.
 * @param amountAssets The amount of assets to consolidate. For purchases, this is the amount of assets purchased. For sales, this is the amount of assets sold.
 */
struct Consolidation {
    ConsolidationOperation operation;
    Entity entity;
    uint256 amountBaseToken;
    uint256 amountAssets;
}

contract TPlusNPortfolio is Portfolio {
    using SafeTransferLib for ERC20;
    using Math for uint256;

    /// STATE

    /// @notice The EBTT contract.
    EntityBaseTokenTransferor public immutable ebtt;
    /// @notice The address to automatically route base tokens from and to on deposits and sales.
    address public processor;
    /// @notice Minimum base token amount that can be deposited.
    uint256 public minDeposit;
    /// @notice Maintenance flag to pause functionality.
    bool public underMaintenance;
    /// @notice Pending purchase balance of base tokens per address.
    mapping(Entity => uint256) public pendingPurchaseBalance;
    /// @notice Pending sale assets per address.
    mapping(Entity => uint256) public pendingSaleAssets;

    /// ERRORS

    /// @notice Emitted when deposit is below minimum.
    error MinDeposit();
    /// @notice Emitted when called under maintenance.
    error UnderMaintenance();
    /// @notice Emitted when informed entity parameter is bad (e.g. missing information, duplicated entities).
    error BadEntityInput();
    /// @notice Emitted when insufficient balances on consolidation.
    error InsufficientBalance();

    /// EVENTS

    /// @notice Emitted when processor is set.
    event ProcessorSet(address newProcessor);
    /// @notice Emitted when minDeposit is set.
    event MinDepositSet(uint256 newMinDeposit);
    /// @notice Emitted when underMaintenance is set.
    event UnderMaintenanceSet(bool newUnderMaintenance);
    /// @notice Emitted when a deposit/purchase consolidation is made.
    event DepositConsolidated(
        Entity indexed entity, uint256 amountBaseToken, uint256 amountAssets, uint256 amountShares
    );
    /// @notice Emitted when a correction mint is made.
    event CorrectionShareMinted(Entity indexed entity, uint256 amountShares);
    /// @notice Emitted when a correction burn is made.
    event CorrectionShareBurned(Entity indexed entity, uint256 amountShares);
    /// @notice Emitted when a redemption/sale consolidation is made.
    event RedeemConsolidated(Entity indexed entity, uint256 amountBaseToken, uint256 amountAssets, uint256 fee);

    /**
     * @param _args Constructor arguments struct.
     * @dev Args are passed in a struct to avoid stack too deep errors.
     * @dev The `true` parameter is to set this portfolio as `async` in the parent `Portfolio` contract. Async portfolios handle share lifecycle differently.
     * @dev EBTT is required to properly process payments back to entites on sale consolidations.
     * @dev While `cap` is not enforced in contract, its value can be utilized by UI to implement the behavior.
     */
    constructor(ConstructorArgs memory _args)
        Portfolio(
            _args.registry,
            _args.receiptAsset,
            _args.shareTokenName,
            _args.shareTokenSymbol,
            true, // Async portfolio, hence setting `_async` to true
            _args.cap,
            _args.feeTreasury,
            _args.depositFee,
            _args.redemptionFee,
            _args.aumRate
        )
    {
        // Approve EBTT to transfer this portfolio's balance for sale consolidations
        ebtt = _args.ebtt;
        baseToken.safeApprove(address(ebtt), type(uint256).max);

        processor = _args.processor;
        emit ProcessorSet(processor);

        minDeposit = _args.minDeposit;
        emit MinDepositSet(minDeposit);
    }

    /**
     * @inheritdoc Portfolio
     */
    function _getAsset(address _receiptAsset) internal pure override returns (address) {
        return _receiptAsset;
    }

    /**
     * @inheritdoc Portfolio
     */
    function convertReceiptAssetsToAssets(uint256 _receiptAssets) public pure override returns (uint256) {
        return _receiptAssets;
    }

    /**
     * @inheritdoc Portfolio
     * @notice T+N portfolios do not enforce cap onchain.
     * @dev Cap is not enforced because async portfolios do not have direct access to the spot price of the asset, hence not being able to determine a cap syncronously to deposits.
     * @dev While `cap` is not enforced in contract, it is settable in the constructor so external logic can read and utilize it.
     */
    function _checkCap() internal pure override {}

    /**
     * @inheritdoc Portfolio
     * @dev As an `async` portfolio, T+N portfolio do not mint shares on `deposit`, rather handling it on consolidations.
     * @dev Deposits smaller than `minDeposit` revert.
     */
    function _deposit(uint256 _amountBaseToken, bytes calldata /* _data */ )
        internal
        override
        returns (uint256, /* shares */ uint256, /* assets */ uint256 /* fee */ )
    {
        // Check if deposit is above minimum
        if (_amountBaseToken < minDeposit) revert MinDeposit();
        // Check if under maintenance
        if (underMaintenance) revert UnderMaintenance();

        // Calculate fee and net amount to be deposited and used for purchase
        // TODO: move deposit fee logic to `Portfolio`, for all portfolios
        (uint256 _amountIn, uint256 _amountFee) = _calculateFee(_amountBaseToken, depositFee);

        // Transfer amount from entity to `processor`
        baseToken.safeTransferFrom(msg.sender, processor, _amountIn);

        // Transfer fee to treasury
        baseToken.safeTransferFrom(msg.sender, feeTreasury, _amountFee);

        // Update pending balance
        unchecked {
            // Unchecked: no realistic amount of base token will overflow
            pendingPurchaseBalance[Entity(payable(msg.sender))] += _amountIn;
        }

        // No acquired shares or assets, and base token fee amount
        return (0, 0, _amountFee);
    }

    /**
     * @inheritdoc Portfolio
     * @notice Returns (0, 0) to signal that no asset and base token are produced on reddem for T+N.
     */
    function _redeem(uint256 _amountShares, bytes calldata /* _data */ )
        internal
        override
        returns (uint256, /* assetsOut */ uint256 /* baseTokenOut */ )
    {
        // Check if under maintenance
        if (underMaintenance) revert UnderMaintenance();

        // Verify how many assets this amount of shares is worth
        // This assumes `takeAUMFees` was already called in the wrapping `Portfolio.redeem` call
        uint256 _amountAssets = convertToAssets(_amountShares);

        // Update pending asset amount to sell
        unchecked {
            // Unchecked: asset total supply is capped at type(uint256).max, so an individual balance will also never overflow
            pendingSaleAssets[Entity(payable(msg.sender))] += _amountAssets;
        }

        // Burn asset to maintain correct supply in portfolio's balance
        // This is important so AUM fees are charged proportional to the asset balance that still
        // belongs to the portfolio. This also implies that once an entity performs a redeem,
        // we won't charge/be entitled to any AUM fees on that portion of the assets.
        TPlusNAsset(receiptAsset).burn(_amountAssets);

        // Return the amount of assets out and 0 base token for a T+N redemption
        return (_amountAssets, 0);
    }

    /**
     * @notice Consolidates pending purchase balances into shares, based on the amount of assets effectively purchased.
     * @param _entity The entity whose pending balance should be consolidated.
     * @param _amountBaseToken The amount of base token to consolidate.
     * @param _amountAssets The amount of assets this amount of base token was capable of purchasing.
     * @dev The value of _amountAssets must be chosen carefully to avoid rounding errors e.g. 1 ether = 1 "real world" asset is a good choice.
     */
    function _consolidateDeposit(Entity _entity, uint256 _amountBaseToken, uint256 _amountAssets) private {
        // Decrease pending balance
        // Checked: we want to revert on underflow
        pendingPurchaseBalance[_entity] -= _amountBaseToken;

        // Mint shares proportional to the amount of assets produced
        // ⚠️ Share calculation must happen before all mints to avoid wrong values
        uint256 _amountShares = convertToShares(_amountAssets);
        _mint(address(_entity), _amountShares);

        // Mint the receipt asset equal to the amount of asset produced, since the portfolio
        // controls the supply of the receipt asset
        TPlusNAsset(receiptAsset).mint(address(this), _amountAssets);

        // Emit
        emit DepositConsolidated(_entity, _amountBaseToken, _amountAssets, _amountShares);
    }

    /**
     * @notice Consolidate pending sale/redeemed assets into base token, based on the amount of assets effectively sold. Transfers base token to entity.
     * @param _entity The entity whose pending asset balance should be consolidated.
     * @param _amountBaseToken The amount of base token the sale of the asset amount was capable of selling for.
     * @param _amountAssets The amount of assets that effectively got sold.
     * @dev Method will revert if portfolio does not have enough base token in its balance to transfer to entity and treasury.
     */
    function _consolidateRedeem(Entity _entity, uint256 _amountBaseToken, uint256 _amountAssets) private {
        // Checked: Desired underflow if larger
        pendingSaleAssets[_entity] -= _amountAssets;

        // Get net and fee values
        (uint256 _amountOut, uint256 _amountFee) = _calculateFee(_amountBaseToken, redemptionFee);

        // Transfer sale-produced base token amount to entity
        // Uses EBTT contract to circumvent any fee or events being triggered incorrectly
        ebtt.transferFromPortfolio(_entity, _amountOut);

        // Transfer fee to treasury
        baseToken.safeTransfer(feeTreasury, _amountFee);

        // Emit
        emit RedeemConsolidated(_entity, _amountOut, _amountAssets, _amountFee);
    }

    /**
     * @notice Consolidate pending balances into shares or base token, based on the amount of assets effectively purchased or sold.
     * @param _consolidations Array of `Consolidation` structs to process.
     */
    function _consolidate(Consolidation[] calldata _consolidations) private {
        for (uint256 i = 0; i < _consolidations.length; ++i) {
            if (_consolidations[i].operation == ConsolidationOperation.Deposit) {
                _consolidateDeposit(
                    _consolidations[i].entity, _consolidations[i].amountBaseToken, _consolidations[i].amountAssets
                );
            } else {
                _consolidateRedeem(
                    _consolidations[i].entity, _consolidations[i].amountBaseToken, _consolidations[i].amountAssets
                );
            }
        }
    }

    /**
     * @notice Perform consolidation while skipping any accrual operations.
     * @param _consolidations Array of `Consolidation` structs to process.
     * @dev Reverts if the contract does not have enough base token to transfer to the entity and treasury on a redeem consolidation.
     */
    function consolidateNoAccrual(Consolidation[] calldata _consolidations) external requiresAuth {
        // AUM fees must be taken whenever the balance of assets changes
        takeAumFees();
        // Consolidate
        _consolidate(_consolidations);
    }

    /**
     * @notice Endaoment role authed method to consolidate pending purchases and sales while distributing accrued assets.
     * @param _consolidations Array of `Consolidation` structs to process.
     * @param _accruedAssets Amount of assets accrued since last consolidation.
     * @dev Reverts if the contract does not have enough base token to transfer to the entity and treasury on a redeem consolidation.
     */
    function consolidateWithAccrual(Consolidation[] calldata _consolidations, uint256 _accruedAssets)
        external
        requiresAuth
    {
        // AUM fees must be taken whenever the balance of assets changes
        takeAumFees();

        // Given how the operational flow of how T+N works, accruals are simply the minting of the underlying asset
        // This *must* be done before any consolidation, to properly reflect the contribution of each existing entity's
        // position to produce the accrued assets
        TPlusNAsset(receiptAsset).mint(address(this), _accruedAssets);

        // Consolidate
        _consolidate(_consolidations);
    }

    /**
     * @notice Endaoment role authed method to mint shares to an entity. Used solely for correcting share balances in case of errors.
     * @param _entity The entity to mint shares to.
     * @param _amount The amount of shares to mint.
     * @dev This method is only callable by Endaoment roles, and used only in case of error corrections.
     */
    function correctionMint(Entity _entity, uint256 _amount) external requiresAuth {
        _mint(address(_entity), _amount);

        emit CorrectionShareMinted(_entity, _amount);
    }

    /**
     * @notice Endaoment role authed method to burn shares from an entity. Used solely for correcting share balances in case of errors.
     * @param _entity The entity to burn shares from.
     * @param _amount The amount of shares to burn.
     * @dev This method is only callable by Endaoment roles, and used only in case of error corrections.
     */
    function correctionBurn(Entity _entity, uint256 _amount) external requiresAuth {
        _burn(address(_entity), _amount);

        emit CorrectionShareBurned(_entity, _amount);
    }

    // @inheritdoc Portfolio
    function _exit(
        uint256,
        /* _amount */
        bytes calldata /* _data */
    ) internal pure override returns (uint256, /* actualAssetsOut */ uint256 /* baseTokenOut */ ) {
        // Noop
        return (0, 0);
    }

    /**
     * @notice Endaoment role authed method to set the processor address.
     * @param _processor Address to automatically route deposit base token to.
     */
    function setProcessor(address _processor) external requiresAuth {
        processor = _processor;
        emit ProcessorSet(_processor);
    }

    /**
     * @notice Role authed method to set the minimum base token amount allowed for a valid deposit.
     * @param _min Minimum base token amount allowed for a valid deposit.
     */
    function setMinDeposit(uint256 _min) external requiresAuth {
        minDeposit = _min;
        emit MinDepositSet(_min);
    }

    /**
     * @notice Role authed method to set the maintenance flag.
     * @param _underMaintenance Maintenance flag to pause functionality.
     */
    function setUnderMaintenance(bool _underMaintenance) external requiresAuth {
        underMaintenance = _underMaintenance;
        emit UnderMaintenanceSet(_underMaintenance);
    }
}
"
    },
    "src/EntityBaseTokenTransferor.sol": {
      "content": "//SPDX-License-Identifier: BSD 3-Clause
pragma solidity >=0.8.0;

import {EndaomentAuth} from "./lib/auth/EndaomentAuth.sol";
import {Registry} from "./Registry.sol";
import {Entity} from "./Entity.sol";
import {Portfolio} from "./Portfolio.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import "solmate/utils/SafeTransferLib.sol";

/**
 * @notice An Endaoment ecosystem contract capabale of transfering base tokens to Endaoment entities, updating their balances without triggering fees or emitting misleading events.
 * @dev This is due to this contract being enabled as a valid entity on the `Registry` and capable of calling `receiveTransfer` on entities.
 */
contract EntityBaseTokenTransferor is EndaomentAuth {
    using SafeTransferLib for ERC20;

    /// STRUCTS
    struct EntityTransfer {
        Entity entity;
        uint256 amount;
    }

    /// STATE
    /// @notice The Endaoment registry contract
    Registry public immutable registry;

    /// ERRORS
    /// @notice Error when the transfer destination isn't a valid and enabled Endaoment entity.
    error InvalidEntity();
    /// @notice Error when the transfer source isn't a valid and enabled Endaoment portfolio.
    error InvalidPortfolio();
    /// @notice Error when a call to another contract fails.
    error CallFailed(bytes response);

    /// EVENTS
    /// @notice Emitted when a transfer is made to an Endaoment entity.
    event TransferredToEntity(address indexed from, Entity indexed entity, uint256 amount);

    /**
     * @param _registry The Endaoment registry contract
     */
    constructor(Registry _registry) {
        __initEndaomentAuth(_registry, "");
        registry = _registry;
    }

    /**
     * Modifier to only allow valid and active Endaoment entities as transfer destinations.
     * @param _entity The attempted entity
     */
    modifier isActiveEntity(Entity _entity) {
        _checkEntity(_entity);
        _;
    }

    /**
     * Modifier to only allow valid and active Endaoment portfolios as callers.
     */
    modifier isActivePortfolioCaller() {
        if (!registry.isActivePortfolio(Portfolio(msg.sender))) revert InvalidPortfolio();
        _;
    }

    /**
     * Check if an entity is valid and active
     * @param _entity The entity to check
     */
    function _checkEntity(Entity _entity) private view {
        if (!registry.isActiveEntity(_entity)) revert InvalidEntity();
    }

    /**
     * Transfer base token from caller and consolidate balance of the Endaoment entity.
     * @param _entity The entity to transfer to.
     * @param _amount The amount to transfer.
     * @notice This functions exists so Endaoment admins can transfer arbitrary amounts of base tokens to entities without triggering fees.
     * @dev Caller must pre `approve` this contract to transfer the desired amount.
     */
    function transferFromCaller(Entity _entity, uint256 _amount) external requiresAuth isActiveEntity(_entity) {
        _transferToEntity(msg.sender, _entity, _amount);
    }

    /**
     * Batch transfer base token from caller and consolidate balance of the receiving Endaoment entities.
     * @param _entityTransfers The entity transfers to make.
     * @notice This functions exists so Endaoment admins can transfer arbitrary amounts of base tokens to entities without triggering fees.
     * @dev Caller must pre `approve` this contract to transfer the desired amount.
     */
    function batchTransferFromCaller(EntityTransfer[] calldata _entityTransfers) external requiresAuth {
        for (uint256 i = 0; i < _entityTransfers.length; ++i) {
            EntityTransfer memory _transfer = _entityTransfers[i];
            _checkEntity(_transfer.entity);
            _transferToEntity(msg.sender, _transfer.entity, _transfer.amount);
        }
    }

    /**
     * Transfer base token from an Endaoment portfolio to an Endaoment entity.
     * @param _entity The entity to transfer to.
     * @param _amount The amount to transfer.
     * @notice This functions exists so Endaoment portfolios can transfer arbitrary amounts of base tokens to entities without triggering fees.
     * An example of this use case is for T+N portfolios and their async nature of transferring base tokens back to entities on sale consolidation.
     * @dev This function is only callable by active Endaoment portfolios.
     * @dev Portfolio caller must pre `approve` this contract to transfer the desired amount.
     */
    function transferFromPortfolio(Entity _entity, uint256 _amount)
        external
        isActivePortfolioCaller
        isActiveEntity(_entity)
    {
        _transferToEntity(msg.sender, _entity, _amount);
    }

    /**
     * Batch transfer base token from an Endaoment portfolio to Endaoment entities.
     * @param _entityTransfers The entity transfers to make.
     * @notice This functions exists so Endaoment portfolios can transfer arbitrary amounts of base tokens to entities without triggering fees.
     * @dev This function is only callable by active Endaoment portfolios.
     * @dev Portfolio caller must pre `approve` this contract to transfer the desired amount.
     */
    function batchTransferFromPortfolio(EntityTransfer[] calldata _entityTransfers) external isActivePortfolioCaller {
        for (uint256 i = 0; i < _entityTransfers.length; ++i) {
            EntityTransfer memory _transfer = _entityTransfers[i];
            _checkEntity(_transfer.entity);
            _transferToEntity(msg.sender, _transfer.entity, _transfer.amount);
        }
    }

    /**
     * Transfer base token to an Endaoment entity.
     * @param _from The address to transfer from.
     * @param _entity The entity to transfer to.
     * @param _amount The amount to transfer.
     */
    function _transferToEntity(address _from, Entity _entity, uint256 _amount) private {
        // Emit event
        emit TransferredToEntity(_from, _entity, _amount);

        // Update entity balance through receiving
        _entity.receiveTransfer(_amount);

        // Transfer to entity, transferring from approved balance from the caller
        registry.baseToken().safeTransferFrom(_from, address(_entity), _amount);
    }

    /**
     * Make arbitrary calls to other contracts as this contract.
     * @param _target The target contract.
     * @param _value The ETH value to send.
     * @param _data The calldata.
     * @return _response The response from the call.
     * @notice This function exists so Endaoment admins can make arbitrary calls to other contracts as this contract, specially if to unlock incorrectly sent assets.
     */
    function callAsContract(address _target, uint256 _value, bytes memory _data)
        external
        payable
        requiresAuth
        returns (bytes memory)
    {
        (bool _success, bytes memory _response) = payable(_target).call{value: _value}(_data);
        if (!_success) revert CallFailed(_response);
        return _response;
    }
}
"
    },
    "src/portfolios/TPlusNAsset.sol": {
      "content": "//SPDX-License-Identifier: BSD 3-Clause
pragma solidity 0.8.13;

import {ERC20} from "solmate/tokens/ERC20.sol";

/// @title TPlusNAsset
/// @notice An ERC20 contract used by TPlusNPortfolio contracts to represent their underlying asset
contract TPlusNAsset is ERC20 {
    /// STATE

    /// @notice Address of the Endaoment Portfolio / Minter.
    address public portfolio;

    /// EVENTS
    event PortfolioSet(address indexed newPortfolio);

    /// ERRORS

    /// @notice Emitted when bad caller on portfolio-only calls.
    error OnlyPortfolio();

    constructor(string memory _name, string memory _symbol, address _portfolio) ERC20(_name, _symbol, 18) {
        portfolio = _portfolio;
        emit PortfolioSet(_portfolio);
    }

    /// MODIFIERS

    /**
     * @notice Make function only callable by the owning portfolio.
     */
    modifier onlyPortfolio() {
        _onlyPortfolio();
        _;
    }

    /**
     * @notice Internal function to check that the caller is the owning portfolio.
     * @dev Added for gas savings.
     */
    function _onlyPortfolio() private view {
        if (msg.sender != portfolio) revert OnlyPortfolio();
    }

    /**
     * @notice Mint assets for a given address.
     * @param _to The address to mint to.
     * @param _amount The amount to mint.
     * @dev Only callable by the owning portfolio.
     */
    function mint(address _to, uint256 _amount) external onlyPortfolio {
        _mint(_to, _amount);
    }

    /**
     * @notice Burn assets from a given address.
     * @param _from The address to burn from.
     * @param _amount The amount to burn.
     * @dev Only callable by the owning portfolio.
     */
    function burn(address _from, uint256 _amount) external onlyPortfolio {
        _burn(_from, _amount);
    }

    /**
     * @notice Burn assets from the caller.
     * @param _amount The amount to burn.
     * @dev Callable by anyone since it burns from the caller's balance.
     */
    function burn(uint256 _amount) external {
        _burn(msg.sender, _amount);
    }

    /**
     * @notice Update the owning portfolio
     * @param _newPortfolio The new portfolio address
     * @notice Should be rarely used but can be in case of a portfolio migration, to be able to use the same asset contract
     * @dev Only callable by the current portfolio
     */
    function setPortfolio(address _newPortfolio) external onlyPortfolio {
        portfolio = _newPortfolio;
        emit PortfolioSet(_newPortfolio);
    }
}
"
    },
    "src/Registry.sol": {
      "content": "//SPDX-License-Identifier: BSD 3-Clause
pragma solidity 0.8.13;

import {Math} from "./lib/Math.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {Auth, Authority} from "./lib/auth/Auth.sol";
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";

import {RegistryAuth} from "./RegistryAuth.sol";
import {Entity} from "./Entity.sol";
import {ISwapWrapper} from "./interfaces/ISwapWrapper.sol";
import {Portfolio} from "./Portfolio.sol";

// --- Errors ---
error Unauthorized();
error UnsupportedSwapper();

/**
 * @notice Registry entity - manages Factory and Entity state info.
 */
contract Registry is RegistryAuth {
    // --- Storage ---

    /// @notice Treasury address can receives fees.
    address public treasury;

    /// @notice Base Token address is the stable coin contract used throughout the system.
    ERC20 public immutable baseToken;

    /// @notice Mapping of approved factory contracts that are allowed to register new Entities.
    mapping(address => bool) public isApprovedFactory;
    /// @notice Mapping of active status of entities.
    mapping(Entity => bool) public isActiveEntity;

    /// @notice Maps entity type to donation fee percentage stored as a zoc, where type(uint32).max represents 0.
    mapping(uint8 => uint32) defaultDonationFee;
    /// @notice Maps specific entity receiver to donation fee percentage stored as a zoc.
    mapping(Entity => uint32) donationFeeReceiverOverride;

    /// @notice Maps entity type to payout fee percentage stored as a zoc, where type(uint32).max represents 0.
    mapping(uint8 => uint32) defaultPayoutFee;
    /// @notice Maps specific entity sender to payout fee percentage stored as a zoc.
    mapping(Entity => uint32) payoutFeeOverride;

    /// @notice Maps sender entity type to receiver entity type to fee percentage as a zoc.
    mapping(uint8 => mapping(uint8 => uint32)) defaultTransferFee;
    /// @notice Maps specific entity sender to receiver entity type to fee percentage as a zoc.
    mapping(Entity => mapping(uint8 => uint32)) transferFeeSenderOverride;
    /// @notice Maps sender entity type to specific entity receiver to fee percentage as a zoc.
    mapping(uint8 => mapping(Entity => uint32)) transferFeeReceiverOverride;
    /// @notice Maps swap wrappers to their enabled/disabled status.

    mapping(ISwapWrapper => bool) public isSwapperSupported;
    /// @notice Maps portfolios to their enabled/disabled status.
    mapping(Portfolio => bool) public isActivePortfolio;

    // --- Events ---

    /// @notice The event emitted when a factory is approved (whitelisted) or has it's approval removed.
    event FactoryApprovalSet(address indexed factory, bool isApproved);

    /// @notice The event emitted when an entity is set active or inactive.
    event EntityStatusSet(address indexed entity, bool isActive);

    /// @notice The event emitted when a swap wrapper is set active or inactive.
    event SwapWrapperStatusSet(address indexed swapWrapper, bool isSupported);

    /// @notice The event emitted when a portfolio is set active or inactive.
    event PortfolioStatusSet(address indexed portfolio, bool isActive);

    /// @notice Emitted when a default donation fee is set for an entity type.
    event DefaultDonationFeeSet(uint8 indexed entityType, uint32 fee);

    /// @notice Emitted when a donation fee override is set for a specific receiving entity.
    event DonationFeeReceiverOverrideSet(address indexed entity, uint32 fee);

    /// @notice Emitted when a default payout fee is set for an entity type.
    event DefaultPayoutFeeSet(uint8 indexed entityType, uint32 fee);

    /// @notice Emitted when a payout fee override is set for a specific sender entity.
    event PayoutFeeOverrideSet(address indexed entity, uint32 fee);

    /// @notice Emitted when a default transfer fee is set for transfers between entity types.
    event DefaultTransferFeeSet(uint8 indexed fromEntityType, uint8 indexed toEntityType, uint32 fee);

    /// @notice Emitted when a transfer fee override is set for transfers from an entity to a specific entityType.
    event TransferFeeSenderOverrideSet(address indexed fromEntity, uint8 indexed toEntityType, uint32 fee);

    /// @notice Emitted when a transfer fee override is set for transfers from an entityType to an entity.
    event TransferFeeReceiverOverrideSet(uint8 indexed fromEntityType, address indexed toEntity, uint32 fee);

    /// @notice Emitted when the registry treasury contract is changed
    event TreasuryChanged(address oldTreasury, address indexed newTreasury);

    /**
     * @notice Modifier for methods that require auth and that the manager cannot access.
     * @dev Overridden from Auth.sol. Reason: use custom error.
     */
    modifier requiresAuth() override {
        if (!isAuthorized(msg.sender, msg.sig)) revert Unauthorized();

        _;
    }

    // --- Constructor ---
    constructor(address _admin, address _treasury, ERC20 _baseToken) RegistryAuth(_admin, Authority(address(this))) {
        treasury = _treasury;
        emit TreasuryChanged(address(0), _treasury);
        baseToken = _baseToken;
    }

    // --- Internal fns ---

    /**
     * @notice Fee parsing to convert the special "type(uint32).max" value to zero, and zero to the "max".
     * @dev After converting, "type(uint32).max" will cause overflow/revert when used as a fee percentage multiplier and zero will mean no fee.
     * @param _value The value to be converted.
     * @return The parsed fee to use.
     */
    function _parseFeeWithFlip(uint32 _value) private pure returns (uint32) {
        if (_value == 0) {
            return type(uint32).max;
        } else if (_value == type(uint32).max) {
            return 0;
        } else {
            return _value;
        }
    }

    // --- External fns ---

    /**
     * @notice Sets a new Endaoment treasury address.
     * @param _newTreasury The new treasury.
     */
    function setTreasury(address _newTreasury) external requiresAuth {
        emit TreasuryChanged(treasury, _newTreasury);
        treasury = _newTreasury;
    }

    /**
     * @notice Sets the approval state of a factory. Grants the factory permissions to set entity status.
     * @param _factory The factory whose approval state is to be updated.
     * @param _isApproved True if the factory should be approved, false otherwise.
     */
    function setFactoryApproval(address _factory, bool _isApproved) external requiresAuth {
        isApprovedFactory[_factory] = _isApproved;
        emit FactoryApprovalSet(address(_factory), _isApproved);
    }

    /**
     * @notice Sets the enable/disable state of an Entity.
     * @param _entity The entity whose active state is to be updated.
     * @param _isActive True if the entity should be active, false otherwise.
     */
    function setEntityStatus(Entity _entity, bool _isActive) external requiresAuth {
        isActiveEntity[_entity] = _isActive;
        emit EntityStatusSet(address(_entity), _isActive);
    }

    /**
     * @notice Sets Entity as active. This is a special method to be called only by approved factories.
     * Other callers should use `setEntityStatus` instead.
     * @param _entity The entity.
     */
    function setEntityActive(Entity _entity) external {
        if (!isApprovedFactory[msg.sender]) revert Unauthorized();
        isActiveEntity[_entity] = true;
        emit EntityStatusSet(address(_entity), true);
    }

    /**
     * @notice Sets the enable/disable state of a Portfolio.
     * @param _portfolio Portfolio.
     * @param _isActive True if setting portfolio to active, false otherwise.
     */
    function setPortfolioStatus(Portfolio _portfolio, bool _isActive) external requiresAuth {
        isActivePortfolio[_portfolio] = _isActive;
        emit PortfolioStatusSet(address(_portfolio), _isActive);
    }

    /**
     * @notice Gets default donation fee pct (as a zoc) for an Entity.
     * @param _entity The receiving entity of the donation for which the fee is being fetched.
     * @return uint32 The default donation fee for the entity's type.
     * @dev Makes use of _parseFeeWithFlip, so if no default exists, "max" will be returned.
     */
    function getDonationFee(Entity _entity) external view returns (uint32) {
        return _parseFeeWithFlip(defaultDonationFee[_entity.entityType()]);
    }

    /**
     * @notice Gets lowest possible donation fee pct (as a zoc) for an Entity, among default and override.
     * @param _entity The receiving entity of the donation for which the fee is being fetched.
     * @return uint32 The minimum of the default donation fee and the receiver's fee override.
     * @dev Makes use of _parseFeeWithFlip, so if no default or override exists, "max" will be returned.
     */
    function getDonationFeeWithOverrides(Entity _entity) external view returns (uint32) {
        uint32 _default = _parseFeeWithFlip(defaultDonationFee[_entity.entityType()]);
        uint32 _receiverOverride = _parseFeeWithFlip(donationFeeReceiverOverride[_entity]);
        return _receiverOverride < _default ? _receiverOverride : _default;
    }

    /**
     * @notice Gets default payout fee pct (as a zoc) for an Entity.
     * @param _entity The sender entity of the payout for which the fee is being fetched.
     * @return uint32 The default payout fee for the entity's type.
     * @dev Makes use of _parseFeeWithFlip, so if no default exists, "max" will be returned.
     */
    function getPayoutFee(Entity _entity) external view returns (uint32) {
        return _parseFeeWithFlip(defaultPayoutFee[_entity.entityType()]);
    }

    /**
     * @notice Gets lowest possible payout fee pct (as a zoc) for an Entity, among default and override.
     * @param _entity The sender entity of the payout for which the fee is being fetched.
     * @return uint32 The minimum of the default payout fee and the sender's fee override.
     * @dev Makes use of _parseFeeWithFlip, so if no default or override exists, "max" will be returned.
     */
    function getPayoutFeeWithOverrides(Entity _entity) external view returns (uint32) {
        uint32 _default = _parseFeeWithFlip(defaultPayoutFee[_entity.entityType()]);
        uint32 _senderOverride = _parseFeeWithFlip(payoutFeeOverride[_entity]);
        return _senderOverride < _default ? _senderOverride : _default;
    }

    /**
     * @notice Gets default transfer fee pct (as a zoc) between sender & receiver Entities.
     * @param _sender The sending entity of the transfer for which the fee is being fetched.
     * @param _receiver The receiving entity of the transfer for which the fee is being fetched.
     * @return uint32 The default transfer fee.
     * @dev Makes use of _parseFeeWithFlip, so if no default exists, "type(uint32).max" will be returned.
     */
    function getTransferFee(Entity _sender, Entity _receiver) external view returns (uint32) {
        return _parseFeeWithFlip(defaultTransferFee[_sender.entityType()][_receiver.entityType()]);
    }

    /**
     * @notice Gets lowest possible transfer fee pct (as a zoc) between sender & receiver Entities, among default and overrides.
     * @param _sender The sending entity of the transfer for which the fee is being fetched.
     * @param _receiver The receiving entity of the transfer for which the fee is being fetched.
     * @return uint32 The minimum of the default transfer fee, and sender and receiver overrides.
     * @dev Makes use of _parseFeeWithFlip, so if no default or overrides exist, "type(uint32).max" will be returned.
     */
    function getTransferFeeWithOverrides(Entity _sender, Entity _receiver) external view returns (uint32) {
        uint32 _default = _parseFeeWithFlip(defaultTransferFee[_sender.entityType()][_receiver.entityType()]);
        uint32 _senderOverride = _parseFeeWithFlip(transferFeeSenderOverride[_sender][_receiver.entityType()]);
        uint32 _receiverOverride = _parseFeeWithFlip(transferFeeReceiverOverride[_sender.entityType()][_receiver]);

        uint32 _lowestFee = _default;
        _lowestFee = _senderOverride < _lowestFee ? _senderOverride : _lowestFee;
        _lowestFee = _receiverOverride < _lowestFee ? _receiverOverride : _lowestFee;
        return _lowestFee;
    }

    /**
     * @notice Sets the default donation fee for an entity type.
     * @param _entityType Entity type.
     * @param _fee The fee percentage to be set (a zoc).
     */
    function setDefaultDonationFee(uint8 _entityType, uint32 _fee) external requiresAuth {
        defaultDonationFee[_entityType] = _parseFeeWithFlip(_fee);
        emit DefaultDonationFeeSet(_entityType, _fee);
    }

    /**
     * @notice Sets the donation fee receiver override for a specific entity.
     * @param _entity Entity.
     * @param _fee The overriding fee (a zoc).
     */
    function setDonationFeeReceiverOverride(Entity _entity, uint32 _fee) external requiresAuth {
        donationFeeReceiverOverride[_entity] = _parseFeeWithFlip(_fee);
        emit DonationFeeReceiverOverrideSet(address(_entity), _fee);
    }

    /**
     * @notice Sets the default payout fee for an entity type.
     * @param _entityType Entity type.
     * @param _fee The fee percentage to be set (a zoc).
     */
    function setDefaultPayoutFee(uint8 _entityType, uint32 _fee) external requiresAuth {
        defaultPayoutFee[_entityType] = _parseFeeWithFlip(_fee);
        emit DefaultPayoutFeeSet(_entityType, _fee);
    }

    /**
     * @notice Sets the payout fee override for a specific entity.
     * @param _entity Entity.
     * @param _fee The overriding fee (a zoc).
     */
    function setPayoutFeeOverride(Entity _entity, uint32 _fee) external requiresAuth {
        payoutFeeOverride[_entity] = _parseFeeWithFlip(_fee);
        emit PayoutFeeOverrideSet(address(_entity), _fee);
    }

    /**
     * @notice Sets the default transfer fee for transfers from one specific entity type to another.
     * @param _fromEntityType The entityType making the transfer.
     * @param _toEntityType The receiving entityType.
     * @param _fee The transfer fee percentage (a zoc).
     */
    function setDefaultTransferFee(uint8 _fromEntityType, uint8 _toEntityType, uint32 _fee) external requiresAuth {
        defaultTransferFee[_fromEntityType][_toEntityType] = _parseFeeWithFlip(_fee);
        emit DefaultTransferFeeSet(_fromEntityType, _toEntityType, _fee);
    }

    /**
     * @notice Sets the transfer fee override for transfers from one specific entity to entities of a given type.
     * @param _fromEntity The entity making the transfer.
     * @param _toEntityType The receiving entityType.
     * @param _fee The overriding fee percentage (a zoc).
     */
    function setTransferFeeSenderOverride(Entity _fromEntity, uint8 _toEntityType, uint32 _fee) external requiresAuth {
        transferFeeSenderOverride[_fromEntity][_toEntityType] = _parseFeeWithFlip(_fee);
        emit TransferFeeSenderOverrideSet(address(_fromEntity), _toEntityType, _fee);
    }

    /**
     * @notice Sets the transfer fee override for transfers from entities of a given type to a specific entity.
     * @param _fromEntityType The entityType making the transfer.
     * @param _toEntity The receiving entity.
     * @param _fee The overriding fee percentage (a zoc).
     */
    function setTransferFeeReceiverOverride(uint8 _fromEntityType, Entity _toEntity, uint32 _fee)
        external
        requiresAuth
    {
        transferFeeReceiverOverride[_fromEntityType][_toEntity] = _parseFeeWithFlip(_fee);
        emit TransferFeeReceiverOverrideSet(_fromEntityType, address(_toEntity), _fee);
    }

    /**
     * @notice Sets the enable/disable state of a SwapWrapper. System owners must ensure meticulous review of SwapWrappers before approving them.
     * @param _swapWrapper A contract that implements ISwapWrapper.
     * @param _supported `true` if supported, `false` if unsupported.
     */
    function setSwapWrapperStatus(ISwapWrapper _swapWrapper, bool _supported) external requiresAuth {
        isSwapperSupported[_swapWrapper] = _supported;
        emit SwapWrapperStatusSet(address(_swapWrapper), _supported);
    }
}
"
    },
    "src/Entity.sol": {
      "content": "//SPDX-License-Identifier: BSD 3-Clause
pragma solidity >=0.8.0;

import "solmate/tokens/ERC20.sol";
import "solmate/utils/SafeTransferLib.sol";
import "./lib/ReentrancyGuard.sol";

import {Registry} from "./Registry.sol";
import {ISwapWrapper} from "./interfaces/ISwapWrapper.sol";
import {EndaomentAuth} from "./lib/auth/EndaomentAuth.sol";
import {Portfolio} from "./Portfolio.sol";
import {Math} from "./lib/Math.sol";

error EntityInactive();
error PortfolioInactive();
error InsufficientFunds();
error InvalidAction();
error BalanceMismatch();
error CallFailed(bytes response);

/**
 * @notice Entity contract inherited by Org and Fund contracts (and all future kinds of Entities).
 */
abstract contract Entity is EndaomentAuth, ReentrancyGuard {
    using Math for uint256;
    using SafeTransferLib for ERC20;

    /// @notice The base registry to which the entity is connected.
    Registry public registry;

    /// @notice The entity's manager.
    address public manager;

    // @notice The base token used for tracking the entity's fund balance.
    ERC20 public baseToken;

    /// @notice The current balance for the entity, denominated in the base token's units.
    uint256 public balance;

    /// @notice Placeholder address used in swapping method to denote usage of ETH instead of a token.
    address public constant ETH_PLACEHOLDER = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

    /// @notice Emitted when manager is set.
    event EntityManagerSet(address indexed oldManager, address indexed newManager);

    /// @notice Emitted when a donation is made.
    event EntityDonationReceived(
        address indexed from,
        address indexed to,
        address indexed tokenIn,
        uint256 amountIn,
        uint256 amountReceived,
        uint256 amountFee
    );

    /// @notice Emitted when a payout is made from an entity.
    event EntityValuePaidOut(address indexed from, address indexed to, uint256 amountSent, uint256 amountFee);

    /// @notice Emitted when a transfer is made between entities.
    event EntityValueTransferred(address indexed from, address indexed to, uint256 amountReceived, uint256 amountFee);

    /// @notice Emitted when a base token reconciliation completes
    event EntityBalanceReconciled(address indexed entity, uint256 amountReceived, uint256 amountFee);

    /// @notice Emitted when a base token balance is used to correct the internal contract balance.
    event EntityBalanceCorrected(address indexed entity, uint256 newBalance);

    /// @notice Emitted when a portfolio deposit is made.
    event EntityDeposit(address indexed portfolio, uint256 baseTokenDeposited, uint256 sharesReceived);

    /// @notice Emitted when a portfolio share redemption is made.
    event EntityRedeem(address indexed portfolio, uint256 sharesRedeemed, uint256 baseTokenReceived);

    /// @notice Emitted when ether is received.
    event EntityEthReceived(address indexed sender, uint256 amount);

    /**
     * @notice Modifier for methods that require auth and that the manager can access.
     * @dev Uses the same condition as `requiresAuth` but with added manager access.
     */
    modifier requiresManager() {
        if (msg.sender != manager && !isAuthorized(msg.sender, msg.sig)) revert Unauthorized();
        _;
    }

    /// @notice Each entity will implement this function to allow a caller to interrogate what kind of entity it is.
    function entityType() public pure virtual returns (uint8);

    /**
     * @notice One time method to be called at deployment to configure the contract. Required so Entity
     * contracts can be deployed as minimal proxies (clones).
     * @param _registry The registry to host the Entity.
     * @param _manager The address of the Entity's manager.
     */
    function __initEntity(Registry _registry, address _manager) internal {
        // Call to EndaomentAuth's initialize function ensures that this can't be called again
        __initEndaomentAuth(_registry, bytes20(bytes.concat("entity", bytes1(entityType()))));
        __initReentrancyGuard();
        registry = _registry;
        manager = _manager;
        baseToken = _registry.baseToken();
    }

    /**
     * @notice Set a new manager for this entity.
     * @param _manager Address of new manager.
     * @dev Callable by current manager or permissioned role.
     */
    function setManager(address _manager) external virtual requiresManager {
        emit EntityManagerSet(manager, _manager);
        manager = _manager;
    }

    /**
     * @notice Receives a donated amount of base tokens to be added to the entity's balance. Transfers default fee to treasury.
     * @param _amount Amount donated in base token.
     * @dev Reverts if the donation fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts if the token transfer fails.
     */
    function donate(uint256 _amount) external virtual {
        uint32 _feeMultiplier = registry.getDonationFee(this);
        _donateWithFeeMultiplier(_amount, _feeMultiplier);
    }

    /**
     * @notice Receives a donated amount of base tokens to be added to the entity's balance. Transfers default or overridden fee to treasury.
     * @param _amount Amount donated in base token.
     * @dev Reverts if the donation fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts if the token transfer fails.
     */
    function donateWithOverrides(uint256 _amount) external virtual {
        uint32 _feeMultiplier = registry.getDonationFeeWithOverrides(this);
        _donateWithFeeMultiplier(_amount, _feeMultiplier);
    }

    /**
     * @notice Receives a donated amount of base tokens to be added to the entity's balance.
     * This method can be called by permissioned actors to make a donation with a manually specified fee.
     * @param _amount Amount donated in base token.
     * @param _feeOverride Fee percentage as zoc.
     * @dev Reverts if the transfer fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts if the token transfer fails.
     * @dev Reverts with `Unauthorized` if the `msg.sender` is not a privileged role.
     */
    function donateWithAdminOverrides(uint256 _amount, uint32 _feeOverride) external virtual requiresAuth {
        _donateWithFeeMultiplier(_amount, _feeOverride);
    }

    /**
     * @notice Receives a donated amount of base tokens to be added to the entity's balance. Transfers fee calculated by fee multiplier to treasury.
     * @param _amount Amount donated in base token.
     * @param _feeMultiplier Value indicating the percentage of the Endaoment donation fee to go to the Endaoment treasury.
     * @dev Reverts if the donation fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts if the token transfer fails.
     */
    function _donateWithFeeMultiplier(uint256 _amount, uint32 _feeMultiplier) internal virtual {
        (uint256 _netAmount, uint256 _fee) = _calculateFee(_amount, _feeMultiplier);
        baseToken.safeTransferFrom(msg.sender, registry.treasury(), _fee);
        baseToken.safeTransferFrom(msg.sender, address(this), _netAmount);

        unchecked {
            // unchecked as no possibility of overflow with baseToken precision
            balance += _netAmount;
        }
        emit EntityDonationReceived(msg.sender, address(this), address(baseToken), _amount, _amount, _fee);
    }

    /**
     * @notice Receive a donated amount of ETH or ERC20 tokens, swaps them to base tokens, and adds the output to the
     * entity's balance. Fee calculated using default rate and sent to treasury.
     * @param _swapWrapper The swap wrapper to use for the donation. Must be whitelisted on the Registry.
     * @param _tokenIn The address of the ERC20 token to swap and donate, or ETH_PLACEHOLDER if donating ETH.
     * @param _amountIn The amount of tokens or ETH being swapped and donated.
     * @param _data Additional call data required by the ISwapWrapper being used.
     */
    function swapAndDonate(ISwapWrapper _swapWrapper, address _tokenIn, uint256 _amountIn, bytes calldata _data)
        external
        payable
        virtual
    {
        uint32 _feeMultiplier = registry.getDonationFee(this);
        _swapAndDonateWithFeeMultiplier(_swapWrapper, _tokenIn, _amountIn, _data, _feeMultiplier);
    }

    /**
     * @notice Receive a donated amount of ETH or ERC20 tokens, swaps them to base tokens, and adds the output to the
     * entity's balance. Fee calculated using override rate and sent to treasury.
     * @param _swapWrapper The swap wrapper to use for the donation. Must be whitelisted on the Registry.
     * @param _tokenIn The address of the ERC20 token to swap and donate, or ETH_PLACEHOLDER if donating ETH.
     * @param _amountIn The amount of tokens or ETH being swapped and donated.
     * @param _data Additional call data required by the ISwapWrapper being used.
     */
    function swapAndDonateWithOverrides(
        ISwapWrapper _swapWrapper,
        address _tokenIn,
        uint256 _amountIn,
        bytes calldata _data
    ) external payable virtual {
        uint32 _feeMultiplier = registry.getDonationFeeWithOverrides(this);
        _swapAndDonateWithFeeMultiplier(_swapWrapper, _tokenIn, _amountIn, _data, _feeMultiplier);
    }

    /// @dev Internal helper implementing swap and donate functionality for any fee multiplier provided.
    function _swapAndDonateWithFeeMultiplier(
        ISwapWrapper _swapWrapper,
        address _tokenIn,
        uint256 _amountIn,
        bytes calldata _data,
        uint32 _feeMultiplier
    ) internal virtual nonReentrant {
        if (!registry.isSwapperSupported(_swapWrapper)) revert InvalidAction();

        // THINK: do we need a re-entrancy guard on this method?
        if (_tokenIn != ETH_PLACEHOLDER) {
            ERC20(_tokenIn).safeTransferFrom(msg.sender, address(this), _amountIn);
            ERC20(_tokenIn).safeApprove(address(_swapWrapper), 0);
            ERC20(_tokenIn).safeApprove(address(_swapWrapper), _amountIn);
        }

        uint256 _amountOut =
            _swapWrapper.swap{value: msg.value}(_tokenIn, address(baseToken), address(this), _amountIn, _data);

        (uint256 _netAmount, uint256 _fee) = _calculateFee(_amountOut, _feeMultiplier);

        baseToken.safeTransfer(registry.treasury(), _fee);

        unchecked {
            // unchecked as no possibility of overflow with baseToken precision
            balance += _netAmount;
        }

        if (balance > baseToken.balanceOf(address(this))) revert BalanceMismatch();

        emit EntityDonationReceived(msg.sender, address(this), _tokenIn, _amountIn, _amountOut, _fee);
    }

    /**
     * @notice Transfers an amount of base tokens from one entity to another. Transfers default fee to treasury.
     * @param _to The entity to receive the tokens.
     * @param _amount Contains the amount being donated (denominated in the base token's units).
     * @dev Reverts if the entity is inactive or if the token transfer fails.
     * @dev Reverts if the transfer fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts with `Unauthorized` if the `msg.sender` is not the entity manager or a privileged role.
     * @dev Renamed from `transfer` to distinguish from ERC20 transfer in 3rd party tools.
     */
    function transferToEntity(Entity _to, uint256 _amount) external virtual requiresManager {
        uint32 _feeMultiplier = registry.getTransferFee(this, _to);
        _transferWithFeeMultiplier(_to, _amount, _feeMultiplier);
    }

    /**
     * @notice Transfers an amount of base tokens from one entity to another. Transfers default or overridden fee to treasury.
     * @param _to The entity to receive the tokens.
     * @param _amount Contains the amount being donated (denominated in the base token's units).
     * @dev Reverts if the entity is inactive or if the token transfer fails.
     * @dev Reverts if the transfer fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts with `Unauthorized` if the `msg.sender` is not the entity manager or a privileged role.
     */
    function transferToEntityWithOverrides(Entity _to, uint256 _amount) external virtual requiresManager {
        uint32 _feeMultiplier = registry.getTransferFeeWithOverrides(this, _to);
        _transferWithFeeMultiplier(_to, _amount, _feeMultiplier);
    }

    /**
     * @notice Transfers an amount of base tokens from one entity to another. Transfers fee specified by a privileged role.
     * @param _to The entity to receive the tokens.
     * @param _amount Contains the amount being donated (denominated in the base token's units).
     * @param _feeOverride Admin override configured by an Admin
     * @dev Reverts if the entity is inactive or if the token transfer fails.
     * @dev Reverts if the transfer fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts with `Unauthorized` if the `msg.sender` is not a privileged role.
     */
    function transferToEntityWithAdminOverrides(Entity _to, uint256 _amount, uint32 _feeOverride)
        external
        virtual
        requiresAuth
    {
        _transferWithFeeMultiplier(_to, _amount, _feeOverride);
    }

    /**
     * @notice Transfers an amount of base tokens from one entity to another. Transfers fee calculated by fee multiplier to treasury.
     * @param _to The entity to receive the tokens.
     * @param _amount Contains the amount being donated (denominated in the base token's units).
     * @param _feeMultiplier Value indicating the percentage of the Endaoment donation fee to go to the Endaoment treasury.
     * @dev Reverts with 'Inactive' if the entity sending the transfer or the entity receiving the transfer is inactive.
     * @dev Reverts if the transfer fee percentage is larger than 100% (equal to 1e4 when represented as a zoc).
     * @dev Reverts with `Unauthorized` if the `msg.sender` is not the entity manager or a privileged role.
     * @dev Reverts if the token transfer fails.
     */
    function _transferWithFeeMultiplier(Entity _to, uint256 _amount, uint32 _feeMultiplier) internal virtual {
        if (!registry.isActiveEntity(this) || !registry.isActiveEntity(_to)) revert EntityInactive();
        if (balance < _amount) revert InsufficientFunds();

        (uint256 _netAmount, uint256 _fee) = _calculateFee(_amount, _feeMultiplier);
        baseToken.safeTransfer(registry.treasury(), _fee);
        baseToken.safeTransfer(address(_to), _netAmount);

        unchecked {
            // unchecked as no possibility of overflow with baseToken precision
            balance -= _amount;
            _to.receiveTransfer(_netAmount);
        }
        emit EntityValueTransferred(address(this), address(_to), _amount, _fee);
    }

    /**
     * @notice Updates the receiving entity balance on a transfer.
     * @param _transferAmount The amount being received on the transfer.
     * @dev This function is external, but is restricted such that it can only be called by other entities.
     */
    function receiveTransfer(uint256 _transferAmount) external virtual {
        if (!registry.isActiveEntity(Entity(payable(msg.sender)))) revert EntityInactive();
        unchecked {
            // Cannot overflow with realistic balances.
            balance += _transferAmount;
        }
    }

    /**
     * @notice Deposits an amount of Entity's `baseToken` into an Endaoment-approved Portfolio.
     * @param _portfolio An Endaoment-approved portfolio.
     * @param _amount Amount of `baseToken` to deposit into the portfolio.
     * @param _data Data required by a portfolio to deposit.
     * @return _shares Amount of portfolio share tokens Entity received as a result of this deposit.
     */
    function portfolioDeposit(Portfolio _portfolio, uint256 _amount, bytes calldata _data)
        external
        virtual
        requiresManager
        returns (uint256)
    {
        if (!registry.isActivePortfolio(_portfolio)) revert PortfolioInactive();
        balance -= _amount;
        baseToken.safeApprove(address(_portfolio), _amount);
        uint256 _shares = _portfolio.deposit(_amount, _data);
        emit EntityDeposit(address(_portfolio), _amount, _shares);
        return _shares;
    }

    /**
     * @notice Redeems an amount of Entity's portfolio shares for an amount of `baseToken`.
     * @param _portfolio An Endaoment-approved portfolio.
     * @param _shares Amount of share tokens to redeem.
     * @param _data Data required by a portfolio to redeem.
     * @return _received Amount of `baseToken` Entity received as a result of this redemption.
     */
    function portfolioRedeem(Portfolio _portfolio, uint256 _shares, bytes calldata _data)
        external
        virtual
        requiresManager
        returns (uint256)
    {
        if (!registry.isActivePortfolio(_portfolio)) revert PortfolioInactive();
        uint256 _received = _portfolio.redeem(_shares, _data);
        // unchecked: a realistic balance can never overflow a uint256
        unchecked {
            balance += _received;
        }
        emit EntityRedeem(address(_portfolio), _shares, _received);
        return _received;
    }

    /**
     * @notice This method should be called to reconcile the Entity's internal baseToken accounting with the baseToken contract's accounting.
     * There are a 2 situations where calling this method is appropriate:
     * 1. To process amounts of baseToken that arrived at this Entity through methods besides Entity:donate or Entity:transfer. For example,
     * if this Entity receives a normal ERC20 transfer of baseToken, the amount received will be unavailable for Entity use until this method
     * is called to adjust the balance and process fees. OrgFundFactory.sol:_donate makes use of this method to do this as well.
     * 2. Unusually, the Entity's perspective of balance could be lower than `baseToken.balanceOf(this)`. This could happen if
     * Entity:callAsEntity is used to transfer baseToken. In this case, this method provides a way of correcting the Entity's internal balance.
     */
    function reconcileBalance() external virtual {
        uint256 _tokenBalance = baseToken.balanceOf(address(this));

        if (_tokenBalance >= balance) {
            uint256 _sweepAmount = _tokenBalance - balance;
            uint32 _feeMultiplier = registry.getDonationFeeWithOverrides(this);
            (uint256 _netAmount, uint256 _fee) = _calculateFee(_sweepAmount, _feeMultiplier);

            baseToken.safeTransfer(registry.treasury(), _fee);
            unchecked {
                balance += _netAmount;
            }
            emit EntityBalanceReconciled(address(this), _sweepAmount, _fee);
        } else {
            // Handle abnormal scenario where _tokenBalance < balance (see method docs)
            balance = _tokenBalance;
            emit EntityBalanceCorrected(address(this), _tokenBalance);
        }
    }

    /**
     * @notice Takes stray tokens or ETH sent directly to this Entity, swaps them for base token, then adds them to the
     * Entity's balance after paying the appropriate fee to the treasury.
     * @param _swapWrapper The swap wrapper to use to convert the assets. Must be whitelisted on the Registry.
     * @param _tokenIn The address of the ERC20 token to swap, or ETH_PLACEHOLDER if ETH.
     * @param _amountIn The amount of tokens or ETH being swapped and added to the balance.
     * @param _data Additional call data required by the ISwapWrapper being used.
     */
    function swapAndReconcileBalance(
        ISwapWrapper _swapWrapper,
        address _tokenIn,
        uint256 _amountIn,
        bytes calldata _data
    ) external virtual nonReentrant requiresManager {
        if (!registry.isSwapperSupported(_swapWrapper)) revert InvalidAction();

        uint32 _feeMultiplier = registry.getDonationFeeWithOverrides(this);

        if (_tokenIn != ETH_PLACEHOLDER) {
            ERC20(_tokenIn).safeApprove(address(_swapWrapper), 0);
            ERC20(_tokenIn).safeApprove(address(_swapWrapper), _amountIn);
        }

        // Send value only if token in is ETH
        uint256 _value = _tokenIn == ETH_PLACEHOLDER ? _amountIn : 0;

        uint256 _amountOut =
            _swapWrapper.swap{value: _value}(_tokenIn, address(baseToken), address(this), _amountIn, _data);

        (uint256 _netAmount, uint256 _fee) = _calculateFee(_amountOut, _feeMultiplier);
        baseToken.safeTransfer(registry.treasury(), _fee);

        unchecked {

Tags:
Multisig, Swap, Yield, Voting, Upgradeable, Multi-Signature, Factory|addr:0x5ac31137f304726a1087b16a4045cc9627a409e2|verified:true|block:23433454|tx:0x94f21d94c5165816756ea56a7a26a2bd9dfdf82719dab99e88fc5bcb895fa4cf|first_check:1758737534

Submitted on: 2025-09-24 20:12:13

Comments

Log in to comment.

No comments yet.