/
LendingPoolCore.sol
1775 lines (1543 loc) · 65.7 KB
/
LendingPoolCore.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
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
pragma solidity ^0.5.0;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-solidity/contracts/utils/Address.sol";
import "../libraries/openzeppelin-upgradeability/VersionedInitializable.sol";
import "../libraries/CoreLibrary.sol";
import "../configuration/LendingPoolAddressesProvider.sol";
import "../interfaces/ILendingRateOracle.sol";
import "../interfaces/IReserveInterestRateStrategy.sol";
import "../libraries/WadRayMath.sol";
import "../tokenization/AToken.sol";
import "../libraries/EthAddressLib.sol";
/**
* @title LendingPoolCore contract
* @author Aave
* @notice Holds the state of the lending pool and all the funds deposited
* @dev NOTE: The core does not enforce security checks on the update of the state
* (eg, updateStateOnBorrow() does not enforce that borrowed is enabled on the reserve).
* The check that an action can be performed is a duty of the overlying LendingPool contract.
**/
contract LendingPoolCore is VersionedInitializable {
using SafeMath for uint256;
using WadRayMath for uint256;
using CoreLibrary for CoreLibrary.ReserveData;
using CoreLibrary for CoreLibrary.UserReserveData;
using SafeERC20 for ERC20;
using Address for address payable;
/**
* @dev Emitted when the state of a reserve is updated
* @param reserve the address of the reserve
* @param liquidityRate the new liquidity rate
* @param stableBorrowRate the new stable borrow rate
* @param variableBorrowRate the new variable borrow rate
* @param liquidityIndex the new liquidity index
* @param variableBorrowIndex the new variable borrow index
**/
event ReserveUpdated(
address indexed reserve,
uint256 liquidityRate,
uint256 stableBorrowRate,
uint256 variableBorrowRate,
uint256 liquidityIndex,
uint256 variableBorrowIndex
);
address public lendingPoolAddress;
LendingPoolAddressesProvider public addressesProvider;
/**
* @dev only lending pools can use functions affected by this modifier
**/
modifier onlyLendingPool {
require(lendingPoolAddress == msg.sender, "The caller must be a lending pool contract");
_;
}
/**
* @dev only lending pools configurator can use functions affected by this modifier
**/
modifier onlyLendingPoolConfigurator {
require(
addressesProvider.getLendingPoolConfigurator() == msg.sender,
"The caller must be a lending pool configurator contract"
);
_;
}
mapping(address => CoreLibrary.ReserveData) internal reserves;
mapping(address => mapping(address => CoreLibrary.UserReserveData)) internal usersReserveData;
address[] public reservesList;
uint256 public constant CORE_REVISION = 0x6;
/**
* @dev returns the revision number of the contract
**/
function getRevision() internal pure returns (uint256) {
return CORE_REVISION;
}
/**
* @dev initializes the Core contract, invoked upon registration on the AddressesProvider
* @param _addressesProvider the addressesProvider contract
**/
function initialize(LendingPoolAddressesProvider _addressesProvider) public initializer {
addressesProvider = _addressesProvider;
refreshConfigInternal();
}
/**
* @dev updates the state of the core as a result of a deposit action
* @param _reserve the address of the reserve in which the deposit is happening
* @param _user the address of the the user depositing
* @param _amount the amount being deposited
* @param _isFirstDeposit true if the user is depositing for the first time
**/
function updateStateOnDeposit(
address _reserve,
address _user,
uint256 _amount,
bool _isFirstDeposit
) external onlyLendingPool {
reserves[_reserve].updateCumulativeIndexes();
updateReserveInterestRatesAndTimestampInternal(_reserve, _amount, 0);
if (_isFirstDeposit) {
//if this is the first deposit of the user, we configure the deposit as enabled to be used as collateral
setUserUseReserveAsCollateral(_reserve, _user, true);
}
}
/**
* @dev updates the state of the core as a result of a redeem action
* @param _reserve the address of the reserve in which the redeem is happening
* @param _user the address of the the user redeeming
* @param _amountRedeemed the amount being redeemed
* @param _userRedeemedEverything true if the user is redeeming everything
**/
function updateStateOnRedeem(
address _reserve,
address _user,
uint256 _amountRedeemed,
bool _userRedeemedEverything
) external onlyLendingPool {
//compound liquidity and variable borrow interests
reserves[_reserve].updateCumulativeIndexes();
updateReserveInterestRatesAndTimestampInternal(_reserve, 0, _amountRedeemed);
//if user redeemed everything the useReserveAsCollateral flag is reset
if (_userRedeemedEverything) {
setUserUseReserveAsCollateral(_reserve, _user, false);
}
}
/**
* @dev updates the state of the core as a result of a flashloan action
* @param _reserve the address of the reserve in which the flashloan is happening
* @param _income the income of the protocol as a result of the action
**/
function updateStateOnFlashLoan(
address _reserve,
uint256 _availableLiquidityBefore,
uint256 _income,
uint256 _protocolFee
) external onlyLendingPool {
transferFlashLoanProtocolFeeInternal(_reserve, _protocolFee);
//compounding the cumulated interest
reserves[_reserve].updateCumulativeIndexes();
uint256 totalLiquidityBefore = _availableLiquidityBefore.add(
getReserveTotalBorrows(_reserve)
);
//compounding the received fee into the reserve
reserves[_reserve].cumulateToLiquidityIndex(totalLiquidityBefore, _income);
//refresh interest rates
updateReserveInterestRatesAndTimestampInternal(_reserve, _income, 0);
}
/**
* @dev updates the state of the core as a consequence of a borrow action.
* @param _reserve the address of the reserve on which the user is borrowing
* @param _user the address of the borrower
* @param _amountBorrowed the new amount borrowed
* @param _borrowFee the fee on the amount borrowed
* @param _rateMode the borrow rate mode (stable, variable)
* @return the new borrow rate for the user
**/
function updateStateOnBorrow(
address _reserve,
address _user,
uint256 _amountBorrowed,
uint256 _borrowFee,
CoreLibrary.InterestRateMode _rateMode
) external onlyLendingPool returns (uint256, uint256) {
// getting the previous borrow data of the user
(uint256 principalBorrowBalance, , uint256 balanceIncrease) = getUserBorrowBalances(
_reserve,
_user
);
updateReserveStateOnBorrowInternal(
_reserve,
_user,
principalBorrowBalance,
balanceIncrease,
_amountBorrowed,
_rateMode
);
updateUserStateOnBorrowInternal(
_reserve,
_user,
_amountBorrowed,
balanceIncrease,
_borrowFee,
_rateMode
);
updateReserveInterestRatesAndTimestampInternal(_reserve, 0, _amountBorrowed);
return (getUserCurrentBorrowRate(_reserve, _user), balanceIncrease);
}
/**
* @dev updates the state of the core as a consequence of a repay action.
* @param _reserve the address of the reserve on which the user is repaying
* @param _user the address of the borrower
* @param _paybackAmountMinusFees the amount being paid back minus fees
* @param _originationFeeRepaid the fee on the amount that is being repaid
* @param _balanceIncrease the accrued interest on the borrowed amount
* @param _repaidWholeLoan true if the user is repaying the whole loan
**/
function updateStateOnRepay(
address _reserve,
address _user,
uint256 _paybackAmountMinusFees,
uint256 _originationFeeRepaid,
uint256 _balanceIncrease,
bool _repaidWholeLoan
) external onlyLendingPool {
updateReserveStateOnRepayInternal(
_reserve,
_user,
_paybackAmountMinusFees,
_balanceIncrease
);
updateUserStateOnRepayInternal(
_reserve,
_user,
_paybackAmountMinusFees,
_originationFeeRepaid,
_balanceIncrease,
_repaidWholeLoan
);
updateReserveInterestRatesAndTimestampInternal(_reserve, _paybackAmountMinusFees, 0);
}
/**
* @dev updates the state of the core as a consequence of a swap rate action.
* @param _reserve the address of the reserve on which the user is repaying
* @param _user the address of the borrower
* @param _principalBorrowBalance the amount borrowed by the user
* @param _compoundedBorrowBalance the amount borrowed plus accrued interest
* @param _balanceIncrease the accrued interest on the borrowed amount
* @param _currentRateMode the current interest rate mode for the user
**/
function updateStateOnSwapRate(
address _reserve,
address _user,
uint256 _principalBorrowBalance,
uint256 _compoundedBorrowBalance,
uint256 _balanceIncrease,
CoreLibrary.InterestRateMode _currentRateMode
) external onlyLendingPool returns (CoreLibrary.InterestRateMode, uint256) {
updateReserveStateOnSwapRateInternal(
_reserve,
_user,
_principalBorrowBalance,
_compoundedBorrowBalance,
_currentRateMode
);
CoreLibrary.InterestRateMode newRateMode = updateUserStateOnSwapRateInternal(
_reserve,
_user,
_balanceIncrease,
_currentRateMode
);
updateReserveInterestRatesAndTimestampInternal(_reserve, 0, 0);
return (newRateMode, getUserCurrentBorrowRate(_reserve, _user));
}
/**
* @dev updates the state of the core as a consequence of a liquidation action.
* @param _principalReserve the address of the principal reserve that is being repaid
* @param _collateralReserve the address of the collateral reserve that is being liquidated
* @param _user the address of the borrower
* @param _amountToLiquidate the amount being repaid by the liquidator
* @param _collateralToLiquidate the amount of collateral being liquidated
* @param _feeLiquidated the amount of origination fee being liquidated
* @param _liquidatedCollateralForFee the amount of collateral equivalent to the origination fee + bonus
* @param _balanceIncrease the accrued interest on the borrowed amount
* @param _liquidatorReceivesAToken true if the liquidator will receive aTokens, false otherwise
**/
function updateStateOnLiquidation(
address _principalReserve,
address _collateralReserve,
address _user,
uint256 _amountToLiquidate,
uint256 _collateralToLiquidate,
uint256 _feeLiquidated,
uint256 _liquidatedCollateralForFee,
uint256 _balanceIncrease,
bool _liquidatorReceivesAToken
) external onlyLendingPool {
updatePrincipalReserveStateOnLiquidationInternal(
_principalReserve,
_user,
_amountToLiquidate,
_balanceIncrease
);
updateCollateralReserveStateOnLiquidationInternal(
_collateralReserve
);
updateUserStateOnLiquidationInternal(
_principalReserve,
_user,
_amountToLiquidate,
_feeLiquidated,
_balanceIncrease
);
updateReserveInterestRatesAndTimestampInternal(_principalReserve, _amountToLiquidate, 0);
if (!_liquidatorReceivesAToken) {
updateReserveInterestRatesAndTimestampInternal(
_collateralReserve,
0,
_collateralToLiquidate.add(_liquidatedCollateralForFee)
);
}
}
/**
* @dev updates the state of the core as a consequence of a stable rate rebalance
* @param _reserve the address of the principal reserve where the user borrowed
* @param _user the address of the borrower
* @param _balanceIncrease the accrued interest on the borrowed amount
* @return the new stable rate for the user
**/
function updateStateOnRebalance(address _reserve, address _user, uint256 _balanceIncrease)
external
onlyLendingPool
returns (uint256)
{
updateReserveStateOnRebalanceInternal(_reserve, _user, _balanceIncrease);
//update user data and rebalance the rate
updateUserStateOnRebalanceInternal(_reserve, _user, _balanceIncrease);
updateReserveInterestRatesAndTimestampInternal(_reserve, 0, 0);
return usersReserveData[_user][_reserve].stableBorrowRate;
}
/**
* @dev enables or disables a reserve as collateral
* @param _reserve the address of the principal reserve where the user deposited
* @param _user the address of the depositor
* @param _useAsCollateral true if the depositor wants to use the reserve as collateral
**/
function setUserUseReserveAsCollateral(address _reserve, address _user, bool _useAsCollateral)
public
onlyLendingPool
{
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
user.useAsCollateral = _useAsCollateral;
}
/**
* @notice ETH/token transfer functions
**/
/**
* @dev fallback function enforces that the caller is a contract, to support flashloan transfers
**/
function() external payable {
//only contracts can send ETH to the core
require(msg.sender.isContract(), "Only contracts can send ether to the Lending pool core");
}
/**
* @dev transfers to the user a specific amount from the reserve.
* @param _reserve the address of the reserve where the transfer is happening
* @param _user the address of the user receiving the transfer
* @param _amount the amount being transferred
**/
function transferToUser(address _reserve, address payable _user, uint256 _amount)
external
onlyLendingPool
{
if (_reserve != EthAddressLib.ethAddress()) {
ERC20(_reserve).safeTransfer(_user, _amount);
} else {
//solium-disable-next-line
(bool result, ) = _user.call.value(_amount).gas(50000)("");
require(result, "Transfer of ETH failed");
}
}
/**
* @dev transfers the protocol fees to the fees collection address
* @param _token the address of the token being transferred
* @param _user the address of the user from where the transfer is happening
* @param _amount the amount being transferred
* @param _destination the fee receiver address
**/
function transferToFeeCollectionAddress(
address _token,
address _user,
uint256 _amount,
address _destination
) external payable onlyLendingPool {
address payable feeAddress = address(uint160(_destination)); //cast the address to payable
if (_token != EthAddressLib.ethAddress()) {
require(
msg.value == 0,
"User is sending ETH along with the ERC20 transfer. Check the value attribute of the transaction"
);
ERC20(_token).safeTransferFrom(_user, feeAddress, _amount);
} else {
require(msg.value >= _amount, "The amount and the value sent to deposit do not match");
//solium-disable-next-line
(bool result, ) = feeAddress.call.value(_amount).gas(50000)("");
require(result, "Transfer of ETH failed");
}
}
/**
* @dev transfers the fees to the fees collection address in the case of liquidation
* @param _token the address of the token being transferred
* @param _amount the amount being transferred
* @param _destination the fee receiver address
**/
function liquidateFee(
address _token,
uint256 _amount,
address _destination
) external payable onlyLendingPool {
address payable feeAddress = address(uint160(_destination)); //cast the address to payable
require(
msg.value == 0,
"Fee liquidation does not require any transfer of value"
);
if (_token != EthAddressLib.ethAddress()) {
ERC20(_token).safeTransfer(feeAddress, _amount);
} else {
//solium-disable-next-line
(bool result, ) = feeAddress.call.value(_amount).gas(50000)("");
require(result, "Transfer of ETH failed");
}
}
/**
* @dev transfers an amount from a user to the destination reserve
* @param _reserve the address of the reserve where the amount is being transferred
* @param _user the address of the user from where the transfer is happening
* @param _amount the amount being transferred
**/
function transferToReserve(address _reserve, address payable _user, uint256 _amount)
external
payable
onlyLendingPool
{
if (_reserve != EthAddressLib.ethAddress()) {
require(msg.value == 0, "User is sending ETH along with the ERC20 transfer.");
ERC20(_reserve).safeTransferFrom(_user, address(this), _amount);
} else {
require(msg.value >= _amount, "The amount and the value sent to deposit do not match");
if (msg.value > _amount) {
//send back excess ETH
uint256 excessAmount = msg.value.sub(_amount);
//solium-disable-next-line
(bool result, ) = _user.call.value(excessAmount).gas(50000)("");
require(result, "Transfer of ETH failed");
}
}
}
/**
* @notice data access functions
**/
/**
* @dev returns the basic data (balances, fee accrued, reserve enabled/disabled as collateral)
* needed to calculate the global account data in the LendingPoolDataProvider
* @param _reserve the address of the reserve
* @param _user the address of the user
* @return the user deposited balance, the principal borrow balance, the fee, and if the reserve is enabled as collateral or not
**/
function getUserBasicReserveData(address _reserve, address _user)
external
view
returns (uint256, uint256, uint256, bool)
{
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
uint256 underlyingBalance = getUserUnderlyingAssetBalance(_reserve, _user);
if (user.principalBorrowBalance == 0) {
return (underlyingBalance, 0, 0, user.useAsCollateral);
}
return (
underlyingBalance,
user.getCompoundedBorrowBalance(reserve),
user.originationFee,
user.useAsCollateral
);
}
/**
* @dev checks if a user is allowed to borrow at a stable rate
* @param _reserve the reserve address
* @param _user the user
* @param _amount the amount the the user wants to borrow
* @return true if the user is allowed to borrow at a stable rate, false otherwise
**/
function isUserAllowedToBorrowAtStable(address _reserve, address _user, uint256 _amount)
external
view
returns (bool)
{
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
if (!reserve.isStableBorrowRateEnabled) return false;
return
!user.useAsCollateral ||
!reserve.usageAsCollateralEnabled ||
_amount > getUserUnderlyingAssetBalance(_reserve, _user);
}
/**
* @dev gets the underlying asset balance of a user based on the corresponding aToken balance.
* @param _reserve the reserve address
* @param _user the user address
* @return the underlying deposit balance of the user
**/
function getUserUnderlyingAssetBalance(address _reserve, address _user)
public
view
returns (uint256)
{
AToken aToken = AToken(reserves[_reserve].aTokenAddress);
return aToken.balanceOf(_user);
}
/**
* @dev gets the interest rate strategy contract address for the reserve
* @param _reserve the reserve address
* @return the address of the interest rate strategy contract
**/
function getReserveInterestRateStrategyAddress(address _reserve) public view returns (address) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.interestRateStrategyAddress;
}
/**
* @dev gets the aToken contract address for the reserve
* @param _reserve the reserve address
* @return the address of the aToken contract
**/
function getReserveATokenAddress(address _reserve) public view returns (address) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.aTokenAddress;
}
/**
* @dev gets the available liquidity in the reserve. The available liquidity is the balance of the core contract
* @param _reserve the reserve address
* @return the available liquidity
**/
function getReserveAvailableLiquidity(address _reserve) public view returns (uint256) {
uint256 balance = 0;
if (_reserve == EthAddressLib.ethAddress()) {
balance = address(this).balance;
} else {
balance = IERC20(_reserve).balanceOf(address(this));
}
return balance;
}
/**
* @dev gets the total liquidity in the reserve. The total liquidity is the balance of the core contract + total borrows
* @param _reserve the reserve address
* @return the total liquidity
**/
function getReserveTotalLiquidity(address _reserve) public view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return getReserveAvailableLiquidity(_reserve).add(reserve.getTotalBorrows());
}
/**
* @dev gets the normalized income of the reserve. a value of 1e27 means there is no income. A value of 2e27 means there
* there has been 100% income.
* @param _reserve the reserve address
* @return the reserve normalized income
**/
function getReserveNormalizedIncome(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.getNormalizedIncome();
}
/**
* @dev gets the reserve total borrows
* @param _reserve the reserve address
* @return the total borrows (stable + variable)
**/
function getReserveTotalBorrows(address _reserve) public view returns (uint256) {
return reserves[_reserve].getTotalBorrows();
}
/**
* @dev gets the reserve total borrows stable
* @param _reserve the reserve address
* @return the total borrows stable
**/
function getReserveTotalBorrowsStable(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.totalBorrowsStable;
}
/**
* @dev gets the reserve total borrows variable
* @param _reserve the reserve address
* @return the total borrows variable
**/
function getReserveTotalBorrowsVariable(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.totalBorrowsVariable;
}
/**
* @dev gets the reserve liquidation threshold
* @param _reserve the reserve address
* @return the reserve liquidation threshold
**/
function getReserveLiquidationThreshold(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.liquidationThreshold;
}
/**
* @dev gets the reserve liquidation bonus
* @param _reserve the reserve address
* @return the reserve liquidation bonus
**/
function getReserveLiquidationBonus(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.liquidationBonus;
}
/**
* @dev gets the reserve current variable borrow rate. Is the base variable borrow rate if the reserve is empty
* @param _reserve the reserve address
* @return the reserve current variable borrow rate
**/
function getReserveCurrentVariableBorrowRate(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
if (reserve.currentVariableBorrowRate == 0) {
return
IReserveInterestRateStrategy(reserve.interestRateStrategyAddress)
.getBaseVariableBorrowRate();
}
return reserve.currentVariableBorrowRate;
}
/**
* @dev gets the reserve current stable borrow rate. Is the market rate if the reserve is empty
* @param _reserve the reserve address
* @return the reserve current stable borrow rate
**/
function getReserveCurrentStableBorrowRate(address _reserve) public view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
ILendingRateOracle oracle = ILendingRateOracle(addressesProvider.getLendingRateOracle());
if (reserve.currentStableBorrowRate == 0) {
//no stable rate borrows yet
return oracle.getMarketBorrowRate(_reserve);
}
return reserve.currentStableBorrowRate;
}
/**
* @dev gets the reserve average stable borrow rate. The average stable rate is the weighted average
* of all the loans taken at stable rate.
* @param _reserve the reserve address
* @return the reserve current average borrow rate
**/
function getReserveCurrentAverageStableBorrowRate(address _reserve)
external
view
returns (uint256)
{
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.currentAverageStableBorrowRate;
}
/**
* @dev gets the reserve liquidity rate
* @param _reserve the reserve address
* @return the reserve liquidity rate
**/
function getReserveCurrentLiquidityRate(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.currentLiquidityRate;
}
/**
* @dev gets the reserve liquidity cumulative index
* @param _reserve the reserve address
* @return the reserve liquidity cumulative index
**/
function getReserveLiquidityCumulativeIndex(address _reserve) external view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.lastLiquidityCumulativeIndex;
}
/**
* @dev gets the reserve variable borrow index
* @param _reserve the reserve address
* @return the reserve variable borrow index
**/
function getReserveVariableBorrowsCumulativeIndex(address _reserve)
external
view
returns (uint256)
{
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.lastVariableBorrowCumulativeIndex;
}
/**
* @dev this function aggregates the configuration parameters of the reserve.
* It's used in the LendingPoolDataProvider specifically to save gas, and avoid
* multiple external contract calls to fetch the same data.
* @param _reserve the reserve address
* @return the reserve decimals
* @return the base ltv as collateral
* @return the liquidation threshold
* @return if the reserve is used as collateral or not
**/
function getReserveConfiguration(address _reserve)
external
view
returns (uint256, uint256, uint256, bool)
{
uint256 decimals;
uint256 baseLTVasCollateral;
uint256 liquidationThreshold;
bool usageAsCollateralEnabled;
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
decimals = reserve.decimals;
baseLTVasCollateral = reserve.baseLTVasCollateral;
liquidationThreshold = reserve.liquidationThreshold;
usageAsCollateralEnabled = reserve.usageAsCollateralEnabled;
return (decimals, baseLTVasCollateral, liquidationThreshold, usageAsCollateralEnabled);
}
/**
* @dev returns the decimals of the reserve
* @param _reserve the reserve address
* @return the reserve decimals
**/
function getReserveDecimals(address _reserve) external view returns (uint256) {
return reserves[_reserve].decimals;
}
/**
* @dev returns true if the reserve is enabled for borrowing
* @param _reserve the reserve address
* @return true if the reserve is enabled for borrowing, false otherwise
**/
function isReserveBorrowingEnabled(address _reserve) external view returns (bool) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.borrowingEnabled;
}
/**
* @dev returns true if the reserve is enabled as collateral
* @param _reserve the reserve address
* @return true if the reserve is enabled as collateral, false otherwise
**/
function isReserveUsageAsCollateralEnabled(address _reserve) external view returns (bool) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.usageAsCollateralEnabled;
}
/**
* @dev returns true if the stable rate is enabled on reserve
* @param _reserve the reserve address
* @return true if the stable rate is enabled on reserve, false otherwise
**/
function getReserveIsStableBorrowRateEnabled(address _reserve) external view returns (bool) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.isStableBorrowRateEnabled;
}
/**
* @dev returns true if the reserve is active
* @param _reserve the reserve address
* @return true if the reserve is active, false otherwise
**/
function getReserveIsActive(address _reserve) external view returns (bool) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.isActive;
}
/**
* @notice returns if a reserve is freezed
* @param _reserve the reserve for which the information is needed
* @return true if the reserve is freezed, false otherwise
**/
function getReserveIsFreezed(address _reserve) external view returns (bool) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
return reserve.isFreezed;
}
/**
* @notice returns the timestamp of the last action on the reserve
* @param _reserve the reserve for which the information is needed
* @return the last updated timestamp of the reserve
**/
function getReserveLastUpdate(address _reserve) external view returns (uint40 timestamp) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
timestamp = reserve.lastUpdateTimestamp;
}
/**
* @dev returns the utilization rate U of a specific reserve
* @param _reserve the reserve for which the information is needed
* @return the utilization rate in ray
**/
function getReserveUtilizationRate(address _reserve) public view returns (uint256) {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
uint256 totalBorrows = reserve.getTotalBorrows();
if (totalBorrows == 0) {
return 0;
}
uint256 availableLiquidity = getReserveAvailableLiquidity(_reserve);
return totalBorrows.rayDiv(availableLiquidity.add(totalBorrows));
}
/**
* @return the array of reserves configured on the core
**/
function getReserves() external view returns (address[] memory) {
return reservesList;
}
/**
* @param _reserve the address of the reserve for which the information is needed
* @param _user the address of the user for which the information is needed
* @return true if the user has chosen to use the reserve as collateral, false otherwise
**/
function isUserUseReserveAsCollateralEnabled(address _reserve, address _user)
external
view
returns (bool)
{
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
return user.useAsCollateral;
}
/**
* @param _reserve the address of the reserve for which the information is needed
* @param _user the address of the user for which the information is needed
* @return the origination fee for the user
**/
function getUserOriginationFee(address _reserve, address _user)
external
view
returns (uint256)
{
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
return user.originationFee;
}
/**
* @dev users with no loans in progress have NONE as borrow rate mode
* @param _reserve the address of the reserve for which the information is needed
* @param _user the address of the user for which the information is needed
* @return the borrow rate mode for the user,
**/
function getUserCurrentBorrowRateMode(address _reserve, address _user)
public
view
returns (CoreLibrary.InterestRateMode)
{
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
if (user.principalBorrowBalance == 0) {
return CoreLibrary.InterestRateMode.NONE;
}
return
user.stableBorrowRate > 0
? CoreLibrary.InterestRateMode.STABLE
: CoreLibrary.InterestRateMode.VARIABLE;
}
/**
* @dev gets the current borrow rate of the user
* @param _reserve the address of the reserve for which the information is needed
* @param _user the address of the user for which the information is needed
* @return the borrow rate for the user,
**/
function getUserCurrentBorrowRate(address _reserve, address _user)
internal
view
returns (uint256)
{
CoreLibrary.InterestRateMode rateMode = getUserCurrentBorrowRateMode(_reserve, _user);
if (rateMode == CoreLibrary.InterestRateMode.NONE) {
return 0;
}
return
rateMode == CoreLibrary.InterestRateMode.STABLE
? usersReserveData[_user][_reserve].stableBorrowRate
: reserves[_reserve].currentVariableBorrowRate;
}
/**
* @dev the stable rate returned is 0 if the user is borrowing at variable or not borrowing at all
* @param _reserve the address of the reserve for which the information is needed
* @param _user the address of the user for which the information is needed
* @return the user stable rate
**/
function getUserCurrentStableBorrowRate(address _reserve, address _user)
external
view
returns (uint256)
{
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
return user.stableBorrowRate;
}
/**
* @dev calculates and returns the borrow balances of the user
* @param _reserve the address of the reserve
* @param _user the address of the user
* @return the principal borrow balance, the compounded balance and the balance increase since the last borrow/repay/swap/rebalance
**/
function getUserBorrowBalances(address _reserve, address _user)
public
view
returns (uint256, uint256, uint256)
{
CoreLibrary.UserReserveData storage user = usersReserveData[_user][_reserve];
if (user.principalBorrowBalance == 0) {
return (0, 0, 0);
}
uint256 principal = user.principalBorrowBalance;
uint256 compoundedBalance = CoreLibrary.getCompoundedBorrowBalance(
user,