ink_metadata/
lib.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![doc(
16    html_logo_url = "https://use.ink/img/crate-docs/logo.png",
17    html_favicon_url = "https://use.ink/crate-docs/favicon.png"
18)]
19#![cfg_attr(not(feature = "std"), no_std)]
20
21#[cfg(not(feature = "std"))]
22extern crate alloc;
23extern crate core;
24
25#[cfg(test)]
26mod tests;
27
28pub mod layout;
29pub mod sol;
30mod specs;
31mod utils;
32
33pub use ink_primitives::LangError;
34
35pub use self::specs::{
36    ConstructorSpec,
37    ConstructorSpecBuilder,
38    ContractSpec,
39    ContractSpecBuilder,
40    DisplayName,
41    EnvironmentSpec,
42    EnvironmentSpecBuilder,
43    EventParamSpec,
44    EventParamSpecBuilder,
45    EventSpec,
46    EventSpecBuilder,
47    MessageParamSpec,
48    MessageParamSpecBuilder,
49    MessageSpec,
50    MessageSpecBuilder,
51    ReturnTypeSpec,
52    Selector,
53    TypeSpec,
54};
55
56use impl_serde::serialize as serde_hex;
57
58pub use scale_info::TypeInfo;
59
60#[cfg(feature = "derive")]
61use scale_info::{
62    IntoPortable as _,
63    PortableRegistry,
64    Registry,
65    form::PortableForm,
66};
67use schemars::JsonSchema;
68use serde::{
69    Deserialize,
70    Serialize,
71};
72
73/// The metadata version of the generated ink! contract.
74///
75/// The serialized metadata format (which this represents) is different from the
76/// version of this crate or the contract for Rust semantic versioning purposes.
77const METADATA_VERSION: u64 = 6;
78
79/// An entire ink! project for metadata file generation purposes.
80#[derive(Debug, Serialize, Deserialize, JsonSchema)]
81pub struct InkProject {
82    version: u64,
83    #[serde(flatten)]
84    registry: PortableRegistry,
85    #[serde(rename = "storage")]
86    /// The layout of the storage data structure
87    layout: layout::Layout<PortableForm>,
88    spec: ContractSpec<PortableForm>,
89}
90
91impl InkProject {
92    /// Create a new ink! project from a layout and a spec.
93    pub fn new<L, S>(layout: L, spec: S) -> Self
94    where
95        L: Into<layout::Layout>,
96        S: Into<ContractSpec>,
97    {
98        let mut registry = Registry::new();
99
100        Self {
101            version: METADATA_VERSION,
102            layout: layout.into().into_portable(&mut registry),
103            spec: spec.into().into_portable(&mut registry),
104            registry: registry.into(),
105        }
106    }
107
108    /// Create a new portable ink! project.
109    ///
110    /// The caller is responsible to register all types into the supplied registry.
111    pub fn new_portable(
112        layout: layout::Layout<PortableForm>,
113        spec: ContractSpec<PortableForm>,
114        registry: PortableRegistry,
115    ) -> Self {
116        Self {
117            version: METADATA_VERSION,
118            layout,
119            spec,
120            registry,
121        }
122    }
123
124    /// Returns the metadata version used by the contract.
125    pub fn version(&self) -> u64 {
126        self.version
127    }
128
129    /// Returns a read-only registry of types in the contract.
130    pub fn registry(&self) -> &PortableRegistry {
131        &self.registry
132    }
133
134    /// Returns the storage layout of the contract.
135    pub fn layout(&self) -> &layout::Layout<PortableForm> {
136        &self.layout
137    }
138
139    /// Returns the specification of the contract.
140    pub fn spec(&self) -> &ContractSpec<PortableForm> {
141        &self.spec
142    }
143}
144
145/// Provides metadata about an ink! event.
146///
147/// Implementations must be registered into the `ink::CONTRACT_EVENTS` distributed slice,
148/// in order to be included in the contract metadata. This is done automatically by the
149/// `#[derive(ink::EventMetadata)]`
150pub trait EventMetadata {
151    /// The full path to the event type, usually provided by [`module_path`].
152    const MODULE_PATH: &'static str;
153
154    /// Returns the metadata of the event.
155    fn event_spec() -> EventSpec;
156}