2626)
2727from .core import AaveV3CoreLib
2828from .. import DemeterError , TokenInfo
29- from .._typing import DECIMAL_0 , UnitDecimal , ChainType
29+ from .._typing import DECIMAL_0 , UnitDecimal , ChainType , USD
3030from ..broker import Market , MarketInfo , write_func
3131from ..utils import get_formatted_predefined , STYLE , get_formatted_from_dict , console_text
3232from ..utils .application import require , float_param_formatter , to_decimal
@@ -58,9 +58,7 @@ def __init__(
5858 self ._supplies : Dict [TokenInfo , SupplyInfo ] = {}
5959 self ._borrows : Dict [TokenInfo , BorrowInfo ] = {}
6060
61- self ._risk_parameters : pd .DataFrame | Dict [str , RiskParameter ] = helper .load_risk_parameter (
62- risk_parameters_path
63- )
61+ self ._risk_parameters : pd .DataFrame | Dict [str , RiskParameter ] = helper .load_risk_parameter (risk_parameters_path )
6462
6563 # caches, since amounts and values are calculated by base_amount, and they are commonly used, cache is introduced to speed up back test.
6664 # when base amount or price has changed, they must be reset.
@@ -131,8 +129,7 @@ def supplies_value(self) -> Dict[TokenInfo, Decimal]:
131129 for k , v in self ._supplies .items ():
132130 self ._supplies_amount_cache .set (
133131 k ,
134- AaveV3CoreLib .get_amount (v .base_amount , self ._market_status .data [k .name ].liquidity_index )
135- * self ._price_status [k .name ],
132+ AaveV3CoreLib .get_amount (v .base_amount , self ._market_status .data [k .name ].liquidity_index ) * self ._price_status [k .name ],
136133 )
137134 return self ._supplies_amount_cache .value
138135
@@ -176,8 +173,7 @@ def borrows_value(self) -> Dict[TokenInfo, Decimal]:
176173 for k , v in self ._borrows .items ():
177174 self ._borrows_amount_cache .set (
178175 k ,
179- AaveV3CoreLib .get_amount (v .base_amount , self ._market_status .data [k .name ].variable_borrow_index )
180- * self ._price_status [k .name ],
176+ AaveV3CoreLib .get_amount (v .base_amount , self ._market_status .data [k .name ].variable_borrow_index ) * self ._price_status [k .name ],
181177 )
182178 return self ._borrows_amount_cache .value
183179
@@ -321,9 +317,7 @@ def total_apy(self) -> Decimal:
321317 total_borrows = self .total_borrows_value
322318 supply_apy = self .supply_apy
323319 borrow_apy = self .borrow_apy
324- return AaveV3CoreLib .safe_div_zero (
325- supply_apy * total_supplies - borrow_apy * total_borrows , total_supplies - total_borrows
326- )
320+ return AaveV3CoreLib .safe_div_zero (supply_apy * total_supplies - borrow_apy * total_borrows , total_supplies - total_borrows )
327321
328322 def get_supply (self , token_info : TokenInfo ) -> Supply :
329323 """
@@ -393,9 +387,7 @@ def get_market_balance(self) -> AaveBalance:
393387
394388 supply_apy = self .supply_apy .quantize (rounding )
395389 borrow_apy = self .borrow_apy .quantize (rounding )
396- net_apy = AaveV3CoreLib .safe_div_zero (
397- supply_apy * total_supplies - borrow_apy * total_borrows , total_supplies - total_borrows
398- )
390+ net_apy = AaveV3CoreLib .safe_div_zero (supply_apy * total_supplies - borrow_apy * total_borrows , total_supplies - total_borrows )
399391
400392 return AaveBalance (
401393 net_value = net_worth ,
@@ -420,6 +412,7 @@ def check_market(self):
420412 """
421413 super ().check_market ()
422414 require (len (self .tokens ) > 0 , "should set tokens" )
415+ require (self .quote_token == USD , "Quote token of AAVE market must be USD" )
423416 for t in self .tokens :
424417 require (
425418 t .name in self .risk_parameters .index ,
@@ -487,19 +480,15 @@ def supply(self, token_info: TokenInfo, amount: Decimal | float, collateral: boo
487480 :rtype: SupplyKey
488481 """
489482 if collateral :
490- require (
491- self ._risk_parameters .loc [token_info .name ].usageAsCollateralEnabled , "Can not supplied as collateral"
492- )
483+ require (self ._risk_parameters .loc [token_info .name ].usageAsCollateralEnabled , "Can not supplied as collateral" )
493484 token_status = self ._market_status .data [token_info .name ]
494485 # calc in pool value
495486 pool_amount = AaveV3CoreLib .get_base_amount (amount , token_status .liquidity_index )
496487
497488 self .broker .subtract_from_balance (token_info , amount )
498489
499490 if token_info not in self ._supplies :
500- self ._supplies [token_info ] = SupplyInfo (
501- base_amount = Decimal (0 ), collateral = collateral , begin_supply_index = token_status .liquidity_index
502- )
491+ self ._supplies [token_info ] = SupplyInfo (base_amount = Decimal (0 ), collateral = collateral , begin_supply_index = token_status .liquidity_index )
503492 else :
504493 require (self ._supplies [token_info ].collateral == collateral , "Collateral different from existing supply" )
505494 self ._supplies [token_info ].base_amount += pool_amount
@@ -571,9 +560,7 @@ def withdraw(
571560 # try calc new health factor after withdraw. if health factor is low, raise an error
572561 if self ._supplies [token_info ].collateral :
573562 old_base_amount = self ._supplies [token_info ].base_amount
574- self ._supplies [token_info ].base_amount -= AaveV3CoreLib .get_base_amount (
575- amount , self ._market_status .data [token_info .name ].liquidity_index
576- )
563+ self ._supplies [token_info ].base_amount -= AaveV3CoreLib .get_base_amount (amount , self ._market_status .data [token_info .name ].liquidity_index )
577564 self ._supplies_amount_cache .reset ()
578565 self ._collaterals_amount_cache .reset ()
579566 if self .health_factor < AaveV3CoreLib .HEALTH_FACTOR_LIQUIDATION_THRESHOLD :
@@ -587,9 +574,7 @@ def withdraw(
587574 market = self .market_info ,
588575 token = token_info .name ,
589576 amount = UnitDecimal (amount , token_info .name ),
590- deposit_after = UnitDecimal (
591- AaveV3CoreLib .get_amount (final_base_amount , token_status .liquidity_index ), token_info .name
592- ),
577+ deposit_after = UnitDecimal (AaveV3CoreLib .get_amount (final_base_amount , token_status .liquidity_index ), token_info .name ),
593578 )
594579 )
595580
@@ -697,9 +682,7 @@ def get_max_repay_amount(self, token_info: TokenInfo) -> Decimal:
697682 :return: max amount to repay
698683 :rtype: Decimal
699684 """
700- return AaveV3CoreLib .get_amount (
701- self ._borrows [token_info ].base_amount , self .market_status .data [token_info .name ].variable_borrow_index
702- )
685+ return AaveV3CoreLib .get_amount (self ._borrows [token_info ].base_amount , self .market_status .data [token_info .name ].variable_borrow_index )
703686
704687 def _get_swap_amount (self , from_token : TokenInfo , to_token : TokenInfo , amount : Decimal , swap_fee = 0 ):
705688 return amount * (1 - swap_fee ) * self ._price_status .loc [from_token .name ] / self ._price_status .loc [to_token .name ]
@@ -738,13 +721,9 @@ def repay(
738721 repay_collateral_token = borrow_token
739722 repay_collateral_key = repay_collateral_token
740723 require (repay_collateral_key in self .supplies .keys (), f"token { repay_collateral_token } is not in supply" )
741- require (
742- self .supplies [repay_collateral_key ].collateral , f"token { repay_collateral_token } is not in collateral"
743- )
724+ require (self .supplies [repay_collateral_key ].collateral , f"token { repay_collateral_token } is not in collateral" )
744725
745- required_amount_in_collateral_token = self ._get_swap_amount (
746- borrow_token , repay_collateral_token , payback_amount
747- )
726+ required_amount_in_collateral_token = self ._get_swap_amount (borrow_token , repay_collateral_token , payback_amount )
748727 supply = self .get_supply (repay_collateral_key )
749728 if required_amount_in_collateral_token > supply .amount :
750729 # contract will change payback amount instead of raise an error
@@ -766,9 +745,7 @@ def repay(
766745 market = self .market_info ,
767746 token = borrow_token .name ,
768747 amount = UnitDecimal (payback_amount , borrow_token .name ),
769- debt_after = UnitDecimal (
770- AaveV3CoreLib .get_amount (debt , token_status .variable_borrow_index ), borrow_token .name
771- ),
748+ debt_after = UnitDecimal (AaveV3CoreLib .get_amount (debt , token_status .variable_borrow_index ), borrow_token .name ),
772749 )
773750 )
774751
@@ -889,37 +866,27 @@ def _do_liquidate(self, collateral_token: TokenInfo, delt_token: TokenInfo, delt
889866 else AaveV3CoreLib .MAX_LIQUIDATION_CLOSE_FACTOR
890867 )
891868 max_liquidateble_debt = total_debt * close_factor
892- actual_debt_to_liquidate = (
893- max_liquidateble_debt if delt_value_to_cover > max_liquidateble_debt else delt_value_to_cover
894- )
869+ actual_debt_to_liquidate = max_liquidateble_debt if delt_value_to_cover > max_liquidateble_debt else delt_value_to_cover
895870
896871 # validate delt
897872 is_collateral_enabled = (
898- self ._risk_parameters .loc [collateral_token .name ].reserveLiquidationThreshold != 0
899- and self ._supplies [collateral_key ].collateral
873+ self ._risk_parameters .loc [collateral_token .name ].reserveLiquidationThreshold != 0 and self ._supplies [collateral_key ].collateral
900874 )
901875
902876 require (is_collateral_enabled , "collateral cannot be liquidated" )
903877 require (total_debt != DECIMAL_0 , "specified currency not borrowed by user" )
904878
905- user_collateral_balance = (
906- self ._supplies [collateral_token ].base_amount
907- * self ._market_status .data [collateral_token .name ].liquidity_index
908- )
879+ user_collateral_balance = self ._supplies [collateral_token ].base_amount * self ._market_status .data [collateral_token .name ].liquidity_index
909880
910881 # calculate actual amount
911- should_collateral = (
912- self ._price_status .loc [delt_token .name ]
913- * actual_debt_to_liquidate
914- / self ._price_status .loc [collateral_token .name ]
915- )
882+ should_collateral = self ._price_status .loc [delt_token .name ] * actual_debt_to_liquidate / self ._price_status .loc [collateral_token .name ]
916883 max_collateral_to_liquidate = should_collateral * (1 + liquidation_bonus )
917884
918885 if max_collateral_to_liquidate > user_collateral_balance :
919886 actual_collateral_to_liquidate = user_collateral_balance
920- actual_debt_to_liquidate = (
921- self ._price_status .loc [collateral_token .name ] * actual_collateral_to_liquidate
922- ) / ( self . _price_status . loc [ delt_token . name ] * ( 1 + liquidation_bonus ))
887+ actual_debt_to_liquidate = (self . _price_status . loc [ collateral_token . name ] * actual_collateral_to_liquidate ) / (
888+ self ._price_status .loc [delt_token .name ] * ( 1 + liquidation_bonus )
889+ )
923890 else :
924891 actual_collateral_to_liquidate = max_collateral_to_liquidate
925892 actual_debt_to_liquidate = actual_debt_to_liquidate
@@ -964,9 +931,7 @@ def _do_liquidate(self, collateral_token: TokenInfo, delt_token: TokenInfo, delt
964931 ),
965932 collateral_token .name ,
966933 ),
967- variable_debt_after = UnitDecimal (
968- AaveV3CoreLib .get_amount (vari_debt_remaining_base , borrow_index ), delt_token .name
969- ),
934+ variable_debt_after = UnitDecimal (AaveV3CoreLib .get_amount (vari_debt_remaining_base , borrow_index ), delt_token .name ),
970935 )
971936 )
972937
0 commit comments