Skip to content

Commit e979deb

Browse files
committed
feat: atomically deposit vault collateral in lending market
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
1 parent a986f9d commit e979deb

16 files changed

Lines changed: 751 additions & 101 deletions

File tree

crates/issue/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ impl vault_registry::Config for Test {
170170
type RuntimeEvent = RuntimeEvent;
171171
type WeightInfo = ();
172172
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
173+
type LoansApi = ();
173174
}
174175

175176
impl nomination::Config for Test {

crates/loans/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use frame_support::{
4343
use frame_system::pallet_prelude::*;
4444
use num_traits::cast::ToPrimitive;
4545
use orml_traits::{MultiCurrency, MultiReservableCurrency};
46-
pub use pallet::*;
4746
use primitives::{Balance, Rate, Ratio, Timestamp};
4847
use sp_runtime::{
4948
traits::{
@@ -53,13 +52,12 @@ use sp_runtime::{
5352
ArithmeticError, FixedPointNumber, FixedU128,
5453
};
5554
use sp_std::{marker, result::Result};
56-
57-
use traits::{
58-
ConvertToBigUint, LoansApi as LoansTrait, LoansMarketDataProvider, MarketInfo, MarketStatus, OnExchangeRateChange,
59-
};
55+
use traits::{ConvertToBigUint, LoansMarketDataProvider, MarketInfo, MarketStatus, OnExchangeRateChange};
6056

6157
pub use default_weights::WeightInfo;
6258
pub use orml_traits::currency::{OnDeposit, OnSlash, OnTransfer};
59+
pub use pallet::*;
60+
pub use traits::LoansApi;
6361
pub use types::{BorrowSnapshot, EarnedSnapshot, Market, MarketState, RewardMarketState};
6462

6563
#[cfg(feature = "runtime-benchmarks")]
@@ -1875,8 +1873,8 @@ impl<T: Config> Pallet<T> {
18751873
}
18761874
}
18771875

1878-
impl<T: Config> LoansTrait<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<T> {
1879-
fn do_mint(supplier: &AccountIdOf<T>, amount: &Amount<T>) -> Result<(), DispatchError> {
1876+
impl<T: Config> LoansApi<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<T> {
1877+
fn do_mint(supplier: &AccountIdOf<T>, amount: &Amount<T>) -> Result<Amount<T>, DispatchError> {
18801878
let asset_id = amount.currency();
18811879
Self::ensure_active_market(asset_id)?;
18821880
Self::ensure_under_supply_cap(&amount)?;
@@ -1899,7 +1897,7 @@ impl<T: Config> LoansTrait<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<
18991897
currency_id: asset_id,
19001898
amount: amount.amount(),
19011899
});
1902-
Ok(())
1900+
Ok(voucher)
19031901
}
19041902

19051903
fn do_borrow(borrower: &AccountIdOf<T>, borrow: &Amount<T>) -> Result<(), DispatchError> {

crates/nomination/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl vault_registry::Config for Test {
176176
type RuntimeEvent = RuntimeEvent;
177177
type WeightInfo = ();
178178
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
179+
type LoansApi = Loans;
179180
}
180181

181182
pub struct CurrencyConvert;

crates/redeem/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ impl vault_registry::Config for Test {
149149
type RuntimeEvent = RuntimeEvent;
150150
type WeightInfo = ();
151151
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
152+
type LoansApi = Loans;
152153
}
153154

154155
impl nomination::Config for Test {

crates/replace/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ impl vault_registry::Config for Test {
196196
type RuntimeEvent = RuntimeEvent;
197197
type WeightInfo = ();
198198
type GetGriefingCollateralCurrencyId = GetNativeCurrencyId;
199+
type LoansApi = ();
199200
}
200201

201202
impl nomination::Config for Test {

crates/traits/src/loans.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use sp_runtime::{FixedU128, RuntimeDebug};
2323
use sp_std::prelude::*;
2424

2525
pub trait LoansApi<CurrencyId, AccountId, Amount> {
26-
fn do_mint(supplier: &AccountId, amount: &Amount) -> Result<(), DispatchError>;
26+
fn do_mint(supplier: &AccountId, amount: &Amount) -> Result<Amount, DispatchError>;
2727
fn do_borrow(borrower: &AccountId, borrow: &Amount) -> Result<(), DispatchError>;
2828
fn do_deposit_collateral(supplier: &AccountId, lend_tokens: &Amount) -> Result<(), DispatchError>;
2929
fn do_withdraw_collateral(supplier: &AccountId, voucher: &Amount) -> Result<(), DispatchError>;
@@ -34,6 +34,36 @@ pub trait LoansApi<CurrencyId, AccountId, Amount> {
3434
fn recompute_collateral_amount(underlying: &Amount) -> Result<Amount, DispatchError>;
3535
}
3636

37+
impl<CurrencyId, AccountId, Amount> LoansApi<CurrencyId, AccountId, Amount> for () {
38+
fn do_mint(_: &AccountId, _: &Amount) -> Result<Amount, DispatchError> {
39+
Err(DispatchError::Unavailable)
40+
}
41+
fn do_borrow(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
42+
Err(DispatchError::Unavailable)
43+
}
44+
fn do_deposit_collateral(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
45+
Err(DispatchError::Unavailable)
46+
}
47+
fn do_withdraw_collateral(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
48+
Err(DispatchError::Unavailable)
49+
}
50+
fn do_repay_borrow(_: &AccountId, _: &Amount) -> Result<(), DispatchError> {
51+
Err(DispatchError::Unavailable)
52+
}
53+
fn do_redeem(_: &AccountId, _: &Amount, _: &Amount) -> Result<(), DispatchError> {
54+
Err(DispatchError::Unavailable)
55+
}
56+
fn recompute_underlying_amount(_: &Amount) -> Result<Amount, DispatchError> {
57+
Err(DispatchError::Unavailable)
58+
}
59+
fn underlying_id(_: CurrencyId) -> Result<CurrencyId, DispatchError> {
60+
Err(DispatchError::Unavailable)
61+
}
62+
fn recompute_collateral_amount(_: &Amount) -> Result<Amount, DispatchError> {
63+
Err(DispatchError::Unavailable)
64+
}
65+
}
66+
3767
pub trait LoansMarketDataProvider<CurrencyId, Balance> {
3868
fn get_market_info(asset_id: CurrencyId) -> Result<MarketInfo, DispatchError>;
3969
fn get_market_status(asset_id: CurrencyId) -> Result<MarketStatus<Balance>, DispatchError>;

crates/vault-registry/src/benchmarking.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn activate_lending_and_get_vault_id<T: crate::Config + loans::Config>() ->
125125
let account_id: T::AccountId = account("Vault", 0, 0);
126126
let lend_token = CurrencyId::LendToken(1);
127127
activate_lending_and_mint::<T>(get_collateral_currency_id::<T>(), lend_token.clone(), &account_id);
128-
let vault_id = VaultId::new(account("Vault", 0, 0), lend_token, get_wrapped_currency_id::<T>());
128+
let vault_id = VaultId::new(account_id, lend_token, get_wrapped_currency_id::<T>());
129129
set_collateral_config::<T>(&vault_id);
130130
vault_id
131131
}
@@ -273,6 +273,33 @@ pub mod benchmarks {
273273
recover_vault_id(RawOrigin::Signed(vault_id.account_id), vault_id.currencies.clone());
274274
}
275275

276+
#[benchmark]
277+
fn deposit_vault_collateral_in_lending_market() {
278+
let old_vault_id = VaultId::new(
279+
account("Vault", 0, 0),
280+
get_collateral_currency_id::<T>(),
281+
get_wrapped_currency_id::<T>(),
282+
);
283+
set_collateral_config::<T>(&old_vault_id);
284+
285+
let new_vault_id = VaultId::new(
286+
account("Vault", 0, 0),
287+
CurrencyId::LendToken(1),
288+
get_wrapped_currency_id::<T>(),
289+
);
290+
set_collateral_config::<T>(&new_vault_id);
291+
292+
register_vault_with_collateral::<T>(old_vault_id.clone());
293+
294+
activate_market::<T>(old_vault_id.collateral_currency(), new_vault_id.collateral_currency());
295+
296+
#[extrinsic_call]
297+
_(
298+
RawOrigin::Signed(old_vault_id.account_id),
299+
old_vault_id.currencies.clone(),
300+
);
301+
}
302+
276303
impl_benchmark_test_suite! {
277304
VaultRegistry,
278305
crate::mock::ExtBuilder::build_with(Default::default()),

0 commit comments

Comments
 (0)