Skip to content

Commit

Permalink
Merge pull request #900 from daniel-savu/test-loan-batch
Browse files Browse the repository at this point in the history
test(loans): batched loans accrue interest
  • Loading branch information
gregdhill authored Feb 1, 2023
2 parents 765699c + 8fe06b2 commit 2b17157
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/loans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "pol
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false, optional = true }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
pallet-utility = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false, optional = true }

# Parachain dependencies
currency = { path = "../currency", default-features = false }
Expand Down Expand Up @@ -61,6 +62,7 @@ std = [
"frame-system/std",
"frame-benchmarking/std",
"pallet-timestamp/std",
"pallet-utility/std",

"currency/std",
"traits/std",
Expand All @@ -75,6 +77,7 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
integration-tests = [
Expand Down
8 changes: 8 additions & 0 deletions crates/loans/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ construct_runtime!(
TimestampPallet: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Tokens: orml_tokens::{Pallet, Call, Storage, Config<T>, Event<T>},
Currency: currency::{Pallet},
Utility: pallet_utility,
}
);

Expand Down Expand Up @@ -177,6 +178,13 @@ impl currency::Config for Test {
type CurrencyConversion = Conversion;
}

impl pallet_utility::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type WeightInfo = ();
}

parameter_types! {
pub const MaxLocks: u32 = 50;
}
Expand Down
42 changes: 41 additions & 1 deletion crates/loans/src/tests/edge_cases.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{mock::*, tests::Loans, Error};
use currency::Amount;
use frame_support::{assert_err, assert_ok};
use frame_support::{assert_err, assert_ok, dispatch::Dispatchable};
use primitives::{
CurrencyId::{ForeignAsset, Token},
DOT, IBTC, KSM,
Expand Down Expand Up @@ -269,6 +269,46 @@ fn new_transferred_collateral_is_not_auto_deposited_if_not_collateral() {

#[test]
fn small_loans_have_interest_rounded_up() {
new_test_ext().execute_with(|| {
assert_ok!(Loans::mint(RuntimeOrigin::signed(ALICE), Token(IBTC), unit(100)));
assert_ok!(Loans::mint(RuntimeOrigin::signed(BOB), Token(DOT), unit(100)));
assert_ok!(Loans::deposit_all_collateral(RuntimeOrigin::signed(BOB), Token(DOT)));

let initial_block = 2;
_run_to_block(initial_block);

assert_eq!(Tokens::balance(Token(IBTC), &BOB), 0);
let borrow_amount_small = 1;
let borrow_amount_big = unit(10);
// If there was an bug in the lazy interest accrual, the second loan would be interest-free
let batch_call = RuntimeCall::Utility(pallet_utility::Call::batch {
calls: vec![
RuntimeCall::Loans(self::Call::borrow {
asset_id: Token(IBTC),
borrow_amount: borrow_amount_small,
}),
RuntimeCall::Loans(self::Call::borrow {
asset_id: Token(IBTC),
borrow_amount: borrow_amount_big,
}),
],
});
assert_ok!(batch_call.clone().dispatch(RuntimeOrigin::signed(BOB)));

_run_to_block(initial_block + 10000);
Loans::accrue_interest(Token(IBTC)).unwrap();
let borrow_index = Loans::borrow_index(Token(IBTC));
let current_borrow_balance = Loans::current_borrow_balance(&BOB, Token(IBTC)).unwrap();
let total_borrowed_amount = borrow_amount_small + borrow_amount_big;
let expected_borrow_balance = borrow_index.checked_mul_int(total_borrowed_amount).unwrap();
assert_eq!(
almost_equal(current_borrow_balance.amount(), expected_borrow_balance),
true,
);
})
}

fn big_loan_following_a_small_loan_still_accrues_interest() {
new_test_ext().execute_with(|| {
assert_ok!(Loans::mint(RuntimeOrigin::signed(ALICE), Token(IBTC), unit(100)));
assert_ok!(Loans::mint(RuntimeOrigin::signed(BOB), Token(DOT), unit(100)));
Expand Down

0 comments on commit 2b17157

Please sign in to comment.