Skip to content

Commit

Permalink
Merge pull request #1049 from interlay/feat/interlay-defi
Browse files Browse the repository at this point in the history
feat: add defi pallets to interlay runtime
  • Loading branch information
sander2 authored May 22, 2023
2 parents 19df9c3 + bb1f0fc commit f0772a0
Show file tree
Hide file tree
Showing 23 changed files with 2,126 additions and 118 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use frame_support::{
log,
pallet_prelude::*,
require_transactional,
traits::{tokens::fungibles::Inspect, OnRuntimeUpgrade, UnixTime},
traits::{tokens::fungibles::Inspect, UnixTime},
transactional, PalletId,
};
use frame_system::pallet_prelude::*;
Expand Down
17 changes: 17 additions & 0 deletions parachain/runtime/interlay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ annuity = { path = "../../../crates/annuity", default-features = false }
supply = { path = "../../../crates/supply", default-features = false }
collator-selection = { path = "../../../crates/collator-selection", default-features = false }
clients-info = { path = "../../../crates/clients-info", default-features = false }
loans = { path = "../../../crates/loans", default-features = false }
traits = { path = "../../../crates/traits", default-features = false }
farming = { path = "../../../crates/farming", default-features = false }
tx-pause = { path = "../../../crates/tx-pause", default-features = false }
dex-general = { path = "../../../crates/dex-general", default-features = false }
dex-stable = { path = "../../../crates/dex-stable", default-features = false }
dex-swap-router = { path = "../../../crates/dex-swap-router", default-features = false }

primitives = { package = "interbtc-primitives", path = "../../../primitives", default-features = false }

