Skip to content

Commit

Permalink
Ep/be 152 error refactoring for protocol (#30)
Browse files Browse the repository at this point in the history
* [BE-152] Change error throwing method && type  from PoolNotFound -> NotValidUnderlyingAssetId. Correct tests.

* [BE-152] Remove PoolNotFresh checking from fn do_repay
  • Loading branch information
Apollo50 committed Jan 20, 2021
1 parent a0c830a commit 48ea51a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
28 changes: 10 additions & 18 deletions pallets/minterest-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ decl_error! {
/// Number overflow in calculation.
NumOverflow,

/// The block number in the pool is equal to the current block number.
PoolNotFresh,

/// An internal failure occurred in the execution of the Accrue Interest function.
AccrueInterestFailed,

Expand All @@ -96,10 +93,6 @@ decl_error! {
/// Repay was blocked due to Controller rejection.
RepayBorrowControllerRejection,

/// Pool not found.
PoolNotFound,


/// Transaction with zero balance is not allowed.
ZeroBalanceTransaction,

Expand Down Expand Up @@ -270,7 +263,10 @@ decl_module! {
#[weight = 10_000]
pub fn enable_as_collateral(origin, pool_id: CurrencyId) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(<LiquidityPools<T>>::pool_exists(&pool_id), Error::<T>::PoolNotFound);
ensure!(
T::UnderlyingAssetId::get().contains(&pool_id),
Error::<T>::NotValidUnderlyingAssetId
);

ensure!(!<LiquidityPools<T>>::check_user_available_collateral(&sender, pool_id), Error::<T>::AlreadyCollateral);

Expand All @@ -288,7 +284,10 @@ decl_module! {
#[weight = 10_000]
pub fn disable_collateral(origin, pool_id: CurrencyId) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(<LiquidityPools<T>>::pool_exists(&pool_id), Error::<T>::PoolNotFound);
ensure!(
T::UnderlyingAssetId::get().contains(&pool_id),
Error::<T>::NotValidUnderlyingAssetId
);

ensure!(<LiquidityPools<T>>::check_user_available_collateral(&sender, pool_id), Error::<T>::AlreadyDisabledCollateral);

Expand Down Expand Up @@ -363,7 +362,8 @@ impl<T: Trait> Module<T> {

<Controller<T>>::accrue_interest_rate(underlying_asset_id).map_err(|_| Error::<T>::AccrueInterestFailed)?;

let wrapped_id = <Controller<T>>::get_wrapped_id_by_underlying_asset_id(&underlying_asset_id)?;
let wrapped_id = <Controller<T>>::get_wrapped_id_by_underlying_asset_id(&underlying_asset_id)
.map_err(|_| Error::<T>::NotValidUnderlyingAssetId)?;

let wrapped_amount = match (underlying_amount, wrapped_amount, all_assets) {
(0, 0, true) => {
Expand Down Expand Up @@ -485,14 +485,6 @@ impl<T: Trait> Module<T> {
<Controller<T>>::repay_borrow_allowed(underlying_asset_id, &who, repay_amount)
.map_err(|_| Error::<T>::RepayBorrowControllerRejection)?;

// Verify pool's block number equals current block number
let current_block_number = <frame_system::Module<T>>::block_number();
let accrual_block_number_previous = <Controller<T>>::controller_dates(underlying_asset_id).timestamp;
ensure!(
current_block_number == accrual_block_number_previous,
Error::<T>::PoolNotFresh
);

// Fetch the amount the borrower owes, with accumulated interest
let account_borrows = <Controller<T>>::borrow_balance_stored(&borrower, underlying_asset_id)
.map_err(|_| Error::<T>::NumOverflow)?;
Expand Down
4 changes: 2 additions & 2 deletions pallets/minterest-protocol/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ fn enable_as_collateral_should_work() {

assert_noop!(
TestProtocol::enable_as_collateral(alice(), CurrencyId::MDOT),
Error::<Test>::PoolNotFound
Error::<Test>::NotValidUnderlyingAssetId
);
});
}
Expand Down Expand Up @@ -552,7 +552,7 @@ fn disable_collateral_should_work() {

assert_noop!(
TestProtocol::disable_collateral(alice(), CurrencyId::MDOT),
Error::<Test>::PoolNotFound
Error::<Test>::NotValidUnderlyingAssetId
);
});
}

0 comments on commit 48ea51a

Please sign in to comment.