forked from reflexer-labs/geb-safe-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GebSafeManager.sol
448 lines (409 loc) · 13.7 KB
/
GebSafeManager.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright (C) 2018-2020 Maker Ecosystem Growth Holdings, INC.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.6.7;
abstract contract SAFEEngineLike {
function safes(bytes32, address) virtual public view returns (uint, uint);
function approveSAFEModification(address) virtual public;
function transferCollateral(bytes32, address, address, uint) virtual public;
function transferInternalCoins(address, address, uint) virtual public;
function modifySAFECollateralization(bytes32, address, address, address, int, int) virtual public;
function transferSAFECollateralAndDebt(bytes32, address, address, int, int) virtual public;
}
abstract contract LiquidationEngineLike {
function protectSAFE(bytes32, address, address) virtual external;
}
abstract contract DebtRewardsLike {
function getRewards(address, address) virtual external;
}
contract SAFEHandler {
constructor(address safeEngine) public {
SAFEEngineLike(safeEngine).approveSAFEModification(msg.sender);
}
}
contract GebSafeManager {
address public safeEngine;
uint public safei; // Auto incremental
mapping (uint => address) public safes; // SAFEId => SAFEHandler
mapping (uint => List) public safeList; // SAFEId => Prev & Next SAFEIds (double linked list)
mapping (uint => address) public ownsSAFE; // SAFEId => Owner
mapping (uint => bytes32) public collateralTypes; // SAFEId => CollateralType
mapping (address => uint) public firstSAFEID; // Owner => First SAFEId
mapping (address => uint) public lastSAFEID; // Owner => Last SAFEId
mapping (address => uint) public safeCount; // Owner => Amount of SAFEs
mapping (
address => mapping (
uint => mapping (
address => uint
)
)
) public safeCan; // Owner => SAFEId => Allowed Addr => True/False
mapping (
address => mapping (
address => uint
)
) public handlerCan; // SAFE handler => Allowed Addr => True/False
struct List {
uint prev;
uint next;
}
// --- Events ---
event AllowSAFE(
address sender,
uint safe,
address usr,
uint ok
);
event AllowHandler(
address sender,
address usr,
uint ok
);
event TransferSAFEOwnership(
address sender,
uint safe,
address dst
);
event OpenSAFE(address indexed sender, address indexed own, uint indexed safe);
event ModifySAFECollateralization(
address sender,
uint safe,
int deltaCollateral,
int deltaDebt
);
event TransferCollateral(
address sender,
uint safe,
address dst,
uint wad
);
event TransferCollateral(
address sender,
bytes32 collateralType,
uint safe,
address dst,
uint wad
);
event TransferInternalCoins(
address sender,
uint safe,
address dst,
uint rad
);
event QuitSystem(
address sender,
uint safe,
address dst
);
event EnterSystem(
address sender,
address src,
uint safe
);
event MoveSAFE(
address sender,
uint safeSrc,
uint safeDst
);
event ProtectSAFE(
address sender,
uint safe,
address liquidationEngine,
address saviour
);
event CollectRewards(
address sender,
uint safe,
address rewards
);
modifier safeAllowed(
uint safe
) {
require(msg.sender == ownsSAFE[safe] || safeCan[ownsSAFE[safe]][safe][msg.sender] == 1, "safe-not-allowed");
_;
}
modifier handlerAllowed(
address handler
) {
require(
msg.sender == handler ||
handlerCan[handler][msg.sender] == 1,
"internal-system-safe-not-allowed"
);
_;
}
constructor(address safeEngine_) public {
safeEngine = safeEngine_;
}
// --- Math ---
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x);
}
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x);
}
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0);
}
// --- SAFE Manipulation ---
// Allow/disallow a usr address to manage the safe
function allowSAFE(
uint safe,
address usr,
uint ok
) public safeAllowed(safe) {
safeCan[ownsSAFE[safe]][safe][usr] = ok;
emit AllowSAFE(
msg.sender,
safe,
usr,
ok
);
}
// Allow/disallow a usr address to quit to the sender handler
function allowHandler(
address usr,
uint ok
) public {
handlerCan[msg.sender][usr] = ok;
emit AllowHandler(
msg.sender,
usr,
ok
);
}
// Open a new safe for a given usr address.
function openSAFE(
bytes32 collateralType,
address usr
) public returns (uint) {
require(usr != address(0), "usr-address-0");
safei = add(safei, 1);
safes[safei] = address(new SAFEHandler(safeEngine));
ownsSAFE[safei] = usr;
collateralTypes[safei] = collateralType;
// Add new SAFE to double linked list and pointers
if (firstSAFEID[usr] == 0) {
firstSAFEID[usr] = safei;
}
if (lastSAFEID[usr] != 0) {
safeList[safei].prev = lastSAFEID[usr];
safeList[lastSAFEID[usr]].next = safei;
}
lastSAFEID[usr] = safei;
safeCount[usr] = add(safeCount[usr], 1);
emit OpenSAFE(msg.sender, usr, safei);
return safei;
}
// Give the safe ownership to a dst address.
function transferSAFEOwnership(
uint safe,
address dst
) public safeAllowed(safe) {
require(dst != address(0), "dst-address-0");
require(dst != ownsSAFE[safe], "dst-already-owner");
// Remove transferred SAFE from double linked list of origin user and pointers
if (safeList[safe].prev != 0) {
safeList[safeList[safe].prev].next = safeList[safe].next; // Set the next pointer of the prev safe (if exists) to the next of the transferred one
}
if (safeList[safe].next != 0) { // If wasn't the last one
safeList[safeList[safe].next].prev = safeList[safe].prev; // Set the prev pointer of the next safe to the prev of the transferred one
} else { // If was the last one
lastSAFEID[ownsSAFE[safe]] = safeList[safe].prev; // Update last pointer of the owner
}
if (firstSAFEID[ownsSAFE[safe]] == safe) { // If was the first one
firstSAFEID[ownsSAFE[safe]] = safeList[safe].next; // Update first pointer of the owner
}
safeCount[ownsSAFE[safe]] = sub(safeCount[ownsSAFE[safe]], 1);
// Transfer ownership
ownsSAFE[safe] = dst;
// Add transferred SAFE to double linked list of destiny user and pointers
safeList[safe].prev = lastSAFEID[dst];
safeList[safe].next = 0;
if (lastSAFEID[dst] != 0) {
safeList[lastSAFEID[dst]].next = safe;
}
if (firstSAFEID[dst] == 0) {
firstSAFEID[dst] = safe;
}
lastSAFEID[dst] = safe;
safeCount[dst] = add(safeCount[dst], 1);
emit TransferSAFEOwnership(
msg.sender,
safe,
dst
);
}
// Modify a SAFE's collateralization ratio while keeping the generated COIN or collateral freed in the SAFE handler address.
function modifySAFECollateralization(
uint safe,
int deltaCollateral,
int deltaDebt
) public safeAllowed(safe) {
address safeHandler = safes[safe];
SAFEEngineLike(safeEngine).modifySAFECollateralization(
collateralTypes[safe],
safeHandler,
safeHandler,
safeHandler,
deltaCollateral,
deltaDebt
);
emit ModifySAFECollateralization(
msg.sender,
safe,
deltaCollateral,
deltaDebt
);
}
// Transfer wad amount of safe collateral from the safe address to a dst address.
function transferCollateral(
uint safe,
address dst,
uint wad
) public safeAllowed(safe) {
SAFEEngineLike(safeEngine).transferCollateral(collateralTypes[safe], safes[safe], dst, wad);
emit TransferCollateral(
msg.sender,
safe,
dst,
wad
);
}
// Transfer wad amount of any type of collateral (collateralType) from the safe address to a dst address.
// This function has the purpose to take away collateral from the system that doesn't correspond to the safe but was sent there wrongly.
function transferCollateral(
bytes32 collateralType,
uint safe,
address dst,
uint wad
) public safeAllowed(safe) {
SAFEEngineLike(safeEngine).transferCollateral(collateralType, safes[safe], dst, wad);
emit TransferCollateral(
msg.sender,
collateralType,
safe,
dst,
wad
);
}
// Transfer rad amount of COIN from the safe address to a dst address.
function transferInternalCoins(
uint safe,
address dst,
uint rad
) public safeAllowed(safe) {
SAFEEngineLike(safeEngine).transferInternalCoins(safes[safe], dst, rad);
emit TransferInternalCoins(
msg.sender,
safe,
dst,
rad
);
}
// Quit the system, migrating the safe (lockedCollateral, generatedDebt) to a different dst handler
function quitSystem(
uint safe,
address dst
) public safeAllowed(safe) handlerAllowed(dst) {
(uint lockedCollateral, uint generatedDebt) = SAFEEngineLike(safeEngine).safes(collateralTypes[safe], safes[safe]);
int deltaCollateral = toInt(lockedCollateral);
int deltaDebt = toInt(generatedDebt);
SAFEEngineLike(safeEngine).transferSAFECollateralAndDebt(
collateralTypes[safe],
safes[safe],
dst,
deltaCollateral,
deltaDebt
);
emit QuitSystem(
msg.sender,
safe,
dst
);
}
// Import a position from src handler to the handler owned by safe
function enterSystem(
address src,
uint safe
) public handlerAllowed(src) safeAllowed(safe) {
(uint lockedCollateral, uint generatedDebt) = SAFEEngineLike(safeEngine).safes(collateralTypes[safe], src);
int deltaCollateral = toInt(lockedCollateral);
int deltaDebt = toInt(generatedDebt);
SAFEEngineLike(safeEngine).transferSAFECollateralAndDebt(
collateralTypes[safe],
src,
safes[safe],
deltaCollateral,
deltaDebt
);
emit EnterSystem(
msg.sender,
src,
safe
);
}
// Move a position from safeSrc handler to the safeDst handler
function moveSAFE(
uint safeSrc,
uint safeDst
) public safeAllowed(safeSrc) safeAllowed(safeDst) {
require(collateralTypes[safeSrc] == collateralTypes[safeDst], "non-matching-safes");
(uint lockedCollateral, uint generatedDebt) = SAFEEngineLike(safeEngine).safes(collateralTypes[safeSrc], safes[safeSrc]);
int deltaCollateral = toInt(lockedCollateral);
int deltaDebt = toInt(generatedDebt);
SAFEEngineLike(safeEngine).transferSAFECollateralAndDebt(
collateralTypes[safeSrc],
safes[safeSrc],
safes[safeDst],
deltaCollateral,
deltaDebt
);
emit MoveSAFE(
msg.sender,
safeSrc,
safeDst
);
}
// Choose a SAFE saviour inside LiquidationEngine for the SAFE with id 'safe'
function protectSAFE(
uint safe,
address liquidationEngine,
address saviour
) public safeAllowed(safe) {
LiquidationEngineLike(liquidationEngine).protectSAFE(
collateralTypes[safe],
safes[safe],
saviour
);
emit ProtectSAFE(
msg.sender,
safe,
liquidationEngine,
saviour
);
}
// Collect debtRewards from the id `safe`
function collectRewards(
uint safe,
address debtRewards
) public safeAllowed(safe) {
DebtRewardsLike(debtRewards).getRewards(safes[safe], msg.sender);
emit CollectRewards(
msg.sender,
safe,
debtRewards
);
}
}