Expand Down Expand Up @@ -208,9 +212,13 @@ std = [
"currency/std",
"democracy/std",
"dex-general/std",
"dex-stable/std",
"dex-swap-router/std",
"escrow/std",
"farming/std",
"fee/std",
"issue/std",
"loans/std",
"nomination/std",
"oracle/std",
"redeem/std",
Expand Down Expand Up @@ -273,9 +281,14 @@ runtime-benchmarks = [
"clients-info/runtime-benchmarks",
"collator-selection/runtime-benchmarks",
"democracy/runtime-benchmarks",
"dex-general/runtime-benchmarks",
"dex-stable/runtime-benchmarks",
"dex-swap-router/runtime-benchmarks",
"escrow/runtime-benchmarks",
"farming/runtime-benchmarks",
"fee/runtime-benchmarks",
"issue/runtime-benchmarks",
"loans/runtime-benchmarks",
"nomination/runtime-benchmarks",
"oracle/runtime-benchmarks",
"redeem/runtime-benchmarks",
Expand Down Expand Up @@ -328,8 +341,12 @@ try-runtime = [
"nomination/try-runtime",
"clients-info/try-runtime",
"tx-pause/try-runtime",
"loans/try-runtime",
"democracy/try-runtime",
"dex-general/try-runtime",
"dex-stable/try-runtime",
"dex-swap-router/try-runtime",
"farming/try-runtime",
"pallet-collective/try-runtime",
"pallet-membership/try-runtime",
"pallet-authorship/try-runtime",
Expand Down
127 changes: 127 additions & 0 deletions parachain/runtime/interlay/src/dex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use super::{
parameter_types, weights, Balance, CurrencyId, DexGeneral, DexStable, OnRuntimeUpgrade, PalletId, Rate, Runtime,
RuntimeEvent, StablePoolId, Timestamp, Tokens, Weight,
};
use sp_runtime::traits::Zero;

#[cfg(feature = "try-runtime")]
use frame_support::ensure;

pub use dex_general::{AssetBalance, GenerateLpAssetId, PairInfo, ValidateAsset};
pub use dex_stable::traits::{StablePoolLpCurrencyIdGenerate, ValidateCurrency};

parameter_types! {
pub const DexGeneralPalletId: PalletId = PalletId(*b"dex/genr");
pub const DexStablePalletId: PalletId = PalletId(*b"dex/stbl");
pub const CurrencyLimit: u32 = 10;
pub const StringLimit: u32 = 50;
pub const MaxBootstrapRewards: u32 = 1000;
pub const MaxBootstrapLimits:u32 = 1000;
}

pub struct PairLpIdentity;
impl GenerateLpAssetId<CurrencyId> for PairLpIdentity {
fn generate_lp_asset_id(asset_0: CurrencyId, asset_1: CurrencyId) -> Option<CurrencyId> {
CurrencyId::join_lp_token(asset_0, asset_1)
}
}

pub struct DexGeneralVerifyPairAsset;
impl ValidateAsset<CurrencyId> for DexGeneralVerifyPairAsset {
fn validate_asset(currency_id: &CurrencyId) -> bool {
currency_id.is_lp_token()
}
}

impl dex_general::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type MultiCurrency = Tokens;
type PalletId = DexGeneralPalletId;
type AssetId = CurrencyId;
type EnsurePairAsset = DexGeneralVerifyPairAsset;
type LpGenerate = PairLpIdentity;
type WeightInfo = weights::dex_general::WeightInfo<Runtime>;
type MaxBootstrapRewards = MaxBootstrapRewards;
type MaxBootstrapLimits = MaxBootstrapLimits;
}

pub struct PoolLpGenerate;
impl StablePoolLpCurrencyIdGenerate<CurrencyId, StablePoolId> for PoolLpGenerate {
fn generate_by_pool_id(pool_id: StablePoolId) -> CurrencyId {
CurrencyId::StableLpToken(pool_id)
}
}

pub struct DexStableVerifyPoolAsset;
impl ValidateCurrency<CurrencyId> for DexStableVerifyPoolAsset {
fn validate_pooled_currency(_currencies: &[CurrencyId]) -> bool {
true
}

fn validate_pool_lp_currency(currency_id: CurrencyId) -> bool {
if Tokens::total_issuance(currency_id) > 0 {
return false;
}
true
}
}

impl dex_stable::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CurrencyId = CurrencyId;
type MultiCurrency = Tokens;
type PoolId = StablePoolId;
type TimeProvider = Timestamp;
type EnsurePoolAsset = DexStableVerifyPoolAsset;
type LpGenerate = PoolLpGenerate;
type PoolCurrencyLimit = CurrencyLimit;
type PoolCurrencySymbolLimit = StringLimit;
type PalletId = DexStablePalletId;
type WeightInfo = weights::dex_stable::WeightInfo<Runtime>;
}

impl dex_swap_router::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type StablePoolId = StablePoolId;
type Balance = Balance;
type CurrencyId = CurrencyId;
type GeneralAmm = DexGeneral;
type StableAmm = DexStable;
type GeneralWeightInfo = weights::dex_general::WeightInfo<Runtime>;
type StableWeightInfo = weights::dex_stable::WeightInfo<Runtime>;
type WeightInfo = weights::dex_swap_router::WeightInfo<Runtime>;
}

pub struct SetLoansExchangeRates;
impl OnRuntimeUpgrade for SetLoansExchangeRates {
fn on_runtime_upgrade() -> Weight {
let min_exchange_rate = loans::MinExchangeRate::<Runtime>::get();
if min_exchange_rate.is_zero() {
loans::MinExchangeRate::<Runtime>::put(Rate::from_inner(loans::DEFAULT_MIN_EXCHANGE_RATE));
}
let max_exchange_rate = loans::MaxExchangeRate::<Runtime>::get();
if max_exchange_rate.is_zero() {
loans::MaxExchangeRate::<Runtime>::put(Rate::from_inner(loans::DEFAULT_MAX_EXCHANGE_RATE));
}
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, 2)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: sp_std::vec::Vec<u8>) -> Result<(), &'static str> {
let min_exchange_rate = loans::MinExchangeRate::<Runtime>::get();
let max_exchange_rate = loans::MaxExchangeRate::<Runtime>::get();
ensure!(
!min_exchange_rate.is_zero(),
"Minimum lending exchange rate must be greater than zero"
);
ensure!(
!max_exchange_rate.is_zero(),
"Minimum lending exchange rate must be greater than zero"
);
ensure!(
min_exchange_rate.lt(&max_exchange_rate),
"Minimum lending exchange rate must be greater than the maximum exchange rate"
);
Ok(())
}
}
Loading

0 comments on commit f0772a0

Please sign in to comment.