ink_primitives/
contract.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
15use crate::types::Environment;
16
17/// Stores the used host environment type of the ink! smart contract.
18///
19/// # Note
20///
21/// The used host environment can be altered using the `env` configuration
22/// parameter in the `#[ink::contract]` parameters. For example if the user
23/// wanted to use an environment type definition called `MyEnvironment` they
24/// issue the ink! smart contract as follows:
25///
26/// ```no_compile
27/// #[ink::contract(env = MyEnvironment)]
28/// ```
29///
30/// # Usage: Default Environment
31///
32/// ```
33/// #[ink::contract]
34/// pub mod contract {
35///     #[ink(storage)]
36///     pub struct Contract {}
37///
38///     impl Contract {
39///         #[ink(constructor)]
40///         pub fn constructor() -> Self {
41///             Self {}
42///         }
43///
44///         #[ink(message)]
45///         pub fn message(&self) {}
46///     }
47/// }
48///
49/// use contract::Contract;
50///
51/// # use ink::env::ContractEnv;
52/// # use ink::codegen::utils::IsSameType;
53///
54/// // The following line only compiles successfully if both
55/// // `ink_env::DefaultEnvironment` and `<Contract as ContractEnv>::Env`
56/// // are of the same type.
57/// const _: IsSameType<<Contract as ContractEnv>::Env> =
58///     <IsSameType<ink_env::DefaultEnvironment>>::new();
59/// ```
60///
61/// # Usage: Custom Environment
62///
63/// ```
64/// # use ink_env::{Environment, DefaultEnvironment};
65/// #[derive(Clone)]
66/// pub struct CustomEnvironment {}
67///
68/// impl Environment for CustomEnvironment {
69///     const NATIVE_TO_ETH_RATIO: u32 =
70///         <DefaultEnvironment as Environment>::NATIVE_TO_ETH_RATIO;
71///     const TRUST_BACKED_ASSETS_PRECOMPILE_INDEX: u16 =
72///         <DefaultEnvironment as Environment>::TRUST_BACKED_ASSETS_PRECOMPILE_INDEX;
73///     const POOL_ASSETS_PRECOMPILE_INDEX: u16 =
74///         <DefaultEnvironment as Environment>::POOL_ASSETS_PRECOMPILE_INDEX;
75///
76///     type AccountId = <DefaultEnvironment as Environment>::AccountId;
77///     type Balance = u64;
78///     type Hash = <DefaultEnvironment as Environment>::Hash;
79///     type BlockNumber = u32;
80///     type Timestamp = u64;
81///     type EventRecord = <DefaultEnvironment as Environment>::EventRecord;
82/// }
83///
84/// #[ink::contract(env = super::CustomEnvironment)]
85/// pub mod contract {
86///     #[ink(storage)]
87///     pub struct Contract {}
88///
89///     impl Contract {
90///         #[ink(constructor)]
91///         pub fn constructor() -> Self {
92///             Self {}
93///         }
94///
95///         #[ink(message)]
96///         pub fn message(&self) {}
97///     }
98/// }
99///
100/// use contract::Contract;
101/// # use ink::env::ContractEnv;
102/// # use ink::codegen::utils::IsSameType;
103///
104/// // The following line only compiles successfully if both
105/// // `CustomEnvironment` and `<Contract as ContractEnv>::Env`
106/// // are of the same type.
107/// const _: IsSameType<<Contract as ContractEnv>::Env> =
108///     <IsSameType<CustomEnvironment>>::new();
109///
110/// fn main() {}
111/// ```
112pub trait ContractEnv {
113    /// The environment type.
114    type Env: Environment;
115}
116
117/// Refers to the generated ink! smart contract reference type.
118///
119/// # Note
120///
121/// Given an ink! storage struct with identifier `Contract` the ink! codegen produces
122/// the ink! root type `Contract` and the ink! reference type `ContractRef`.
123///
124/// This trait exists so that users can avoid using a generated identifier to refer to
125/// the generated reference type of the ink! smart contract.
126///
127/// # Usage
128///
129/// ```
130/// #[ink::contract]
131/// pub mod contract {
132///     #[ink(storage)]
133///     pub struct Contract {}
134///
135///     impl Contract {
136///         #[ink(constructor)]
137///         pub fn constructor() -> Self {
138///             Self {}
139///         }
140///
141///         #[ink(message)]
142///         pub fn message(&self) {}
143///     }
144/// }
145///
146/// use contract::Contract;
147/// # use ink::codegen::utils::IsSameType;
148/// # use ink::env::ContractReference;
149///
150/// // The following line only compiles successfully if both
151/// // `ContractReference` and `<Contract as ContractReference>::Type`
152/// // are of the same type.
153/// const _: IsSameType<<Contract as ContractReference>::Type> =
154///     <IsSameType<ContractRef>>::new();
155/// ```
156pub trait ContractReference {
157    /// The generated contract reference type.
158    type Type;
159}
160
161/// Refers back to the original contract from the generated ink! smart contract
162/// reference type.
163pub trait ContractReverseReference {
164    /// The original contract type.
165    type Type;
166}