From 1a2b4937698219d0b8d2c4091e4805e5565b386d Mon Sep 17 00:00:00 2001 From: alannotnerd Date: Tue, 16 Aug 2022 08:51:39 +0800 Subject: [PATCH] Fix redeem issue (#1842) * Fix redeem issue * Add sepracated unit test for reserved balance * fix: typo Signed-off-by: Cheng JIANG * remove InsufficientMarketLiquidity Signed-off-by: Cheng JIANG Signed-off-by: Cheng JIANG Co-authored-by: Cheng JIANG --- Makefile | 2 +- pallets/loans/src/lib.rs | 18 +++++++++--------- pallets/loans/src/tests.rs | 30 ++++++++++++++++++++++++------ pallets/router/src/lib.rs | 2 +- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 93e82dee8..0c819ac94 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/pallets/loans/src/lib.rs b/pallets/loans/src/lib.rs index 388fdb180..5b16d73b3 100644 --- a/pallets/loans/src/lib.rs +++ b/pallets/loans/src/lib.rs @@ -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, @@ -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 @@ -1367,13 +1365,15 @@ impl Pallet { if deposit.voucher_balance < voucher_amount { return Err(Error::::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)?; @@ -1429,7 +1429,7 @@ impl Pallet { })?; T::Assets::transfer(asset_id, &Self::account_id(), who, redeem_amount, false) - .map_err(|_| Error::::InsufficientMarketLiquidity)?; + .map_err(|_| Error::::InsufficientCash)?; Ok(redeem_amount) } @@ -1996,7 +1996,7 @@ impl Pallet { } } -impl Loans, AccountIdOf, BalanceOf> for Pallet { +impl LoansTrait, AccountIdOf, BalanceOf> for Pallet { fn do_mint( supplier: &AccountIdOf, asset_id: AssetIdOf, diff --git a/pallets/loans/src/tests.rs b/pallets/loans/src/tests.rs index 9dcbde3e8..b9a8d7c37 100644 --- a/pallets/loans/src/tests.rs +++ b/pallets/loans/src/tests.rs @@ -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; @@ -27,9 +26,7 @@ use sp_runtime::{ FixedU128, Permill, }; -use mock::*; - -use crate::mock::Loans; +use crate::mock::*; #[test] fn init_minting_ok() { @@ -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::::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)); @@ -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::::InsufficientMarketLiquidity + Error::::InsufficientCash ); }) } diff --git a/pallets/router/src/lib.rs b/pallets/router/src/lib.rs index 71d39f638..cf33431fd 100644 --- a/pallets/router/src/lib.rs +++ b/pallets/router/src/lib.rs @@ -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))