Skip to content

Instantly share code, notes, and snippets.

@piorot
Created March 29, 2022 11:09
Show Gist options
  • Save piorot/2b585719456bf23803fa7d06a6477543 to your computer and use it in GitHub Desktop.
Save piorot/2b585719456bf23803fa7d06a6477543 to your computer and use it in GitHub Desktop.
Each withdrawal should be calculated against new income, not the total balance
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Apartment is ERC20 {
uint public balance;
uint public totalIncome;
mapping(address => uint) withdrawRegister;
constructor() ERC20("ApartmentContract", "APRTM") {
super._mint(_msgSender(), 100);
console.log("Deploying a Greeter with greeting:");
}
function withdraw() public {
require(this.balanceOf(msg.sender) > 0, "unauthorized");
require(totalIncome > withdrawRegister[msg.sender], "0 funds to withdraw");
uint meansToWithdraw = (totalIncome - withdrawRegister[msg.sender]) / 100 * this.balanceOf(msg.sender);
balance = balance - meansToWithdraw;
withdrawRegister[msg.sender] = totalIncome;
payable(msg.sender).transfer(meansToWithdraw);
}
receive() external payable {
balance += msg.value;
totalIncome +=msg.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment