Created
July 18, 2017 22:19
-
-
Save loong/101a4c6756ec7636952aac07ed608154 to your computer and use it in GitHub Desktop.
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.4.0; | |
contract Rental { | |
uint deposit; | |
uint rent; | |
uint balance; | |
address tenant; | |
address landlord; | |
event DepositPayed(address payer, address tenant); | |
event DepositWithdrawn(); | |
event RentWithdrawn(uint amount); | |
enum State {Vacant, Occupied} | |
State state; | |
// Constructor | |
function Rental(uint _deposit, uint _rent) { | |
landlord = msg.sender; | |
deposit = _deposit; | |
rent = _rent; | |
state = State.Vacant; | |
} | |
function moveIn(address _tenant) payable { | |
require(state == State.Vacant); | |
// need to pay the deposit in full or the money will be sent back | |
require(msg.value >= deposit); | |
tenant = _tenant; | |
DepositPayed(msg.sender, tenant); | |
state = State.Occupied; | |
} | |
function moveOut() { | |
require(state == State.Occupied); | |
require(msg.sender == tenant); | |
tenant.transfer(deposit); // error check needed? | |
deposit = 0; | |
tenant = 0; // not sure if this is good | |
DepositWithdrawn(); | |
state = State.Vacant; | |
} | |
function payRent() payable { | |
require(state == State.Occupied); | |
balance += msg.value; | |
} | |
function withdrawRent() { | |
require(msg.sender == landlord); | |
require(balance > 0); | |
RentWithdrawn(balance); | |
landlord.transfer(deposit); // fails here | |
balance = 0; | |
} | |
function getDeposit() returns (uint) { | |
return deposit; | |
} | |
function getBalance() returns (uint) { | |
return balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment