Skip to content

Commit

Permalink
wip(kreivo-runtime): adjust parameters for runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
pandres95 committed Jun 14, 2024
1 parent 976ce39 commit 6ac53d7
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 206 deletions.
97 changes: 44 additions & 53 deletions common/src/multilocation_asset_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ impl From<u32> for FungibleAssetLocation {
pub mod runtime {
use super::{FungibleAssetLocation, Para};
use sp_runtime::traits::MaybeEquivalence;
use xcm::v3::{
use xcm::latest::{
Junction::{GeneralIndex, GlobalConsensus, PalletInstance, Parachain},
Junctions, MultiLocation, NetworkId,
Location, NetworkId,
};

impl TryFrom<NetworkId> for super::NetworkId {
Expand All @@ -82,72 +82,63 @@ pub mod runtime {
}

pub struct AsFungibleAssetLocation;
impl MaybeEquivalence<MultiLocation, FungibleAssetLocation> for AsFungibleAssetLocation {
fn convert(value: &MultiLocation) -> Option<FungibleAssetLocation> {
match *value {
MultiLocation {
parents: 2,
interior: Junctions::X1(GlobalConsensus(network)),
} => Some(FungibleAssetLocation::External {
network: network.try_into().ok()?,
impl MaybeEquivalence<Location, FungibleAssetLocation> for AsFungibleAssetLocation {
fn convert(value: &Location) -> Option<FungibleAssetLocation> {
match value.unpack() {
(2, [GlobalConsensus(network)]) => Some(FungibleAssetLocation::External {
network: (*network).try_into().ok()?,
child: None,
}),
MultiLocation {
parents: 2,
interior:
Junctions::X4(GlobalConsensus(network), Parachain(id), PalletInstance(pallet), GeneralIndex(index)),
} => Some(FungibleAssetLocation::External {
network: network.try_into().ok()?,
child: Some(Para {
id: id.try_into().ok()?,
pallet,
index: index.try_into().ok()?,
}),
}),
MultiLocation {
parents: 1,
interior: Junctions::X3(Parachain(id), PalletInstance(pallet), GeneralIndex(index)),
} => Some(FungibleAssetLocation::Sibling(Para {
id: id.try_into().ok()?,
pallet,
index: index.try_into().ok()?,
})),
MultiLocation {
parents: 0,
interior: Junctions::X2(PalletInstance(13), GeneralIndex(index)),
} => Some(FungibleAssetLocation::Here(
index.try_into().expect("as it is here, we the types will match; qed"),
(2, [GlobalConsensus(network), Parachain(id), PalletInstance(pallet), GeneralIndex(index)]) => {
Some(FungibleAssetLocation::External {
network: (*network).try_into().ok()?,
child: Some(Para {
id: (*id).try_into().ok()?,
pallet: *pallet,
index: (*index).try_into().ok()?,
}),
})
}
(1, [Parachain(id), PalletInstance(pallet), GeneralIndex(index)]) => {
Some(FungibleAssetLocation::Sibling(Para {
id: (*id).try_into().ok()?,
pallet: *pallet,
index: (*index).try_into().ok()?,
}))
}
(0, [PalletInstance(13), GeneralIndex(index)]) => Some(FungibleAssetLocation::Here(
(*index)
.try_into()
.expect("as it is here, we the types will match; qed"),
)),
_ => None,
}
}

fn convert_back(value: &FungibleAssetLocation) -> Option<MultiLocation> {
fn convert_back(value: &FungibleAssetLocation) -> Option<Location> {
match *value {
FungibleAssetLocation::Here(index) => Some(MultiLocation {
parents: 0,
interior: Junctions::X2(PalletInstance(13), GeneralIndex(index.into())),
}),
FungibleAssetLocation::Sibling(Para { id, pallet, index }) => Some(MultiLocation {
parents: 1,
interior: Junctions::X3(Parachain(id.into()), PalletInstance(pallet), GeneralIndex(index.into())),
}),
FungibleAssetLocation::External { network, child: None } => Some(MultiLocation {
parents: 2,
interior: Junctions::X1(GlobalConsensus(network.into())),
}),
FungibleAssetLocation::Here(index) => {
Some(Location::new(0, [PalletInstance(13), GeneralIndex(index.into())]))
}
FungibleAssetLocation::Sibling(Para { id, pallet, index }) => Some(Location::new(
1,
[Parachain(id.into()), PalletInstance(pallet), GeneralIndex(index.into())],
)),
FungibleAssetLocation::External { network, child: None } => {
Some(Location::new(2, [GlobalConsensus(network.into())]))
}
FungibleAssetLocation::External {
network,
child: Some(Para { id, pallet, index }),
} => Some(MultiLocation {
parents: 2,
interior: Junctions::X4(
} => Some(Location::new(
2,
[
GlobalConsensus(network.into()),
Parachain(id.into()),
PalletInstance(pallet),
GeneralIndex(index.into()),
),
}),
],
)),
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions pallets/communities/src/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,31 @@ pub enum Subset<T: Config> {
}

#[cfg(feature = "xcm")]
impl<T> TryConvert<RuntimeOriginFor<T>, xcm::v3::MultiLocation> for RawOrigin<T>
impl<T> TryConvert<RuntimeOriginFor<T>, xcm::latest::Location> for RawOrigin<T>
where
T: Config,
RuntimeOriginFor<T>: Into<Result<RawOrigin<T>, RuntimeOriginFor<T>>>,
xcm::v3::Junction: TryFrom<RawOrigin<T>>,
xcm::latest::Junction: TryFrom<RawOrigin<T>>,
{
fn try_convert(o: RuntimeOriginFor<T>) -> Result<xcm::v3::MultiLocation, RuntimeOriginFor<T>> {
fn try_convert(o: RuntimeOriginFor<T>) -> Result<xcm::latest::Location, RuntimeOriginFor<T>> {
let Ok(community @ RawOrigin { .. }) = o.clone().into() else {
return Err(o);
};
let j = xcm::v3::Junction::try_from(community).map_err(|_| o)?;
let j = xcm::latest::Junction::try_from(community).map_err(|_| o)?;
Ok(j.into())
}
}

#[cfg(feature = "xcm")]
impl<T> TryFrom<RawOrigin<T>> for xcm::v3::Junction
impl<T> TryFrom<RawOrigin<T>> for xcm::latest::Junction
where
T: Config,
u32: From<CommunityIdOf<T>>,
{
type Error = ();

fn try_from(o: RawOrigin<T>) -> Result<Self, Self::Error> {
use xcm::v3::{BodyId, BodyPart, Junction::Plurality};
use xcm::latest::{BodyId, BodyPart, Junction::Plurality};
let part = match o.subset {
None => BodyPart::Voice,
Some(Subset::Member(_)) => BodyPart::Members { count: 1 },
Expand All @@ -170,15 +170,15 @@ where
}

#[cfg(feature = "xcm")]
impl<T: Config> TryFrom<xcm::v3::Junction> for RawOrigin<T>
impl<T: Config> TryFrom<xcm::latest::Junction> for RawOrigin<T>
where
T: Config,
T::CommunityId: From<u32> + From<u64>,
{
type Error = ();

fn try_from(value: xcm::v3::Junction) -> Result<Self, Self::Error> {
use xcm::v3::{BodyId::Index, BodyPart::*, Junction::Plurality};
fn try_from(value: xcm::latest::Junction) -> Result<Self, Self::Error> {
use xcm::latest::{BodyId::Index, BodyPart::*, Junction::Plurality};
let Plurality { id: Index(id), part } = value else {
return Err(());
};
Expand Down
83 changes: 44 additions & 39 deletions runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,67 @@
//! Taken from polkadot/runtime/common (at a21cd64) and adapted for parachains.
use frame_support::traits::{
fungible::{DecreaseIssuance, IncreaseIssuance},
fungibles::{Balanced, Credit},
Currency, Imbalance, OnUnbalanced,
};
use pallet_asset_tx_payment::HandleCredit;
use sp_runtime::traits::MaybeEquivalence;
// use pallet_balances::Pallet as Balances;
use pallet_treasury::Pallet as Treasury;
use sp_std::marker::PhantomData;
use xcm::latest::Location;

// TODO - Create and import XCM common types
//use xcm::latest::{AssetId, Fungibility::Fungible, MultiAsset, MultiLocation};

/// Type alias to conveniently refer to the `Currency::NegativeImbalance`
/// associated type.
pub type NegativeImbalance<T> =
<pallet_balances::Pallet<T> as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;

/// Type alias to conveniently refer to `frame_system`'s `Config::AccountId`.
pub type AccountIdOf<R> = <R as frame_system::Config>::AccountId;
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

// /// Type alias to conveniently refer to the `Currency::NegativeImbalance`
// /// associated type.
pub type NegativeImbalance<T, I> = <pallet_balances::Pallet<T, I> as Currency<AccountIdOf<T>>>::NegativeImbalance;

/// Type Alias to represent fungible imbalances
pub type FungibleImbalance<T, I> = frame_support::traits::fungible::Imbalance<
<T as pallet_balances::Config<I>>::Balance,
DecreaseIssuance<AccountIdOf<T>, pallet_balances::Pallet<T, I>>,
IncreaseIssuance<AccountIdOf<T>, pallet_balances::Pallet<T, I>>,
>;

/// [OnUnbalanced] handler that takes 100% of the fees + tips (if any), and
/// makes them go to the treasury.
pub struct DealWithFees<T, I: 'static = ()>(PhantomData<(T, I)>);

pub struct DealWithFees<R>(PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for DealWithFees<R>
impl<T, I: 'static, IB> OnUnbalanced<IB> for DealWithFees<T, I>
where
R: pallet_balances::Config + pallet_collator_selection::Config + pallet_treasury::Config,
pallet_treasury::Pallet<R>: OnUnbalanced<NegativeImbalance<R>>,
AccountIdOf<R>: From<polkadot_core_primitives::v2::AccountId> + Into<polkadot_core_primitives::v2::AccountId>,
<R as frame_system::Config>::RuntimeEvent: From<pallet_balances::Event<R>>,
T: pallet_balances::Config<I> + pallet_treasury::Config<I> + pallet_collator_selection::Config,
Treasury<T, I>: OnUnbalanced<IB>,
IB: Imbalance<<T as pallet_balances::Config<I>>::Balance>,
{
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance<R>>) {
use pallet_treasury::Pallet as Treasury;
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = IB>) {
if let Some(mut fees) = fees_then_tips.next() {
if let Some(tips) = fees_then_tips.next() {
tips.merge_into(&mut fees);
Treasury::<T, I>::on_unbalanced(fees);
}
}
}
}

/// [OnUnbalanced] handler that takes 100% of the fees + tips (if any), and
/// makes them go to the treasury.
pub struct DealWithFungibleFees<T, I: 'static = ()>(PhantomData<(T, I)>);

impl<T, I: 'static> OnUnbalanced<FungibleImbalance<T, I>> for DealWithFungibleFees<T, I>
where
T: pallet_balances::Config<I> + pallet_treasury::Config<I> + pallet_collator_selection::Config,
Treasury<T, I>: OnUnbalanced<FungibleImbalance<T, I>>,
{
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = FungibleImbalance<T, I>>) {
if let Some(mut fees) = fees_then_tips.next() {
if let Some(tips) = fees_then_tips.next() {
tips.merge_into(&mut fees);
Treasury::<T, I>::on_unbalanced(fees);
}
// 100% of the fees + tips (if any) go to the treasury
<Treasury<R> as OnUnbalanced<_>>::on_unbalanced(fees);
}
}
}
Expand All @@ -73,24 +99,3 @@ where
}
}
}

pub struct AsAssetMultiLocation<AssetId, AssetIdInfoGetter>(PhantomData<(AssetId, AssetIdInfoGetter)>);
impl<AssetId, AssetIdInfoGetter> MaybeEquivalence<Location, AssetId>
for AsAssetMultiLocation<AssetId, AssetIdInfoGetter>
where
AssetId: Clone,
AssetIdInfoGetter: AssetMultiLocationGetter<AssetId>,
{
fn convert(asset_multi_location: &Location) -> Option<AssetId> {
AssetIdInfoGetter::get_asset_id(asset_multi_location)
}

fn convert_back(asset_id: &AssetId) -> Option<Location> {
AssetIdInfoGetter::get_asset_multi_location(asset_id.clone())
}
}

pub trait AssetMultiLocationGetter<AssetId> {
fn get_asset_multi_location(asset_id: AssetId) -> Option<Location>;
fn get_asset_id(asset_multi_location: &Location) -> Option<AssetId>;
}
24 changes: 23 additions & 1 deletion runtime/kreivo/src/collective/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

use frame_system::EnsureRootWithSuccess;
use frame_system::{EnsureNever, EnsureRootWithSuccess};
use pallet_ranked_collective::Rank;
use sp_core::ConstU16;
use sp_runtime::traits::Convert;
Expand All @@ -21,17 +21,39 @@ impl pallet_ranked_collective::Config<KreivoCollectiveInstance> for Runtime {
type WeightInfo = pallet_ranked_collective::weights::SubstrateWeight<Self>;
type RuntimeEvent = RuntimeEvent;

type AddOrigin = EnsureNever<()>;

// Initially, members of kreivo collective are promoted via governance action
// In the future, it's expected to have an auxilliary pallet to observe the
// criteria for ranking
type PromoteOrigin = EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>;

type RemoveOrigin = Self::DemoteOrigin;

// Initially, members of kreivo collective are demoted via governance action
// In the future, it's expected to have an auxilliary pallet to observe the
// criteria for ranking
type DemoteOrigin = EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>;

type ExchangeOrigin = EnsureRoot<AccountId>;
type MemberSwappedHandler = ();

type Polls = KreivoReferenda;
type MinRankOfClass = AtLeastRank<1>;
type VoteWeight = pallet_ranked_collective::Linear;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkSetup = CollectiveBenchmarkSetup;
}

#[cfg(feature = "runtime-benchmarks")]
use frame_support::traits::RankedMembers;
#[cfg(feature = "runtime-benchmarks")]
pub struct CollectiveBenchmarkSetup;
#[cfg(feature = "runtime-benchmarks")]
impl pallet_ranked_collective::BenchmarkSetup<AccountId> for CollectiveBenchmarkSetup {
/// Ensure that this member is registered correctly.
fn ensure_member(acc: &AccountId) {
<pallet_ranked_collective::Pallet<Runtime, KreivoCollectiveInstance> as RankedMembers>::induct(acc);
}
}
5 changes: 2 additions & 3 deletions runtime/kreivo/src/collective/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ pub struct TracksInfo;
impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
type Id = TrackId;
type RuntimeOrigin = <RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;
type TracksIter = pallet_referenda::StaticTracksIter<Self::Id, Balance, BlockNumber>;

fn tracks() -> Self::TracksIter {
const DATA: [pallet_referenda::Track<TrackId, Balance, BlockNumber>; 4] = [
fn tracks() -> impl Iterator<Item = Cow<'static, Track<TrackId, Balance, BlockNumber>>> {
const DATA: [Track<TrackId, Balance, BlockNumber>; 4] = [
Track {
id: 0,
info: pallet_referenda::TrackInfo {
Expand Down
3 changes: 3 additions & 0 deletions runtime/kreivo/src/communities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl pallet_communities::Config for Runtime {

type PalletId = CommunityPalletId;

type ItemConfig = pallet_nfts::ItemConfig;
type RuntimeFreezeReason = RuntimeFreezeReason;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = CommunityBenchmarkHelper;
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/kreivo/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub mod fee {
}

pub mod locations {
pub const STATEMINE_PARA_ID: u32 = 1000;
pub const ASSET_HUB_ID: u32 = 1000;
// Even if they are not used yet, let's keep these for the future.
pub const STATEMINE_ASSET_PALLET_ID: u8 = 50;
pub const USDT_ASSET_ID: u128 = 1984;
Expand Down
Loading

0 comments on commit 6ac53d7

Please sign in to comment.