Skip to content

Commit

Permalink
fix: 🐛 increase_leverage (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
yooml authored Dec 28, 2023
1 parent 1eab81e commit b3815f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 52 deletions.
63 changes: 14 additions & 49 deletions pallets/leverage-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,7 @@ impl<T: Config> Pallet<T> {
.checked_sub(&current_rate)
.and_then(|r| r.checked_mul_int(base_token_value))
.ok_or(ArithmeticError::Overflow)?;
Self::increase_leverage(
&who,
asset_id,
vtoken_id,
increase_amount,
deposits_token_value,
account_borrows,
)?;
Self::increase_leverage(&who, asset_id, vtoken_id, increase_amount)?;
},
}
Self::deposit_event(Event::<T>::FlashLoanDeposited {
Expand Down Expand Up @@ -221,48 +214,20 @@ impl<T: Config> Pallet<T> {
who: &T::AccountId,
asset_id: AssetIdOf<T>,
vtoken_id: AssetIdOf<T>,
mut increase_amount: BalanceOf<T>,
deposits_token_value: BalanceOf<T>,
borrows: BalanceOf<T>,
increase_amount: BalanceOf<T>,
) -> DispatchResult {
let market = lend_market::Pallet::<T>::market(asset_id)?;
let mut token_value = (market.collateral_factor * deposits_token_value)
.checked_sub(borrows)
.ok_or(ArithmeticError::Overflow)?;
while increase_amount > Zero::zero() {
match increase_amount < token_value {
true => {
T::LendMarket::do_borrow(&who, asset_id, increase_amount)?;
let vtoken_value = T::VtokenMinting::mint(
who.clone(),
asset_id,
increase_amount,
BoundedVec::default(),
)?;
T::LendMarket::do_mint(&who, vtoken_id, vtoken_value)?;
break;
},
false => {
T::LendMarket::do_borrow(&who, asset_id, token_value)?;
increase_amount = increase_amount.saturating_sub(token_value);
let vtoken_value = T::VtokenMinting::mint(
who.clone(),
asset_id,
token_value,
BoundedVec::default(),
)?;
T::LendMarket::do_mint(&who, vtoken_id, vtoken_value)?;
let underlying_amount = Self::current_collateral_amount(&who, vtoken_id)?;
let deposits_token_value =
T::VtokenMinting::vtoken_to_token(asset_id, vtoken_id, underlying_amount)?;
let account_borrows =
lend_market::Pallet::<T>::get_current_borrow_balance(&who, asset_id)?;
token_value = (market.collateral_factor * deposits_token_value)
.checked_sub(account_borrows)
.ok_or(ArithmeticError::Overflow)?;
},
};
}
<T as lend_market::Config>::Assets::mint_into(asset_id, &who, increase_amount)?;
let vtoken_value =
T::VtokenMinting::mint(who.clone(), asset_id, increase_amount, BoundedVec::default())?;
T::LendMarket::do_mint(&who, vtoken_id, vtoken_value)?;
T::LendMarket::do_borrow(&who, asset_id, increase_amount)?;
<T as lend_market::Config>::Assets::burn_from(
asset_id,
&who,
increase_amount,
Precision::Exact,
Fortitude::Force,
)?;
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions pallets/leverage-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn init() {
0,
vec![(DOT, (1, 1)), (VDOT, (100_000_000, 100_000_000))]
));
assert_ok!(VtokenMinting::set_minimum_mint(RuntimeOrigin::signed(1), DOT, 0));
assert_ok!(VtokenMinting::set_minimum_mint(RuntimeOrigin::signed(1), DOT, 1000));
assert_ok!(VtokenMinting::mint(Some(0).into(), DOT, unit(100_000), BoundedVec::default()));
assert_eq!(Tokens::balance(VDOT, &0), unit(100_000));
assert_ok!(VtokenMinting::mint(Some(1).into(), DOT, unit(10), BoundedVec::default()));
Expand All @@ -65,9 +65,9 @@ fn increase_leverage_should_not_work() {
LeverageStaking::flash_loan_deposit(
RuntimeOrigin::signed(1),
DOT,
FixedU128::from_inner(unit(1_000_000)),
FixedU128::from_inner(unit(1_000_100)),
),
lend_market::Error::<Test>::InvalidAmount
lend_market::Error::<Test>::InsufficientLiquidity
);
});
}
Expand Down

0 comments on commit b3815f6

Please sign in to comment.