Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

derive serialize/deserialize for xcm primitives #5036

Merged
merged 7 commits into from
Mar 14, 2022
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.

2 changes: 2 additions & 0 deletions xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parity-scale-codec = { version = "3.0.0", default-features = false, features = [
scale-info = { version = "2.0.0", default-features = false, features = ["derive"] }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
derivative = {version = "2.2.0", default-features = false, features = [ "use_core" ] }
serde = { version = "1.0.136", optional = true, features = ["derive"] }
log = { version = "0.4.14", default-features = false }
xcm-procedural = { path = "procedural" }

Expand All @@ -21,5 +22,6 @@ runtime-benchmarks = []
std = [
"parity-scale-codec/std",
"scale-info/std",
"serde",
"sp-io/std"
]
1 change: 1 addition & 0 deletions xcm/src/v2/junction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use scale_info::TypeInfo;
///
/// Each item assumes a pre-existing location as its context and is defined in terms of it.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Junction {
/// An indexed parachain belonging to and operated by the context.
///
Expand Down
3 changes: 3 additions & 0 deletions xcm/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub enum OriginKind {

/// A global identifier of an account-bearing consensus system.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum NetworkId {
/// Unidentified/any.
Any,
Expand All @@ -133,6 +134,7 @@ impl TryInto<NetworkId> for Option<NewNetworkId> {

/// An identifier of a pluralistic body.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum BodyId {
/// The only body in its context.
Unit,
Expand Down Expand Up @@ -169,6 +171,7 @@ impl From<NewBodyId> for BodyId {

/// A part of a pluralistic body.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum BodyPart {
/// The body's declaration, under whatever means it decides.
Voice,
Expand Down
8 changes: 8 additions & 0 deletions xcm/src/v2/multiasset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use scale_info::TypeInfo;

/// A general identifier for an instance of a non-fungible asset class.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo)]
#[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 @@ -117,6 +118,7 @@ impl TryFrom<NewAssetInstance> for AssetInstance {

/// Classification of an asset being concrete or abstract.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum AssetId {
Concrete(MultiLocation),
Abstract(Vec<u8>),
Expand Down Expand Up @@ -180,6 +182,7 @@ impl AssetId {

/// Classification of whether an asset is fungible or not, along with a mandatory amount or instance.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Fungibility {
Fungible(#[codec(compact)] u128),
NonFungible(AssetInstance),
Expand Down Expand Up @@ -218,6 +221,7 @@ impl TryFrom<NewFungibility> for Fungibility {
}

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct MultiAsset {
pub id: AssetId,
pub fun: Fungibility,
Expand Down Expand Up @@ -301,6 +305,7 @@ impl TryFrom<NewMultiAsset> for MultiAsset {

/// A `Vec` of `MultiAsset`s. There may be no duplicate fungible items in here and when decoding, they must be sorted.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct MultiAssets(Vec<MultiAsset>);

impl Decode for MultiAssets {
Expand Down Expand Up @@ -466,6 +471,7 @@ impl MultiAssets {

/// Classification of whether an asset is fungible or not.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum WildFungibility {
Fungible,
NonFungible,
Expand All @@ -484,6 +490,7 @@ impl TryFrom<NewWildFungibility> for WildFungibility {

/// A wildcard representing a set of assets.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum WildMultiAsset {
/// All assets in the holding register, up to `usize` individual assets (different instances of non-fungibles could
/// be separate assets).
Expand Down Expand Up @@ -528,6 +535,7 @@ impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildMultiAsset
/// Note: Vectors of wildcards whose encoding is supported in XCM v0 are unsupported
/// in this implementation and will result in a decode error.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum MultiAssetFilter {
Definite(MultiAssets),
Wild(WildMultiAsset),
Expand Down
2 changes: 2 additions & 0 deletions xcm/src/v2/multilocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use scale_info::TypeInfo;
///
/// The `MultiLocation` value of `Null` simply refers to the interpreting consensus system.
#[derive(Clone, Decode, Encode, Eq, PartialEq, Ord, PartialOrd, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct MultiLocation {
/// The number of parent junctions at the beginning of this `MultiLocation`.
pub parents: u8,
Expand Down Expand Up @@ -470,6 +471,7 @@ const MAX_JUNCTIONS: usize = 8;
/// Parent junctions cannot be constructed with this type. Refer to `MultiLocation` for
/// instructions on constructing parent junctions.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Junctions {
/// The interpreting consensus system.
Here,
Expand Down
4 changes: 4 additions & 0 deletions xcm/src/v3/junction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use scale_info::TypeInfo;
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum NetworkId {
/// Network specified by the first 32 bytes of its genesis block.
ByGenesis([u8; 32]),
Expand Down Expand Up @@ -76,6 +77,7 @@ impl From<OldNetworkId> for Option<NetworkId> {
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum BodyId {
/// The only body in its context.
Unit,
Expand Down Expand Up @@ -122,6 +124,7 @@ impl TryFrom<OldBodyId> for BodyId {
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum BodyPart {
/// The body's declaration, under whatever means it decides.
Voice,
Expand Down Expand Up @@ -185,6 +188,7 @@ impl TryFrom<OldBodyPart> for BodyPart {
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Junction {
/// An indexed parachain belonging to and operated by the context.
///
Expand Down
1 change: 1 addition & 0 deletions xcm/src/v3/junctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub(crate) const MAX_JUNCTIONS: usize = 8;
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Junctions {
/// The interpreting consensus system.
Here,
Expand Down
8 changes: 8 additions & 0 deletions xcm/src/v3/multiasset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use scale_info::TypeInfo;
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
)]
#[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 @@ -235,6 +236,7 @@ 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, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Fungibility {
Fungible(#[codec(compact)] u128),
NonFungible(AssetInstance),
Expand Down Expand Up @@ -283,6 +285,7 @@ impl TryFrom<OldFungibility> for Fungibility {
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum WildFungibility {
Fungible,
NonFungible,
Expand All @@ -303,6 +306,7 @@ impl TryFrom<OldWildFungibility> for WildFungibility {
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum AssetId {
Concrete(MultiLocation),
Abstract([u8; 32]),
Expand Down Expand Up @@ -371,6 +375,7 @@ impl AssetId {
}

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct MultiAsset {
pub id: AssetId,
pub fun: Fungibility,
Expand Down Expand Up @@ -458,6 +463,7 @@ impl TryFrom<OldMultiAsset> for MultiAsset {

/// A `Vec` of `MultiAsset`s. There may be no duplicate fungible items in here and when decoding, they must be sorted.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, Default)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
// TODO: Change to a `BoundedVec`.
pub struct MultiAssets(Vec<MultiAsset>);

Expand Down Expand Up @@ -644,6 +650,7 @@ 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))]
pub enum WildMultiAsset {
/// All assets in Holding.
All,
Expand Down Expand Up @@ -758,6 +765,7 @@ impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildMultiAsset

/// `MultiAsset` collection, either `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))]
pub enum MultiAssetFilter {
Definite(MultiAssets),
Wild(WildMultiAsset),
Expand Down
1 change: 1 addition & 0 deletions xcm/src/v3/multilocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use scale_info::TypeInfo;
#[derive(
Copy, Clone, Decode, Encode, Eq, PartialEq, Ord, PartialOrd, Debug, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct MultiLocation {
/// The number of parent junctions at the beginning of this `MultiLocation`.
pub parents: u8,
Expand Down