Skip to content

Commit

Permalink
fix: 馃悰 reduce_amount (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
yooml committed Dec 28, 2023
1 parent b3815f6 commit fcf0acb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pallets/leverage-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ impl<T: Config> Pallet<T> {

match rate.cmp(&current_rate) {
Ordering::Less => {
let reduce_amount = current_rate
.checked_sub(&rate)
.and_then(|r| r.checked_mul_int(base_token_value))
.ok_or(ArithmeticError::Overflow)?;
let reduce_amount = if rate.is_zero() {
account_borrows
} else {
current_rate
.checked_sub(&rate)
.and_then(|r| r.checked_mul_int(base_token_value))
.ok_or(ArithmeticError::Overflow)?
};
Self::reduce_leverage(&who, asset_id, vtoken_id, reduce_amount)?;
},
Ordering::Equal => return Err(Error::<T>::ArgumentsError.into()),
Expand Down
17 changes: 17 additions & 0 deletions pallets/leverage-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ fn reduce_leverage_should_work() {
Deposits { voucher_balance: 9500000, is_collateral: true },
);
assert_eq!(Tokens::balance(VDOT, &1), 9999999900000);
assert_eq!(Tokens::balance(DOT, &1), 990000000000000);
assert_ok!(LeverageStaking::flash_loan_deposit(
RuntimeOrigin::signed(1),
DOT,
FixedU128::from_inner(unit(800_000)),
));
assert_eq!(Tokens::balance(VDOT, &1), 9999999900000);
assert_eq!(Tokens::balance(DOT, &1), 990000000000098);
assert_eq!(
AccountBorrows::<Test>::get(DOT, 1),
BorrowSnapshot { principal: 80_000, borrow_index: 1.into() },
Expand All @@ -155,5 +157,20 @@ fn reduce_leverage_should_work() {
AccountDeposits::<Test>::get(VDOT, 1),
Deposits { voucher_balance: 8994050, is_collateral: true },
);
assert_ok!(LeverageStaking::flash_loan_deposit(
RuntimeOrigin::signed(1),
DOT,
FixedU128::from_inner(0),
));
assert_eq!(Tokens::balance(VDOT, &1), 9999999900000);
assert_eq!(Tokens::balance(DOT, &1), 990000000000196);
assert_eq!(
AccountBorrows::<Test>::get(DOT, 1),
BorrowSnapshot { principal: 0, borrow_index: 1.into() },
);
assert_eq!(
AccountDeposits::<Test>::get(VDOT, 1),
Deposits { voucher_balance: 4981100, is_collateral: true },
);
});
}

0 comments on commit fcf0acb

Please sign in to comment.