-
-
Save piorot/2b585719456bf23803fa7d06a6477543 to your computer and use it in GitHub Desktop.
Each withdrawal should be calculated against new income, not the total balance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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