-
-
Save piorot/3a63e0da547e45582111cbd223ffbacf to your computer and use it in GitHub Desktop.
It should not be possible to withdraw more than one has
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 = address(this).balance / 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