Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/redeem and lend for #1948

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pallets/amm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ fn handling_fees_should_work() {

// we can check the total balance
//
// no extra fees should be minted becuase liquidty has not been added or removed
// no extra fees should be minted because liquidty has not been added or removed
//
assert_eq!(Assets::total_issuance(SAMPLE_LP_TOKEN), 100_000_000_000);

Expand Down
136 changes: 88 additions & 48 deletions pallets/crowdloans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ pub mod pallet {
use xcm::latest::prelude::*;

use pallet_traits::{
DecimalProvider, Streaming, VaultTokenCurrenciesFilter, VaultTokenExchangeRateProvider,
DecimalProvider, Loans, Streaming, VaultTokenCurrenciesFilter,
VaultTokenExchangeRateProvider,
};

use parallel_support::math_helper::f64::{
Expand Down Expand Up @@ -202,6 +203,9 @@ pub mod pallet {

/// Decimal provider.
type Decimal: DecimalProvider<CurrencyId>;

/// Money market
type Loans: Loans<AssetIdOf<Self>, Self::AccountId, BalanceOf<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -872,53 +876,7 @@ pub mod pallet {
#[pallet::compact] amount: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let ctoken = Self::ctoken_of((&lease_start, &lease_end))
.ok_or(Error::<T>::CTokenDoesNotExist)?;
let mut vault = Self::vaults((&crowdloan, &lease_start, &lease_end))
.ok_or(Error::<T>::VaultDoesNotExist)?;

ensure!(
vault.phase == VaultPhase::Expired,
Error::<T>::IncorrectVaultPhase
);

log::trace!(
target: "crowdloans::redeem",
"who: {:?}, ctoken: {:?}, amount: {:?}, para_id: {:?}, lease_start: {:?}, lease_end: {:?}",
&who,
&ctoken,
&amount,
&crowdloan,
&lease_start,
&lease_end
);

let ctoken_balance = T::Assets::reducible_balance(ctoken, &who, false);
ensure!(ctoken_balance >= amount, Error::<T>::InsufficientBalance);

vault.contributed = vault
.contributed
.checked_sub(amount)
.ok_or(ArithmeticError::Underflow)?;

T::Assets::burn_from(ctoken, &who, amount)?;
// SovereignAccount on relaychain must have
// withdrawn the contribution
T::Assets::mint_into(T::RelayCurrency::get(), &who, amount)?;

Vaults::<T>::insert((&crowdloan, &lease_start, &lease_end), vault);

Self::deposit_event(Event::<T>::VaultRedeemed(
crowdloan,
(lease_start, lease_end),
ctoken,
who,
amount,
VaultPhase::Expired,
));

Ok(())
Self::do_redeem(who, crowdloan, lease_start, lease_end, amount)
}

/// If a `crowdloan` succeeded and its slot expired, use `call` to
Expand Down Expand Up @@ -1237,6 +1195,32 @@ pub mod pallet {
let who = T::Lookup::lookup(dest)?;
Self::do_claim_for(who, crowdloan, lease_start, lease_end, true)
}

/// If a `crowdloan` expired, redeem the contributed assets
/// and lend it to Money market
#[pallet::call_index(23)]
#[pallet::weight(<T as Config>::WeightInfo::redeem())]
#[transactional]
pub fn redeem_and_lend_for(
origin: OriginFor<T>,
dest: <T::Lookup as StaticLookup>::Source,
crowdloan: ParaId,
lease_start: LeasePeriod,
lease_end: LeasePeriod,
#[pallet::compact] amount: BalanceOf<T>,
) -> DispatchResult {
ensure_origin!(SlotExpiredOrigin, origin)?;
let who = T::Lookup::lookup(dest)?;

ensure!(
frame_system::Pallet::<T>::account_nonce(who.clone()) > Zero::zero(),
Error::<T>::InvalidParams,
);

Self::do_redeem(who.clone(), crowdloan, lease_start, lease_end, amount)?;
T::Loans::do_mint(&who, T::RelayCurrency::get(), amount)?;
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -1816,6 +1800,62 @@ pub mod pallet {
Ok(())
}

#[require_transactional]
fn do_redeem(
who: T::AccountId,
crowdloan: ParaId,
lease_start: LeasePeriod,
lease_end: LeasePeriod,
amount: BalanceOf<T>,
) -> DispatchResult {
let ctoken = Self::ctoken_of((&lease_start, &lease_end))
.ok_or(Error::<T>::CTokenDoesNotExist)?;
let mut vault = Self::vaults((&crowdloan, &lease_start, &lease_end))
.ok_or(Error::<T>::VaultDoesNotExist)?;

ensure!(
vault.phase == VaultPhase::Expired,
Error::<T>::IncorrectVaultPhase
);

log::trace!(
target: "crowdloans::redeem",
"who: {:?}, ctoken: {:?}, amount: {:?}, para_id: {:?}, lease_start: {:?}, lease_end: {:?}",
&who,
&ctoken,
&amount,
&crowdloan,
&lease_start,
&lease_end
);

let ctoken_balance = T::Assets::reducible_balance(ctoken, &who, false);
ensure!(ctoken_balance >= amount, Error::<T>::InsufficientBalance);

vault.contributed = vault
.contributed
.checked_sub(amount)
.ok_or(ArithmeticError::Underflow)?;

T::Assets::burn_from(ctoken, &who, amount)?;
// SovereignAccount on relaychain must have
// withdrawn the contribution
T::Assets::mint_into(T::RelayCurrency::get(), &who, amount)?;

Vaults::<T>::insert((&crowdloan, &lease_start, &lease_end), vault);

Self::deposit_event(Event::<T>::VaultRedeemed(
crowdloan,
(lease_start, lease_end),
ctoken,
who,
amount,
VaultPhase::Expired,
));

Ok(())
}

// just iterate now and require improve later when CTokensRegistry increased
fn find_vault_by_asset_id(asset_id: &AssetIdOf<T>) -> Option<(AssetIdOf<T>, AssetIdOf<T>)> {
for (vault, ctoken_id) in CTokensRegistry::<T>::iter() {
Expand Down
44 changes: 43 additions & 1 deletion pallets/crowdloans/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use kusama_runtime;
use pallet_traits::{
ump::{XcmCall, XcmWeightFeeMisc},
xcm::MultiCurrencyAdapter,
DecimalProvider,
DecimalProvider, Loans,
};

pub struct RelayChainBlockNumberProvider<T>(sp_std::marker::PhantomData<T>);
Expand Down Expand Up @@ -560,6 +560,48 @@ impl crate::Config for Test {
type Streaming = ();
type GetNativeCurrencyId = NativeCurrencyId;
type Decimal = Decimal;
type Loans = MockLoans;
}

pub struct MockLoans;

#[allow(unused)]
impl Loans<CurrencyId, AccountId, Balance> for MockLoans {
fn do_mint(
supplier: &AccountId,
asset_id: CurrencyId,
amount: Balance,
) -> Result<(), DispatchError> {
Ok(())
}
fn do_borrow(
borrower: &AccountId,
asset_id: CurrencyId,
amount: Balance,
) -> Result<(), DispatchError> {
Ok(())
}
fn do_collateral_asset(
supplier: &AccountId,
asset_id: CurrencyId,
enable: bool,
) -> Result<(), DispatchError> {
Ok(())
}
fn do_repay_borrow(
borrower: &AccountId,
asset_id: CurrencyId,
amount: Balance,
) -> Result<(), DispatchError> {
Ok(())
}
fn do_redeem(
supplier: &AccountId,
asset_id: CurrencyId,
amount: Balance,
) -> Result<(), DispatchError> {
Ok(())
}
}

pub struct Decimal;
Expand Down
1 change: 1 addition & 0 deletions runtime/heiko/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,7 @@ impl pallet_crowdloans::Config for Runtime {
type Streaming = ();
type GetNativeCurrencyId = NativeCurrencyId;
type Decimal = Decimal;
type Loans = Loans;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions runtime/kerria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,7 @@ impl pallet_crowdloans::Config for Runtime {
type Streaming = Streaming;
type GetNativeCurrencyId = NativeCurrencyId;
type Decimal = Decimal;
type Loans = Loans;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions runtime/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,7 @@ impl pallet_crowdloans::Config for Runtime {
type Streaming = Streaming;
type GetNativeCurrencyId = NativeCurrencyId;
type Decimal = Decimal;
type Loans = Loans;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions runtime/vanilla/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,7 @@ impl pallet_crowdloans::Config for Runtime {
type Streaming = ();
type GetNativeCurrencyId = NativeCurrencyId;
type Decimal = Decimal;
type Loans = Loans;
}

parameter_types! {
Expand Down