Skip to content

Commit

Permalink
Serialize/Deserialize trait implemented in no-std for XCM assets (#3057)
Browse files Browse the repository at this point in the history
Continues the work of paritytech/polkadot#7312.
`MultiLocation` was modified to implement serialize in no-std in that
PR, but not the assets types.
This PR does the same for assets, and this also allows to inject them in
pallets that require them in genesis.
  • Loading branch information
fgamundi authored and al3mart committed Jan 29, 2024
1 parent 272af77 commit 632a655
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 32 deletions.
121 changes: 105 additions & 16 deletions polkadot/xcm/src/v3/multiasset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,20 @@ use scale_info::TypeInfo;

/// A general identifier for an instance of a non-fungible asset class.
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
Debug,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum AssetInstance {
Expand Down Expand Up @@ -264,8 +275,19 @@ impl TryFrom<AssetInstance> for u128 {

/// Classification of whether an asset is fungible or not, along with a mandatory amount or
/// instance.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum Fungibility {
Expand Down Expand Up @@ -345,9 +367,20 @@ impl TryFrom<NewFungibility> for Fungibility {

/// Classification of whether an asset is fungible or not.
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum WildFungibility {
Expand Down Expand Up @@ -381,9 +414,20 @@ impl TryFrom<NewWildFungibility> for WildFungibility {

/// Classification of an asset being concrete or abstract.
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum AssetId {
Expand Down Expand Up @@ -465,8 +509,18 @@ impl AssetId {
}

/// Either an amount of a single fungible asset, or a single well-identified non-fungible asset.
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub struct MultiAsset {
Expand Down Expand Up @@ -571,8 +625,19 @@ impl TryFrom<NewMultiAsset> for MultiAsset {
/// - It may contain no items of duplicate asset class;
/// - All items must be ordered;
/// - The number of items should grow no larger than `MAX_ITEMS_IN_MULTIASSETS`.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, Default)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
TypeInfo,
Default,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub struct MultiAssets(Vec<MultiAsset>);
Expand Down Expand Up @@ -784,8 +849,20 @@ impl MultiAssets {
}

/// A wildcard representing a set of assets.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum WildMultiAsset {
Expand Down Expand Up @@ -912,8 +989,20 @@ impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildMultiAsset
}

/// `MultiAsset` collection, defined either by a number of `MultiAssets` or a single wildcard.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum MultiAssetFilter {
Expand Down
122 changes: 106 additions & 16 deletions polkadot/xcm/src/v4/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ use scale_info::TypeInfo;

/// A general identifier for an instance of a non-fungible asset class.
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
Debug,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum AssetInstance {
/// Undefined - used if the non-fungible asset class has only one instance.
Undefined,
Expand Down Expand Up @@ -237,8 +248,19 @@ impl TryFrom<AssetInstance> for u128 {

/// Classification of whether an asset is fungible or not, along with a mandatory amount or
/// instance.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
pub enum Fungibility {
/// A fungible asset; we record a number of units, as a `u128` in the inner item.
Fungible(#[codec(compact)] u128),
Expand Down Expand Up @@ -305,9 +327,20 @@ impl TryFrom<OldFungibility> for Fungibility {

/// Classification of whether an asset is fungible or not.
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum WildFungibility {
/// The asset is fungible.
Fungible,
Expand All @@ -327,8 +360,20 @@ impl TryFrom<OldWildFungibility> for WildFungibility {
}

/// Location to identify an asset.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
pub struct AssetId(pub Location);

impl<T: Into<Location>> From<T> for AssetId {
Expand Down Expand Up @@ -387,8 +432,18 @@ impl Reanchorable for AssetId {
}

/// Either an amount of a single fungible asset, or a single well-identified non-fungible asset.
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
pub struct Asset {
/// The overall asset identity (aka *class*, in the case of a non-fungible).
pub id: AssetId,
Expand Down Expand Up @@ -480,8 +535,19 @@ impl TryFrom<OldAsset> for Asset {
/// - It may contain no items of duplicate asset class;
/// - All items must be ordered;
/// - The number of items should grow no larger than `MAX_ITEMS_IN_ASSETS`.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, Default)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
TypeInfo,
Default,
serde::Serialize,
serde::Deserialize,
)]
pub struct Assets(Vec<Asset>);

/// Maximum number of items we expect in a single `Assets` value. Note this is not (yet)
Expand Down Expand Up @@ -681,8 +747,20 @@ impl Reanchorable for Assets {
}

/// A wildcard representing a set of assets.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
pub enum WildAsset {
/// All assets in Holding.
All,
Expand Down Expand Up @@ -779,8 +857,20 @@ impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildAsset {
}

/// `Asset` collection, defined either by a number of `Assets` or a single wildcard.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
pub enum AssetFilter {
/// Specify the filter as being everything contained by the given `Assets` inner.
Definite(Assets),
Expand Down
13 changes: 13 additions & 0 deletions prdoc/pr_3057.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Serialize/Deserialize trait implemented in no-std for XCM assets types

doc:
- audience: Runtime Dev
description: |
Serialize/Deserialize trait implemented in no-std for XCM v3 and v4
assets types

crates:
- name: staging-xcm

0 comments on commit 632a655

Please sign in to comment.