@@ -476,8 +476,8 @@ makeSuite('Pool Liquidation: Liquidates borrows in eMode with price change', (te
476
476
477
477
const balanceAfter = await aDai . balanceOf ( user1 . address ) ;
478
478
479
- const debtPrice = await oracle . getAssetPrice ( usdc . address ) ;
480
- const collateralPrice = await oracle . getAssetPrice ( dai . address ) ;
479
+ const debtPrice = await oracle . getAssetPrice ( EMODE_ORACLE_ADDRESS ) ;
480
+ const collateralPrice = await oracle . getAssetPrice ( EMODE_ORACLE_ADDRESS ) ;
481
481
482
482
const expectedCollateralLiquidated = debtPrice
483
483
. mul ( toBorrow . div ( 2 ) )
@@ -489,4 +489,133 @@ makeSuite('Pool Liquidation: Liquidates borrows in eMode with price change', (te
489
489
490
490
expect ( collateralLiquidated ) . to . be . closeTo ( expectedCollateralLiquidated , 2 ) ;
491
491
} ) ;
492
+
493
+ it ( 'Liquidation of non-eMode collateral with eMode debt in eMode with custom price feed' , async ( ) => {
494
+ await evmRevert ( snap ) ;
495
+ snap = await evmSnapshot ( ) ;
496
+
497
+ const {
498
+ helpersContract,
499
+ oracle,
500
+ configurator,
501
+ pool,
502
+ poolAdmin,
503
+ dai,
504
+ usdc,
505
+ weth,
506
+ aWETH,
507
+ users : [ user1 , user2 ] ,
508
+ } = testEnv ;
509
+
510
+ // We need an extra oracle for prices. USe user address as asset in price oracle
511
+ const EMODE_ORACLE_ADDRESS = user1 . address ;
512
+ await oracle . setAssetPrice ( EMODE_ORACLE_ADDRESS , utils . parseUnits ( '1' , 8 ) ) ;
513
+ await oracle . setAssetPrice ( dai . address , utils . parseUnits ( '0.99' , 8 ) ) ;
514
+ await oracle . setAssetPrice ( usdc . address , utils . parseUnits ( '1.01' , 8 ) ) ;
515
+ await oracle . setAssetPrice ( weth . address , utils . parseUnits ( '4000' , 8 ) ) ;
516
+
517
+ // Create category
518
+ expect (
519
+ await configurator
520
+ . connect ( poolAdmin . signer )
521
+ . setEModeCategory (
522
+ 1 ,
523
+ CATEGORY . ltv ,
524
+ CATEGORY . lt ,
525
+ CATEGORY . lb ,
526
+ EMODE_ORACLE_ADDRESS ,
527
+ CATEGORY . label
528
+ )
529
+ ) ;
530
+
531
+ const categoryData = await pool . getEModeCategoryData ( CATEGORY . id ) ;
532
+
533
+ expect ( categoryData . ltv ) . to . be . equal ( CATEGORY . ltv , 'invalid eMode category ltv' ) ;
534
+ expect ( categoryData . liquidationThreshold ) . to . be . equal (
535
+ CATEGORY . lt ,
536
+ 'invalid eMode category liq threshold'
537
+ ) ;
538
+ expect ( categoryData . liquidationBonus ) . to . be . equal (
539
+ CATEGORY . lb ,
540
+ 'invalid eMode category liq bonus'
541
+ ) ;
542
+ expect ( categoryData . priceSource ) . to . be . equal (
543
+ EMODE_ORACLE_ADDRESS ,
544
+ 'invalid eMode category price source'
545
+ ) ;
546
+
547
+ // Add Dai and USDC to category
548
+ await configurator . connect ( poolAdmin . signer ) . setAssetEModeCategory ( dai . address , CATEGORY . id ) ;
549
+ await configurator . connect ( poolAdmin . signer ) . setAssetEModeCategory ( usdc . address , CATEGORY . id ) ;
550
+ expect ( await helpersContract . getReserveEModeCategory ( dai . address ) ) . to . be . eq ( CATEGORY . id ) ;
551
+ expect ( await helpersContract . getReserveEModeCategory ( usdc . address ) ) . to . be . eq ( CATEGORY . id ) ;
552
+
553
+ // User 1 supply 1 dai + 1 eth, user 2 supply 10000 usdc
554
+ const wethSupplyAmount = utils . parseUnits ( '1' , 18 ) ;
555
+ const daiSupplyAmount = utils . parseUnits ( '1' , 18 ) ;
556
+ const usdcSupplyAmount = utils . parseUnits ( '10000' , 6 ) ;
557
+
558
+ expect ( await dai . connect ( user1 . signer ) [ 'mint(uint256)' ] ( daiSupplyAmount ) ) ;
559
+ expect ( await weth . connect ( user1 . signer ) [ 'mint(uint256)' ] ( wethSupplyAmount ) ) ;
560
+ expect ( await usdc . connect ( user2 . signer ) [ 'mint(uint256)' ] ( usdcSupplyAmount . mul ( 2 ) ) ) ;
561
+
562
+ expect ( await dai . connect ( user1 . signer ) . approve ( pool . address , MAX_UINT_AMOUNT ) ) ;
563
+ expect ( await weth . connect ( user1 . signer ) . approve ( pool . address , MAX_UINT_AMOUNT ) ) ;
564
+ expect ( await usdc . connect ( user2 . signer ) . approve ( pool . address , MAX_UINT_AMOUNT ) ) ;
565
+
566
+ expect ( await pool . connect ( user1 . signer ) . supply ( dai . address , daiSupplyAmount , user1 . address , 0 ) ) ;
567
+ expect (
568
+ await pool . connect ( user1 . signer ) . supply ( weth . address , wethSupplyAmount , user1 . address , 0 )
569
+ ) ;
570
+ expect (
571
+ await pool . connect ( user2 . signer ) . supply ( usdc . address , usdcSupplyAmount , user2 . address , 0 )
572
+ ) ;
573
+
574
+ // Activate emode
575
+ expect ( await pool . connect ( user1 . signer ) . setUserEMode ( CATEGORY . id ) ) ;
576
+
577
+ // Borrow a as much usdc as possible
578
+ const userData = await pool . getUserAccountData ( user1 . address ) ;
579
+ const toBorrow = userData . availableBorrowsBase . div ( 100 ) ;
580
+
581
+ expect (
582
+ await pool
583
+ . connect ( user1 . signer )
584
+ . borrow ( usdc . address , toBorrow , RateMode . Variable , 0 , user1 . address )
585
+ ) ;
586
+
587
+ // Drop weth price
588
+ const oraclePrice = await oracle . getAssetPrice ( EMODE_ORACLE_ADDRESS ) ;
589
+
590
+ const userGlobalDataBefore = await pool . getUserAccountData ( user1 . address ) ;
591
+ expect ( userGlobalDataBefore . healthFactor ) . to . be . gt ( utils . parseUnits ( '1' , 18 ) ) ;
592
+
593
+ await oracle . setAssetPrice ( EMODE_ORACLE_ADDRESS , oraclePrice . mul ( 2 ) ) ;
594
+
595
+ const userGlobalDataAfter = await pool . getUserAccountData ( user1 . address ) ;
596
+ expect ( userGlobalDataAfter . healthFactor ) . to . be . lt ( utils . parseUnits ( '1' , 18 ) , INVALID_HF ) ;
597
+
598
+ const balanceBefore = await aWETH . balanceOf ( user1 . address ) ;
599
+
600
+ // Liquidate
601
+ await pool
602
+ . connect ( user2 . signer )
603
+ . liquidationCall ( weth . address , usdc . address , user1 . address , toBorrow . div ( 2 ) , false ) ;
604
+
605
+ const balanceAfter = await aWETH . balanceOf ( user1 . address ) ;
606
+
607
+ const debtPrice = await oracle . getAssetPrice ( EMODE_ORACLE_ADDRESS ) ;
608
+ const collateralPrice = await oracle . getAssetPrice ( weth . address ) ;
609
+
610
+ const wethConfig = await helpersContract . getReserveConfigurationData ( weth . address ) ;
611
+
612
+ const expectedCollateralLiquidated = debtPrice
613
+ . mul ( toBorrow . div ( 2 ) )
614
+ . percentMul ( wethConfig . liquidationBonus )
615
+ . mul ( BigNumber . from ( 10 ) . pow ( 18 ) )
616
+ . div ( collateralPrice . mul ( BigNumber . from ( 10 ) . pow ( 6 ) ) ) ;
617
+
618
+ const collateralLiquidated = balanceBefore . sub ( balanceAfter ) ;
619
+ expect ( collateralLiquidated ) . to . be . closeTo ( expectedCollateralLiquidated , 2 ) ;
620
+ } ) ;
492
621
} ) ;
0 commit comments