-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoanManager.sol
More file actions
690 lines (609 loc) · 25.7 KB
/
Copy pathLoanManager.sol
File metadata and controls
690 lines (609 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//SPDX-License-Identifier:MIT
// Layout of Contract:
// version
// imports
// interfaces, libraries, contracts
// errors
// Type declarations
// State variables
// Events
// Modifiers
// Functions
// Layout of Functions:
// constructor
// receive function (if exists)
// fallback function (if exists)
// external
// public
// internal
// private
// view & pure functions
pragma solidity ^0.8.24;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
SafeERC20
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {
ReentrancyGuard
} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {ReceiverTemplate} from "./ReceiverTemplate.sol";
// Note: Ownable is inherited via ReceiverTemplate
import {ILendingPool} from "./interfaces/ILendingPool.sol";
import {IPropertyNFT} from "./interfaces/IPropertyNFT.sol";
import {ILienFiAuction} from "./interfaces/ILienFiAuction.sol";
/**
* @title LoanManager
* @author LienFi Team
*
* The single contract that owns the complete mortgage lifecycle:
*
* Architecture:
* - Inherits ReceiverTemplate — CRE reports arrive via KeystoneForwarder → onReport()
* - _processReport() dispatches by workflowName (bytes10) to _writeVerdict()
* - Same pattern as LienFiAuction.sol for consistency across the protocol
*
* Flow:
* - Borrower anchors a loan request on-chain via requestHash
* - LoanRequestSubmitted event auto-triggers CRE credit assessment workflow
* - CRE writes verdict back via KeystoneForwarder → onReport → _processReport → _writeVerdict
* - Borrower calls claimLoan() to lock NFT collateral and receive USDC
* - Monthly repayments via repay() — full EMI to pool, exchange rate rises
* - 3 consecutive missed payments → default → sealed-bid auction
* - Auction settlement → pool repaid, surplus to borrower
*
* Privacy guarantees:
* - No financial data ever touches this contract
* - Only requestHash (opaque) and verdict (approve/reject + limit) are on-chain
* - Credit score, Plaid data, Anthropic reasoning all discarded in CRE enclave
*
* CRE workflow report encoding (credit-assessment-workflow):
* abi.encode(address borrower, bytes32 requestHash, bool approved,
* uint256 tokenId, uint256 approvedLimit, uint256 tenureMonths,
* uint256 computedEMI, uint256 expiresAt)
*/
contract LoanManager is ReceiverTemplate, ReentrancyGuard {
using SafeERC20 for IERC20;
///////////////////
// Workflow Name Constants (bytes10)
///////////////////
// Encoding: SHA256("credit-assessment") → first 10 hex chars → hex-encode ASCII → bytes10
// "credit-assessment" → SHA256: 0001d1ed53... → ASCII hex: 0x30303031643165643533
bytes10 private constant WORKFLOW_CREDIT = bytes10(0x30303031643165643533);
// "create-auction" → SHA256: 605b9393da... → ASCII hex: 0x36303562393339336461
bytes10 private constant WORKFLOW_CREATE = bytes10(0x36303562393339336461);
/////////////////
//Errors
/////////////////
error LoanManager__NotLienFiAuction();
error LoanManager__ZeroAddress();
error LoanManager__ZeroAmount();
error LoanManager__RequestAlreadyPending();
error LoanManager__HasActiveLoan();
error LoanManager__NoApproval();
error LoanManager__ApprovalExpired();
error LoanManager__RequestHashMismatch();
error LoanManager__LoanNotFound();
error LoanManager__LoanNotActive();
error LoanManager__NotBorrower();
error LoanManager__LoanNotDefaulted();
error LoanManager__NoPendingRequest();
error LoanManager__UnknownWorkflow(bytes10 workflowName);
///////////////////
// Type Declarations
////////////////////
enum LoanStatus {
ACTIVE,
DEFAULTED,
CLOSED
}
/**
* @notice CRE-written approval stored after credit assessment.
* @dev Consumed by claimLoan() — single-use, cleared after claim.
* tokenId is included so claimLoan() reads all values from the approval
* with zero user-supplied parameters beyond requestHash.
*/
struct Approval {
bytes32 requestHash; // tamper-proof link to exact assessed request
uint256 tokenId; // PropertyNFT to lock as collateral
uint256 approvedLimit; // max USDC borrowable (6 decimals)
uint256 tenureMonths; // approved loan tenure
uint256 computedEMI; // monthly payment (6 decimals), computed in enclave
uint256 expiresAt; // approval validity window (unix timestamp)
bool exists;
}
/**
* @notice On-chain loan record — created by claimLoan(), updated by repay()/_processDefault().
*/
struct Loan {
uint256 loanId;
address borrower;
uint256 tokenId; // PropertyNFT locked as collateral
uint256 principal; // USDC disbursed (6 decimals)
uint256 interestRateBps; // annual rate e.g. 800 = 8%
uint256 tenureMonths;
uint256 emiAmount; // fixed monthly payment (6 decimals)
uint256 nextDueDate; // unix timestamp of next payment due
uint256 missedPayments; // consecutive missed payment counter
uint256 remainingPrincipal; // outstanding balance (6 decimals)
LoanStatus status;
}
///////////////////
// Constants
///////////////////
uint256 public constant EMI_PERIOD = 1 minutes; //1 minute in secs
uint256 public constant BPS_DENOMINATOR = 10_000; //basis points denominator for interest rate calculations (10000 = 100%)
///////////////////
// State Variables
///////////////////
ILendingPool public immutable lendingPool;
IPropertyNFT public immutable propertyNFT;
ILienFiAuction public immutable lienFiAuction;
IERC20 public immutable usdc;
uint256 public interestRateBps; //annual interest rate in basis points (e.g. 800 = 8%)
mapping(address => bytes32) public pendingRequests;
mapping(address => Approval) public pendingApprovals;
mapping(uint256 => Loan) public loans; // loanId → Loan
mapping(address => uint256) public borrowerActiveLoan; // borrower → active loanId (0 if none)
mapping(uint256 => uint256) public tokenIdToLoanId;
uint256 public loanCounter; //Auto-incrementing loan ID counter (starts at 1, 0 = no loan)
///////////////////
// Events
///////////////////
event LoanRequestSubmitted(address indexed borrower, bytes32 requestHash);
event LoanRequestApproved(
address indexed borrower,
bytes32 requestHash,
uint256 approvedLimit,
uint256 tenureMonths,
uint256 computedEMI
);
event LoanRequestRejected(address indexed borrower, bytes32 requestHash);
event LoanOriginated(
uint256 indexed loanId,
address indexed borrower,
uint256 tokenId,
uint256 principal,
uint256 emiAmount,
uint256 tenureMonths
);
event RepaymentMade(
uint256 indexed loanId,
uint256 emiAmount,
uint256 remainingPrincipal
);
event LoanClosed(uint256 indexed loanId);
event LoanDefaulted(
uint256 indexed loanId,
address indexed borrower,
uint256 tokenId,
uint256 remainingPrincipal
);
event LoanLiquidated(
uint256 indexed loanId,
uint256 proceeds,
uint256 remainingPrincipal
);
///////////////////
// Modifiers
////////////////////
modifier onlyLienFiAuction() {
if (msg.sender != address(lienFiAuction))
revert LoanManager__NotLienFiAuction();
_;
}
//////////////////
// Constructor
///////////////////
/**
* @param _forwarder CRE KeystoneForwarder address (passed to ReceiverTemplate)
* @param _lendingPool LendingPool contract address
* @param _propertyNFT PropertyNFT contract address
* @param _lienFiAuction LienFiAuction contract address
* @param _usdc USDC token address
* @param _interestRateBps Annual interest rate in basis points (e.g. 800 = 8%)
*/
constructor(
address _forwarder,
address _lendingPool,
address _propertyNFT,
address _lienFiAuction,
address _usdc,
uint256 _interestRateBps
) ReceiverTemplate(_forwarder) {
if (
_lendingPool == address(0) ||
_propertyNFT == address(0) ||
_lienFiAuction == address(0) ||
_usdc == address(0)
) revert LoanManager__ZeroAddress();
lendingPool = ILendingPool(_lendingPool);
propertyNFT = IPropertyNFT(_propertyNFT);
lienFiAuction = ILienFiAuction(_lienFiAuction);
usdc = IERC20(_usdc);
interestRateBps = _interestRateBps;
}
/**
* @notice Dispatches CRE reports to the correct handler using workflowName from metadata.
* @dev Called by ReceiverTemplate.onReport() after forwarder validation.
* Handles two workflows:
* "credit" → _writeVerdict (credit assessment result)
* "create" → _processDefault (default detection + auction creation)
*
* Encoding matches LienFiAuction pattern:
* SHA256(name) → first 10 hex chars → hex-encode ASCII → bytes10
*/
function _processReport(
bytes calldata metadata,
bytes calldata report
) internal override {
(, bytes10 workflowName, ) = _decodeMetadata(metadata);
if (workflowName == WORKFLOW_CREDIT) {
_writeVerdict(report);
} else if (workflowName == WORKFLOW_CREATE) {
_processDefault(report);
} else {
revert LoanManager__UnknownWorkflow(workflowName);
}
}
/**
* @notice Anchor a loan request on-chain. Emits event that triggers CRE assessment.
* @dev Borrower first POSTs full request to API (gets requestHash back),
* then calls this function with that requestHash.
*
* Flow:
* 1. Borrower → API: POST /loanRequest { plaidToken, tokenId, amount, tenure }
* 2. API computes requestHash = keccak256(borrower + tokenId + amount + tenure + nonce)
* 3. API returns requestHash to borrower
* 4. Borrower → this function: submitRequest(requestHash)
* 5. Event emitted → CRE auto-triggered → assessment → _writeVerdict()
*
* Rejects if:
* - Borrower already has a pending request
* - Borrower already has an active loan
*
* @param requestHash The hash received from the API, anchoring the off-chain request
*/
function submitRequest(bytes32 requestHash) external {
if (pendingRequests[msg.sender] != bytes32(0))
revert LoanManager__RequestAlreadyPending();
if (borrowerActiveLoan[msg.sender] != 0)
revert LoanManager__HasActiveLoan();
pendingRequests[msg.sender] = requestHash;
emit LoanRequestSubmitted(msg.sender, requestHash);
}
/**
* @notice Process credit assessment verdict from CRE workflow.
* @dev Reached via: KeystoneForwarder → onReport → _processReport → _writeVerdict
*
* The CRE credit-assessment-workflow:
* 1. Fetched request details from API DB using requestHash
* 2. Verified hash integrity (recomputed and matched on-chain hash)
* 3. Fetched Plaid financial data via Confidential HTTP
* 4. Ran hard gates (LTV ≤ 80%, coverage ≥ 3×, no recent defaults)
* 5. Sent metrics to Gemini for scoring
* 6. Checked LendingPool.availableLiquidity() ≥ approvedAmount
* 7. Encoded verdict into this report
*
* All raw financial data is discarded in the enclave. Only the verdict
* (approve/reject + limit) reaches the chain.
*
* Report encoding (approved):
* abi.encode(borrower, requestHash, true, tokenId, approvedLimit, tenureMonths, computedEMI, expiresAt)
* Report encoding (rejected):
* abi.encode(borrower, requestHash, false, 0, 0, 0, 0, 0)
*
* @param report ABI-encoded verdict from CRE workflow
*/
function _writeVerdict(bytes calldata report) internal {
(
address borrower,
bytes32 requestHash,
bool approved,
uint256 tokenId,
uint256 approvedLimit,
uint256 tenureMonths,
uint256 computedEMI,
uint256 expiresAt
) = abi.decode(
report,
(
address,
bytes32,
bool,
uint256,
uint256,
uint256,
uint256,
uint256
)
);
if (pendingRequests[borrower] == bytes32(0))
revert LoanManager__NoPendingRequest();
if (pendingRequests[borrower] != requestHash)
revert LoanManager__RequestHashMismatch();
delete pendingRequests[borrower];
if (approved) {
pendingApprovals[borrower] = Approval({
requestHash: requestHash,
tokenId: tokenId,
approvedLimit: approvedLimit,
tenureMonths: tenureMonths,
computedEMI: computedEMI,
expiresAt: expiresAt,
exists: true
});
emit LoanRequestApproved(
borrower,
requestHash,
approvedLimit,
tenureMonths,
computedEMI
);
} else {
emit LoanRequestRejected(borrower, requestHash);
}
}
/**
* @notice Activate an approved loan. Lock PropertyNFT, receive USDC.
* @dev Borrower calls this after CRE has written an approval via _writeVerdict().
* ALL loan parameters are read from the stored approval — no user-supplied
* values beyond requestHash (which acts as a claim ticket).
*
* The liquidity check was already performed by the CRE workflow before
* writing the approval. claimLoan() does NOT re-check liquidity.
* This is safe because:
* - The approval has an expiry window (expiresAt)
* - If liquidity dried up between approval and claim, disburse() will revert
* - The CRE check prevents most race conditions; disburse() catches the rest
*
* Flow:
* 1. Load approval — must exist and not be expired
* 2. Verify requestHash matches (proves borrower is claiming the right approval)
* 3. Check no active loan
* 4. Read all values from approval (tokenId, amount, tenure, EMI)
* 5. Transfer PropertyNFT from borrower → LoanManager (collateral lock)
* 6. Clear approval (single-use)
* 7. Disburse USDC from pool to borrower
* 8. Create loan record
*
* @param requestHash The request hash that links to the approval being claimed
*/
function claimLoan(bytes32 requestHash) external nonReentrant {
Approval memory approval = pendingApprovals[msg.sender];
// 1. Approval must exist
if (!approval.exists) revert LoanManager__NoApproval();
// 2. Must not be expired
if (block.timestamp > approval.expiresAt)
revert LoanManager__ApprovalExpired();
// 3. RequestHash must match
if (approval.requestHash != requestHash)
revert LoanManager__RequestHashMismatch();
// 4. No active loan
if (borrowerActiveLoan[msg.sender] != 0)
revert LoanManager__HasActiveLoan();
// 5. Read all values from approval
uint256 tokenId = approval.tokenId;
uint256 principal = approval.approvedLimit;
uint256 tenureMonths = approval.tenureMonths;
uint256 emiAmount = approval.computedEMI;
// 6. Clear approval — single-use
delete pendingApprovals[msg.sender];
// 7. Lock PropertyNFT as collateral (borrower must have approved LoanManager)
propertyNFT.transferFrom(msg.sender, address(this), tokenId);
// 8. Create loan record
loanCounter++;
uint256 loanId = loanCounter;
loans[loanId] = Loan({
loanId: loanId,
borrower: msg.sender,
tokenId: tokenId,
principal: principal,
interestRateBps: interestRateBps,
tenureMonths: tenureMonths,
emiAmount: emiAmount,
nextDueDate: block.timestamp + EMI_PERIOD,
missedPayments: 0,
remainingPrincipal: principal,
status: LoanStatus.ACTIVE
});
borrowerActiveLoan[msg.sender] = loanId;
tokenIdToLoanId[tokenId] = loanId;
// 9. Disburse USDC from pool to borrower
// If pool doesn't have enough liquidity, disburse() reverts — safety net
lendingPool.disburse(msg.sender, principal);
emit LoanOriginated(
loanId,
msg.sender,
tokenId,
principal,
emiAmount,
tenureMonths
);
}
/**
* @notice Make a monthly EMI payment on an active loan.
* @dev Accepts exactly emiAmount USDC from the borrower.
* Computes principal vs interest split for internal accounting.
* Full EMI goes to pool — interest portion causes clUSDC exchange rate to rise.
*
* Interest calculation (standard amortization):
* interestPortion = remainingPrincipal × (annualRate / 10000 / 12)
* principalPortion = emiAmount - interestPortion
*
* The USDC transfer flow (Pattern A from Phase 2):
* 1. This function transfers USDC from borrower → LendingPool
* 2. Then calls lendingPool.repayEMI() for accounting only
* (Borrower only needs to approve LoanManager, not LendingPool)
*
* On final payment (remainingPrincipal reaches 0):
* - Loan status → CLOSED
* - PropertyNFT returned to borrower
* - borrowerActiveLoan cleared
*
* @param loanId The ID of the loan to repay
*/
function repay(uint256 loanId) external nonReentrant {
Loan storage loan = loans[loanId];
if (loan.loanId == 0) revert LoanManager__LoanNotFound();
if (loan.status != LoanStatus.ACTIVE)
revert LoanManager__LoanNotActive();
if (msg.sender != loan.borrower) revert LoanManager__NotBorrower();
uint256 emiAmount = loan.emiAmount;
// Compute interest/principal split
// interestPortion = remainingPrincipal * interestRateBps / (BPS_DENOMINATOR * 12)
uint256 interestPortion = (loan.remainingPrincipal * interestRateBps) /
(BPS_DENOMINATOR * 12);
uint256 principalPortion = emiAmount - interestPortion;
// Handle final payment — if remaining principal is less than or equal to
// the computed principal portion, this is the last payment
if (principalPortion >= loan.remainingPrincipal) {
principalPortion = loan.remainingPrincipal;
}
// Reduce remaining principal
loan.remainingPrincipal -= principalPortion;
// Reset missed payments counter (on-time payment)
loan.missedPayments = 0;
// Advance next due date
loan.nextDueDate += EMI_PERIOD;
// Transfer USDC from borrower → LendingPool (Pattern A)
usdc.safeTransferFrom(msg.sender, address(lendingPool), emiAmount);
// Update pool accounting
lendingPool.repayEMI(emiAmount, principalPortion);
emit RepaymentMade(loanId, emiAmount, loan.remainingPrincipal);
// Check if loan is fully repaid
if (loan.remainingPrincipal == 0) {
_closeLoan(loanId);
}
}
/**
* @notice Callback from LienFiAuction after auction settles. LienFiAuction only.
* @dev Called by LienFiAuction._settleAuction() after NFT is transferred to winner.
* Handles fund distribution:
* - proceeds >= debt: full repayment to pool, surplus to borrower
* - proceeds < debt: partial repayment, shortfall absorbed by pool
*
* The USDC transfer flow:
* - LienFiAuction transfers `proceeds` USDC to this contract before calling
* - This function routes USDC to pool (and surplus to borrower if applicable)
*
* @param tokenId The PropertyNFT tokenId that was auctioned
* @param proceeds The USDC amount from auction settlement (6 decimals)
*/
function onAuctionSettled(
uint256 tokenId,
uint256 proceeds
) external onlyLienFiAuction nonReentrant {
uint256 loanId = tokenIdToLoanId[tokenId];
Loan storage loan = loans[loanId];
if (loan.loanId == 0) revert LoanManager__LoanNotFound();
if (loan.status != LoanStatus.DEFAULTED)
revert LoanManager__LoanNotDefaulted();
uint256 debt = loan.remainingPrincipal;
if (proceeds >= debt) {
// Full recovery — repay entire debt to pool
usdc.safeTransfer(address(lendingPool), debt);
lendingPool.repayEMI(debt, debt);
uint256 surplus = proceeds - debt;
if (surplus > 0) {
usdc.safeTransfer(loan.borrower, surplus);
}
} else {
usdc.safeTransfer(address(lendingPool), proceeds);
lendingPool.repayEMI(proceeds, proceeds);
}
emit LoanLiquidated(loanId, proceeds, debt);
// Close loan record
loan.status = LoanStatus.CLOSED;
loan.remainingPrincipal = 0;
delete borrowerActiveLoan[loan.borrower];
delete tokenIdToLoanId[tokenId];
}
// ═══════════════════════════════════════════════════════════════
// INTERNAL FUNCTIONS
// ═══════════════════════════════════════════════════════════════
/**
* @notice Process a default report from the CRE create-auction workflow.
* @dev The cron workflow scans active loans, detects overdue payments (>= 3 periods),
* and writes a DON report with the loanId. This function:
* 1. Validates the loan exists and is ACTIVE
* 2. Sets status to DEFAULTED
* 3. Transfers PropertyNFT to LienFiAuction
* 4. Calls initiateDefaultAuction with reservePrice = remainingPrincipal
*
* Report encoding: abi.encode(uint256 loanId, bytes32 listingHash)
*/
function _processDefault(bytes calldata report) internal {
(uint256 loanId, bytes32 listingHash) = abi.decode(report, (uint256, bytes32));
Loan storage loan = loans[loanId];
if (loan.loanId == 0) revert LoanManager__LoanNotFound();
if (loan.status != LoanStatus.ACTIVE)
revert LoanManager__LoanNotActive();
loan.status = LoanStatus.DEFAULTED;
uint256 tokenId = loan.tokenId;
uint256 reservePrice = loan.remainingPrincipal;
propertyNFT.transferFrom(
address(this),
address(lienFiAuction),
tokenId
);
bytes32 auctionId = keccak256(abi.encodePacked(tokenId, loanId));
lienFiAuction.initiateDefaultAuction(
tokenId,
reservePrice,
auctionId,
block.timestamp + 5 minutes,
listingHash
);
emit LoanDefaulted(loanId, loan.borrower, tokenId, reservePrice);
}
/**
* @notice Close a fully repaid loan — return NFT and clean up state.
*/
function _closeLoan(uint256 loanId) internal {
Loan storage loan = loans[loanId];
loan.status = LoanStatus.CLOSED;
propertyNFT.transferFrom(address(this), loan.borrower, loan.tokenId);
delete borrowerActiveLoan[loan.borrower];
delete tokenIdToLoanId[loan.tokenId];
emit LoanClosed(loanId);
}
// ═══════════════════════════════════════════════════════════════
// ADMIN FUNCTIONS
// ═══════════════════════════════════════════════════════════════
/**
* @notice Update the interest rate. Owner only.
* @dev Only affects NEW loans. Existing loans keep their rate.
*/
function setInterestRate(uint256 _interestRateBps) external onlyOwner {
interestRateBps = _interestRateBps;
}
// ═══════════════════════════════════════════════════════════════
// VIEW FUNCTIONS
// ═══════════════════════════════════════════════════════════════
/**
* @notice Get full loan details.
*/
function getLoan(uint256 loanId) external view returns (Loan memory) {
return loans[loanId];
}
/**
* @notice Get the active loan ID for a borrower (0 = no active loan).
*/
function getActiveLoanId(address borrower) external view returns (uint256) {
return borrowerActiveLoan[borrower];
}
/**
* @notice Get pending approval for a borrower.
*/
function getApproval(
address borrower
) external view returns (Approval memory) {
return pendingApprovals[borrower];
}
/**
* @notice Check if a borrower has a pending request.
*/
function hasPendingRequest(address borrower) external view returns (bool) {
return pendingRequests[borrower] != bytes32(0);
}
}