Skip to content

Commit

Permalink
precompile for liquid-crowdloan
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Jul 12, 2023
1 parent b997a7e commit c66ebf3
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 31 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.

66 changes: 38 additions & 28 deletions modules/liquid-crowdloan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub mod module {
}

#[pallet::event]
#[pallet::generate_deposit(fn deposit_event)]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Liquid Crowdloan asset was redeemed.
Redeemed { currency_id: CurrencyId, amount: Balance },
Expand All @@ -100,33 +100,7 @@ pub mod module {
pub fn redeem(origin: OriginFor<T>, #[pallet::compact] amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

let (currency_id, redeem_amount) = if let Some(redeem_currency_id) = RedeemCurrencyId::<T>::get() {
// redeem the RedeemCurrencyId
// amount_pect = amount / lcdot_total_supply
// amount_redeem = amount_pect * redeem_currency_balance

let redeem_currency_balance = T::Currency::free_balance(redeem_currency_id, &Self::account_id());
let lcdot_total_supply = T::Currency::total_issuance(T::LiquidCrowdloanCurrencyId::get());

let amount_redeem = amount
.checked_mul(redeem_currency_balance)
.and_then(|x| x.checked_div(lcdot_total_supply))
.ok_or(ArithmeticError::Overflow)?;

(redeem_currency_id, amount_redeem)
} else {
// redeem DOT
let currency_id = T::RelayChainCurrencyId::get();
(currency_id, amount)
};

T::Currency::withdraw(T::LiquidCrowdloanCurrencyId::get(), &who, amount)?;
T::Currency::transfer(currency_id, &Self::account_id(), &who, redeem_amount)?;

Self::deposit_event(Event::Redeemed {
currency_id,
amount: redeem_amount,
});
Self::do_redeem(&who, amount)?;

Ok(())
}
Expand Down Expand Up @@ -175,4 +149,40 @@ impl<T: Config> Pallet<T> {
pub fn account_id() -> T::AccountId {
T::PalletId::get().into_account_truncating()
}

pub fn do_redeem(who: &T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
let (currency_id, redeem_amount) = if let Some(redeem_currency_id) = RedeemCurrencyId::<T>::get() {
// redeem the RedeemCurrencyId
// amount_pect = amount / lcdot_total_supply
// amount_redeem = amount_pect * redeem_currency_balance

let redeem_currency_balance = T::Currency::free_balance(redeem_currency_id, &Self::account_id());
let lcdot_total_supply = T::Currency::total_issuance(T::LiquidCrowdloanCurrencyId::get());

let amount_redeem = amount
.checked_mul(redeem_currency_balance)
.and_then(|x| x.checked_div(lcdot_total_supply))
.ok_or(ArithmeticError::Overflow)?;

(redeem_currency_id, amount_redeem)
} else {
// redeem DOT
let currency_id = T::RelayChainCurrencyId::get();
(currency_id, amount)
};

T::Currency::withdraw(T::LiquidCrowdloanCurrencyId::get(), &who, amount)?;
T::Currency::transfer(currency_id, &Self::account_id(), &who, redeem_amount)?;

Self::deposit_event(Event::Redeemed {
currency_id,
amount: redeem_amount,
});

Ok(redeem_amount)
}

pub fn redeem_currency() -> CurrencyId {
RedeemCurrencyId::<T>::get().unwrap_or_else(|| T::RelayChainCurrencyId::get())
}
}
2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module-cdp-engine = { path = "../../modules/cdp-engine", default-features = fals
module-cdp-treasury = { path = "../../modules/cdp-treasury", default-features = false, optional = true }
module-incentives = { path = "../../modules/incentives", default-features = false }
module-transaction-pause = { path = "../../modules/transaction-pause", default-features = false }
module-liquid-crowdloan = { path = "../../modules/liquid-crowdloan", default-features = false }

# orml
orml-oracle = { path = "../../orml/oracle", default-features = false }
Expand Down Expand Up @@ -142,6 +143,7 @@ std = [
"module-support/std",
"module-transaction-pause/std",
"module-transaction-payment/std",
"module-liquid-crowdloan/std",
"primitives/std",

"nutsfinance-stable-asset/std",
Expand Down
Loading

0 comments on commit c66ebf3

Please sign in to comment.