ProportionalVault

Description:

Smart contract deployed on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

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

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
    function totalSupply() external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
}

contract ProportionalVault {
    IERC20 public immutable token; 

    
    mapping(address => uint256) public withdrawnEther;

    
    uint256 public totalWithdrawn;

    event Deposit(address indexed from, uint256 amount);
    event Withdraw(address indexed to, uint256 amount);

    constructor(address _token) {
        require(_token != address(0), "Invalid token address");
        token = IERC20(_token);
    }

    
    receive() external payable {
        emit Deposit(msg.sender, msg.value);
    }

    fallback() external payable {
        emit Deposit(msg.sender, msg.value);
    }

    
    function withdrawableAmount(address user) public view returns (uint256) {
        uint256 userBalance = token.balanceOf(user);
        if (userBalance == 0) return 0;

        uint256 totalSupply = token.totalSupply();
        if (totalSupply == 0) return 0;

        
        uint256 totalReceived = address(this).balance + totalWithdrawn;

        
        uint256 entitled = (totalReceived * userBalance) / totalSupply;

        
        if (entitled <= withdrawnEther[user]) {
            return 0;
        }

        return entitled - withdrawnEther[user];
    }

   
    function withdraw() external {
        uint256 amount = withdrawableAmount(msg.sender);
        require(amount > 0, "No withdrawable balance");

        
        withdrawnEther[msg.sender] += amount;
        totalWithdrawn += amount;

        
        (bool sent, ) = payable(msg.sender).call{value: amount}("");
        require(sent, "Ether transfer failed");

        emit Withdraw(msg.sender, amount);
    }

    
    function getBalance() external view returns (uint256) {
        return address(this).balance;
    }
}

Tags:
addr:0x3415f5ce1f553c16193a4c66f54c3513d942ef5b|verified:true|block:23447201|tx:0xf54e24e5aa9c9fded79f5d72e0475b7afc438bb42bac3b700f0ec6b8629478da|first_check:1758893456

Submitted on: 2025-09-26 15:30:59

Comments

Log in to comment.

No comments yet.