Skip to content

Instantly share code, notes, and snippets.

@piorot
Created March 29, 2022 11:04
Show Gist options
  • Save piorot/3a63e0da547e45582111cbd223ffbacf to your computer and use it in GitHub Desktop.
Save piorot/3a63e0da547e45582111cbd223ffbacf to your computer and use it in GitHub Desktop.
It should not be possible to withdraw more than one has
//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