ink_metadata/
sol.rs

1// Copyright (C) ink! contributors.
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//! Types for representing Solidity ABI compatibility metadata for ink! projects.
16
17use std::borrow::Cow;
18
19use serde::{
20    Deserialize,
21    Serialize,
22};
23
24/// ink! contract metadata for Solidity ABI compatible metadata generation.
25#[derive(Debug, Serialize, Deserialize)]
26pub struct ContractMetadata {
27    /// Name of ink! contract.
28    pub name: Cow<'static, str>,
29    /// Metadata for all constructors of ink! contract.
30    pub constructors: Vec<ConstructorMetadata>,
31    /// Metadata for all messages of ink! contract.
32    pub functions: Vec<FunctionMetadata>,
33    /// Metadata for all events of ink! contract.
34    pub events: Vec<EventMetadata>,
35    /// Metadata for all errors encoded as Solidity custom errors for ink! contract.
36    pub errors: Vec<ErrorMetadata>,
37    /// Documentation for ink! contract.
38    pub docs: Cow<'static, str>,
39}
40
41/// ink! constructor info for Solidity ABI compatible metadata generation.
42#[derive(Debug, Serialize, Deserialize)]
43pub struct ConstructorMetadata {
44    /// Name of ink! constructor.
45    pub name: Cow<'static, str>,
46    /// Parameter info for ink! constructor.
47    pub inputs: Vec<ParamMetadata>,
48    /// Whether the ink! constructor is marked as payable.
49    pub is_payable: bool,
50    /// Whether the ink! constructor is marked as default.
51    pub is_default: bool,
52    /// Documentation for ink! constructor.
53    pub docs: Cow<'static, str>,
54}
55
56/// ink! message info for Solidity ABI compatible metadata generation.
57#[derive(Debug, Serialize, Deserialize)]
58pub struct FunctionMetadata {
59    /// Name of ink! message.
60    pub name: Cow<'static, str>,
61    /// Parameter info for ink! message.
62    pub inputs: Vec<ParamMetadata>,
63    /// Return type of ink! message.
64    pub output: Option<Cow<'static, str>>,
65    /// Whether the ink! message has a mutable self receiver.
66    pub mutates: bool,
67    /// Whether the ink! message is marked as payable.
68    pub is_payable: bool,
69    /// Whether the ink! message is marked as default.
70    pub is_default: bool,
71    /// Documentation for ink! message.
72    pub docs: Cow<'static, str>,
73}
74
75/// ink! event info for Solidity ABI compatible metadata generation.
76#[derive(Debug, Serialize, Deserialize)]
77pub struct EventMetadata {
78    /// Name of ink! event.
79    pub name: Cow<'static, str>,
80    /// Whether the ink! event is marked as anonymous.
81    pub is_anonymous: bool,
82    /// Parameter info for ink! event.
83    pub params: Vec<EventParamMetadata>,
84    /// Documentation for ink! event.
85    pub docs: Cow<'static, str>,
86}
87
88/// ink! constructor and message parameter info.
89#[derive(Debug, Serialize, Deserialize)]
90pub struct ParamMetadata {
91    /// Name of parameter.
92    pub name: Cow<'static, str>,
93    /// Solidity ABI type of parameter.
94    pub ty: Cow<'static, str>,
95}
96
97/// ink! event parameter info.
98#[derive(Debug, Serialize, Deserialize)]
99pub struct EventParamMetadata {
100    /// Name of parameter.
101    pub name: Cow<'static, str>,
102    /// Solidity ABI type of parameter.
103    pub ty: Cow<'static, str>,
104    /// Whether the parameter is marked as a topic (i.e. is indexed).
105    pub is_topic: bool,
106    /// Documentation for parameter.
107    pub docs: Cow<'static, str>,
108}
109
110/// Error info for Solidity ABI compatible metadata generation.
111#[derive(Debug, Serialize, Deserialize)]
112pub struct ErrorMetadata {
113    /// Name of error.
114    pub name: Cow<'static, str>,
115    /// Parameter info for error.
116    pub params: Vec<ErrorParamMetadata>,
117    /// Documentation for error or error variant.
118    pub docs: Cow<'static, str>,
119}
120
121/// Error parameter info.
122#[derive(Debug, Serialize, Deserialize)]
123pub struct ErrorParamMetadata {
124    /// Name of parameter.
125    pub name: Cow<'static, str>,
126    /// Solidity ABI type of parameter.
127    pub ty: Cow<'static, str>,
128    /// Documentation for parameter.
129    pub docs: Cow<'static, str>,
130}
131
132/// Provides [Solidity custom error metadata][abi-json] for an error type.
133///
134/// # Note
135///
136/// For enums, each variant typically corresponds to its own
137/// [Solidity custom error][sol-error] type.
138///
139/// [abi-json]: https://docs.soliditylang.org/en/latest/abi-spec.html#json
140/// [sol-error]: https://soliditylang.org/blog/2021/04/21/custom-errors/
141pub trait SolErrorMetadata {
142    /// Returns the metadata for the error type.
143    fn error_specs() -> Vec<ErrorMetadata>;
144}