-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathV3Vault.sol
1315 lines (1088 loc) · 54.9 KB
/
V3Vault.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "v3-core/interfaces/IUniswapV3Factory.sol";
import "v3-core/interfaces/IUniswapV3Pool.sol";
import "v3-core/libraries/TickMath.sol";
import "v3-core/libraries/FixedPoint128.sol";
import "v3-periphery/libraries/LiquidityAmounts.sol";
import "v3-periphery/interfaces/INonfungiblePositionManager.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "@openzeppelin/contracts/utils/Multicall.sol";
import "permit2/interfaces/IPermit2.sol";
import "./interfaces/IVault.sol";
import "./interfaces/IV3Oracle.sol";
import "./interfaces/IInterestRateModel.sol";
import "./interfaces/IErrors.sol";
/// @title Revert Lend Vault for token lending / borrowing using Uniswap V3 LP positions as collateral
/// @notice The vault manages ONE ERC20 (eg. USDC) asset for lending / borrowing, but collateral positions can be composed of any 2 tokens configured each with a collateralFactor > 0
/// Vault implements IERC4626 Vault Standard and is itself a ERC20 which represent shares of total lending pool
contract V3Vault is ERC20, Multicall, Ownable, IVault, IERC721Receiver, IErrors {
using Math for uint256;
uint256 private constant Q32 = 2 ** 32;
uint256 private constant Q96 = 2 ** 96;
uint32 public constant MAX_COLLATERAL_FACTOR_X32 = uint32(Q32 * 90 / 100); // 90%
uint32 public constant MIN_LIQUIDATION_PENALTY_X32 = uint32(Q32 * 2 / 100); // 2%
uint32 public constant MAX_LIQUIDATION_PENALTY_X32 = uint32(Q32 * 10 / 100); // 10%
uint32 public constant MIN_RESERVE_PROTECTION_FACTOR_X32 = uint32(Q32 / 100); //1%
uint32 public constant MAX_DAILY_LEND_INCREASE_X32 = uint32(Q32 / 10); //10%
uint32 public constant MAX_DAILY_DEBT_INCREASE_X32 = uint32(Q32 / 10); //10%
/// @notice Uniswap v3 position manager
INonfungiblePositionManager public immutable nonfungiblePositionManager;
/// @notice Uniswap v3 factory
IUniswapV3Factory public immutable factory;
/// @notice interest rate model implementation
IInterestRateModel public immutable interestRateModel;
/// @notice oracle implementation
IV3Oracle public immutable oracle;
/// @notice permit2 contract
IPermit2 public immutable permit2;
/// @notice underlying asset for lending / borrowing
address public immutable override asset;
/// @notice decimals of underlying token (are the same as ERC20 share token)
uint8 private immutable assetDecimals;
// events
event ApprovedTransform(uint256 indexed tokenId, address owner, address target, bool isActive);
event Add(uint256 indexed tokenId, address owner, uint256 oldTokenId); // when a token is added replacing another token - oldTokenId > 0
event Remove(uint256 indexed tokenId, address recipient);
event ExchangeRateUpdate(uint256 debtExchangeRateX96, uint256 lendExchangeRateX96);
// Deposit and Withdraw events are defined in IERC4626
event WithdrawCollateral(
uint256 indexed tokenId, address owner, address recipient, uint128 liquidity, uint256 amount0, uint256 amount1
);
event Borrow(uint256 indexed tokenId, address owner, uint256 assets, uint256 shares);
event Repay(uint256 indexed tokenId, address repayer, address owner, uint256 assets, uint256 shares);
event Liquidate(
uint256 indexed tokenId,
address liquidator,
address owner,
uint256 value,
uint256 cost,
uint256 amount0,
uint256 amount1,
uint256 reserve,
uint256 missing
); // shows exactly how liquidation amounts were divided
// admin events
event WithdrawReserves(uint256 amount, address receiver);
event SetTransformer(address transformer, bool active);
event SetLimits(
uint256 minLoanSize,
uint256 globalLendLimit,
uint256 globalDebtLimit,
uint256 dailyLendIncreaseLimitMin,
uint256 dailyDebtIncreaseLimitMin
);
event SetReserveFactor(uint32 reserveFactorX32);
event SetReserveProtectionFactor(uint32 reserveProtectionFactorX32);
event SetTokenConfig(address token, uint32 collateralFactorX32, uint32 collateralValueLimitFactorX32);
event SetEmergencyAdmin(address emergencyAdmin);
// configured tokens
struct TokenConfig {
uint32 collateralFactorX32; // how much this token is valued as collateral
uint32 collateralValueLimitFactorX32; // how much asset equivalent may be lent out given this collateral
uint192 totalDebtShares; // how much debt shares are theoretically backed by this collateral
}
mapping(address => TokenConfig) public tokenConfigs;
// percentage of interest which is kept in the protocol for reserves
uint32 public reserveFactorX32 = 0;
// percentage of lend amount which needs to be in reserves before withdrawn
uint32 public reserveProtectionFactorX32 = MIN_RESERVE_PROTECTION_FACTOR_X32;
// total of debt shares - increases when borrow - decreases when repay
uint256 public debtSharesTotal = 0;
// exchange rates are Q96 at the beginning - 1 share token per 1 asset token
uint256 public lastExchangeRateUpdate = 0;
uint256 public lastDebtExchangeRateX96 = Q96;
uint256 public lastLendExchangeRateX96 = Q96;
uint256 public globalDebtLimit = 0;
uint256 public globalLendLimit = 0;
// minimal size of loan (to protect from non-liquidatable positions because of gas-cost)
uint256 public minLoanSize = 0;
// daily lend increase limit handling
uint256 public dailyLendIncreaseLimitMin = 0;
uint256 public dailyLendIncreaseLimitLeft = 0;
uint256 public dailyLendIncreaseLimitLastReset = 0;
// daily debt increase limit handling
uint256 public dailyDebtIncreaseLimitMin = 0;
uint256 public dailyDebtIncreaseLimitLeft = 0;
uint256 public dailyDebtIncreaseLimitLastReset = 0;
// lender balances are handled with ERC-20 mint/burn
// loans are handled with this struct
struct Loan {
uint256 debtShares;
}
mapping(uint256 => Loan) public loans; // tokenID -> loan mapping
// storage variables to handle enumerable token ownership
mapping(address => uint256[]) private ownedTokens; // Mapping from owner address to list of owned token IDs
mapping(uint256 => uint256) private ownedTokensIndex; // Mapping from token ID to index of the owner tokens list (for removal without loop)
mapping(uint256 => address) private tokenOwner; // Mapping from token ID to owner
uint256 private transformedTokenId = 0; // transient storage (when available in dencun)
mapping(address => bool) public transformerAllowList; // contracts allowed to transform positions (selected audited contracts e.g. V3Utils)
mapping(address => mapping(uint256 => mapping(address => bool))) public transformApprovals; // owners permissions for other addresses to call transform on owners behalf (e.g. AutoRange contract)
// address which can call special emergency actions without timelock
address public emergencyAdmin;
constructor(
string memory name,
string memory symbol,
address _asset,
INonfungiblePositionManager _nonfungiblePositionManager,
IInterestRateModel _interestRateModel,
IV3Oracle _oracle,
IPermit2 _permit2
) ERC20(name, symbol) {
asset = _asset;
assetDecimals = IERC20Metadata(_asset).decimals();
nonfungiblePositionManager = _nonfungiblePositionManager;
factory = IUniswapV3Factory(_nonfungiblePositionManager.factory());
interestRateModel = _interestRateModel;
oracle = _oracle;
permit2 = _permit2;
}
////////////////// EXTERNAL VIEW FUNCTIONS
/// @notice Retrieves global information about the vault
/// @return debt Total amount of debt asset tokens
/// @return lent Total amount of lent asset tokens
/// @return balance Balance of asset token in contract
/// @return available Available balance of asset token in contract (balance - reserves)
/// @return reserves Amount of reserves
function vaultInfo()
external
view
override
returns (
uint256 debt,
uint256 lent,
uint256 balance,
uint256 available,
uint256 reserves,
uint256 debtExchangeRateX96,
uint256 lendExchangeRateX96
)
{
(debtExchangeRateX96, lendExchangeRateX96) = _calculateGlobalInterest();
(balance, available, reserves) = _getAvailableBalance(debtExchangeRateX96, lendExchangeRateX96);
debt = _convertToAssets(debtSharesTotal, debtExchangeRateX96, Math.Rounding.Up);
lent = _convertToAssets(totalSupply(), lendExchangeRateX96, Math.Rounding.Up);
}
/// @notice Retrieves lending information for a specified account.
/// @param account The address of the account for which lending info is requested.
/// @return amount Amount of lent assets for the account
function lendInfo(address account) external view override returns (uint256 amount) {
(, uint256 newLendExchangeRateX96) = _calculateGlobalInterest();
amount = _convertToAssets(balanceOf(account), newLendExchangeRateX96, Math.Rounding.Down);
}
/// @notice Retrieves details of a loan identified by its token ID.
/// @param tokenId The unique identifier of the loan - which is the corresponding UniV3 Position
/// @return debt Amount of debt for this position
/// @return fullValue Current value of the position priced as asset token
/// @return collateralValue Current collateral value of the position priced as asset token
/// @return liquidationCost If position is liquidatable - cost to liquidate position - otherwise 0
/// @return liquidationValue If position is liquidatable - the value of the (partial) position which the liquidator recieves - otherwise 0
function loanInfo(uint256 tokenId)
external
view
override
returns (
uint256 debt,
uint256 fullValue,
uint256 collateralValue,
uint256 liquidationCost,
uint256 liquidationValue
)
{
(uint256 newDebtExchangeRateX96,) = _calculateGlobalInterest();
debt = _convertToAssets(loans[tokenId].debtShares, newDebtExchangeRateX96, Math.Rounding.Up);
bool isHealthy;
(isHealthy, fullValue, collateralValue,) = _checkLoanIsHealthy(tokenId, debt);
if (!isHealthy) {
(liquidationValue, liquidationCost,) = _calculateLiquidation(debt, fullValue, collateralValue);
}
}
/// @notice Retrieves owner of a loan
/// @param tokenId The unique identifier of the loan - which is the corresponding UniV3 Position
/// @return owner Owner of the loan
function ownerOf(uint256 tokenId) external view override returns (address owner) {
return tokenOwner[tokenId];
}
/// @notice Retrieves count of loans for owner (for enumerating owners loans)
/// @param owner Owner address
function loanCount(address owner) external view override returns (uint256) {
return ownedTokens[owner].length;
}
/// @notice Retrieves tokenid of loan at given index for owner (for enumerating owners loans)
/// @param owner Owner address
/// @param index Index
function loanAtIndex(address owner, uint256 index) external view override returns (uint256) {
return ownedTokens[owner][index];
}
////////////////// OVERRIDDEN EXTERNAL VIEW FUNCTIONS FROM ERC20
/// @inheritdoc IERC20Metadata
function decimals() public view override(IERC20Metadata, ERC20) returns (uint8) {
return assetDecimals;
}
////////////////// OVERRIDDEN EXTERNAL VIEW FUNCTIONS FROM ERC4626
/// @inheritdoc IERC4626
function totalAssets() public view override returns (uint256) {
return IERC20(asset).balanceOf(address(this));
}
/// @inheritdoc IERC4626
function convertToShares(uint256 assets) external view override returns (uint256 shares) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToShares(assets, lendExchangeRateX96, Math.Rounding.Down);
}
/// @inheritdoc IERC4626
function convertToAssets(uint256 shares) external view override returns (uint256 assets) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToAssets(shares, lendExchangeRateX96, Math.Rounding.Down);
}
/// @inheritdoc IERC4626
function maxDeposit(address) external view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
uint256 value = _convertToAssets(totalSupply(), lendExchangeRateX96, Math.Rounding.Up);
if (value >= globalLendLimit) {
return 0;
} else {
return globalLendLimit - value;
}
}
/// @inheritdoc IERC4626
function maxMint(address) external view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
uint256 value = _convertToAssets(totalSupply(), lendExchangeRateX96, Math.Rounding.Up);
if (value >= globalLendLimit) {
return 0;
} else {
return _convertToShares(globalLendLimit - value, lendExchangeRateX96, Math.Rounding.Down);
}
}
/// @inheritdoc IERC4626
function maxWithdraw(address owner) external view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToAssets(balanceOf(owner), lendExchangeRateX96, Math.Rounding.Down);
}
/// @inheritdoc IERC4626
function maxRedeem(address owner) external view override returns (uint256) {
return balanceOf(owner);
}
/// @inheritdoc IERC4626
function previewDeposit(uint256 assets) public view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToShares(assets, lendExchangeRateX96, Math.Rounding.Down);
}
/// @inheritdoc IERC4626
function previewMint(uint256 shares) public view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToAssets(shares, lendExchangeRateX96, Math.Rounding.Up);
}
/// @inheritdoc IERC4626
function previewWithdraw(uint256 assets) public view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToShares(assets, lendExchangeRateX96, Math.Rounding.Up);
}
/// @inheritdoc IERC4626
function previewRedeem(uint256 shares) public view override returns (uint256) {
(, uint256 lendExchangeRateX96) = _calculateGlobalInterest();
return _convertToAssets(shares, lendExchangeRateX96, Math.Rounding.Down);
}
////////////////// OVERRIDDEN EXTERNAL FUNCTIONS FROM ERC4626
/// @inheritdoc IERC4626
function deposit(uint256 assets, address receiver) external override returns (uint256) {
(, uint256 shares) = _deposit(receiver, assets, false, "");
return shares;
}
/// @inheritdoc IERC4626
function mint(uint256 shares, address receiver) external override returns (uint256) {
(uint256 assets,) = _deposit(receiver, shares, true, "");
return assets;
}
/// @inheritdoc IERC4626
function withdraw(uint256 assets, address receiver, address owner) external override returns (uint256) {
(, uint256 shares) = _withdraw(receiver, owner, assets, false);
return shares;
}
/// @inheritdoc IERC4626
function redeem(uint256 shares, address receiver, address owner) external override returns (uint256) {
(uint256 assets,) = _withdraw(receiver, owner, shares, true);
return assets;
}
// deposit using permit2 data
function deposit(uint256 assets, address receiver, bytes calldata permitData) external override returns (uint256) {
(, uint256 shares) = _deposit(receiver, assets, false, permitData);
return shares;
}
// mint using permit2 data
function mint(uint256 shares, address receiver, bytes calldata permitData) external override returns (uint256) {
(uint256 assets,) = _deposit(receiver, shares, true, permitData);
return assets;
}
////////////////// EXTERNAL FUNCTIONS
/// @notice Creates a new collateralized position (transfer approved position)
/// @param tokenId The token ID associated with the new position.
/// @param recipient Address to recieve the position in the vault
function create(uint256 tokenId, address recipient) external override {
nonfungiblePositionManager.safeTransferFrom(msg.sender, address(this), tokenId, abi.encode(recipient));
}
/// @notice Creates a new collateralized position with a permit for token spending (transfer position with permit)
/// @param tokenId The token ID associated with the new position.
/// @param owner Current owner of the position (signature owner)
/// @param recipient Address to recieve the position in the vault
/// @param deadline Timestamp until which the permit is valid.
/// @param v, r, s Components of the signature for the permit.
function createWithPermit(
uint256 tokenId,
address owner,
address recipient,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external override {
if (msg.sender != owner) {
revert Unauthorized();
}
nonfungiblePositionManager.permit(address(this), tokenId, deadline, v, r, s);
nonfungiblePositionManager.safeTransferFrom(owner, address(this), tokenId, abi.encode(recipient));
}
/// @notice Whenever a token is recieved it either creates a new loan, or modifies an existing one when in transform mode.
/// @inheritdoc IERC721Receiver
function onERC721Received(address, address from, uint256 tokenId, bytes calldata data)
external
override
returns (bytes4)
{
// only Uniswap v3 NFTs allowed - sent from other contract
if (msg.sender != address(nonfungiblePositionManager) || from == address(this)) {
revert WrongContract();
}
(uint256 debtExchangeRateX96, uint256 lendExchangeRateX96) = _updateGlobalInterest();
if (transformedTokenId == 0) {
address owner = from;
if (data.length > 0) {
owner = abi.decode(data, (address));
}
loans[tokenId] = Loan(0);
_addTokenToOwner(owner, tokenId);
emit Add(tokenId, owner, 0);
} else {
uint256 oldTokenId = transformedTokenId;
// if in transform mode - and a new position is sent - current position is replaced and returned
if (tokenId != oldTokenId) {
address owner = tokenOwner[oldTokenId];
// set transformed token to new one
transformedTokenId = tokenId;
// copy debt to new token
loans[tokenId] = Loan(loans[oldTokenId].debtShares);
_addTokenToOwner(owner, tokenId);
emit Add(tokenId, owner, oldTokenId);
// clears data of old loan
_cleanupLoan(oldTokenId, debtExchangeRateX96, lendExchangeRateX96, owner);
// sets data of new loan
_updateAndCheckCollateral(
tokenId, debtExchangeRateX96, lendExchangeRateX96, 0, loans[tokenId].debtShares
);
}
}
return IERC721Receiver.onERC721Received.selector;
}
/// @notice Allows another address to call transform on behalf of owner (on a given token)
/// @param tokenId The token to be permitted
/// @param target The address to be allowed
/// @param isActive If it allowed or not
function approveTransform(uint256 tokenId, address target, bool isActive) external override {
if (tokenOwner[tokenId] != msg.sender) {
revert Unauthorized();
}
transformApprovals[msg.sender][tokenId][target] = isActive;
emit ApprovedTransform(tokenId, msg.sender, target, isActive);
}
/// @notice Method which allows a contract to transform a loan by changing it (and only at the end checking collateral)
/// @param tokenId The token ID to be processed
/// @param transformer The address of a whitelisted transformer contract
/// @param data Encoded transformation params
/// @return newTokenId Final token ID (may be different than input token ID when the position was replaced by transformation)
function transform(uint256 tokenId, address transformer, bytes calldata data)
external
override
returns (uint256 newTokenId)
{
if (tokenId == 0 || !transformerAllowList[transformer]) {
revert TransformNotAllowed();
}
if (transformedTokenId > 0) {
revert Reentrancy();
}
transformedTokenId = tokenId;
(uint256 newDebtExchangeRateX96,) = _updateGlobalInterest();
address loanOwner = tokenOwner[tokenId];
// only the owner of the loan, the vault itself or any approved caller can call this
if (loanOwner != msg.sender && !transformApprovals[loanOwner][tokenId][msg.sender]) {
revert Unauthorized();
}
// give access to transformer
nonfungiblePositionManager.approve(transformer, tokenId);
(bool success,) = transformer.call(data);
if (!success) {
revert TransformFailed();
}
// may have changed in the meantime
tokenId = transformedTokenId;
// check owner not changed (NEEDED because token could have been moved somewhere else in the meantime)
address owner = nonfungiblePositionManager.ownerOf(tokenId);
if (owner != address(this)) {
revert Unauthorized();
}
// remove access for transformer
nonfungiblePositionManager.approve(address(0), tokenId);
uint256 debt = _convertToAssets(loans[tokenId].debtShares, newDebtExchangeRateX96, Math.Rounding.Up);
_requireLoanIsHealthy(tokenId, debt);
transformedTokenId = 0;
return tokenId;
}
/// @notice Borrows specified amount using token as collateral
/// @param tokenId The token ID to use as collateral
/// @param assets How much assets to borrow
function borrow(uint256 tokenId, uint256 assets) external override {
bool isTransformMode =
transformedTokenId > 0 && transformedTokenId == tokenId && transformerAllowList[msg.sender];
(uint256 newDebtExchangeRateX96, uint256 newLendExchangeRateX96) = _updateGlobalInterest();
_resetDailyDebtIncreaseLimit(newLendExchangeRateX96, false);
Loan storage loan = loans[tokenId];
// if not in transfer mode - must be called from owner or the vault itself
if (!isTransformMode && tokenOwner[tokenId] != msg.sender && address(this) != msg.sender) {
revert Unauthorized();
}
uint256 shares = _convertToShares(assets, newDebtExchangeRateX96, Math.Rounding.Up);
uint256 loanDebtShares = loan.debtShares + shares;
loan.debtShares = loanDebtShares;
debtSharesTotal += shares;
if (debtSharesTotal > _convertToShares(globalDebtLimit, newDebtExchangeRateX96, Math.Rounding.Down)) {
revert GlobalDebtLimit();
}
if (assets > dailyDebtIncreaseLimitLeft) {
revert DailyDebtIncreaseLimit();
} else {
dailyDebtIncreaseLimitLeft -= assets;
}
_updateAndCheckCollateral(
tokenId, newDebtExchangeRateX96, newLendExchangeRateX96, loanDebtShares - shares, loanDebtShares
);
uint256 debt = _convertToAssets(loanDebtShares, newDebtExchangeRateX96, Math.Rounding.Up);
if (debt < minLoanSize) {
revert MinLoanSize();
}
// only does check health here if not in transform mode
if (!isTransformMode) {
_requireLoanIsHealthy(tokenId, debt);
}
address owner = tokenOwner[tokenId];
// fails if not enough asset available
// if called from transform mode - send funds to transformer contract
SafeERC20.safeTransfer(IERC20(asset), isTransformMode ? msg.sender : owner, assets);
emit Borrow(tokenId, owner, assets, shares);
}
/// @dev Decreases the liquidity of a given position and collects the resultant assets (and possibly additional fees)
/// This function is not allowed during transformation (if a transformer wants to decreaseLiquidity he can call the methods directly on the NonfungiblePositionManager)
/// @param params Struct containing various parameters for the operation. Includes tokenId, liquidity amount, minimum asset amounts, and deadline.
/// @return amount0 The amount of the first type of asset collected.
/// @return amount1 The amount of the second type of asset collected.
function decreaseLiquidityAndCollect(DecreaseLiquidityAndCollectParams calldata params)
external
override
returns (uint256 amount0, uint256 amount1)
{
// this method is not allowed during transform - can be called directly on nftmanager if needed from transform contract
if (transformedTokenId > 0) {
revert TransformNotAllowed();
}
address owner = tokenOwner[params.tokenId];
if (owner != msg.sender) {
revert Unauthorized();
}
(uint256 newDebtExchangeRateX96,) = _updateGlobalInterest();
(amount0, amount1) = nonfungiblePositionManager.decreaseLiquidity(
INonfungiblePositionManager.DecreaseLiquidityParams(
params.tokenId, params.liquidity, params.amount0Min, params.amount1Min, params.deadline
)
);
INonfungiblePositionManager.CollectParams memory collectParams = INonfungiblePositionManager.CollectParams(
params.tokenId,
params.recipient,
params.feeAmount0 == type(uint128).max ? type(uint128).max : SafeCast.toUint128(amount0 + params.feeAmount0),
params.feeAmount1 == type(uint128).max ? type(uint128).max : SafeCast.toUint128(amount1 + params.feeAmount1)
);
(amount0, amount1) = nonfungiblePositionManager.collect(collectParams);
uint256 debt = _convertToAssets(loans[params.tokenId].debtShares, newDebtExchangeRateX96, Math.Rounding.Up);
_requireLoanIsHealthy(params.tokenId, debt);
emit WithdrawCollateral(params.tokenId, owner, params.recipient, params.liquidity, amount0, amount1);
}
/// @notice Repays borrowed tokens. Can be denominated in assets or debt share amount
/// @param tokenId The token ID to use as collateral
/// @param amount How many assets/debt shares to repay
/// @param isShare Is amount specified in assets or debt shares.
function repay(uint256 tokenId, uint256 amount, bool isShare) external override {
_repay(tokenId, amount, isShare, "");
}
/// @notice Repays borrowed tokens. Can be denominated in assets or debt share amount
/// @param tokenId The token ID to use as collateral
/// @param amount How many assets/debt shares to repay
/// @param isShare Is amount specified in assets or debt shares.
/// @param permitData Permit2 data and signature
function repay(uint256 tokenId, uint256 amount, bool isShare, bytes calldata permitData) external override {
_repay(tokenId, amount, isShare, permitData);
}
// state used in liquidation function to avoid stack too deep errors
struct LiquidateState {
uint256 newDebtExchangeRateX96;
uint256 newLendExchangeRateX96;
uint256 debt;
bool isHealthy;
uint256 liquidationValue;
uint256 liquidatorCost;
uint256 reserveCost;
uint256 missing;
uint256 fullValue;
uint256 collateralValue;
uint256 feeValue;
}
/// @notice Liquidates position - needed assets are depending on current price.
/// Sufficient assets need to be approved to the contract for the liquidation to succeed.
/// @param params The params defining liquidation
/// @return amount0 The amount of the first type of asset collected.
/// @return amount1 The amount of the second type of asset collected.
function liquidate(LiquidateParams calldata params) external override returns (uint256 amount0, uint256 amount1) {
// liquidation is not allowed during transformer mode
if (transformedTokenId > 0) {
revert TransformNotAllowed();
}
LiquidateState memory state;
(state.newDebtExchangeRateX96, state.newLendExchangeRateX96) = _updateGlobalInterest();
uint256 debtShares = loans[params.tokenId].debtShares;
if (debtShares != params.debtShares) {
revert DebtChanged();
}
state.debt = _convertToAssets(debtShares, state.newDebtExchangeRateX96, Math.Rounding.Up);
(state.isHealthy, state.fullValue, state.collateralValue, state.feeValue) =
_checkLoanIsHealthy(params.tokenId, state.debt);
if (state.isHealthy) {
revert NotLiquidatable();
}
(state.liquidationValue, state.liquidatorCost, state.reserveCost) =
_calculateLiquidation(state.debt, state.fullValue, state.collateralValue);
// calculate reserve (before transfering liquidation money - otherwise calculation is off)
if (state.reserveCost > 0) {
state.missing =
_handleReserveLiquidation(state.reserveCost, state.newDebtExchangeRateX96, state.newLendExchangeRateX96);
}
if (params.permitData.length > 0) {
(ISignatureTransfer.PermitTransferFrom memory permit, bytes memory signature) =
abi.decode(params.permitData, (ISignatureTransfer.PermitTransferFrom, bytes));
permit2.permitTransferFrom(
permit,
ISignatureTransfer.SignatureTransferDetails(address(this), state.liquidatorCost),
msg.sender,
signature
);
} else {
// take value from liquidator
SafeERC20.safeTransferFrom(IERC20(asset), msg.sender, address(this), state.liquidatorCost);
}
debtSharesTotal -= debtShares;
// send promised collateral tokens to liquidator
(amount0, amount1) =
_sendPositionValue(params.tokenId, state.liquidationValue, state.fullValue, state.feeValue, msg.sender);
if (amount0 < params.amount0Min || amount1 < params.amount1Min) {
revert SlippageError();
}
address owner = tokenOwner[params.tokenId];
// disarm loan and send remaining position to owner
_cleanupLoan(params.tokenId, state.newDebtExchangeRateX96, state.newLendExchangeRateX96, owner);
emit Liquidate(
params.tokenId,
msg.sender,
owner,
state.fullValue,
state.liquidatorCost,
amount0,
amount1,
state.reserveCost,
state.missing
);
}
////////////////// ADMIN FUNCTIONS only callable by owner
/// @notice withdraw protocol reserves (onlyOwner)
/// only allows to withdraw excess reserves (> globalLendAmount * reserveProtectionFactor)
/// @param amount amount to withdraw
/// @param receiver receiver address
function withdrawReserves(uint256 amount, address receiver) external onlyOwner {
(uint256 newDebtExchangeRateX96, uint256 newLendExchangeRateX96) = _updateGlobalInterest();
uint256 protected =
_convertToAssets(totalSupply(), newLendExchangeRateX96, Math.Rounding.Up) * reserveProtectionFactorX32 / Q32;
(uint256 balance,, uint256 reserves) = _getAvailableBalance(newDebtExchangeRateX96, newLendExchangeRateX96);
uint256 unprotected = reserves > protected ? reserves - protected : 0;
uint256 available = balance > unprotected ? unprotected : balance;
if (amount > available) {
revert InsufficientLiquidity();
}
if (amount > 0) {
SafeERC20.safeTransfer(IERC20(asset), receiver, amount);
}
emit WithdrawReserves(amount, receiver);
}
/// @notice configure transformer contract (onlyOwner)
/// @param transformer address of transformer contract
/// @param active should the transformer be active?
function setTransformer(address transformer, bool active) external onlyOwner {
// protects protocol from owner trying to set dangerous transformer
if (
transformer == address(0) || transformer == address(this) || transformer == asset
|| transformer == address(nonfungiblePositionManager)
) {
revert InvalidConfig();
}
transformerAllowList[transformer] = active;
emit SetTransformer(transformer, active);
}
/// @notice set limits (this doesnt affect existing loans) - this method can be called by owner OR emergencyAdmin
/// @param _minLoanSize min size of a loan - trying to create smaller loans will revert
/// @param _globalLendLimit global limit of lent amount
/// @param _globalDebtLimit global limit of debt amount
/// @param _dailyLendIncreaseLimitMin min daily increasable amount of lent amount
/// @param _dailyDebtIncreaseLimitMin min daily increasable amount of debt amount
function setLimits(
uint256 _minLoanSize,
uint256 _globalLendLimit,
uint256 _globalDebtLimit,
uint256 _dailyLendIncreaseLimitMin,
uint256 _dailyDebtIncreaseLimitMin
) external {
if (msg.sender != emergencyAdmin && msg.sender != owner()) {
revert Unauthorized();
}
minLoanSize = _minLoanSize;
globalLendLimit = _globalLendLimit;
globalDebtLimit = _globalDebtLimit;
dailyLendIncreaseLimitMin = _dailyLendIncreaseLimitMin;
dailyDebtIncreaseLimitMin = _dailyDebtIncreaseLimitMin;
(, uint256 newLendExchangeRateX96) = _updateGlobalInterest();
// force reset daily limits with new values
_resetDailyLendIncreaseLimit(newLendExchangeRateX96, true);
_resetDailyDebtIncreaseLimit(newLendExchangeRateX96, true);
emit SetLimits(
_minLoanSize, _globalLendLimit, _globalDebtLimit, _dailyLendIncreaseLimitMin, _dailyDebtIncreaseLimitMin
);
}
/// @notice sets reserve factor - percentage difference between debt and lend interest (onlyOwner)
/// @param _reserveFactorX32 reserve factor multiplied by Q32
function setReserveFactor(uint32 _reserveFactorX32) external onlyOwner {
reserveFactorX32 = _reserveFactorX32;
emit SetReserveFactor(_reserveFactorX32);
}
/// @notice sets reserve protection factor - percentage of globalLendAmount which can't be withdrawn by owner (onlyOwner)
/// @param _reserveProtectionFactorX32 reserve protection factor multiplied by Q32
function setReserveProtectionFactor(uint32 _reserveProtectionFactorX32) external onlyOwner {
if (_reserveProtectionFactorX32 < MIN_RESERVE_PROTECTION_FACTOR_X32) {
revert InvalidConfig();
}
reserveProtectionFactorX32 = _reserveProtectionFactorX32;
emit SetReserveProtectionFactor(_reserveProtectionFactorX32);
}
/// @notice Sets or updates the configuration for a token (onlyOwner)
/// @param token Token to configure
/// @param collateralFactorX32 collateral factor for this token mutiplied by Q32
/// @param collateralValueLimitFactorX32 how much of it maybe used as collateral measured as percentage of total lent assets mutiplied by Q32
function setTokenConfig(address token, uint32 collateralFactorX32, uint32 collateralValueLimitFactorX32)
external
onlyOwner
{
if (collateralFactorX32 > MAX_COLLATERAL_FACTOR_X32) {
revert CollateralFactorExceedsMax();
}
tokenConfigs[token].collateralFactorX32 = collateralFactorX32;
tokenConfigs[token].collateralValueLimitFactorX32 = collateralValueLimitFactorX32;
emit SetTokenConfig(token, collateralFactorX32, collateralValueLimitFactorX32);
}
/// @notice Updates emergency admin address (onlyOwner)
/// @param admin Emergency admin address
function setEmergencyAdmin(address admin) external onlyOwner {
emergencyAdmin = admin;
emit SetEmergencyAdmin(admin);
}
////////////////// INTERNAL FUNCTIONS
function _deposit(address receiver, uint256 amount, bool isShare, bytes memory permitData)
internal
returns (uint256 assets, uint256 shares)
{
(, uint256 newLendExchangeRateX96) = _updateGlobalInterest();
_resetDailyLendIncreaseLimit(newLendExchangeRateX96, false);
if (isShare) {
shares = amount;
assets = _convertToAssets(shares, newLendExchangeRateX96, Math.Rounding.Up);
} else {
assets = amount;
shares = _convertToShares(assets, newLendExchangeRateX96, Math.Rounding.Down);
}
if (permitData.length > 0) {
(ISignatureTransfer.PermitTransferFrom memory permit, bytes memory signature) =
abi.decode(permitData, (ISignatureTransfer.PermitTransferFrom, bytes));
permit2.permitTransferFrom(
permit, ISignatureTransfer.SignatureTransferDetails(address(this), assets), msg.sender, signature
);
} else {
// fails if not enough token approved
SafeERC20.safeTransferFrom(IERC20(asset), msg.sender, address(this), assets);
}
_mint(receiver, shares);
if (totalSupply() > globalLendLimit) {
revert GlobalLendLimit();
}
if (assets > dailyLendIncreaseLimitLeft) {
revert DailyLendIncreaseLimit();
} else {
dailyLendIncreaseLimitLeft -= assets;
}
emit Deposit(msg.sender, receiver, assets, shares);
}
// withdraws lent tokens. can be denominated in token or share amount
function _withdraw(address receiver, address owner, uint256 amount, bool isShare)
internal
returns (uint256 assets, uint256 shares)
{
(uint256 newDebtExchangeRateX96, uint256 newLendExchangeRateX96) = _updateGlobalInterest();
if (isShare) {
shares = amount;
assets = _convertToAssets(amount, newLendExchangeRateX96, Math.Rounding.Down);
} else {
assets = amount;
shares = _convertToShares(amount, newLendExchangeRateX96, Math.Rounding.Up);
}
// if caller has allowance for owners shares - may call withdraw
if (msg.sender != owner) {
_spendAllowance(owner, msg.sender, shares);
}
(, uint256 available,) = _getAvailableBalance(newDebtExchangeRateX96, newLendExchangeRateX96);
if (available < assets) {
revert InsufficientLiquidity();
}
// fails if not enough shares
_burn(owner, shares);
SafeERC20.safeTransfer(IERC20(asset), receiver, assets);
// when amounts are withdrawn - they may be deposited again
dailyLendIncreaseLimitLeft += assets;
emit Withdraw(msg.sender, receiver, owner, assets, shares);
}
function _repay(uint256 tokenId, uint256 amount, bool isShare, bytes memory permitData) internal {
(uint256 newDebtExchangeRateX96, uint256 newLendExchangeRateX96) = _updateGlobalInterest();
Loan storage loan = loans[tokenId];
uint256 currentShares = loan.debtShares;
uint256 shares;
uint256 assets;
if (isShare) {
shares = amount;
assets = _convertToAssets(amount, newDebtExchangeRateX96, Math.Rounding.Up);
} else {
assets = amount;
shares = _convertToShares(amount, newDebtExchangeRateX96, Math.Rounding.Down);
}
// fails if too much repayed
if (shares > currentShares) {
revert RepayExceedsDebt();
}
if (assets > 0) {
if (permitData.length > 0) {
(ISignatureTransfer.PermitTransferFrom memory permit, bytes memory signature) =
abi.decode(permitData, (ISignatureTransfer.PermitTransferFrom, bytes));
permit2.permitTransferFrom(
permit, ISignatureTransfer.SignatureTransferDetails(address(this), assets), msg.sender, signature
);
} else {
// fails if not enough token approved
SafeERC20.safeTransferFrom(IERC20(asset), msg.sender, address(this), assets);
}
}
uint256 loanDebtShares = loan.debtShares - shares;
loan.debtShares = loanDebtShares;
debtSharesTotal -= shares;
// when amounts are repayed - they maybe borrowed again
dailyDebtIncreaseLimitLeft += assets;
_updateAndCheckCollateral(
tokenId, newDebtExchangeRateX96, newLendExchangeRateX96, loanDebtShares + shares, loanDebtShares
);