forked from decentraland/land
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RentingContract.sol
177 lines (143 loc) · 4.08 KB
/
RentingContract.sol
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
pragma solidity ^0.4.15;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
import 'zeppelin-solidity/contracts/math/Math.sol';
import 'zeppelin-solidity/contracts/math/SafeMath.sol';
import './Land.sol';
contract RentingContract is Ownable{
using SafeMath for uint256;
// Coordinates to locate the land
uint256 public x;
uint256 public y;
uint256 public tokenId;
// Land contract for Decentraland
Land public landContract = Land(0x0);
uint256 public upfrontCost;
uint256 public ownerTerminationCost;
uint256 public weeklyCost;
uint256 public costPerSecond;
address public tenant;
uint256 public rentStartedAt;
uint256 public tenantBalance;
function RentingContract()
{
require(landContract != 0);
}
function initRentContract(
uint256 _x,
uint256 _y,
uint256 _upfrontCost,
uint256 _ownerTerminationCost,
uint256 _weeklyCost
)
public
onlyOwner
onlyIfNotRented
returns (bool)
{
tokenId = landContract.buildTokenId(x, y);
x = _x;
y = _y;
upfrontCost = _upfrontCost;
weeklyCost = _weeklyCost;
ownerTerminationCost = _ownerTerminationCost;
costPerSecond = weeklyCost / 1 weeks;
require(landContract.ownerOf(tokenId) == address(this));
return true
}
function isSetup() public returns(bool) {
// We use tokenId = 0 to signify that the contract has not been setup yet
return tokenId != 0;
}
function totalDue(uint256 time) returns (uint256) {
return time.sub(rentStartedAt).mul(costPerSecond).sum(upfrontCost);
}
function totalDueSoFar() returns (uint256) {
return totalDue(now);
}
function isRented() public returns(bool) {
return tenant != 0;
}
function isDue() public {
return isRented() && totalDueSoFar() >= tenantBalance;
}
modifier onlyIfSetup {
require(isSetup());
_;
}
modifier onlyIfRented {
require(isRented());
_;
}
modifier onlyIfNotRented {
require(!isRented());
_;
}
modifier onlyTenant {
require(msg.sender == tenant);
_;
}
modifier onlyTenantOrOwner {
require(msg.sender == tenant || msg.sender == );
_;
}
/**
* Allow someone to rent the land
*/
function rent() payable onlyIfSetup onlyIfNotRented {
uint256 paid = msg.value;
// require 1 week in advance
require(totalDue(now + 1 weeks) >= upfrontCost.sum(weeklyCost));
tenant = msg.sender;
rentStartedAt = now;
tenantBalance = paid;
}
/**
* Allow someone to rent the land
*/
function payRent() payable onlyIfRented {
uint256 paid = msg.value;
tenantBalance = tenantBalance.sum(paid);
}
function evict() public returns (bool) {
if (isDue()) {
_release();
return true;
}
return false;
}
function _release() internal {
tenant = 0;
tenantBalance = 0;
rentStartedAt = 0;
}
function _clear() internal {
tokenId = 0;
x = 0;
y = 0;
upfrontCost = 0;
weeklyCost = 0;
ownerTerminationCost = 0;
costPerSecond = 0;
}
function cancelContract() payable public onlyOwner onlyIfRented {
require(msg.value >= ownerTerminationCost);
_release();
tenant.send(msg.value);
}
function transfer(address target) public onlyOwner onlyIfNotRented {
_clear();
land.transfer(target, tokenId);
}
function retrieveFunds() public onlyOwner {
owner.send(this.balance);
}
function updateLandForOwner(string _metadata) public onlyOwner onlyIfNotRented {
updateTokenMetadata(land, _metadata);
}
function updateLand(string _metadata) public onlyTenant onlyIfRented {
updateTokenMetadata(land, _metadata);
}
function PingLand() public onlyTenantOrOwner {
landContract.ping(tokenId);
}
}