Created
January 31, 2022 10:46
-
-
Save ZakriaJanjua/a0a0ee6e3c0615fa58ac50c0b82f88dd to your computer and use it in GitHub Desktop.
payments receiving in solidity
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
pragma solidity >=0.7.0 <0.9.0; | |
contract Hotel { | |
enum Status { Vacant, Occupied } | |
Status currentStatus; | |
event Occupy(address _occupant, uint _value); | |
address payable public owner; | |
constructor() { | |
owner = payable(msg.sender); | |
currentStatus = Status.Vacant; | |
} | |
modifier onlyWhileVacant { | |
require(currentStatus == Status.Vacant, "Currently Occupied"); | |
_; | |
} | |
modifier rightAmount(uint _amount) { | |
require(msg.value >= _amount, "Not the right amount"); | |
_; | |
} | |
receive() external payable onlyWhileVacant rightAmount( 2 ether ) { | |
currentStatus = Status.Occupied; | |
owner.transfer(msg.value); | |
emit Occupy(msg.sender, msg.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment