This repository was archived by the owner on Jan 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLender.sol
More file actions
275 lines (237 loc) · 10.5 KB
/
Copy pathLender.sol
File metadata and controls
275 lines (237 loc) · 10.5 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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.28;
import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import { Access } from "../access/Access.sol";
import { ILender } from "../interfaces/ILender.sol";
import { LenderStorageUtils } from "../storage/LenderStorageUtils.sol";
import { BorrowLogic } from "./libraries/BorrowLogic.sol";
import { LiquidationLogic } from "./libraries/LiquidationLogic.sol";
import { ReserveLogic } from "./libraries/ReserveLogic.sol";
import { ViewLogic } from "./libraries/ViewLogic.sol";
/// @title Lender for covered agents
/// @author kexley, Cap Labs
/// @notice Whitelisted tokens are borrowed and repaid from this contract by covered agents.
/// @dev Borrow interest rates are calculated from the underlying utilization rates of the assets
/// in the vaults.
contract Lender is ILender, UUPSUpgradeable, Access, LenderStorageUtils {
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/// @inheritdoc ILender
function initialize(
address _accessControl,
address _delegation,
address _oracle,
uint256 _targetHealth,
uint256 _grace,
uint256 _expiry,
uint256 _bonusCap,
uint256 _emergencyLiquidationThreshold
) external initializer {
__Access_init(_accessControl);
__UUPSUpgradeable_init();
if (_delegation == address(0) || _oracle == address(0)) revert ZeroAddressNotValid();
if (_targetHealth < 1e27) revert InvalidTargetHealth();
if (_grace >= _expiry) revert GraceGreaterThanExpiry();
if (_bonusCap > 1e27) revert InvalidBonusCap();
LenderStorage storage $ = getLenderStorage();
$.delegation = _delegation;
$.oracle = _oracle;
$.targetHealth = _targetHealth;
$.grace = _grace;
$.expiry = _expiry;
$.bonusCap = _bonusCap;
$.emergencyLiquidationThreshold = _emergencyLiquidationThreshold;
}
/// @inheritdoc ILender
function borrow(address _asset, uint256 _amount, address _receiver) external returns (uint256 borrowed) {
borrowed = BorrowLogic.borrow(
getLenderStorage(),
BorrowParams({
agent: msg.sender,
asset: _asset,
amount: _amount,
receiver: _receiver,
maxBorrow: _amount == type(uint256).max
})
);
}
/// @inheritdoc ILender
function repay(address _asset, uint256 _amount, address _agent) external returns (uint256 repaid) {
if (_agent == address(0) || _asset == address(0)) revert ZeroAddressNotValid();
repaid = BorrowLogic.repay(
getLenderStorage(), RepayParams({ agent: _agent, asset: _asset, amount: _amount, caller: msg.sender })
);
}
/// @inheritdoc ILender
function realizeInterest(address _asset) external returns (uint256 actualRealized) {
actualRealized = BorrowLogic.realizeInterest(getLenderStorage(), _asset);
}
/// @inheritdoc ILender
function realizeRestakerInterest(address _agent, address _asset) external returns (uint256 actualRealized) {
actualRealized = BorrowLogic.realizeRestakerInterest(getLenderStorage(), _agent, _asset);
}
/// @inheritdoc ILender
function openLiquidation(address _agent) external {
LiquidationLogic.openLiquidation(getLenderStorage(), _agent);
}
/// @inheritdoc ILender
function closeLiquidation(address _agent) external {
LiquidationLogic.closeLiquidation(getLenderStorage(), _agent);
}
/// @inheritdoc ILender
function liquidate(address _agent, address _asset, uint256 _amount) external returns (uint256 liquidatedValue) {
if (_agent == address(0) || _asset == address(0)) revert ZeroAddressNotValid();
liquidatedValue = LiquidationLogic.liquidate(
getLenderStorage(), RepayParams({ agent: _agent, asset: _asset, amount: _amount, caller: msg.sender })
);
}
/// @inheritdoc ILender
function addAsset(AddAssetParams calldata _params) external checkAccess(this.addAsset.selector) {
LenderStorage storage $ = getLenderStorage();
if (!ReserveLogic.addAsset($, _params)) ++$.reservesCount;
}
/// @inheritdoc ILender
function removeAsset(address _asset) external checkAccess(this.removeAsset.selector) {
if (_asset == address(0)) revert ZeroAddressNotValid();
ReserveLogic.removeAsset(getLenderStorage(), _asset);
}
/// @inheritdoc ILender
function pauseAsset(address _asset, bool _pause) external checkAccess(this.pauseAsset.selector) {
if (_asset == address(0)) revert ZeroAddressNotValid();
ReserveLogic.pauseAsset(getLenderStorage(), _asset, _pause);
}
/// @inheritdoc ILender
function setMinBorrow(address _asset, uint256 _minBorrow) external checkAccess(this.setMinBorrow.selector) {
if (_asset == address(0)) revert ZeroAddressNotValid();
ReserveLogic.setMinBorrow(getLenderStorage(), _asset, _minBorrow);
}
/// @inheritdoc ILender
function setGrace(uint256 _grace) external checkAccess(this.setGrace.selector) {
if (_grace >= getLenderStorage().expiry) revert GraceGreaterThanExpiry();
getLenderStorage().grace = _grace;
}
/// @inheritdoc ILender
function setExpiry(uint256 _expiry) external checkAccess(this.setExpiry.selector) {
if (_expiry <= getLenderStorage().grace) revert ExpiryLessThanGrace();
getLenderStorage().expiry = _expiry;
}
/// @inheritdoc ILender
function setBonusCap(uint256 _bonusCap) external checkAccess(this.setBonusCap.selector) {
if (_bonusCap > 1e27) revert InvalidBonusCap();
getLenderStorage().bonusCap = _bonusCap;
}
/// @inheritdoc ILender
function agent(address _agent)
external
view
returns (
uint256 totalDelegation,
uint256 totalSlashableCollateral,
uint256 totalDebt,
uint256 ltv,
uint256 liquidationThreshold,
uint256 health
)
{
(totalDelegation, totalSlashableCollateral, totalDebt, ltv, liquidationThreshold, health) =
ViewLogic.agent(getLenderStorage(), _agent);
}
/// @inheritdoc ILender
function maxBorrowable(address _agent, address _asset) external view returns (uint256 maxBorrowableAmount) {
if (_agent == address(0) || _asset == address(0)) revert ZeroAddressNotValid();
maxBorrowableAmount = ViewLogic.maxBorrowable(getLenderStorage(), _agent, _asset);
}
/// @inheritdoc ILender
function maxLiquidatable(address _agent, address _asset) external view returns (uint256 maxLiquidatableAmount) {
if (_agent == address(0) || _asset == address(0)) revert ZeroAddressNotValid();
maxLiquidatableAmount = ViewLogic.maxLiquidatable(getLenderStorage(), _agent, _asset);
}
/// @inheritdoc ILender
function bonus(address _agent) external view returns (uint256 maxBonus) {
if (_agent == address(0)) revert ZeroAddressNotValid();
maxBonus = ViewLogic.bonus(getLenderStorage(), _agent);
}
/// @inheritdoc ILender
function debt(address _agent, address _asset) external view returns (uint256 totalDebt) {
if (_agent == address(0) || _asset == address(0)) revert ZeroAddressNotValid();
totalDebt = ViewLogic.debt(getLenderStorage(), _agent, _asset);
}
/// @inheritdoc ILender
function maxRealization(address _asset) external view returns (uint256 _maxRealization) {
_maxRealization = BorrowLogic.maxRealization(getLenderStorage(), _asset);
}
/// @inheritdoc ILender
function maxRestakerRealization(address _agent, address _asset)
external
view
returns (uint256 newRealizedInterest, uint256 newUnrealizedInterest)
{
(newRealizedInterest, newUnrealizedInterest) =
BorrowLogic.maxRestakerRealization(getLenderStorage(), _agent, _asset);
}
/// @inheritdoc ILender
function accruedRestakerInterest(address _agent, address _asset) external view returns (uint256 accruedInterest) {
if (_agent == address(0) || _asset == address(0)) revert ZeroAddressNotValid();
accruedInterest = ViewLogic.accruedRestakerInterest(getLenderStorage(), _agent, _asset);
}
/// @inheritdoc ILender
function reservesCount() external view returns (uint256 count) {
count = getLenderStorage().reservesCount;
}
/// @inheritdoc ILender
function grace() external view returns (uint256 gracePeriod) {
gracePeriod = getLenderStorage().grace;
}
/// @inheritdoc ILender
function expiry() external view returns (uint256 expiryPeriod) {
expiryPeriod = getLenderStorage().expiry;
}
/// @inheritdoc ILender
function targetHealth() external view returns (uint256 target) {
target = getLenderStorage().targetHealth;
}
/// @inheritdoc ILender
function bonusCap() external view returns (uint256 cap) {
cap = getLenderStorage().bonusCap;
}
/// @inheritdoc ILender
function emergencyLiquidationThreshold() external view returns (uint256 threshold) {
threshold = getLenderStorage().emergencyLiquidationThreshold;
}
/// @inheritdoc ILender
function liquidationStart(address _agent) external view returns (uint256 startTime) {
startTime = getLenderStorage().liquidationStart[_agent];
}
/// @inheritdoc ILender
function reservesData(address _asset)
external
view
returns (
uint256 id,
address vault,
address debtToken,
address interestReceiver,
uint8 decimals,
bool paused,
uint256 minBorrow
)
{
ReserveData storage reserve = getLenderStorage().reservesData[_asset];
id = reserve.id;
vault = reserve.vault;
debtToken = reserve.debtToken;
interestReceiver = reserve.interestReceiver;
decimals = reserve.decimals;
paused = reserve.paused;
minBorrow = reserve.minBorrow;
}
/// @inheritdoc ILender
function unrealizedInterest(address _agent, address _asset) external view returns (uint256 _unrealizedInterest) {
ReserveData storage reserve = getLenderStorage().reservesData[_asset];
_unrealizedInterest = reserve.unrealizedInterest[_agent];
}
/// @inheritdoc UUPSUpgradeable
function _authorizeUpgrade(address) internal override checkAccess(bytes4(0)) { }
}