@@ -1195,13 +1195,12 @@ pub mod pallet {
11951195 Ok ( ( ) . into ( ) )
11961196 }
11971197
1198- /// Add reserves by transferring from payer.
1199- /// TODO: This extrinsic currently does nothing useful. See the TODO comment
1200- /// of the `ensure_enough_cash` function for more details. Based on that
1201- /// TODO, decide whether this extrinsic should be kept .
1198+ /// Add reserves by transferring from payer. This is useful because the reserve balance can be used as
1199+ /// cash for redeeming. In case the market reaches 100% utilization, the protocol can deposit
1200+ /// more cash to the reserve, to allow users to exit (redeem) their lend token positions.
1201+ /// The reserve cash cannot be used to borrowing .
12021202 ///
12031203 /// May only be called from `T::ReserveOrigin`.
1204- ///
12051204 /// - `payer`: the payer account.
12061205 /// - `asset_id`: the assets to be added.
12071206 /// - `add_amount`: the amount to be added.
@@ -1454,7 +1453,9 @@ impl<T: Config> Pallet<T> {
14541453 // Ensure there is enough cash in the market
14551454 let exchange_rate = Self :: exchange_rate_stored ( asset_id) ?;
14561455 let redeem_amount = Self :: calc_underlying_amount ( voucher_amount, exchange_rate) ?;
1457- Self :: ensure_enough_cash ( asset_id, redeem_amount) ?;
1456+ if Self :: get_total_cash ( asset_id) < redeem_amount {
1457+ return Err ( Error :: < T > :: InsufficientCash . into ( ) ) ;
1458+ }
14581459
14591460 // TODO: Only free lend tokens are redeemable. Replace logic below with this:
14601461 // if voucher_amount > Self::free_lend_tokens(asset_id, redeemer)?.amount() {
@@ -1511,7 +1512,7 @@ impl<T: Config> Pallet<T> {
15111512 /// Borrower shouldn't borrow more than their total collateral value allows
15121513 fn borrow_allowed ( asset_id : AssetIdOf < T > , borrower : & T :: AccountId , borrow_amount : BalanceOf < T > ) -> DispatchResult {
15131514 Self :: ensure_under_borrow_cap ( asset_id, borrow_amount) ?;
1514- Self :: ensure_enough_cash ( asset_id, borrow_amount) ?;
1515+ Self :: ensure_borrow_cash ( asset_id, borrow_amount) ?;
15151516 let borrow_value = Self :: get_asset_value ( asset_id, borrow_amount) ?;
15161517 Self :: ensure_liquidity ( borrower, borrow_value) ?;
15171518
@@ -1833,7 +1834,7 @@ impl<T: Config> Pallet<T> {
18331834 /// https://github.com/compound-finance/compound-protocol/blob/a3214f67b73310d547e00fc578e8355911c9d376/contracts/CToken.sol#L518
18341835 /// - but getCashPrior is the entire balance of the contract:
18351836 /// https://github.com/compound-finance/compound-protocol/blob/a3214f67b73310d547e00fc578e8355911c9d376/contracts/CToken.sol#L1125
1836- fn ensure_enough_cash ( asset_id : AssetIdOf < T > , amount : BalanceOf < T > ) -> DispatchResult {
1837+ fn ensure_borrow_cash ( asset_id : AssetIdOf < T > , amount : BalanceOf < T > ) -> DispatchResult {
18371838 let reducible_cash = Self :: get_total_cash ( asset_id)
18381839 . checked_sub ( Self :: total_reserves ( asset_id) )
18391840 . ok_or ( ArithmeticError :: Underflow ) ?;
0 commit comments