This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
BigBang.sol
825 lines (722 loc) · 28.7 KB
/
BigBang.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
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
import "@boringcrypto/boring-solidity/contracts/BoringOwnable.sol";
import "@boringcrypto/boring-solidity/contracts/ERC20.sol";
import "tapioca-periph/contracts/interfaces/IBigBang.sol";
import "tapioca-periph/contracts/interfaces/ISendFrom.sol";
import "tapioca-periph/contracts/interfaces/ISwapper.sol";
import {IUSDOBase} from "tapioca-periph/contracts/interfaces/IUSDO.sol";
import "../Market.sol";
// solhint-disable max-line-length
/*
__/\\\\\\\\\\\\\\\_____/\\\\\\\\\_____/\\\\\\\\\\\\\____/\\\\\\\\\\\_______/\\\\\_____________/\\\\\\\\\_____/\\\\\\\\\____
_\///////\\\/////____/\\\\\\\\\\\\\__\/\\\/////////\\\_\/////\\\///______/\\\///\\\________/\\\////////____/\\\\\\\\\\\\\__
_______\/\\\________/\\\/////////\\\_\/\\\_______\/\\\_____\/\\\_______/\\\/__\///\\\____/\\\/____________/\\\/////////\\\_
_______\/\\\_______\/\\\_______\/\\\_\/\\\\\\\\\\\\\/______\/\\\______/\\\______\//\\\__/\\\_____________\/\\\_______\/\\\_
_______\/\\\_______\/\\\\\\\\\\\\\\\_\/\\\/////////________\/\\\_____\/\\\_______\/\\\_\/\\\_____________\/\\\\\\\\\\\\\\\_
_______\/\\\_______\/\\\/////////\\\_\/\\\_________________\/\\\_____\//\\\______/\\\__\//\\\____________\/\\\/////////\\\_
_______\/\\\_______\/\\\_______\/\\\_\/\\\_________________\/\\\______\///\\\__/\\\_____\///\\\__________\/\\\_______\/\\\_
_______\/\\\_______\/\\\_______\/\\\_\/\\\______________/\\\\\\\\\\\____\///\\\\\/________\////\\\\\\\\\_\/\\\_______\/\\\_
_______\///________\///________\///__\///______________\///////////_______\/////_____________\/////////__\///________\///__
*/
/// @title BigBang market
/// @notice Adaptation of the Singularity contract
/// @dev owner of the contract is Penrose
/// - the borrow action performs a mint on USDO
/// - the repay action performs a burn on USDO
/// - interest rate is not fixed, but dynamic based on the main BigBang market, minDebtRate, maxDebtRate and debtRateAgainstEthMarket
/// - BigBang markets can either be main or secondary markets; the main market is set on Penrose and has a fixed rate
/// - BigBang secondary markets has a dynamic interest rate which is starts from `minDebtRate` to `maxDebtRate`
/// - if current debt is over _maxDebtPoint = (_ethMarketTotalDebt * debtRateAgainstEthMarket) / 1e18, the interest rate is automatically `maxDebtRate`
/// - simulation: https://dotnetfiddle.net/cuKVpf
contract BigBang is BoringOwnable, Market {
using RebaseLibrary for Rebase;
using BoringERC20 for IERC20;
// ************ //
// *** VARS *** //
// ************ //
mapping(address => mapping(address => bool)) public operators;
IBigBang.AccrueInfo public accrueInfo;
uint256 public totalFees;
bool private _isEthMarket;
uint256 public maxDebtRate;
uint256 public minDebtRate;
uint256 public debtRateAgainstEthMarket;
uint256 public debtStartPoint;
uint256 private constant DEBT_PRECISION = 1e18;
// ************** //
// *** EVENTS *** //
// ************** //
/// @notice event emitted when accrue is called
event LogAccrue(uint256 accruedAmount, uint64 rate);
/// @notice event emitted when collateral is added
event LogAddCollateral(
address indexed from,
address indexed to,
uint256 share
);
/// @notice event emitted when collateral is removed
event LogRemoveCollateral(
address indexed from,
address indexed to,
uint256 share
);
/// @notice event emitted when borrow is performed
event LogBorrow(
address indexed from,
address indexed to,
uint256 amount,
uint256 feeAmount,
uint256 part
);
/// @notice event emitted when a repay operation is performed
event LogRepay(
address indexed from,
address indexed to,
uint256 amount,
uint256 part
);
/// @notice event emitted when the minimum debt rate is updated
event MinDebtRateUpdated(uint256 oldVal, uint256 newVal);
/// @notice event emitted when the maximum debt rate is updated
event MaxDebtRateUpdated(uint256 oldVal, uint256 newVal);
/// @notice event emitted when the debt rate against the main market is updated
event DebtRateAgainstEthUpdated(uint256 oldVal, uint256 newVal);
constructor() MarketERC20("Tapioca BigBang") {}
/// @notice The init function that acts as a constructor
function init(bytes calldata data) external onlyOnce {
(
IPenrose tapiocaBar_,
IERC20 _collateral,
uint256 _collateralId,
IOracle _oracle,
uint256 _exchangeRatePrecision,
uint256 _debtRateAgainstEth,
uint256 _debtRateMin,
uint256 _debtRateMax,
uint256 _debtStartPoint
) = abi.decode(
data,
(
IPenrose,
IERC20,
uint256,
IOracle,
uint256,
uint256,
uint256,
uint256,
uint256
)
);
penrose = tapiocaBar_;
yieldBox = YieldBox(tapiocaBar_.yieldBox());
owner = address(penrose);
address _asset = penrose.usdoToken();
require(
address(_collateral) != address(0) &&
address(_asset) != address(0) &&
address(_oracle) != address(0),
"BigBang: bad pair"
);
asset = IERC20(_asset);
assetId = penrose.usdoAssetId();
collateral = _collateral;
collateralId = _collateralId;
oracle = _oracle;
updateExchangeRate();
callerFee = 90000; // 90%
protocolFee = 10000; // 10%
collateralizationRate = 75000; // 75%
EXCHANGE_RATE_PRECISION = _exchangeRatePrecision > 0
? _exchangeRatePrecision
: 1e18;
_isEthMarket = collateralId == penrose.wethAssetId();
if (!_isEthMarket) {
debtRateAgainstEthMarket = _debtRateAgainstEth;
maxDebtRate = _debtRateMax;
minDebtRate = _debtRateMin;
debtStartPoint = _debtStartPoint;
}
minLiquidatorReward = 1e3;
maxLiquidatorReward = 1e4;
liquidationBonusAmount = 1e4;
borrowOpeningFee = 50; // 0.05%
liquidationMultiplier = 12000; //12%
}
// ********************** //
// *** VIEW FUNCTIONS *** //
// ********************** //
/// @notice returns total market debt
function getTotalDebt() external view returns (uint256) {
return totalBorrow.elastic;
}
/// @notice returns the current debt rate
function getDebtRate() public view returns (uint256) {
if (_isEthMarket) return penrose.bigBangEthDebtRate(); // default 0.5%
if (totalBorrow.elastic == 0) return minDebtRate;
uint256 _ethMarketTotalDebt = BigBang(penrose.bigBangEthMarket())
.getTotalDebt();
uint256 _currentDebt = totalBorrow.elastic;
uint256 _maxDebtPoint = (_ethMarketTotalDebt *
debtRateAgainstEthMarket) / 1e18;
if (_currentDebt >= _maxDebtPoint) return maxDebtRate;
uint256 debtPercentage = ((_currentDebt - debtStartPoint) *
DEBT_PRECISION) / (_maxDebtPoint - debtStartPoint);
uint256 debt = ((maxDebtRate - minDebtRate) * debtPercentage) /
DEBT_PRECISION +
minDebtRate;
if (debt > maxDebtRate) return maxDebtRate;
return debt;
}
// ************************ //
// *** PUBLIC FUNCTIONS *** //
// ************************ //
/// @notice Allows batched call to BingBang.
/// @param calls An array encoded call data.
/// @param revertOnFail If True then reverts after a failed call and stops doing further calls.
function execute(
bytes[] calldata calls,
bool revertOnFail
) external returns (bool[] memory successes, string[] memory results) {
successes = new bool[](calls.length);
results = new string[](calls.length);
for (uint256 i = 0; i < calls.length; i++) {
(bool success, bytes memory result) = address(this).delegatecall(
calls[i]
);
require(success || !revertOnFail, _getRevertMsg(result));
successes[i] = success;
results[i] = _getRevertMsg(result);
}
}
/// @notice allows 'operator' to act on behalf of the sender
/// @param status true/false
function updateOperator(address operator, bool status) external {
operators[msg.sender][operator] = status;
}
/// @notice Accrues the interest on the borrowed tokens and handles the accumulation of fees.
function accrue() public {
_accrue();
}
/// @notice Sender borrows `amount` and transfers it to `to`.
/// @param from Account to borrow for.
/// @param to The receiver of borrowed tokens.
/// @param amount Amount to borrow.
/// @return part Total part of the debt held by borrowers.
/// @return share Total amount in shares borrowed.
function borrow(
address from,
address to,
uint256 amount
) public notPaused solvent(from) returns (uint256 part, uint256 share) {
uint256 allowanceShare = _computeAllowanceAmountInAsset(
from,
exchangeRate,
amount,
asset.safeDecimals()
);
_allowedBorrow(from, allowanceShare);
(part, share) = _borrow(from, to, amount);
}
/// @notice Repays a loan.
/// @dev The bool param is not used but we added it to respect the ISingularity interface for MarketsHelper compatibility
/// @param from Address to repay from.
/// @param to Address of the user this payment should go.
/// @param part The amount to repay. See `userBorrowPart`.
/// @return amount The total amount repayed.
function repay(
address from,
address to,
bool,
uint256 part
) public notPaused allowedBorrow(from, part) returns (uint256 amount) {
updateExchangeRate();
accrue();
amount = _repay(from, to, part);
}
/// @notice Adds `collateral` from msg.sender to the account `to`.
/// @param from Account to transfer shares from.
/// @param to The receiver of the tokens.
/// @param skim True if the amount should be skimmed from the deposit balance of msg.sender.
/// False if tokens from msg.sender in `yieldBox` should be transferred.
/// @param share The amount of shares to add for `to`.
function addCollateral(
address from,
address to,
bool skim,
uint256 amount,
uint256 share
) public allowedBorrow(from, share) notPaused {
_addCollateral(from, to, skim, amount, share);
}
/// @notice Removes `share` amount of collateral and transfers it to `to`.
/// @param from Account to debit collateral from.
/// @param to The receiver of the shares.
/// @param share Amount of shares to remove.
function removeCollateral(
address from,
address to,
uint256 share
) public notPaused solvent(from) allowedBorrow(from, share) {
_removeCollateral(from, to, share);
}
/// @notice Entry point for liquidations.
/// @param users An array of user addresses.
/// @param maxBorrowParts A one-to-one mapping to `users`, contains maximum (partial) borrow amounts (to liquidate) of the respective user.
/// @param swapper Contract address of the `MultiSwapper` implementation. See `setSwapper`.
/// @param collateralToAssetSwapData Extra swap data
function liquidate(
address[] calldata users,
uint256[] calldata maxBorrowParts,
ISwapper swapper,
bytes calldata collateralToAssetSwapData
) external notPaused {
// Oracle can fail but we still need to allow liquidations
(, uint256 _exchangeRate) = updateExchangeRate();
_accrue();
_closedLiquidation(
users,
maxBorrowParts,
swapper,
_exchangeRate,
collateralToAssetSwapData
);
}
/// @notice Lever up: Borrow more and buy collateral with it.
/// @param from The user who buys
/// @param borrowAmount Amount of extra asset borrowed
/// @param supplyAmount Amount of asset supplied (down payment)
/// @param minAmountOut Mininal collateral amount to receive
/// @param swapper Swapper to execute the purchase
/// @param dexData Additional data to pass to the swapper
/// @return amountOut Actual collateral amount purchased
function buyCollateral(
address from,
uint256 borrowAmount,
uint256 supplyAmount,
uint256 minAmountOut,
ISwapper swapper,
bytes calldata dexData
) external notPaused solvent(from) returns (uint256 amountOut) {
require(penrose.swappers(swapper), "SGL: Invalid swapper");
// Let this fail first to save gas:
uint256 supplyShare = yieldBox.toShare(assetId, supplyAmount, true);
if (supplyShare > 0) {
yieldBox.transfer(from, address(swapper), assetId, supplyShare);
}
uint256 borrowShare;
(, borrowShare) = _borrow(from, address(swapper), borrowAmount);
ISwapper.SwapData memory swapData = swapper.buildSwapData(
assetId,
collateralId,
0,
supplyShare + borrowShare,
true,
true
);
uint256 collateralShare;
(amountOut, collateralShare) = swapper.swap(
swapData,
minAmountOut,
from,
dexData
);
require(amountOut >= minAmountOut, "SGL: not enough");
_allowedBorrow(from, collateralShare);
_addCollateral(from, from, false, 0, collateralShare);
}
/// @notice Lever down: Sell collateral to repay debt; excess goes to YB
/// @param from The user who sells
/// @param share Collateral YieldBox-shares to sell
/// @param minAmountOut Mininal proceeds required for the sale
/// @param swapper Swapper to execute the sale
/// @param dexData Additional data to pass to the swapper
/// @return amountOut Actual asset amount received in the sale
function sellCollateral(
address from,
uint256 share,
uint256 minAmountOut,
ISwapper swapper,
bytes calldata dexData
) external notPaused solvent(from) returns (uint256 amountOut) {
require(penrose.swappers(swapper), "SGL: Invalid swapper");
_allowedBorrow(from, share);
_removeCollateral(from, address(swapper), share);
ISwapper.SwapData memory swapData = swapper.buildSwapData(
collateralId,
assetId,
0,
share,
true,
true
);
uint256 shareOut;
(amountOut, shareOut) = swapper.swap(
swapData,
minAmountOut,
from,
dexData
);
// As long as the ratio is correct, we trust `amountOut` resp.
// `shareOut`, because all money received by the swapper gets used up
// one way or another, or the transaction will revert.
require(amountOut >= minAmountOut, "SGL: not enough");
uint256 partOwed = userBorrowPart[from];
uint256 amountOwed = totalBorrow.toElastic(partOwed, true);
uint256 shareOwed = yieldBox.toShare(assetId, amountOwed, true);
if (shareOwed <= shareOut) {
_repay(from, from, partOwed);
} else {
//repay as much as we can
uint256 partOut = totalBorrow.toBase(amountOut, false);
_repay(from, from, partOut);
}
}
function transfer(
address to,
uint256 amount
) public override returns (bool) {}
function transferFrom(
address from,
address to,
uint256 amount
) public override returns (bool) {}
// ************************* //
// *** OWNER FUNCTIONS ***** //
// ************************* //
/// @notice Transfers fees to penrose
function refreshPenroseFees(
address
) external onlyOwner notPaused returns (uint256 feeShares) {
uint256 balance = asset.balanceOf(address(this));
totalFees += balance;
feeShares = yieldBox.toShare(assetId, totalFees, false);
if (totalFees > 0) {
asset.approve(address(yieldBox), totalFees);
yieldBox.depositAsset(
assetId,
address(this),
msg.sender,
totalFees,
0
);
totalFees = 0;
}
}
/// @notice sets BigBang specific configuration
/// @dev values are updated only if > 0 or not address(0)
function setBigBangConfig(
uint256 _minDebtRate,
uint256 _maxDebtRate,
uint256 _debtRateAgainstEthMarket,
uint256 _liquidationMultiplier
) external onlyOwner {
_isEthMarket = collateralId == penrose.wethAssetId();
if (!_isEthMarket) {
if (_minDebtRate > 0) {
require(_minDebtRate < maxDebtRate, "BigBang: not valid");
emit MinDebtRateUpdated(minDebtRate, _minDebtRate);
minDebtRate = _minDebtRate;
}
if (_maxDebtRate > 0) {
require(_maxDebtRate > minDebtRate, "BigBang: not valid");
emit MaxDebtRateUpdated(maxDebtRate, _maxDebtRate);
maxDebtRate = _maxDebtRate;
}
if (_debtRateAgainstEthMarket > 0) {
emit DebtRateAgainstEthUpdated(
debtRateAgainstEthMarket,
_debtRateAgainstEthMarket
);
debtRateAgainstEthMarket = _debtRateAgainstEthMarket;
}
if (_liquidationMultiplier > 0) {
require(
_liquidationMultiplier < FEE_PRECISION,
"BigBang: not valid"
);
emit LiquidationMultiplierUpdated(
liquidationMultiplier,
_liquidationMultiplier
);
liquidationMultiplier = _liquidationMultiplier;
}
}
}
// ************************* //
// *** PRIVATE FUNCTIONS *** //
// ************************* //
function _accrue() internal override {
IBigBang.AccrueInfo memory _accrueInfo = accrueInfo;
// Number of seconds since accrue was called
uint256 elapsedTime = block.timestamp - _accrueInfo.lastAccrued;
if (elapsedTime == 0) {
return;
}
//update debt rate
uint256 annumDebtRate = getDebtRate();
_accrueInfo.debtRate = uint64(annumDebtRate / 31536000); //per second
_accrueInfo.lastAccrued = uint64(block.timestamp);
Rebase memory _totalBorrow = totalBorrow;
uint256 extraAmount = 0;
// Calculate fees
extraAmount =
(uint256(_totalBorrow.elastic) *
_accrueInfo.debtRate *
elapsedTime) /
1e18;
_totalBorrow.elastic += uint128(extraAmount);
totalBorrow = _totalBorrow;
accrueInfo = _accrueInfo;
emit LogAccrue(extraAmount, _accrueInfo.debtRate);
}
function _addCollateral(
address from,
address to,
bool skim,
uint256 amount,
uint256 share
) internal {
if (share == 0) {
share = yieldBox.toShare(collateralId, amount, false);
}
userCollateralShare[to] += share;
uint256 oldTotalCollateralShare = totalCollateralShare;
totalCollateralShare = oldTotalCollateralShare + share;
_addTokens(from, collateralId, share, oldTotalCollateralShare, skim);
emit LogAddCollateral(skim ? address(yieldBox) : from, to, share);
}
function _liquidateUser(
address user,
uint256 maxBorrowPart,
ISwapper swapper,
uint256 _exchangeRate,
bytes calldata _dexData
) private {
if (_isSolvent(user, _exchangeRate)) return;
(
uint256 startTVLInAsset,
uint256 maxTVLInAsset
) = _computeMaxAndMinLTVInAsset(
userCollateralShare[user],
_exchangeRate
);
uint256 callerReward = _getCallerReward(
userBorrowPart[user],
startTVLInAsset,
maxTVLInAsset
);
(
uint256 borrowAmount,
uint256 borrowPart,
uint256 collateralShare
) = _updateBorrowAndCollateralShare(user, maxBorrowPart, _exchangeRate);
emit LogRemoveCollateral(user, address(swapper), collateralShare);
emit LogRepay(address(swapper), user, borrowAmount, borrowPart);
uint256 borrowShare = yieldBox.toShare(assetId, borrowAmount, true);
// Closed liquidation using a pre-approved swapper
require(penrose.swappers(swapper), "BigBang: Invalid swapper");
// Swaps the users collateral for the borrowed asset
yieldBox.transfer(
address(this),
address(swapper),
collateralId,
collateralShare
);
uint256 minAssetMount = 0;
if (_dexData.length > 0) {
minAssetMount = abi.decode(_dexData, (uint256));
}
uint256 balanceBefore = yieldBox.balanceOf(address(this), assetId);
ISwapper.SwapData memory swapData = swapper.buildSwapData(
collateralId,
assetId,
0,
collateralShare,
true,
true
);
swapper.swap(swapData, minAssetMount, address(this), "");
uint256 balanceAfter = yieldBox.balanceOf(address(this), assetId);
uint256 returnedShare = balanceAfter - balanceBefore;
(uint256 feeShare, uint256 callerShare) = _extractLiquidationFees(
returnedShare,
borrowShare,
callerReward
);
address[] memory _users = new address[](1);
_users[0] = user;
emit Liquidated(
msg.sender,
_users,
callerShare,
feeShare,
borrowAmount,
collateralShare
);
}
function _extractLiquidationFees(
uint256 returnedShare,
uint256 borrowShare,
uint256 callerReward
) private returns (uint256 feeShare, uint256 callerShare) {
uint256 extraShare = returnedShare - borrowShare;
feeShare = (extraShare * protocolFee) / FEE_PRECISION; // x% of profit goes to fee.
callerShare = (extraShare * callerReward) / FEE_PRECISION; // y% of profit goes to caller.
yieldBox.transfer(address(this), penrose.feeTo(), assetId, feeShare);
yieldBox.transfer(address(this), msg.sender, assetId, callerShare);
}
/// @notice Handles the liquidation of users' balances, once the users' amount of collateral is too low.
/// @dev Closed liquidations Only, 90% of extra shares goes to caller and 10% to protocol
/// @param users An array of user addresses.
/// @param maxBorrowParts A one-to-one mapping to `users`, contains maximum (partial) borrow amounts (to liquidate) of the respective user.
/// @param swapper Contract address of the `MultiSwapper` implementation. See `setSwapper`.
/// @param swapData Swap necessar data
function _closedLiquidation(
address[] calldata users,
uint256[] calldata maxBorrowParts,
ISwapper swapper,
uint256 _exchangeRate,
bytes calldata swapData
) private {
uint256 liquidatedCount = 0;
for (uint256 i = 0; i < users.length; i++) {
address user = users[i];
if (!_isSolvent(user, _exchangeRate)) {
liquidatedCount++;
_liquidateUser(
user,
maxBorrowParts[i],
swapper,
_exchangeRate,
swapData
);
}
}
require(liquidatedCount > 0, "SGL: no users found");
}
/// @dev Helper function to move tokens.
/// @param from Account to debit tokens from, in `yieldBox`.
/// @param _tokenId The ERC-20 token asset ID in yieldBox.
/// @param share The amount in shares to add.
/// @param total Grand total amount to deduct from this contract's balance. Only applicable if `skim` is True.
/// Only used for accounting checks.
/// @param skim If True, only does a balance check on this contract.
/// False if tokens from msg.sender in `yieldBox` should be transferred.
function _addTokens(
address from,
uint256 _tokenId,
uint256 share,
uint256 total,
bool skim
) internal {
if (skim) {
require(
share <= yieldBox.balanceOf(address(this), _tokenId) - total,
"BigBang: too much"
);
} else {
yieldBox.transfer(from, address(this), _tokenId, share);
}
}
/// @dev Concrete implementation of `removeCollateral`.
function _removeCollateral(
address from,
address to,
uint256 share
) internal {
userCollateralShare[from] -= share;
totalCollateralShare -= share;
emit LogRemoveCollateral(from, to, share);
yieldBox.transfer(address(this), to, collateralId, share);
}
/// @dev Concrete implementation of `repay`.
function _repay(
address from,
address to,
uint256 part
) internal returns (uint256 amount) {
(totalBorrow, amount) = totalBorrow.sub(part, true);
userBorrowPart[to] -= part;
uint256 toWithdraw = (amount - part); //acrrued
uint256 toBurn = amount - toWithdraw;
yieldBox.withdraw(assetId, from, address(this), amount, 0);
//burn USDO
if (toBurn > 0) {
IUSDOBase(address(asset)).burn(address(this), toBurn);
}
emit LogRepay(from, to, amount, part);
}
/// @dev Concrete implementation of `borrow`.
function _borrow(
address from,
address to,
uint256 amount
) internal returns (uint256 part, uint256 share) {
uint256 feeAmount = (amount * borrowOpeningFee) / FEE_PRECISION; // A flat % fee is charged for any borrow
(totalBorrow, part) = totalBorrow.add(amount + feeAmount, true);
require(
totalBorrowCap == 0 || totalBorrow.elastic <= totalBorrowCap,
"BigBang: borrow cap reached"
);
userBorrowPart[from] += part;
//mint USDO
IUSDOBase(address(asset)).mint(address(this), amount);
//deposit borrowed amount to user
asset.approve(address(yieldBox), amount);
yieldBox.depositAsset(assetId, address(this), to, amount, 0);
share = yieldBox.toShare(assetId, amount, false);
emit LogBorrow(from, to, amount, feeAmount, part);
}
function _updateBorrowAndCollateralShare(
address user,
uint256 maxBorrowPart,
uint256 _exchangeRate
)
private
returns (
uint256 borrowAmount,
uint256 borrowPart,
uint256 collateralShare
)
{
uint256 collateralPartInAsset = (yieldBox.toAmount(
collateralId,
userCollateralShare[user],
false
) * EXCHANGE_RATE_PRECISION) / _exchangeRate;
uint256 borrowAssetDecimals = asset.safeDecimals();
uint256 collateralDecimals = collateral.safeDecimals();
uint256 availableBorrowPart = computeClosingFactor(
userBorrowPart[user],
collateralPartInAsset,
borrowAssetDecimals,
collateralDecimals,
FEE_PRECISION_DECIMALS
);
borrowPart = maxBorrowPart > availableBorrowPart
? availableBorrowPart
: maxBorrowPart;
if (borrowPart > userBorrowPart[user]) {
borrowPart = userBorrowPart[user];
}
userBorrowPart[user] = userBorrowPart[user] - borrowPart;
borrowAmount = totalBorrow.toElastic(borrowPart, false);
uint256 amountWithBonus = borrowAmount +
(borrowAmount * liquidationMultiplier) /
FEE_PRECISION;
collateralShare = yieldBox.toShare(
collateralId,
(amountWithBonus * _exchangeRate) / EXCHANGE_RATE_PRECISION,
false
);
if (collateralShare > userCollateralShare[user]) {
collateralShare = userCollateralShare[user];
}
userCollateralShare[user] -= collateralShare;
require(borrowAmount != 0, "SGL: solvent");
totalBorrow.elastic -= uint128(borrowAmount);
totalBorrow.base -= uint128(borrowPart);
}
}