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;
}
}
Submitted on: 2025-09-26 15:30:59
Comments
Log in to comment.
No comments yet.