Skip to content

Commit

Permalink
test: update tests for vault collateral change
Browse files Browse the repository at this point in the history
  • Loading branch information
sander2 committed Nov 18, 2022
1 parent 705ad75 commit 47750de
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 389 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/issue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fee = { path = "../fee", default-features = false }
security = { path = "../security", default-features = false }
currency = { path = "../currency", default-features = false }
vault-registry = { path = "../vault-registry", default-features = false }
nomination = { path = "../nomination", default-features = false }

primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false }

Expand Down Expand Up @@ -74,6 +75,7 @@ std = [
"security/std",
"currency/std",
"vault-registry/std",
"nomination/std",

"primitives/std",

Expand Down
7 changes: 7 additions & 0 deletions crates/issue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ frame_support::construct_runtime!(
Fee: fee::{Pallet, Call, Config<T>, Storage},
Staking: staking::{Pallet, Storage, Event<T>},
Currency: currency::{Pallet},
Nomination: nomination::{Pallet, Call, Config, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -154,6 +155,12 @@ impl vault_registry::Config for Test {
type Balance = Balance;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type NominationApi = Nomination;
}

impl nomination::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub struct CurrencyConvert;
Expand Down
24 changes: 14 additions & 10 deletions crates/nomination/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ impl<T: Config> Pallet<T> {
Error::<T>::CannotWithdrawCollateral
);

ensure!(Self::is_nomination_enabled(), Error::<T>::VaultNominationDisabled);
ensure!(Self::is_opted_in(vault_id)?, Error::<T>::VaultNotOptedInToNomination);

if &vault_id.account_id != nominator_id {
ensure!(Self::is_nomination_enabled(), Error::<T>::VaultNominationDisabled);
ensure!(Self::is_opted_in(vault_id)?, Error::<T>::VaultNotOptedInToNomination);
}
ext::vault_registry::decrease_total_backing_collateral(&vault_id.currencies, &amount)?;
}

Expand All @@ -271,18 +272,21 @@ impl<T: Config> Pallet<T> {
nominator_id: &T::AccountId,
amount: BalanceOf<T>,
) -> DispatchResult {
ensure!(Self::is_nomination_enabled(), Error::<T>::VaultNominationDisabled);
ensure!(Self::is_opted_in(vault_id)?, Error::<T>::VaultNotOptedInToNomination);

let amount = Amount::new(amount, vault_id.collateral_currency());

let total_nominated_collateral = Self::get_total_nominated_collateral(vault_id)?;
let new_nominated_collateral = total_nominated_collateral.checked_add(&amount)?;
let max_nominatable_collateral = Self::get_nomination_limit(vault_id);
ensure!(
new_nominated_collateral.le(&max_nominatable_collateral)?,
Error::<T>::NominationExceedsLimit
);

if &vault_id.account_id != nominator_id {
ensure!(Self::is_nomination_enabled(), Error::<T>::VaultNominationDisabled);
ensure!(Self::is_opted_in(vault_id)?, Error::<T>::VaultNotOptedInToNomination);

ensure!(
new_nominated_collateral.le(&max_nominatable_collateral)?,
Error::<T>::NominationExceedsLimit
);
}

// Withdraw all vault rewards first, to prevent the nominator from withdrawing past rewards
ext::fee::withdraw_all_vault_rewards::<T>(vault_id)?;
Expand Down
1 change: 1 addition & 0 deletions crates/nomination/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl vault_registry::Config for Test {
type Balance = Balance;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type NominationApi = Nomination;
}

pub struct CurrencyConvert;
Expand Down
2 changes: 2 additions & 0 deletions crates/redeem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch =
reward = { path = "../reward" }
staking = { path = "../staking" }
currency = { path = "../currency", features = ["testing-utils"] }
nomination = { path = "../nomination" }

# Orml dependencies
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "f4ef99fb108232c4c1fe9ed54d08be3681b77669" }
Expand Down Expand Up @@ -73,6 +74,7 @@ std = [
"fee/std",
"security/std",
"vault-registry/std",
"nomination/std",
"primitives/std",

"orml-tokens/std",
Expand Down
7 changes: 7 additions & 0 deletions crates/redeem/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ frame_support::construct_runtime!(
Fee: fee::{Pallet, Call, Config<T>, Storage},
Staking: staking::{Pallet, Storage, Event<T>},
Currency: currency::{Pallet},
Nomination: nomination::{Pallet, Call, Config, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -151,6 +152,12 @@ impl vault_registry::Config for Test {
type Balance = Balance;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type NominationApi = Nomination;
}

impl nomination::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub struct CurrencyConvert;
Expand Down
11 changes: 6 additions & 5 deletions crates/replace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ impl vault_registry::Config for Test {
type Balance = Balance;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type NominationApi = Nomination;
}

impl nomination::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

impl staking::Config for Test {
Expand All @@ -205,11 +211,6 @@ impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

impl nomination::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

parameter_types! {
pub const MinimumPeriod: Moment = 5;
}
Expand Down
1 change: 1 addition & 0 deletions crates/vault-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mocktopus = "0.7.0"
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
currency = { path = "../currency", default-features = false, features = ["testing-utils"] }
pretty_assertions = "0.7.2"
visibility = { version = "0.0.1" }

[features]
default = ["std"]
Expand Down
20 changes: 0 additions & 20 deletions crates/vault-registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,6 @@ benchmarks! {
VaultRegistry::<T>::register_public_key(origin.clone().into(), public_key).unwrap();
}: _(origin, vault_id.currencies.clone(), amount.into())

deposit_collateral {
let vault_id = get_vault_id::<T>();
mint_collateral::<T>(&vault_id.account_id, (1u32 << 31).into());
let amount = 100u32.into();
register_vault_with_collateral::<T>(vault_id.clone(), 100000000);
Oracle::<T>::_set_exchange_rate(get_collateral_currency_id::<T>(),
UnsignedFixedPoint::<T>::one()
).unwrap();
}: _(RawOrigin::Signed(vault_id.account_id), vault_id.currencies.clone(), amount)

withdraw_collateral {
let vault_id = get_vault_id::<T>();
mint_collateral::<T>(&vault_id.account_id, (1u32 << 31).into());
let amount = 100u32.into();
register_vault_with_collateral::<T>(vault_id.clone(), 100000000);
Oracle::<T>::_set_exchange_rate(get_collateral_currency_id::<T>(),
UnsignedFixedPoint::<T>::one()
).unwrap();
}: _(RawOrigin::Signed(vault_id.account_id), vault_id.currencies.clone(), amount)

register_public_key {
let vault_id = get_vault_id::<T>();
mint_collateral::<T>(&vault_id.account_id, (1u32 << 31).into());
Expand Down
32 changes: 1 addition & 31 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,6 @@ impl<T: Config> Pallet<T> {

Self::try_deposit_collateral(&vault_id, &amount)?;

T::NominationApi::deposit_vault_collateral(&vault_id, &amount)?;

Self::deposit_event(Event::<T>::RegisterVault {
vault_id: vault_id.clone(),
collateral,
Expand Down Expand Up @@ -801,21 +799,7 @@ impl<T: Config> Pallet<T> {
/// * `vault_id` - the id of the vault
/// * `amount` - the amount of collateral
pub fn try_deposit_collateral(vault_id: &DefaultVaultId<T>, amount: &Amount<T>) -> DispatchResult {
// ensure the vault is active
let _vault = Self::get_active_rich_vault_from_id(vault_id)?;

// will fail if collateral ceiling exceeded
Self::try_increase_total_backing_collateral(&vault_id.currencies, amount)?;
// will fail if free_balance is insufficient
amount.lock_on(&vault_id.account_id)?;

// withdraw first such that past rewards don't get changed by this deposit
ext::fee::withdraw_all_vault_rewards::<T>(vault_id)?;

// Deposit `amount` of stake in the pool
ext::staking::deposit_stake::<T>(vault_id, &vault_id.account_id, amount)?;

Ok(())
T::NominationApi::deposit_vault_collateral(&vault_id, &amount)
}

/// Withdraw an `amount` of collateral without checking collateralization
Expand All @@ -837,20 +821,6 @@ impl<T: Config> Pallet<T> {
Ok(())
}

/// Withdraw an `amount` of collateral, ensuring that the vault is sufficiently
/// over-collateralized
///
/// # Arguments
/// * `vault_id` - the id of the vault
/// * `amount` - the amount of collateral
pub fn try_withdraw_collateral(vault_id: &DefaultVaultId<T>, amount: &Amount<T>) -> DispatchResult {
ensure!(
Self::is_allowed_to_withdraw_collateral(vault_id, amount)?,
Error::<T>::InsufficientCollateral
);
Self::force_withdraw_collateral(vault_id, amount)
}

/// Checks if the vault would be above the secure threshold after withdrawing collateral
pub fn is_allowed_to_withdraw_collateral(
vault_id: &DefaultVaultId<T>,
Expand Down
28 changes: 27 additions & 1 deletion crates/vault-registry/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate as vault_registry;
use crate::{Config, Error};
use crate::{ext, Config, Error};
use frame_support::{
parameter_types,
traits::{ConstU32, Everything, GenesisBuild},
Expand All @@ -14,6 +14,7 @@ use sp_core::H256;
use sp_runtime::{
testing::{Header, TestXt},
traits::{BlakeTwo256, IdentityLookup, One, Zero},
DispatchError,
};
use traits::OracleApi;

Expand Down Expand Up @@ -204,13 +205,38 @@ impl fee::Config for Test {
parameter_types! {
pub const VaultPalletId: PalletId = PalletId(*b"mod/vreg");
}
pub struct MockDeposit;

impl traits::NominationApi<VaultId<AccountId, CurrencyId>, currency::Amount<Test>> for MockDeposit {
fn deposit_vault_collateral(
vault_id: &VaultId<AccountId, CurrencyId>,
amount: &currency::Amount<Test>,
) -> Result<(), DispatchError> {
// ensure the vault is active
let _vault = VaultRegistry::get_active_rich_vault_from_id(vault_id)?;

// will fail if collateral ceiling exceeded
VaultRegistry::try_increase_total_backing_collateral(&vault_id.currencies, amount)?;
// will fail if free_balance is insufficient
amount.lock_on(&vault_id.account_id)?;

// withdraw first such that past rewards don't get changed by this deposit
ext::fee::withdraw_all_vault_rewards::<Test>(vault_id)?;

// Deposit `amount` of stake in the pool
ext::staking::deposit_stake::<Test>(vault_id, &vault_id.account_id, amount)?;

Ok(())
}
}

impl Config for Test {
type PalletId = VaultPalletId;
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type NominationApi = MockDeposit;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Test
Expand Down
Loading

0 comments on commit 47750de

Please sign in to comment.