@@ -11,6 +11,7 @@ import {UpgradeableClaimable} from "../common/UpgradeableClaimable.sol";
11
11
import {OneInchExchange} from "./libraries/OneInchExchange.sol " ;
12
12
13
13
import {ILoanToken2} from "./interface/ILoanToken2.sol " ;
14
+ import {ILoanFactory2} from "./interface/ILoanFactory2.sol " ;
14
15
import {IDebtToken} from "../truefi2/interface/ILoanToken2.sol " ;
15
16
import {IStakingPool} from "../truefi/interface/IStakingPool.sol " ;
16
17
import {IFixedTermLoanAgency} from "./interface/IFixedTermLoanAgency.sol " ;
@@ -63,7 +64,7 @@ contract FixedTermLoanAgency is IFixedTermLoanAgency, UpgradeableClaimable {
63
64
64
65
IStakingPool public stakingPool;
65
66
66
- IPoolFactory public factory ;
67
+ IPoolFactory public poolFactory ;
67
68
68
69
I1Inch3 public _1inch;
69
70
@@ -89,6 +90,8 @@ contract FixedTermLoanAgency is IFixedTermLoanAgency, UpgradeableClaimable {
89
90
// mutex ensuring there's only one running loan or credit line for borrower
90
91
IBorrowingMutex public borrowingMutex;
91
92
93
+ ILoanFactory2 public loanFactory;
94
+
92
95
mapping (address => bool ) public isBorrowerAllowed;
93
96
94
97
// ======= STORAGE DECLARATION END ============
@@ -162,7 +165,7 @@ contract FixedTermLoanAgency is IFixedTermLoanAgency, UpgradeableClaimable {
162
165
* @dev Can be only called by a pool
163
166
*/
164
167
modifier onlySupportedPool () {
165
- require (factory .isSupportedPool (ITrueFiPool2 (msg .sender )), "FixedTermLoanAgency: Pool not supported by the factory " );
168
+ require (poolFactory .isSupportedPool (ITrueFiPool2 (msg .sender )), "FixedTermLoanAgency: Pool not supported by the factory " );
166
169
_;
167
170
}
168
171
@@ -174,25 +177,27 @@ contract FixedTermLoanAgency is IFixedTermLoanAgency, UpgradeableClaimable {
174
177
/**
175
178
* @dev Initialize the contract with parameters
176
179
* @param _stakingPool stkTRU address
177
- * @param _factory PoolFactory address
180
+ * @param _poolFactory PoolFactory address
178
181
* @param __1inch 1Inch exchange address (0x11111112542d85b3ef69ae05771c2dccff4faa26 for mainnet)
179
182
*/
180
183
function initialize (
181
184
IStakingPool _stakingPool ,
182
- IPoolFactory _factory ,
185
+ IPoolFactory _poolFactory ,
183
186
I1Inch3 __1inch ,
184
187
ITrueFiCreditOracle _creditOracle ,
185
188
ITrueRateAdjuster _rateAdjuster ,
186
- IBorrowingMutex _borrowingMutex
189
+ IBorrowingMutex _borrowingMutex ,
190
+ ILoanFactory2 _loanFactory
187
191
) public initializer {
188
192
UpgradeableClaimable.initialize (msg .sender );
189
193
190
194
stakingPool = _stakingPool;
191
- factory = _factory ;
195
+ poolFactory = _poolFactory ;
192
196
_1inch = __1inch;
193
197
creditOracle = _creditOracle;
194
198
rateAdjuster = _rateAdjuster;
195
199
borrowingMutex = _borrowingMutex;
200
+ loanFactory = _loanFactory;
196
201
197
202
swapFeeSlippage = 100 ; // 1%
198
203
fee = 1000 ;
@@ -302,55 +307,47 @@ contract FixedTermLoanAgency is IFixedTermLoanAgency, UpgradeableClaimable {
302
307
address borrower ,
303
308
uint256 amount ,
304
309
uint256 term
305
- ) internal view returns (uint256 ) {
310
+ ) public view returns (uint256 ) {
306
311
uint8 borrowerScore = creditOracle.score (borrower);
307
312
uint256 fixedTermLoanAdjustment = rateAdjuster.fixedTermLoanAdjustment (term);
308
313
return rateAdjuster.rate (pool, borrowerScore, amount).add (fixedTermLoanAdjustment);
309
314
}
310
315
311
316
/**
312
- * @dev Fund a loan
313
- * LoanToken should be created by the LoanFactory over the pool
314
- * than was also created by the PoolFactory.
317
+ * @dev Create and fund a loan via LoanFactory for a pool supported by PoolFactory
315
318
* Method should be called by the loan borrower
316
319
*
317
320
* When called, agency takes funds from the pool, gives it to the loan and holds all LoanTokens
318
321
* Origination fee is transferred to the stake
319
- *
320
- * @param loanToken LoanToken to fund
321
322
*/
322
- function fund (ILoanToken2 loanToken ) external onlyAllowedBorrowers {
323
- // TODO Replace hardcoded _maxApy value with a param
324
- uint256 _maxApy = 10000 ;
325
-
326
- ITrueFiPool2 pool = loanToken. pool ();
327
- require (factory. isSupportedPool (pool), " FixedTermLoanAgency: Pool not supported by the factory " );
328
- require (loanToken. token () == pool. token ( ), "FixedTermLoanAgency: Loan and pool token mismatch " );
323
+ function fund (
324
+ ITrueFiPool2 pool ,
325
+ uint256 amount ,
326
+ uint256 term ,
327
+ uint256 _maxApy
328
+ ) external onlyAllowedBorrowers {
329
+ require (poolFactory. isSupportedPool ( pool), "FixedTermLoanAgency: Pool not supported by the factory " );
329
330
require (poolLoans[pool].length < maxLoans, "FixedTermLoanAgency: Loans number has reached the limit " );
330
331
331
- address borrower = loanToken.borrower ();
332
- require (msg .sender == borrower, "FixedTermLoanAgency: Sender is not borrower " );
332
+ address borrower = msg .sender ;
333
333
require (borrowingMutex.isUnlocked (borrower), "FixedTermLoanAgency: There is an ongoing loan or credit line " );
334
- borrowingMutex.lock (borrower, address (loanToken));
335
334
require (
336
335
creditOracle.status (borrower) == ITrueFiCreditOracle.Status.Eligible,
337
336
"FixedTermLoanAgency: Sender is not eligible for loan "
338
337
);
339
338
340
- uint256 term = loanToken.term ();
341
- require (term > 0 , "LoanFactory: Loans cannot have instantaneous term of repay " );
339
+ require (amount > 0 , "FixedTermLoanAgency: Loans of amount 0, will not be approved " );
340
+ require (amount <= borrowLimit (pool, borrower), "FixedTermLoanAgency: Loan amount cannot exceed borrow limit " );
341
+
342
+ require (term > 0 , "FixedTermLoanAgency: Loans cannot have instantaneous term of repay " );
342
343
require (isTermBelowMax (term), "FixedTermLoanAgency: Loan's term is too long " );
343
344
require (isCredibleForTerm (term), "FixedTermLoanAgency: Credit score is too low for loan's term " );
344
345
345
- uint256 amount = loanToken.amount ();
346
- require (amount > 0 , "LoanFactory: Loans of amount 0, will not be approved " );
347
- require (amount <= borrowLimit (pool, loanToken.borrower ()), "FixedTermLoanAgency: Loan amount cannot exceed borrow limit " );
348
-
349
346
uint256 apy = rate (pool, borrower, amount, term);
350
- // TODO use the above apy calculation and delete the below apy when calling LF2.createFTLALoanToken()
351
- apy = loanToken.apy ();
352
- require (apy <= _maxApy, "LoanFactory: Calculated apy is higher than max apy " );
347
+ require (apy <= _maxApy, "FixedTermLoanAgency: Calculated apy is higher than max apy " );
353
348
349
+ ILoanToken2 loanToken = loanFactory.createFTLALoanToken (pool, borrower, amount, term, apy);
350
+ borrowingMutex.lock (borrower, address (loanToken));
354
351
poolLoans[pool].push (loanToken);
355
352
pool.borrow (amount);
356
353
pool.token ().safeApprove (address (loanToken), amount);
@@ -415,7 +412,7 @@ contract FixedTermLoanAgency is IFixedTermLoanAgency, UpgradeableClaimable {
415
412
uint256 resultPrecision = uint256 (10 )** decimals;
416
413
417
414
// loop through loans and sum amount borrowed accounting for precision
418
- ITrueFiPool2[] memory pools = factory .getSupportedPools ();
415
+ ITrueFiPool2[] memory pools = poolFactory .getSupportedPools ();
419
416
for (uint8 i = 0 ; i < pools.length ; i++ ) {
420
417
ITrueFiPool2 pool = pools[i];
421
418
uint256 poolPrecision = uint256 (10 )** ITrueFiPool2WithDecimals (address (pool)).decimals ();
0 commit comments