Skip to content

Commit

Permalink
feat: atomically deposit vault collateral in lending market
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Jun 30, 2023
1 parent cfb2b6d commit 1a75365
Show file tree
Hide file tree
Showing 16 changed files with 819 additions and 192 deletions.
1 change: 1 addition & 0 deletions crates/issue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl vault_registry::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type LoansApi = ();
type NominationApi = Nomination;
}

Expand Down
14 changes: 6 additions & 8 deletions crates/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use frame_support::{
use frame_system::pallet_prelude::*;
use num_traits::cast::ToPrimitive;
use orml_traits::{MultiCurrency, MultiReservableCurrency};
pub use pallet::*;
use primitives::{Balance, Rate, Ratio, Timestamp};
use sp_runtime::{
traits::{
Expand All @@ -50,13 +49,12 @@ use sp_runtime::{
ArithmeticError, FixedPointNumber, FixedU128,
};
use sp_std::{marker, result::Result};

use traits::{
ConvertToBigUint, LoansApi as LoansTrait, LoansMarketDataProvider, MarketInfo, MarketStatus, OnExchangeRateChange,
};
use traits::{ConvertToBigUint, LoansMarketDataProvider, MarketInfo, MarketStatus, OnExchangeRateChange};

pub use default_weights::WeightInfo;
pub use orml_traits::currency::{OnDeposit, OnSlash, OnTransfer};
pub use pallet::*;
pub use traits::LoansApi;
pub use types::{BorrowSnapshot, EarnedSnapshot, Market, MarketState, RewardMarketState};

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -1864,8 +1862,8 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> LoansTrait<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<T> {
fn do_mint(supplier: &AccountIdOf<T>, amount: &Amount<T>) -> Result<(), DispatchError> {
impl<T: Config> LoansApi<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<T> {
fn do_mint(supplier: &AccountIdOf<T>, amount: &Amount<T>) -> Result<Amount<T>, DispatchError> {
let asset_id = amount.currency();
Self::ensure_active_market(asset_id)?;
Self::ensure_under_supply_cap(&amount)?;
Expand All @@ -1888,7 +1886,7 @@ impl<T: Config> LoansTrait<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<
currency_id: asset_id,
amount: amount.amount(),
});
Ok(())
Ok(voucher)
}

fn do_borrow(borrower: &AccountIdOf<T>, borrow: &Amount<T>) -> Result<(), DispatchError> {
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 @@ -176,6 +176,7 @@ impl vault_registry::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type LoansApi = Loans;
type NominationApi = Nomination;
}

Expand Down
1 change: 1 addition & 0 deletions crates/redeem/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl vault_registry::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type LoansApi = Loans;
type NominationApi = Nomination;
}

Expand Down
1 change: 1 addition & 0 deletions crates/replace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl vault_registry::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
type LoansApi = ();
type NominationApi = Nomination;
}

Expand Down
32 changes: 31 additions & 1 deletion crates/traits/src/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sp_runtime::{FixedU128, RuntimeDebug};
use sp_std::prelude::*;

pub trait LoansApi<CurrencyId, AccountId, Amount> {
fn do_mint(supplier: &AccountId, amount: &Amount) -> Result<(), DispatchError>;
fn do_mint(supplier: &AccountId, amount: &Amount) -> Result<Amount, DispatchError>;
fn do_borrow(borrower: &AccountId, borrow: &Amount) -> Result<(), DispatchError>;
fn do_deposit_collateral(supplier: &AccountId, lend_tokens: &Amount) -> Result<(), DispatchError>;
fn do_withdraw_collateral(supplier: &AccountId, voucher: &Amount) -> Result<(), DispatchError>;
Expand All @@ -34,6 +34,36 @@ pub trait LoansApi<CurrencyId, AccountId, Amount> {
fn recompute_collateral_amount(underlying: &Amount) -> Result<Amount, DispatchError>;
}

impl<CurrencyId, AccountId, Amount> LoansApi<CurrencyId, AccountId, Amount> for () {
fn do_mint(_: &AccountId, _: &Amount) -> Result<Amount, DispatchError> {
Err(DispatchError::Unavailable)
}
fn do_borrow(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
Err(DispatchError::Unavailable)
}
fn do_deposit_collateral(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
Err(DispatchError::Unavailable)
}
fn do_withdraw_collateral(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
Err(DispatchError::Unavailable)
}
fn do_repay_borrow(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
Err(DispatchError::Unavailable)
}
fn do_redeem(_: &AccountId, _: &Amount, _: &Amount) -> Result<(), DispatchError> {
Err(DispatchError::Unavailable)
}
fn recompute_underlying_amount(_: &Amount) -> Result<Amount, DispatchError> {
Err(DispatchError::Unavailable)
}
fn underlying_id(_: CurrencyId) -> Result<CurrencyId, DispatchError> {
Err(DispatchError::Unavailable)
}
fn recompute_collateral_amount(_: &Amount) -> Result<Amount, DispatchError> {
Err(DispatchError::Unavailable)
}
}

pub trait LoansMarketDataProvider<CurrencyId, Balance> {
fn get_market_info(asset_id: CurrencyId) -> Result<MarketInfo, DispatchError>;
fn get_market_status(asset_id: CurrencyId) -> Result<MarketStatus<Balance>, DispatchError>;
Expand Down
29 changes: 28 additions & 1 deletion crates/vault-registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn activate_lending_and_get_vault_id<T: crate::Config + loans::Config>() ->
let account_id: T::AccountId = account("Vault", 0, 0);
let lend_token = CurrencyId::LendToken(1);
activate_lending_and_mint::<T>(get_collateral_currency_id::<T>(), lend_token.clone(), &account_id);
let vault_id = VaultId::new(account("Vault", 0, 0), lend_token, get_wrapped_currency_id::<T>());
let vault_id = VaultId::new(account_id, lend_token, get_wrapped_currency_id::<T>());
set_collateral_config::<T>(&vault_id);
vault_id
}
Expand Down Expand Up @@ -273,6 +273,33 @@ pub mod benchmarks {
recover_vault_id(RawOrigin::Signed(vault_id.account_id), vault_id.currencies.clone());
}

#[benchmark]
fn deposit_vault_collateral_in_lending_market() {
let old_vault_id = VaultId::new(
account("Vault", 0, 0),
get_collateral_currency_id::<T>(),
get_wrapped_currency_id::<T>(),
);
set_collateral_config::<T>(&old_vault_id);

let new_vault_id = VaultId::new(
account("Vault", 0, 0),
CurrencyId::LendToken(1),
get_wrapped_currency_id::<T>(),
);
set_collateral_config::<T>(&new_vault_id);

register_vault_with_collateral::<T>(old_vault_id.clone());

activate_market::<T>(old_vault_id.collateral_currency(), new_vault_id.collateral_currency());

#[extrinsic_call]
_(
RawOrigin::Signed(old_vault_id.account_id),
old_vault_id.currencies.clone(),
);
}

impl_benchmark_test_suite! {
VaultRegistry,
crate::mock::ExtBuilder::build_with(Default::default()),
Expand Down
Loading

0 comments on commit 1a75365

Please sign in to comment.