forked from sky-ecosystem/dss-proxy-actions
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGebProxyActions.sol
More file actions
1299 lines (1189 loc) · 50.3 KB
/
Copy pathGebProxyActions.sol
File metadata and controls
1299 lines (1189 loc) · 50.3 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// GebProxyActions.sol
// 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;
import "ds-auth/auth.sol";
abstract contract CollateralLike {
function approve(address, uint) virtual public;
function transfer(address, uint) virtual public;
function transferFrom(address, address, uint) virtual public;
function deposit() virtual public payable;
function withdraw(uint) virtual public;
}
abstract contract ManagerLike {
function safeCan(address, uint, address) virtual public view returns (uint);
function collateralTypes(uint) virtual public view returns (bytes32);
function ownsSAFE(uint) virtual public view returns (address);
function safes(uint) virtual public view returns (address);
function safeEngine() virtual public view returns (address);
function openSAFE(bytes32, address) virtual public returns (uint);
function transferSAFEOwnership(uint, address) virtual public;
function allowSAFE(uint, address, uint) virtual public;
function allowHandler(address, uint) virtual public;
function modifySAFECollateralization(uint, int, int) virtual public;
function transferCollateral(uint, address, uint) virtual public;
function transferInternalCoins(uint, address, uint) virtual public;
function quitSystem(uint, address) virtual public;
function enterSystem(address, uint) virtual public;
function moveSAFE(uint, uint) virtual public;
function protectSAFE(uint, address, address) virtual public;
}
abstract contract SAFEEngineLike {
function canModifySAFE(address, address) virtual public view returns (uint);
function collateralTypes(bytes32) virtual public view returns (uint, uint, uint, uint, uint);
function coinBalance(address) virtual public view returns (uint);
function safes(bytes32, address) virtual public view returns (uint, uint);
function modifySAFECollateralization(bytes32, address, address, address, int, int) virtual public;
function approveSAFEModification(address) virtual public;
function transferInternalCoins(address, address, uint) virtual public;
}
abstract contract CollateralJoinLike {
function decimals() virtual public returns (uint);
function collateral() virtual public returns (CollateralLike);
function join(address, uint) virtual public payable;
function exit(address, uint) virtual public;
}
abstract contract GNTJoinLike {
function bags(address) virtual public view returns (address);
function make(address) virtual public returns (address);
}
abstract contract DSTokenLike {
function balanceOf(address) virtual public view returns (uint);
function approve(address, uint) virtual public;
function transfer(address, uint) virtual public returns (bool);
function transferFrom(address, address, uint) virtual public returns (bool);
}
abstract contract WethLike {
function balanceOf(address) virtual public view returns (uint);
function approve(address, uint) virtual public;
function transfer(address, uint) virtual public;
function transferFrom(address, address, uint) virtual public;
function deposit() virtual public payable;
function withdraw(uint) virtual public;
}
abstract contract CoinJoinLike {
function safeEngine() virtual public returns (SAFEEngineLike);
function systemCoin() virtual public returns (DSTokenLike);
function join(address, uint) virtual public payable;
function exit(address, uint) virtual public;
}
abstract contract ApproveSAFEModificationLike {
function approveSAFEModification(address) virtual public;
function denySAFEModification(address) virtual public;
}
abstract contract GlobalSettlementLike {
function collateralCashPrice(bytes32) virtual public view returns (uint);
function redeemCollateral(bytes32, uint) virtual public;
function freeCollateral(bytes32) virtual public;
function prepareCoinsForRedeeming(uint) virtual public;
function processSAFE(bytes32, address) virtual public;
}
abstract contract TaxCollectorLike {
function taxSingle(bytes32) virtual public returns (uint);
}
abstract contract CoinSavingsAccountLike {
function savings(address) virtual public view returns (uint);
function updateAccumulatedRate() virtual public returns (uint);
function deposit(uint) virtual public;
function withdraw(uint) virtual public;
}
abstract contract ProxyRegistryLike {
function proxies(address) virtual public view returns (address);
function build(address) virtual public returns (address);
}
abstract contract ProxyLike {
function owner() virtual public view returns (address);
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// WARNING: These functions meant to be used as a a library for a DSProxy. Some are unsafe if you call them directly.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
contract Common {
uint256 constant RAY = 10 ** 27;
// Internal functions
function multiply(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "mul-overflow");
}
function _coinJoin_join(address apt, address safeHandler, uint wad) internal {
// Approves adapter to take the COIN amount
CoinJoinLike(apt).systemCoin().approve(apt, wad);
// Joins COIN into the safeEngine
CoinJoinLike(apt).join(safeHandler, wad);
}
// Public functions
function coinJoin_join(address apt, address safeHandler, uint wad) public {
// Gets COIN from the user's wallet
CoinJoinLike(apt).systemCoin().transferFrom(msg.sender, address(this), wad);
_coinJoin_join(apt, safeHandler, wad);
}
}
contract BasicActions is Common {
// Internal functions
/// @notice Safe subtraction
/// @dev Reverts on overflows
function subtract(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "sub-overflow");
}
/// @notice Safe conversion uint -> int
/// @dev Reverts on overflows
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0, "int-overflow");
}
/// @notice Converts a wad (18 decimal places) to rad (45 decimal places)
function toRad(uint wad) internal pure returns (uint rad) {
rad = multiply(wad, 10 ** 27);
}
function convertTo18(address collateralJoin, uint256 amt) internal returns (uint256 wad) {
// For those collaterals that have other than 18 decimals precision we need to do the conversion before passing to modifySAFECollateralization function
// Adapters will automatically handle the difference of precision
uint decimals = CollateralJoinLike(collateralJoin).decimals();
wad = amt;
if (decimals < 18) {
wad = multiply(
amt,
10 ** (18 - decimals)
);
} else if (decimals > 18) {
wad = amt / 10 ** (decimals - 18);
}
}
/// @notice Gets delta debt generated (Total Safe debt minus available safeHandler COIN balance)
/// @param safeEngine address
/// @param taxCollector address
/// @param safeHandler address
/// @param collateralType bytes32
/// @return deltaDebt
function _getGeneratedDeltaDebt(
address safeEngine,
address taxCollector,
address safeHandler,
bytes32 collateralType,
uint wad
) internal returns (int deltaDebt) {
// Updates stability fee rate
uint rate = TaxCollectorLike(taxCollector).taxSingle(collateralType);
require(rate > 0, "invalid-collateral-type");
// Gets COIN balance of the handler in the safeEngine
uint coin = SAFEEngineLike(safeEngine).coinBalance(safeHandler);
// If there was already enough COIN in the safeEngine balance, just exits it without adding more debt
if (coin < multiply(wad, RAY)) {
// Calculates the needed deltaDebt so together with the existing coins in the safeEngine is enough to exit wad amount of COIN tokens
deltaDebt = toInt(subtract(multiply(wad, RAY), coin) / rate);
// This is neeeded due lack of precision. It might need to sum an extra deltaDebt wei (for the given COIN wad amount)
deltaDebt = multiply(uint(deltaDebt), rate) < multiply(wad, RAY) ? deltaDebt + 1 : deltaDebt;
}
}
/// @notice Gets repaid delta debt generated (rate adjusted debt)
/// @param safeEngine address
/// @param coin uint amount
/// @param safe uint - safeId
/// @param collateralType bytes32
/// @return deltaDebt
function _getRepaidDeltaDebt(
address safeEngine,
uint coin,
address safe,
bytes32 collateralType
) internal view returns (int deltaDebt) {
// Gets actual rate from the safeEngine
(, uint rate,,,) = SAFEEngineLike(safeEngine).collateralTypes(collateralType);
require(rate > 0, "invalid-collateral-type");
// Gets actual generatedDebt value of the safe
(, uint generatedDebt) = SAFEEngineLike(safeEngine).safes(collateralType, safe);
// Uses the whole coin balance in the safeEngine to reduce the debt
deltaDebt = toInt(coin / rate);
// Checks the calculated deltaDebt is not higher than safe.generatedDebt (total debt), otherwise uses its value
deltaDebt = uint(deltaDebt) <= generatedDebt ? - deltaDebt : - toInt(generatedDebt);
}
/// @notice Gets repaid debt (rate adjusted rate minus COIN balance available in usr's address)
/// @param safeEngine address
/// @param usr address
/// @param safe uint
/// @param collateralType address
/// @return wad
function _getRepaidAlDebt(
address safeEngine,
address usr,
address safe,
bytes32 collateralType
) internal view returns (uint wad) {
// Gets actual rate from the safeEngine
(, uint rate,,,) = SAFEEngineLike(safeEngine).collateralTypes(collateralType);
// Gets actual generatedDebt value of the safe
(, uint generatedDebt) = SAFEEngineLike(safeEngine).safes(collateralType, safe);
// Gets actual coin amount in the safe
uint coin = SAFEEngineLike(safeEngine).coinBalance(usr);
uint rad = subtract(multiply(generatedDebt, rate), coin);
wad = rad / RAY;
// If the rad precision has some dust, it will need to request for 1 extra wad wei
wad = multiply(wad, RAY) < rad ? wad + 1 : wad;
}
/// @notice Generates Debt (and sends coin balance to address to)
/// @param manager address
/// @param taxCollector address
/// @param coinJoin address
/// @param safe uint
/// @param wad uint - amount of debt to be generated
/// @param to address - receiver of the balance of generated COIN
function _generateDebt(address manager, address taxCollector, address coinJoin, uint safe, uint wad, address to) internal {
address safeHandler = ManagerLike(manager).safes(safe);
address safeEngine = ManagerLike(manager).safeEngine();
bytes32 collateralType = ManagerLike(manager).collateralTypes(safe);
// Generates debt in the SAFE
modifySAFECollateralization(manager, safe, 0, _getGeneratedDeltaDebt(safeEngine, taxCollector, safeHandler, collateralType, wad));
// Moves the COIN amount (balance in the safeEngine in rad) to proxy's address
transferInternalCoins(manager, safe, address(this), toRad(wad));
// Allows adapter to access to proxy's COIN balance in the safeEngine
if (SAFEEngineLike(safeEngine).canModifySAFE(address(this), address(coinJoin)) == 0) {
SAFEEngineLike(safeEngine).approveSAFEModification(coinJoin);
}
// Exits COIN to this contract
CoinJoinLike(coinJoin).exit(to, wad);
}
/// @notice Generates Debt (and sends coin balance to address to)
/// @param manager address
/// @param ethJoin address
/// @param safe uint
/// @param value uint - amount of ETH to be locked in the Safe.
/// @dev Proxy needs to have enough balance (> value), public functions should handle this.
function _lockETH(
address manager,
address ethJoin,
uint safe,
uint value
) internal {
// Receives ETH amount, converts it to WETH and joins it into the safeEngine
ethJoin_join(ethJoin, address(this), value);
// Locks WETH amount into the SAFE
SAFEEngineLike(ManagerLike(manager).safeEngine()).modifySAFECollateralization(
ManagerLike(manager).collateralTypes(safe),
ManagerLike(manager).safes(safe),
address(this),
address(this),
toInt(value),
0
);
}
/// @notice Repays debt
/// @param manager address
/// @param coinJoin address
/// @param safe uint
/// @param wad uint - amount of debt to be repayed
function _repayDebt(
address manager,
address coinJoin,
uint safe,
uint wad,
bool transferFromCaller
) internal {
address safeEngine = ManagerLike(manager).safeEngine();
address safeHandler = ManagerLike(manager).safes(safe);
bytes32 collateralType = ManagerLike(manager).collateralTypes(safe);
address own = ManagerLike(manager).ownsSAFE(safe);
if (own == address(this) || ManagerLike(manager).safeCan(own, safe, address(this)) == 1) {
// Joins COIN amount into the safeEngine
if (transferFromCaller) coinJoin_join(coinJoin, safeHandler, wad);
else _coinJoin_join(coinJoin, safeHandler, wad);
// // Paybacks debt to the SAFE
modifySAFECollateralization(manager, safe, 0, _getRepaidDeltaDebt(safeEngine, SAFEEngineLike(safeEngine).coinBalance(safeHandler), safeHandler, collateralType));
} else {
// Joins COIN amount into the safeEngine
if (transferFromCaller) coinJoin_join(coinJoin, address(this), wad);
else _coinJoin_join(coinJoin, address(this), wad);
// Paybacks debt to the SAFE
SAFEEngineLike(safeEngine).modifySAFECollateralization(
collateralType,
safeHandler,
address(this),
address(this),
0,
_getRepaidDeltaDebt(safeEngine, wad * RAY, safeHandler, collateralType)
);
}
}
/// @notice Repays debt and frees collateral ETH
/// @param manager address
/// @param ethJoin address
/// @param coinJoin address
/// @param safe uint
/// @param collateralWad uint - amount of ETH to free
/// @param deltaWad uint - amount of debt to be repayed
/// @param transferFromCaller True if transferring coin from caller, false if balance in the proxy
function _repayDebtAndFreeETH(
address manager,
address ethJoin,
address coinJoin,
uint safe,
uint collateralWad,
uint deltaWad,
bool transferFromCaller
) internal {
address safeHandler = ManagerLike(manager).safes(safe);
// Joins COIN amount into the safeEngine
if (transferFromCaller) coinJoin_join(coinJoin, safeHandler, deltaWad);
else _coinJoin_join(coinJoin, safeHandler, deltaWad);
// Paybacks debt to the SAFE and unlocks WETH amount from it
modifySAFECollateralization(
manager,
safe,
-toInt(collateralWad),
_getRepaidDeltaDebt(ManagerLike(manager).safeEngine(), SAFEEngineLike(ManagerLike(manager).safeEngine()).coinBalance(safeHandler), safeHandler, ManagerLike(manager).collateralTypes(safe))
);
// Moves the amount from the SAFE handler to proxy's address
transferCollateral(manager, safe, address(this), collateralWad);
// Exits WETH amount to proxy address as a token
CollateralJoinLike(ethJoin).exit(address(this), collateralWad);
// Converts WETH to ETH
CollateralJoinLike(ethJoin).collateral().withdraw(collateralWad);
}
// Public functions
/// @notice ERC20 transfer
/// @param collateral address - address of ERC20 collateral
/// @param dst address - Transfer destination
/// @param amt address - Amount to transfer
function transfer(address collateral, address dst, uint amt) external {
CollateralLike(collateral).transfer(dst, amt);
}
/// @notice Joins the system with the full msg.value
/// @param apt address - Address of the adapter
/// @param safe uint - Safe Id
function ethJoin_join(address apt, address safe) external payable {
ethJoin_join(apt, safe, msg.value);
}
/// @notice Joins the system with the a specified value
/// @param apt address - Address of the adapter
/// @param safe uint - Safe Id
/// @param value uint - Value to join
function ethJoin_join(address apt, address safe, uint value) public payable {
// Wraps ETH in WETH
CollateralJoinLike(apt).collateral().deposit{value: value}();
// Approves adapter to take the WETH amount
CollateralJoinLike(apt).collateral().approve(address(apt), value);
// Joins WETH collateral into the safeEngine
CollateralJoinLike(apt).join(safe, value);
}
/// @notice Approves an address to modify the Safe
/// @param safeEngine address
/// @param usr address - Address allowed to modify Safe
function approveSAFEModification(
address safeEngine,
address usr
) external {
ApproveSAFEModificationLike(safeEngine).approveSAFEModification(usr);
}
/// @notice Denies an address to modify the Safe
/// @param safeEngine address
/// @param usr address - Address disallowed to modify Safe
function denySAFEModification(
address safeEngine,
address usr
) external {
ApproveSAFEModificationLike(safeEngine).denySAFEModification(usr);
}
/// @notice Opens a brand new Safe
/// @param manager address - Safe Manager
/// @param collateralType bytes32 - collateral type
/// @param usr address - Owner of the safe
function openSAFE(
address manager,
bytes32 collateralType,
address usr
) public returns (uint safe) {
safe = ManagerLike(manager).openSAFE(collateralType, usr);
}
/// @notice Transfer the ownership of a proxy owned Safe
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param usr address - Owner of the safe
function transferSAFEOwnership(
address manager,
uint safe,
address usr
) public {
ManagerLike(manager).transferSAFEOwnership(safe, usr);
}
/// @notice Transfer the ownership to a new proxy owned by a different address
/// @param proxyRegistry address - Safe Manager
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param dst address - Owner of the new proxy
function transferSAFEOwnershipToProxy(
address proxyRegistry,
address manager,
uint safe,
address dst
) external {
// Gets actual proxy address
address proxy = ProxyRegistryLike(proxyRegistry).proxies(dst);
// Checks if the proxy address already existed and dst address is still the owner
if (proxy == address(0) || ProxyLike(proxy).owner() != dst) {
uint csize;
assembly {
csize := extcodesize(dst)
}
// We want to avoid creating a proxy for a contract address that might not be able to handle proxies, then losing the SAFE
require(csize == 0, "dst-is-a-contract");
// Creates the proxy for the dst address
proxy = ProxyRegistryLike(proxyRegistry).build(dst);
}
// Transfers SAFE to the dst proxy
transferSAFEOwnership(manager, safe, proxy);
}
/// @notice Allow/disallow a usr address to manage the safe
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param usr address - usr address
/// uint ok - 1 for allowed
function allowSAFE(
address manager,
uint safe,
address usr,
uint ok
) external {
ManagerLike(manager).allowSAFE(safe, usr, ok);
}
/// @notice Allow/disallow a usr address to quit to the sender handler
/// @param manager address - Safe Manager
/// @param usr address - usr address
/// uint ok - 1 for allowed
function allowHandler(
address manager,
address usr,
uint ok
) external {
ManagerLike(manager).allowHandler(usr, ok);
}
/// @notice Transfer wad amount of safe collateral from the safe address to a dst address.
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param dst address - destination address
/// uint wad - amount
function transferCollateral(
address manager,
uint safe,
address dst,
uint wad
) public {
ManagerLike(manager).transferCollateral(safe, dst, wad);
}
/// @notice Transfer rad amount of COIN from the safe address to a dst address.
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param dst address - destination address
/// uint rad - amount
function transferInternalCoins(
address manager,
uint safe,
address dst,
uint rad
) public {
ManagerLike(manager).transferInternalCoins(safe, dst, rad);
}
/// @notice Modify a SAFE's collateralization ratio while keeping the generated COIN or collateral freed in the SAFE handler address.
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param deltaCollateral - int
/// @param deltaDebt - int
function modifySAFECollateralization(
address manager,
uint safe,
int deltaCollateral,
int deltaDebt
) public {
ManagerLike(manager).modifySAFECollateralization(safe, deltaCollateral, deltaDebt);
}
/// @notice Quit the system, migrating the safe (lockedCollateral, generatedDebt) to a different dst handler
/// @param manager address - Safe Manager
/// @param safe uint - Safe Id
/// @param dst - destination handler
function quitSystem(
address manager,
uint safe,
address dst
) external {
ManagerLike(manager).quitSystem(safe, dst);
}
/// @notice Import a position from src handler to the handler owned by safe
/// @param manager address - Safe Manager
/// @param src - source handler
/// @param safe uint - Safe Id
function enterSystem(
address manager,
address src,
uint safe
) external {
ManagerLike(manager).enterSystem(src, safe);
}
/// @notice Move a position from safeSrc handler to the safeDst handler
/// @param manager address - Safe Manager
/// @param safeSrc uint - Source Safe Id
/// @param safeDst uint - Destination Safe Id
function moveSAFE(
address manager,
uint safeSrc,
uint safeDst
) external {
ManagerLike(manager).moveSAFE(safeSrc, safeDst);
}
/// @notice Lock ETH (msg.value) as collateral in safe
/// @param manager address - Safe Manager
/// @param ethJoin address
/// @param safe uint - Safe Id
function lockETH(
address manager,
address ethJoin,
uint safe
) public payable {
_lockETH(manager, ethJoin, safe, msg.value);
}
/// @notice Free ETH (wad) from safe and sends it to msg.sender
/// @param manager address - Safe Manager
/// @param ethJoin address
/// @param safe uint - Safe Id
/// @param wad uint - Amount
function freeETH(
address manager,
address ethJoin,
uint safe,
uint wad
) public {
// Unlocks WETH amount from the SAFE
modifySAFECollateralization(manager, safe, -toInt(wad), 0);
// Moves the amount from the SAFE handler to proxy's address
transferCollateral(manager, safe, address(this), wad);
// Exits WETH amount to proxy address as a token
CollateralJoinLike(ethJoin).exit(address(this), wad);
// Converts WETH to ETH
CollateralJoinLike(ethJoin).collateral().withdraw(wad);
// Sends ETH back to the user's wallet
msg.sender.transfer(wad);
}
/// @notice Exits ETH (wad) from balance available in the handler
/// @param manager address - Safe Manager
/// @param ethJoin address
/// @param safe uint - Safe Id
/// @param wad uint - Amount
function exitETH(
address manager,
address ethJoin,
uint safe,
uint wad
) external {
// Moves the amount from the SAFE handler to proxy's address
transferCollateral(manager, safe, address(this), wad);
// Exits WETH amount to proxy address as a token
CollateralJoinLike(ethJoin).exit(address(this), wad);
// Converts WETH to ETH
CollateralJoinLike(ethJoin).collateral().withdraw(wad);
// Sends ETH back to the user's wallet
msg.sender.transfer(wad);
}
/// @notice Generates debt and sends COIN amount to msg.sender
/// @param manager address
/// @param taxCollector address
/// @param coinJoin address
/// @param safe uint - Safe Id
/// @param wad uint - Amount
function generateDebt(
address manager,
address taxCollector,
address coinJoin,
uint safe,
uint wad
) public {
_generateDebt(manager, taxCollector, coinJoin, safe, wad, msg.sender);
}
/// @notice Repays debt
/// @param manager address
/// @param coinJoin address
/// @param safe uint - Safe Id
/// @param wad uint - Amount
function repayDebt(
address manager,
address coinJoin,
uint safe,
uint wad
) public {
_repayDebt(manager, coinJoin, safe, wad, true);
}
/// @notice Locks Eth, generates debt and sends COIN amount (deltaWad) to msg.sender
/// @param manager address
/// @param taxCollector address
/// @param ethJoin address
/// @param coinJoin address
/// @param safe uint - Safe Id
/// @param deltaWad uint - Amount
function lockETHAndGenerateDebt(
address manager,
address taxCollector,
address ethJoin,
address coinJoin,
uint safe,
uint deltaWad
) public payable {
_lockETH(manager, ethJoin, safe, msg.value);
_generateDebt(manager, taxCollector, coinJoin, safe, deltaWad, msg.sender);
}
/// @notice Opens Safe, locks Eth, generates debt and sends COIN amount (deltaWad) to msg.sender
/// @param manager address
/// @param taxCollector address
/// @param ethJoin address
/// @param coinJoin address
/// @param deltaWad uint - Amount
function openLockETHAndGenerateDebt(
address manager,
address taxCollector,
address ethJoin,
address coinJoin,
bytes32 collateralType,
uint deltaWad
) external payable returns (uint safe) {
safe = openSAFE(manager, collateralType, address(this));
lockETHAndGenerateDebt(manager, taxCollector, ethJoin, coinJoin, safe, deltaWad);
}
/// @notice Repays debt and frees ETH (sends it to msg.sender)
/// @param manager address
/// @param ethJoin address
/// @param coinJoin address
/// @param safe uint - Safe Id
/// @param collateralWad uint - Amount of collateral to free
/// @param deltaWad uint - Amount of debt to repay
function repayDebtAndFreeETH(
address manager,
address ethJoin,
address coinJoin,
uint safe,
uint collateralWad,
uint deltaWad
) external {
_repayDebtAndFreeETH(manager, ethJoin, coinJoin, safe, collateralWad, deltaWad, true);
// Sends ETH back to the user's wallet
msg.sender.transfer(collateralWad);
}
}
contract GebProxyActions is BasicActions {
function tokenCollateralJoin_join(address apt, address safe, uint amt, bool transferFrom) public {
// Only executes for tokens that have approval/transferFrom implementation
if (transferFrom) {
// Gets token from the user's wallet
CollateralJoinLike(apt).collateral().transferFrom(msg.sender, address(this), amt);
// Approves adapter to take the token amount
CollateralJoinLike(apt).collateral().approve(apt, amt);
}
// Joins token collateral into the safeEngine
CollateralJoinLike(apt).join(safe, amt);
}
function protectSAFE(
address manager,
uint safe,
address liquidationEngine,
address saviour
) public {
ManagerLike(manager).protectSAFE(safe, liquidationEngine, saviour);
}
function makeCollateralBag(
address collateralJoin
) public returns (address bag) {
bag = GNTJoinLike(collateralJoin).make(address(this));
}
function safeLockETH(
address manager,
address ethJoin,
uint safe,
address owner
) public payable {
require(ManagerLike(manager).ownsSAFE(safe) == owner, "owner-missmatch");
lockETH(manager, ethJoin, safe);
}
function lockTokenCollateral(
address manager,
address collateralJoin,
uint safe,
uint amt,
bool transferFrom
) public {
// Takes token amount from user's wallet and joins into the safeEngine
tokenCollateralJoin_join(collateralJoin, address(this), amt, transferFrom);
// Locks token amount into the SAFE
SAFEEngineLike(ManagerLike(manager).safeEngine()).modifySAFECollateralization(
ManagerLike(manager).collateralTypes(safe),
ManagerLike(manager).safes(safe),
address(this),
address(this),
toInt(convertTo18(collateralJoin, amt)),
0
);
}
function safeLockTokenCollateral(
address manager,
address collateralJoin,
uint safe,
uint amt,
bool transferFrom,
address owner
) public {
require(ManagerLike(manager).ownsSAFE(safe) == owner, "owner-missmatch");
lockTokenCollateral(manager, collateralJoin, safe, amt, transferFrom);
}
function freeTokenCollateral(
address manager,
address collateralJoin,
uint safe,
uint amt
) public {
uint wad = convertTo18(collateralJoin, amt);
// Unlocks token amount from the SAFE
modifySAFECollateralization(manager, safe, -toInt(wad), 0);
// Moves the amount from the SAFE handler to proxy's address
transferCollateral(manager, safe, address(this), wad);
// Exits token amount to the user's wallet as a token
CollateralJoinLike(collateralJoin).exit(msg.sender, amt);
}
function exitTokenCollateral(
address manager,
address collateralJoin,
uint safe,
uint amt
) public {
// Moves the amount from the SAFE handler to proxy's address
transferCollateral(manager, safe, address(this), convertTo18(collateralJoin, amt));
// Exits token amount to the user's wallet as a token
CollateralJoinLike(collateralJoin).exit(msg.sender, amt);
}
function generateDebtAndProtectSAFE(
address manager,
address taxCollector,
address coinJoin,
uint safe,
uint wad,
address liquidationEngine,
address saviour
) external {
generateDebt(manager, taxCollector, coinJoin, safe, wad);
protectSAFE(manager, safe, liquidationEngine, saviour);
}
function safeRepayDebt(
address manager,
address coinJoin,
uint safe,
uint wad,
address owner
) public {
require(ManagerLike(manager).ownsSAFE(safe) == owner, "owner-missmatch");
repayDebt(manager, coinJoin, safe, wad);
}
function repayAllDebt(
address manager,
address coinJoin,
uint safe
) public {
address safeEngine = ManagerLike(manager).safeEngine();
address safeHandler = ManagerLike(manager).safes(safe);
bytes32 collateralType = ManagerLike(manager).collateralTypes(safe);
(, uint generatedDebt) = SAFEEngineLike(safeEngine).safes(collateralType, safeHandler);
address own = ManagerLike(manager).ownsSAFE(safe);
if (own == address(this) || ManagerLike(manager).safeCan(own, safe, address(this)) == 1) {
// Joins COIN amount into the safeEngine
coinJoin_join(coinJoin, safeHandler, _getRepaidAlDebt(safeEngine, safeHandler, safeHandler, collateralType));
// Paybacks debt to the SAFE
modifySAFECollateralization(manager, safe, 0, -int(generatedDebt));
} else {
// Joins COIN amount into the safeEngine
coinJoin_join(coinJoin, address(this), _getRepaidAlDebt(safeEngine, address(this), safeHandler, collateralType));
// Paybacks debt to the SAFE
SAFEEngineLike(safeEngine).modifySAFECollateralization(
collateralType,
safeHandler,
address(this),
address(this),
0,
-int(generatedDebt)
);
}
}
function safeRepayAllDebt(
address manager,
address coinJoin,
uint safe,
address owner
) public {
require(ManagerLike(manager).ownsSAFE(safe) == owner, "owner-missmatch");
repayAllDebt(manager, coinJoin, safe);
}
function openLockETHGenerateDebtAndProtectSAFE(
address manager,
address taxCollector,
address ethJoin,
address coinJoin,
bytes32 collateralType,
uint deltaWad,
address liquidationEngine,
address saviour
) public payable returns (uint safe) {
safe = openSAFE(manager, collateralType, address(this));
lockETHAndGenerateDebt(manager, taxCollector, ethJoin, coinJoin, safe, deltaWad);
protectSAFE(manager, safe, liquidationEngine, saviour);
}
function lockTokenCollateralAndGenerateDebt(
address manager,
address taxCollector,
address collateralJoin,
address coinJoin,
uint safe,
uint collateralAmount,
uint deltaWad,
bool transferFrom
) public {
address safeHandler = ManagerLike(manager).safes(safe);
address safeEngine = ManagerLike(manager).safeEngine();
bytes32 collateralType = ManagerLike(manager).collateralTypes(safe);
// Takes token amount from user's wallet and joins into the safeEngine
tokenCollateralJoin_join(collateralJoin, safeHandler, collateralAmount, transferFrom);
// Locks token amount into the SAFE and generates debt
modifySAFECollateralization(manager, safe, toInt(convertTo18(collateralJoin, collateralAmount)), _getGeneratedDeltaDebt(safeEngine, taxCollector, safeHandler, collateralType, deltaWad));
// Moves the COIN amount (balance in the safeEngine in rad) to proxy's address
transferInternalCoins(manager, safe, address(this), toRad(deltaWad));
// Allows adapter to access to proxy's COIN balance in the safeEngine
if (SAFEEngineLike(safeEngine).canModifySAFE(address(this), address(coinJoin)) == 0) {
SAFEEngineLike(safeEngine).approveSAFEModification(coinJoin);
}
// Exits COIN to the user's wallet as a token
CoinJoinLike(coinJoin).exit(msg.sender, deltaWad);
}
function lockTokenCollateralGenerateDebtAndProtectSAFE(
address manager,
address taxCollector,
address collateralJoin,
address coinJoin,
uint safe,
uint collateralAmount,
uint deltaWad,
bool transferFrom,
address liquidationEngine,
address saviour
) public {
lockTokenCollateralAndGenerateDebt(
manager,
taxCollector,
collateralJoin,
coinJoin,
safe,
collateralAmount,
deltaWad,
transferFrom
);
protectSAFE(manager, safe, liquidationEngine, saviour);
}
function openLockTokenCollateralAndGenerateDebt(
address manager,
address taxCollector,
address collateralJoin,
address coinJoin,
bytes32 collateralType,
uint collateralAmount,
uint deltaWad,
bool transferFrom
) public returns (uint safe) {
safe = openSAFE(manager, collateralType, address(this));
lockTokenCollateralAndGenerateDebt(manager, taxCollector, collateralJoin, coinJoin, safe, collateralAmount, deltaWad, transferFrom);
}
function openLockTokenCollateralGenerateDebtAndProtectSAFE(
address manager,
address taxCollector,
address collateralJoin,
address coinJoin,
bytes32 collateralType,
uint collateralAmount,
uint deltaWad,
bool transferFrom,
address liquidationEngine,