Skip to content

Commit

Permalink
Added benchmarking for repay_borrow and minor refactoring #34
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmishra2005 committed May 11, 2021
1 parent c4b0833 commit a6fa75a
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,50 @@ benchmarks! {
redeem {
let caller: T::AccountId = whitelisted_caller();
initial_set_up::<T>(caller.clone());
<AccountCollateral<T>>::insert(DOT, caller.clone(), INITIAL_AMOUNT);
<TotalSupply<T>>::insert(DOT, INITIAL_AMOUNT);
assert_ok!(Loans::<T>::mint(SystemOrigin::Signed(caller.clone()).into(), DOT, 100_000_000));
let amount = 100_000;
let initial_balance = <T as Config>::Currency::free_balance(DOT, &Loans::<T>::account_id());
}: _(SystemOrigin::Signed(caller.clone()), DOT, amount)
verify {
assert_eq!(
<T as Config>::Currency::free_balance(DOT, &Loans::<T>::account_id()),
INITIAL_AMOUNT - amount,
initial_balance - amount,
);
}

redeem_all {
redeem_all {
let caller: T::AccountId = whitelisted_caller();
initial_set_up::<T>(caller.clone());
<AccountCollateral<T>>::insert(DOT, caller.clone(), INITIAL_AMOUNT);
<TotalSupply<T>>::insert(DOT, INITIAL_AMOUNT);
let exchange_rate = Loans::<T>::exchange_rate(DOT);
let redeem_amount = exchange_rate
.checked_mul_int(INITIAL_AMOUNT)
.ok_or(Error::<T>::CollateralOverflow)?;
assert_ok!(Loans::<T>::mint(SystemOrigin::Signed(caller.clone()).into(), DOT, 100_000_000));
let collateral = Loans::<T>::account_collateral(DOT, caller.clone());
let exchange_rate = Loans::<T>::exchange_rate(DOT);
let redeem_amount = exchange_rate
.checked_mul_int(collateral)
.ok_or(Error::<T>::CollateralOverflow)?;
let initial_balance = <T as Config>::Currency::free_balance(DOT, &Loans::<T>::account_id());
}: _(SystemOrigin::Signed(caller.clone()), DOT)
verify {
assert_eq!(
<T as Config>::Currency::free_balance(DOT, &Loans::<T>::account_id()),
INITIAL_AMOUNT - redeem_amount,
initial_balance - redeem_amount,
);
}

repay_borrow {
let caller: T::AccountId = whitelisted_caller();
initial_set_up::<T>(caller.clone());
let amount = 200_000_000;
let borrowed_amount = 100_000_000;
let repay_amount = 100;
assert_ok!(Loans::<T>::mint(SystemOrigin::Signed(caller.clone()).into(), DOT, INITIAL_AMOUNT));
assert_ok!(Loans::<T>::collateral_asset(SystemOrigin::Signed(caller.clone()).into(), DOT, true));
assert_ok!(Loans::<T>::borrow(SystemOrigin::Signed(caller.clone()).into(), DOT, borrowed_amount));
let total_borrows = Loans::<T>::total_borrows(DOT);
}: _(SystemOrigin::Signed(caller.clone()), DOT, repay_amount)
verify {
assert_eq!(
Loans::<T>::total_borrows(DOT),
total_borrows - repay_amount,
);
}
}
Expand Down

0 comments on commit a6fa75a

Please sign in to comment.