Skip to content

Commit

Permalink
Fix redeem issue (#1842)
Browse files Browse the repository at this point in the history
* Fix redeem issue

* Add sepracated unit test for reserved balance

* fix: typo

Signed-off-by: Cheng JIANG <[email protected]>

* remove InsufficientMarketLiquidity

Signed-off-by: Cheng JIANG <[email protected]>

Signed-off-by: Cheng JIANG <[email protected]>
Co-authored-by: Cheng JIANG <[email protected]>
  • Loading branch information
alannotnerd and 0x8f701 authored Aug 16, 2022
1 parent fa0e302 commit 1a2b493
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ init: submodules
.PHONY: submodules
submodules:
git submodule update --init --recursive
git submodule foreach git pull origin master
git submodule update --remote --merge

.PHONY: build
build:
Expand Down
18 changes: 9 additions & 9 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ use frame_system::pallet_prelude::*;
use num_traits::cast::ToPrimitive;
pub use pallet::*;
use pallet_traits::{
ConvertToBigUint, Loans, LoansMarketDataProvider, LoansPositionDataProvider, MarketInfo,
MarketStatus, PriceFeeder,
ConvertToBigUint, Loans as LoansTrait, LoansMarketDataProvider, LoansPositionDataProvider,
MarketInfo, MarketStatus, PriceFeeder,
};
use primitives::{
is_auxiliary_token, Balance, CurrencyId, Liquidity, Price, Rate, Ratio, Shortfall, Timestamp,
Expand Down Expand Up @@ -196,8 +196,6 @@ pub mod pallet {
InvalidAmount,
/// Payer cannot be signer
PayerIsSigner,
/// Insufficient Market Liquidity
InsufficientMarketLiquidity,
/// Codec error
CodecError,
/// Collateral is reserved and cannot be liquidated
Expand Down Expand Up @@ -1367,13 +1365,15 @@ impl<T: Config> Pallet<T> {
if deposit.voucher_balance < voucher_amount {
return Err(Error::<T>::InsufficientDeposit.into());
}
if !deposit.is_collateral {
return Ok(());
}

let exchange_rate = Self::exchange_rate_stored(asset_id)?;
let redeem_amount = Self::calc_underlying_amount(voucher_amount, exchange_rate)?;
Self::ensure_enough_cash(asset_id, redeem_amount)?;

if !deposit.is_collateral {
return Ok(());
}

let market = Self::market(asset_id)?;
let effects_amount = market.collateral_factor.mul_ceil(redeem_amount);
let redeem_effects_value = Self::get_asset_value(asset_id, effects_amount)?;
Expand Down Expand Up @@ -1429,7 +1429,7 @@ impl<T: Config> Pallet<T> {
})?;

T::Assets::transfer(asset_id, &Self::account_id(), who, redeem_amount, false)
.map_err(|_| Error::<T>::InsufficientMarketLiquidity)?;
.map_err(|_| Error::<T>::InsufficientCash)?;
Ok(redeem_amount)
}

Expand Down Expand Up @@ -1996,7 +1996,7 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> Loans<AssetIdOf<T>, AccountIdOf<T>, BalanceOf<T>> for Pallet<T> {
impl<T: Config> LoansTrait<AssetIdOf<T>, AccountIdOf<T>, BalanceOf<T>> for Pallet<T> {
fn do_mint(
supplier: &AccountIdOf<T>,
asset_id: AssetIdOf<T>,
Expand Down
30 changes: 24 additions & 6 deletions pallets/loans/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod liquidate_borrow;
mod market;
mod ptokens;

use super::*;
use frame_support::{assert_err, assert_noop, assert_ok};

use primitives::tokens::CDOT_6_13;
Expand All @@ -27,9 +26,7 @@ use sp_runtime::{
FixedU128, Permill,
};

use mock::*;

use crate::mock::Loans;
use crate::mock::*;

#[test]
fn init_minting_ok() {
Expand Down Expand Up @@ -274,7 +271,27 @@ fn redeem_fails() {
}

#[test]
fn withdraw_fails_when_insufficient_liquidity() {
fn redeem_fails_when_insufficient_liquidity() {
new_test_ext().execute_with(|| {
// Prepare: Bob Deposit 200 DOT
assert_ok!(Loans::mint(Origin::signed(BOB), DOT, 200));

// Deposit 200 KSM as collateral
assert_ok!(Loans::mint(Origin::signed(ALICE), KSM, 200));

assert_ok!(Loans::collateral_asset(Origin::signed(ALICE), KSM, true));
// Borrow 50 DOT will reduce 100 KSM liquidity for collateral_factor is 50%
assert_ok!(Loans::borrow(Origin::signed(ALICE), DOT, 50));

assert_noop!(
Loans::redeem(Origin::signed(BOB), DOT, 151),
Error::<Test>::InsufficientCash
);
})
}

#[test]
fn redeem_fails_when_would_use_reserved_balanace() {
new_test_ext().execute_with(|| {
// Prepare: Bob Deposit 200 DOT
assert_ok!(Loans::mint(Origin::signed(BOB), DOT, 200));
Expand All @@ -285,10 +302,11 @@ fn withdraw_fails_when_insufficient_liquidity() {
assert_ok!(Loans::collateral_asset(Origin::signed(ALICE), KSM, true));
// Borrow 50 DOT will reduce 100 KSM liquidity for collateral_factor is 50%
assert_ok!(Loans::borrow(Origin::signed(ALICE), DOT, 50));
assert_ok!(Loans::add_reserves(Origin::root(), ALICE, DOT, 50));

assert_noop!(
Loans::redeem(Origin::signed(BOB), DOT, 151),
Error::<Test>::InsufficientMarketLiquidity
Error::<Test>::InsufficientCash
);
})
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub mod pallet {
// cant error because we fetch pools above
let adjacents = graph.get(&start).unwrap();

// items that are adjecent but not already in path
// items that are adjacent but not already in path
let difference: Vec<_> = adjacents
.iter()
.filter(|item| !path.contains(item))
Expand Down

0 comments on commit 1a2b493

Please sign in to comment.