Created
February 22, 2019 06:27
-
-
Save amit-bhandari/7d59b983c4a4609db10bbc2b3b23fc59 to your computer and use it in GitHub Desktop.
eLocations Smart Contract
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
contract AgreementContract { | |
struct PaidRent { | |
uint id; | |
uint value; | |
} | |
PaidRent[] public paidrents; | |
uint public lastRentPaidTimestamp; | |
uint public createdTimestamp; | |
uint public rent; | |
string public deposit; | |
string public house; | |
string public country; | |
string public referenceD; | |
address public landlord; | |
address public tenant; | |
enum State {Created, Started, Terminated} | |
State public state; | |
function AgreementContract(uint _rent, string _deposit, string _house, string _referenceD, string _country) { | |
rent = _rent; | |
deposit = _deposit; | |
house = _house; | |
country = _country; | |
referenceD = _referenceD; | |
landlord = msg.sender; | |
createdTimestamp = block.timestamp; | |
} | |
modifier require(bool _condition) { | |
if (!_condition) throw; | |
_; | |
} | |
modifier onlyLandlord() { | |
if (msg.sender != landlord) throw; | |
_; | |
} | |
modifier onlyTenant() { | |
if (msg.sender != tenant) throw; | |
_; | |
} | |
modifier inState(State _state) { | |
if (state != _state) throw; | |
_; | |
} | |
/* We also have some getters so that we can read the values | |
from the blockchain at any time */ | |
function getPaidRents() internal returns (PaidRent[]) { | |
return paidrents; | |
} | |
function getHouse() constant returns (string) { | |
return house; | |
} | |
function getDeposit() constant returns (string) { | |
return deposit; | |
} | |
function getCountry() constant returns (string) { | |
return country; | |
} | |
function getLandlord() constant returns (address) { | |
return landlord; | |
} | |
function getTenant() constant returns (address) { | |
return tenant; | |
} | |
function getRent() constant returns (uint) { | |
return rent; | |
} | |
function getReferenceDetails() constant returns (string) { | |
return referenceD; | |
} | |
function getContractCreated() constant returns (uint) { | |
return createdTimestamp; | |
} | |
function getContractAddress() constant returns (address) { | |
return this; | |
} | |
function getLastRentPaidTime() constant returns (uint) { | |
return lastRentPaidTimestamp; | |
} | |
function getState() returns (State) { | |
return state; | |
} | |
/* Events for DApps to listen to */ | |
event agreementConfirmed(); | |
event paidRent(); | |
event contractTerminated(); | |
/* Confirm the lease agreement as customer*/ | |
function confirmAgreement() | |
inState(State.Created) | |
require(msg.sender != landlord) | |
{ | |
agreementConfirmed(); | |
tenant = msg.sender; | |
state = State.Started; | |
} | |
function payRent() payable | |
onlyTenant | |
inState(State.Started) | |
require(msg.value >= rent) | |
{ | |
paidRent(); | |
landlord.send(msg.value); | |
paidrents.push(PaidRent({ | |
id : paidrents.length + 1, | |
value : msg.value | |
})); | |
lastRentPaidTimestamp = block.timestamp; | |
} | |
/* Terminate the contract so the customer can’t pay rent anymore, | |
and the contract is terminated */ | |
function terminateContract() payable | |
onlyLandlord | |
//Comment the above line and uncomment the line below if you want both tenant and landlord to have the authority to cancel/terminate the contract, otherwise it's only the landlord who can terminate the contract | |
//require(msg.sender == landlord || msg.sender == tenant) | |
{ | |
contractTerminated(); | |
landlord.send(this.balance); | |
/* If there is any value on the | |
contract send it to the owner (company)*/ | |
state = State.Terminated; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment