Skip to content
Verified contractExactMIT

ShareVault

Address0xfaa7ea4c9e05091a4f9cf729a9c747452cd823c8
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,463,917
Creation transaction
0x9f6e748cea…444eede39a

src/vault4626/ShareVault.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

interface IERC20Minimal {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

/// @notice ERC-4626-style share vault with verified accounting (module 13).
/// @dev totalAssets is never a hardcoded constant. Off-ledger reserves are not assumed transferable.
contract ShareVault {
    IERC20Minimal public immutable asset;
    address public admin;
    address public oracle;

    uint256 public totalShares;
    /// @notice Authorized verified asset units (adapter). Must not exceed on-chain token balance.
    uint256 public verifiedAssets;
    mapping(address => uint256) public balanceOf;

    error ZeroAddress();
    error Unauthorized();
    error AssetMismatch();
    error UnsafeRounding();
    error Unverified();
    error ZeroAmount();
    error InsufficientShares();
    error InsufficientAssets();
    error TransferFailed();

    event OracleUpdated(address indexed oracle);
    event VerifiedAssetsUpdated(uint256 amount, bytes32 evidenceHash);
    event Deposit(address indexed caller, address indexed owner, uint256 assets, uint256 shares);
    event Withdraw(address indexed caller, address indexed receiver, address indexed owner, uint256 assets, uint256 shares);

    modifier onlyAdmin() {
        if (msg.sender != admin) revert Unauthorized();
        _;
    }

    constructor(address admin_, address asset_) {
        if (admin_ == address(0) || asset_ == address(0)) revert ZeroAddress();
        admin = admin_;
        asset = IERC20Minimal(asset_);
        oracle = admin_;
    }

    function setOracle(address oracle_) external onlyAdmin {
        if (oracle_ == address(0)) revert ZeroAddress();
        oracle = oracle_;
        emit OracleUpdated(oracle_);
    }

    /// @notice Publish independently verified asset figure. Capped by actual ERC-20 balance.
    function setVerifiedAssets(uint256 amount, bytes32 evidenceHash) external {
        if (msg.sender != oracle && msg.sender != admin) revert Unauthorized();
        if (evidenceHash == bytes32(0)) revert Unverified();
        uint256 bal = asset.balanceOf(address(this));
        if (amount > bal) amount = bal;
        verifiedAssets = amount;
        emit VerifiedAssetsUpdated(amount, evidenceHash);
    }

    function totalAssets() public view returns (uint256) {
        uint256 bal = asset.balanceOf(address(this));
        return verifiedAssets < bal ? verifiedAssets : bal;
    }

    function convertToShares(uint256 assets) public view returns (uint256) {
        uint256 supply = totalShares;
        uint256 ta = totalAssets();
        if (supply == 0 || ta == 0) return assets; // 1:1 bootstrap
        return assets * supply / ta; // round down
    }

    function convertToAssets(uint256 shares) public view returns (uint256) {
        uint256 supply = totalShares;
        if (supply == 0) return shares;
        return shares * totalAssets() / supply; // round down
    }

    function previewDeposit(uint256 assets) external view returns (uint256) {
        return convertToShares(assets);
    }

    function previewRedeem(uint256 shares) external view returns (uint256) {
        return convertToAssets(shares);
    }

    /// @dev Legacy sandbox API retained for invariant tests.
    function previewDeposit(bytes32 vaultAsset, bytes32 depositAsset, uint256 assets, bool verified, bool roundDown)
        external
        view
        returns (uint256 shares)
    {
        if (vaultAsset != depositAsset) revert AssetMismatch();
        if (!roundDown) revert UnsafeRounding();
        if (!verified || assets == 0) revert Unverified();
        return convertToShares(assets);
    }

    function deposit(uint256 assets, address receiver, bool verified) external returns (uint256 shares) {
        if (!verified) revert Unverified();
        if (assets == 0 || receiver == address(0)) revert ZeroAmount();
        shares = convertToShares(assets);
        if (shares == 0) revert ZeroAmount();
        if (!asset.transferFrom(msg.sender, address(this), assets)) revert TransferFailed();
        verifiedAssets += assets;
        totalShares += shares;
        balanceOf[receiver] += shares;
        emit Deposit(msg.sender, receiver, assets, shares);
    }

    function redeem(uint256 shares, address receiver, address owner_) external returns (uint256 assetsOut) {
        if (shares == 0 || receiver == address(0) || owner_ == address(0)) revert ZeroAmount();
        if (balanceOf[owner_] < shares) revert InsufficientShares();
        if (msg.sender != owner_) revert Unauthorized();
        assetsOut = convertToAssets(shares);
        if (assetsOut == 0) revert InsufficientAssets();
        if (assetsOut > totalAssets()) revert InsufficientAssets();

        balanceOf[owner_] -= shares;
        totalShares -= shares;
        verifiedAssets -= assetsOut;
        if (!asset.transfer(receiver, assetsOut)) revert TransferFailed();
        emit Withdraw(msg.sender, receiver, owner_, assetsOut, shares);
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.