Skip to content

Commit 6a38610

Browse files
feat(xcm): support json schema (for CosmWasm VM support) (#1454)
# Description - What does this PR do? Allows to generate JSON schema for subset of XCM in std builds - Why are these changes needed? To support XCM messages in CosmWasm contracts which require Schemars to generate contract clients - How were these changes implemented and what do they affect? We will use schema feature flag to build XCM pallet with JSON schema enabled # Checklist - [x] My PR includes a detailed description as outlined in the "Description" section above - [x] My PR follows the [labeling requirements](CONTRIBUTING.md#Process) of this project (at minimum one label for `T` required) - [x] I have made corresponding changes to the documentation (if applicable) - [x] I have added tests that prove my fix is effective or that my feature works (if applicable) - [x] If this PR alters any external APIs or interfaces used by Polkadot, the corresponding Polkadot PR is ready as well as the corresponding Cumulus PR (optional)
1 parent c0b2711 commit 6a38610

File tree

9 files changed

+30
-1
lines changed

9 files changed

+30
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ edition.workspace = true
77
license.workspace = true
88

99
[dependencies]
10-
bounded-collections = { version = "0.1.8", default-features = false, features = ["serde"] }
10+
bounded-collections = { version = "0.1.9", default-features = false, features = ["serde"] }
1111
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
1212
impl-trait-for-tuples = "0.2.2"
1313
log = { version = "0.4.17", default-features = false }
1414
parity-scale-codec = { version = "3.6.1", default-features = false, features = ["derive", "max-encoded-len"] }
1515
scale-info = { version = "2.10.0", default-features = false, features = ["derive", "serde"] }
1616
sp-weights = { path = "../../substrate/primitives/weights", default-features = false, features = ["serde"] }
1717
serde = { version = "1.0.193", default-features = false, features = ["alloc", "derive"] }
18+
schemars = { version = "0.8.13", default-features = true, optional = true }
1819
xcm-procedural = { path = "procedural" }
1920
environmental = { version = "1.1.4", default-features = false }
2021

@@ -35,3 +36,4 @@ std = [
3536
"serde/std",
3637
"sp-weights/std",
3738
]
39+
json-schema = ["bounded-collections/json-schema", "dep:schemars", "sp-weights/json-schema"]

src/double_encoded.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use parity_scale_codec::{Decode, DecodeLimit, Encode};
2525
#[codec(decode_bound())]
2626
#[scale_info(bounds(), skip_type_params(T))]
2727
#[scale_info(replace_segment("staging_xcm", "xcm"))]
28+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2829
pub struct DoubleEncoded<T> {
2930
encoded: Vec<u8>,
3031
#[codec(skip)]

src/v2/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub use traits::{Error, ExecuteXcm, GetWeight, Outcome, Result, SendError, SendR
8282
/// Basically just the XCM (more general) version of `ParachainDispatchOrigin`.
8383
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo)]
8484
#[scale_info(replace_segment("staging_xcm", "xcm"))]
85+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
8586
pub enum OriginKind {
8687
/// Origin should just be the native dispatch origin representation for the sender in the
8788
/// local runtime framework. For Cumulus/Frame chains this is the `Parachain` or `Relay` origin

src/v3/junction.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use serde::{Deserialize, Serialize};
4949
Serialize,
5050
Deserialize,
5151
)]
52+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5253
#[scale_info(replace_segment("staging_xcm", "xcm"))]
5354
pub enum NetworkId {
5455
/// Network specified by the first 32 bytes of its genesis block.
@@ -119,6 +120,7 @@ impl TryFrom<OldNetworkId> for NetworkId {
119120
Serialize,
120121
Deserialize,
121122
)]
123+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
122124
#[scale_info(replace_segment("staging_xcm", "xcm"))]
123125
pub enum BodyId {
124126
/// The only body in its context.
@@ -190,6 +192,7 @@ impl TryFrom<OldBodyId> for BodyId {
190192
Serialize,
191193
Deserialize,
192194
)]
195+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
193196
#[scale_info(replace_segment("staging_xcm", "xcm"))]
194197
pub enum BodyPart {
195198
/// The body's declaration, under whatever means it decides.
@@ -266,6 +269,7 @@ impl TryFrom<OldBodyPart> for BodyPart {
266269
Serialize,
267270
Deserialize,
268271
)]
272+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
269273
#[scale_info(replace_segment("staging_xcm", "xcm"))]
270274
pub enum Junction {
271275
/// An indexed parachain belonging to and operated by the context.

src/v3/junctions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub(crate) const MAX_JUNCTIONS: usize = 8;
4444
serde::Serialize,
4545
serde::Deserialize,
4646
)]
47+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4748
#[scale_info(replace_segment("staging_xcm", "xcm"))]
4849
pub enum Junctions {
4950
/// The interpreting consensus system.

src/v3/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub type QueryId = u64;
6969
#[codec(encode_bound())]
7070
#[scale_info(bounds(), skip_type_params(Call))]
7171
#[scale_info(replace_segment("staging_xcm", "xcm"))]
72+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
7273
pub struct Xcm<Call>(pub Vec<Instruction<Call>>);
7374

7475
/// The maximal number of instructions in an XCM before decoding fails.
@@ -232,15 +233,19 @@ pub mod prelude {
232233
}
233234

234235
parameter_types! {
236+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
235237
pub MaxPalletNameLen: u32 = 48;
236238
/// Maximum size of the encoded error code coming from a `Dispatch` result, used for
237239
/// `MaybeErrorCode`. This is not (yet) enforced, so it's just an indication of expectation.
240+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
238241
pub MaxDispatchErrorLen: u32 = 128;
242+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
239243
pub MaxPalletsInfo: u32 = 64;
240244
}
241245

242246
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
243247
#[scale_info(replace_segment("staging_xcm", "xcm"))]
248+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
244249
pub struct PalletInfo {
245250
#[codec(compact)]
246251
pub index: u32,
@@ -272,6 +277,7 @@ impl PalletInfo {
272277

273278
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
274279
#[scale_info(replace_segment("staging_xcm", "xcm"))]
280+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
275281
pub enum MaybeErrorCode {
276282
Success,
277283
Error(BoundedVec<u8, MaxDispatchErrorLen>),
@@ -296,6 +302,7 @@ impl Default for MaybeErrorCode {
296302
/// Response data to a query.
297303
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
298304
#[scale_info(replace_segment("staging_xcm", "xcm"))]
305+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
299306
pub enum Response {
300307
/// No response. Serves as a neutral default.
301308
Null,
@@ -320,6 +327,7 @@ impl Default for Response {
320327
/// Information regarding the composition of a query response.
321328
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo)]
322329
#[scale_info(replace_segment("staging_xcm", "xcm"))]
330+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
323331
pub struct QueryResponseInfo {
324332
/// The destination to which the query response message should be send.
325333
pub destination: MultiLocation,
@@ -333,6 +341,7 @@ pub struct QueryResponseInfo {
333341
/// An optional weight limit.
334342
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo)]
335343
#[scale_info(replace_segment("staging_xcm", "xcm"))]
344+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
336345
pub enum WeightLimit {
337346
/// No weight limit imposed.
338347
Unlimited,
@@ -417,6 +426,7 @@ impl XcmContext {
417426
#[codec(decode_bound())]
418427
#[scale_info(bounds(), skip_type_params(Call))]
419428
#[scale_info(replace_segment("staging_xcm", "xcm"))]
429+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
420430
pub enum Instruction<Call> {
421431
/// Withdraw asset(s) (`assets`) from the ownership of `origin` and place them into the Holding
422432
/// Register.

src/v3/multiasset.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use scale_info::TypeInfo;
4747
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, TypeInfo, MaxEncodedLen,
4848
)]
4949
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
50+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5051
#[scale_info(replace_segment("staging_xcm", "xcm"))]
5152
pub enum AssetInstance {
5253
/// Undefined - used if the non-fungible asset class has only one instance.
@@ -243,6 +244,7 @@ impl TryFrom<AssetInstance> for u128 {
243244
/// instance.
244245
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, MaxEncodedLen)]
245246
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
247+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
246248
#[scale_info(replace_segment("staging_xcm", "xcm"))]
247249
pub enum Fungibility {
248250
/// A fungible asset; we record a number of units, as a `u128` in the inner item.
@@ -313,6 +315,7 @@ impl TryFrom<OldFungibility> for Fungibility {
313315
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
314316
)]
315317
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
318+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
316319
#[scale_info(replace_segment("staging_xcm", "xcm"))]
317320
pub enum WildFungibility {
318321
/// The asset is fungible.
@@ -337,6 +340,7 @@ impl TryFrom<OldWildFungibility> for WildFungibility {
337340
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
338341
)]
339342
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
343+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
340344
#[scale_info(replace_segment("staging_xcm", "xcm"))]
341345
pub enum AssetId {
342346
/// A specific location identifying an asset.
@@ -412,6 +416,7 @@ impl AssetId {
412416
/// Either an amount of a single fungible asset, or a single well-identified non-fungible asset.
413417
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
414418
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
419+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
415420
#[scale_info(replace_segment("staging_xcm", "xcm"))]
416421
pub struct MultiAsset {
417422
/// The overall asset identity (aka *class*, in the case of a non-fungible).
@@ -510,6 +515,7 @@ impl TryFrom<OldMultiAsset> for MultiAsset {
510515
/// - The number of items should grow no larger than `MAX_ITEMS_IN_MULTIASSETS`.
511516
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, Default)]
512517
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
518+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
513519
#[scale_info(replace_segment("staging_xcm", "xcm"))]
514520
pub struct MultiAssets(Vec<MultiAsset>);
515521

@@ -710,6 +716,7 @@ impl MultiAssets {
710716
/// A wildcard representing a set of assets.
711717
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
712718
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
719+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
713720
#[scale_info(replace_segment("staging_xcm", "xcm"))]
714721
pub enum WildMultiAsset {
715722
/// All assets in Holding.
@@ -823,6 +830,7 @@ impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildMultiAsset
823830
/// `MultiAsset` collection, defined either by a number of `MultiAssets` or a single wildcard.
824831
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
825832
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
833+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
826834
#[scale_info(replace_segment("staging_xcm", "xcm"))]
827835
pub enum MultiAssetFilter {
828836
/// Specify the filter as being everything contained by the given `MultiAssets` inner.

src/v3/multilocation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use scale_info::TypeInfo;
6666
serde::Serialize,
6767
serde::Deserialize,
6868
)]
69+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
6970
pub struct MultiLocation {
7071
/// The number of parent junctions at the beginning of this `MultiLocation`.
7172
pub parents: u8,

src/v3/traits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use super::*;
3030
/// they will retain the same index over time.
3131
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
3232
#[scale_info(replace_segment("staging_xcm", "xcm"))]
33+
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3334
pub enum Error {
3435
// Errors that happen due to instructions being executed. These alone are defined in the
3536
// XCM specification.

0 commit comments

Comments
 (0)