-
Notifications
You must be signed in to change notification settings - Fork 22
/
SmartRealEstate.aes
86 lines (66 loc) · 3.8 KB
/
SmartRealEstate.aes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@compiler >= 6
include "String.aes"
contract SmartRealEstate =
record rent = { tenant : address
, price : int
, is_paid : bool
, property_address : string }
type state = map(address, map(string, rent))
entrypoint init(price : int, name : string, address : string) : state =
{[Call.caller] = {[name] =
{ tenant = Call.caller,
price = price,
is_paid = false,
property_address = address }}}
payable stateful entrypoint pay_rent(owner : address, name : string) =
// check if key of the owner exists
require(Map.member(owner, state), "Owner does not exist")
// check if the property exists
require(Map.member(name, state[owner]), "Owner's property does not exist")
// check if is not paid
require(!state[owner][name].is_paid, "Rent is already paid")
let owner_property =
switch(Map.lookup(name, state[owner]))
None => abort("No property")
Some(e) => e
// check if there is enoght money to pay/buy this rent
require(Contract.balance >= owner_property.price, String.concat("Not enough money to pay this rent: ", name))
Chain.spend(owner, owner_property.price)
let updated_record = owner_property{tenant = Call.caller, is_paid = true}
put(state{[owner][name] = updated_record})
stateful entrypoint add_owner(name : string, price : int, property_address' : string) =
require(!Map.member(Call.caller, state), "The owner is already in the list of owners")
put(state{[Call.caller] = {[name] = {tenant = Call.caller, price = price, is_paid = false, property_address = property_address'}}})
stateful entrypoint delete_owner() =
require(Map.member(Call.caller, state), "Owner does not exist")
put(Map.delete(Call.caller, state))
stateful entrypoint add_property(name : string, price : int, property_address' : string) =
require(!Map.member(name, state[Call.caller]), "The property is already in the list!")
put(state{[Call.caller] @ current = current{[name] = {tenant = Call.caller, price = price, is_paid = false, property_address = property_address'}}})
stateful entrypoint delete_property(name : string) =
require(Map.member(name, state[Call.caller]), "Property does not exist")
let updated_owners_properties = Map.delete(name, state[Call.caller])
put(state{[Call.caller] = updated_owners_properties})
stateful entrypoint change_address(name : string, property_address' : string) =
require(is_owner(name), "You don't own this property")
put(state{[Call.caller][name] @ current = current{property_address = property_address'}})
stateful entrypoint change_price(name : string, price : int) =
require(is_owner(name), "You don't own this property")
put(state{[Call.caller][name] @ current = current{price = price}})
stateful entrypoint change_tenant(name : string, tenant : address) =
require(is_owner(name), "You don't own this property")
put(state{[Call.caller][name] @ current = current{tenant = tenant}})
entrypoint get_payment_status(owner : address, name : string) : bool =
require(Map.member(name, state[owner]), "Owner does not have this property")
state[owner][name].is_paid
entrypoint get_property_address(owner : address, name : string) : string =
require(Map.member(name, state[owner]), "Owner does not have this property")
state[owner][name].property_address
entrypoint get_tenant(owner : address, name : string) : address =
require(Map.member(name, state[owner]), "Owner does not have this property")
state[owner][name].tenant
entrypoint get_price(owner : address, name : string) : int =
require(Map.member(name, state[owner]), "Owner does not have this property")
state[owner][name].price
function is_owner(name) : bool =
Map.member(name, state[Call.caller])