Skip to content

Commit

Permalink
Merge pull request #625 from sander2/test/theft-tests
Browse files Browse the repository at this point in the history
test: some theft and escrow related tests
  • Loading branch information
gregdhill committed Jun 6, 2022
2 parents f82e56d + b6a3eea commit 9434750
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
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.

4 changes: 2 additions & 2 deletions crates/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub mod pallet {
/// Previous lock has expired.
LockHasExpired,
/// Lock amount is too large.
LockAmountTooLarge,
LockAmountTooLow,
/// Insufficient account balance.
InsufficientFunds,
/// Not supported.
Expand Down Expand Up @@ -490,7 +490,7 @@ impl<T: Config> Pallet<T> {
// total amount can't be less than the max period to prevent rounding errors
ensure!(
new_locked.amount >= T::BlockNumberToBalance::convert(T::MaxPeriod::get()),
Error::<T>::LockAmountTooLarge,
Error::<T>::LockAmountTooLow,
);

ensure!(
Expand Down
2 changes: 1 addition & 1 deletion crates/escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn should_not_allow_amount_smaller_than_max_period() {
<Balances as Currency<AccountId>>::make_free_balance_be(&ALICE, amount);
assert_err!(
Escrow::create_lock(Origin::signed(ALICE), amount, end_time),
TestError::LockAmountTooLarge
TestError::LockAmountTooLow
);
})
}
Expand Down
1 change: 1 addition & 0 deletions standalone/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mocktopus = "0.7.0"
serde_json = "1.0"
pretty_assertions = "0.7.2"
flate2= "1.0"
itertools = "0.10.0"

bitcoin = { path = "../../crates/bitcoin", default-features = false }

Expand Down
4 changes: 3 additions & 1 deletion standalone/runtime/tests/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub mod redeem_testing_utils;
pub mod replace_testing_utils;
pub mod reward_testing_utils;

pub use itertools::Itertools;
pub use pretty_assertions::assert_eq;

pub type VaultId = DefaultVaultId<Runtime>;
Expand Down Expand Up @@ -355,7 +356,7 @@ pub fn iter_currency_pairs() -> impl Iterator<Item = DefaultVaultCurrencyPair<Ru
}

pub fn iter_collateral_currencies() -> impl Iterator<Item = CurrencyId> {
vec![Token(DOT), Token(KSM), ForeignAsset(1)].into_iter()
vec![Token(DOT), Token(KSM), Token(INTR), ForeignAsset(1)].into_iter()
}

pub fn iter_native_currencies() -> impl Iterator<Item = CurrencyId> {
Expand Down Expand Up @@ -1355,6 +1356,7 @@ impl ExtBuilder {
.flat_map(|(account, balance)| {
iter_collateral_currencies()
.chain(iter_native_currencies())
.unique()
.map(move |currency| (account.clone(), currency, balance))
})
.chain(iter_wrapped_currencies().map(move |currency| (account_of(FAUCET), currency, 1 << 60)))
Expand Down
31 changes: 31 additions & 0 deletions standalone/runtime/tests/test_escrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ fn integration_test_individual_balance_and_total_supply() {
});
}

#[test]
fn integration_test_lock_reserved() {
// check that locking does not touch the reserved balance
ExtBuilder::build().execute_with(|| {
let span = <Runtime as escrow::Config>::Span::get();
let current_height = SystemPallet::block_number();
let lock_amount = 1000_000_000_000_000;
let initial_free = 1500_000_000_000_000;
let initial_reserved = 500_000_000_000_000;

assert_ok!(Call::Tokens(TokensCall::set_balance {
who: account_of(ALICE),
currency_id: DEFAULT_NATIVE_CURRENCY,
new_free: initial_free,
new_reserved: initial_reserved,
})
.dispatch(root()));

assert_ok!(Call::Escrow(EscrowCall::create_lock {
amount: lock_amount,
unlock_height: current_height + span
})
.dispatch(origin_of(account_of(ALICE))));

let account = orml_tokens::Pallet::<Runtime>::accounts(account_of(ALICE), DEFAULT_NATIVE_CURRENCY);
assert_eq!(account.free, initial_free);
assert_eq!(account.reserved, initial_reserved);
assert_eq!(account.frozen, lock_amount);
});
}

fn ensure_reward_stake_is_escrow_balance(height: BlockNumber) {
assert_ok!(
<EscrowRewardsPallet as Rewards<AccountId, Balance, CurrencyId>>::get_stake(&account_of(ALICE)),
Expand Down
36 changes: 36 additions & 0 deletions standalone/runtime/tests/test_vault_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,42 @@ mod deposit_collateral_test {
});
}

#[test]
fn integration_test_vault_registry_lock_additional_using_locked_tokens_fails() {
ExtBuilder::build().execute_with(|| {
let vault_id = VaultId::new(account_of(VAULT), DEFAULT_NATIVE_CURRENCY, DEFAULT_WRAPPED_CURRENCY);

let currency_id = vault_id.collateral_currency();

let amount_1 = 1000_000_000_000_000;

let mut vault_data = default_vault_state(&vault_id);
*vault_data.free_balance.get_mut(&currency_id).unwrap() = Amount::new(amount_1, currency_id);
CoreVaultData::force_to(&vault_id, vault_data);

let q = currency::get_free_balance::<Runtime>(currency_id, &vault_id.account_id);
assert_eq!(q.amount(), amount_1);

let span = <Runtime as escrow::Config>::Span::get();
let current_height = SystemPallet::block_number();

assert_ok!(Call::Escrow(EscrowCall::create_lock {
amount: amount_1 / 2,
unlock_height: current_height + span
})
.dispatch(origin_of(vault_id.account_id.clone())));

assert_noop!(
Call::VaultRegistry(VaultRegistryCall::deposit_collateral {
currency_pair: vault_id.currencies.clone(),
amount: amount_1
})
.dispatch(origin_of(vault_id.account_id.clone())),
TokensError::LiquidityRestrictions
);
});
}

#[test]
fn integration_test_vault_registry_lock_additional_respects_fund_limit() {
test_with(|vault_id| {
Expand Down

0 comments on commit 9434750

Please sign in to comment.