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

precompile for liquid-crowdloan #2575

Merged
merged 8 commits into from
Jul 13, 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
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)
}
}
8 changes: 6 additions & 2 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::{Decode, DecodeLimit, Encode};
use runtime_common::precompile::AcalaPrecompiles;
use scale_info::TypeInfo;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160};
Expand Down Expand Up @@ -1407,7 +1408,9 @@ parameter_types! {
pub NetworkContractSource: H160 = H160::from_low_u64_be(0);
pub DeveloperDeposit: Balance = 50 * dollar(ACA);
pub PublicationFee: Balance = 10 * dollar(ACA);
pub PrecompilesValue: AllPrecompiles<Runtime, module_transaction_pause::PausedPrecompileFilter<Runtime>> = AllPrecompiles::<_, _>::acala();
pub PrecompilesValue: AllPrecompiles<
Runtime, module_transaction_pause::PausedPrecompileFilter<Runtime>, AcalaPrecompiles<Runtime>
> = AllPrecompiles::<_, _, _>::acala();
}

#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -1448,7 +1451,8 @@ impl module_evm::Config for Runtime {
type StorageDepositPerByte = StorageDepositPerByte;
type TxFeePerGas = TxFeePerGas;
type RuntimeEvent = RuntimeEvent;
type PrecompilesType = AllPrecompiles<Self, module_transaction_pause::PausedPrecompileFilter<Self>>;
type PrecompilesType =
AllPrecompiles<Self, module_transaction_pause::PausedPrecompileFilter<Self>, AcalaPrecompiles<Self>>;
type PrecompilesValue = PrecompilesValue;
type GasToWeight = GasToWeight;
type ChargeTransactionPayment = module_transaction_payment::ChargeTransactionPayment<Runtime>;
Expand Down
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