openbrush_contracts/traits/diamond/diamond.rs
1// Copyright (c) 2012-2022 Supercolony
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the"Software"),
5// to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22pub use crate::traits::{
23 errors::{
24 DiamondError,
25 OwnableError,
26 },
27 ownable::*,
28};
29use ink::prelude::vec::Vec;
30use openbrush::traits::Hash;
31
32#[openbrush::wrapper]
33pub type DiamondRef = dyn Diamond;
34
35pub type Selector = [u8; 4];
36
37/// Struct which we use to initialize/update/remove a facet in the diamond
38#[derive(Default, Debug, Clone, PartialEq, scale::Encode, scale::Decode)]
39#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
40pub struct FacetCut {
41 /// The `hash` of the code that should be executed.
42 pub hash: Hash,
43 /// The selector bytes that identify the function that should be called.
44 pub selectors: Vec<Selector>,
45}
46
47/// Struct which we use to initialize the diamond contract
48#[derive(Default, Debug, Clone, PartialEq, scale::Encode, scale::Decode)]
49#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
50pub struct InitCall {
51 /// The `hash` of the code that should be executed.
52 pub hash: Hash,
53 /// The selector bytes that identify the function that should be called.
54 pub selector: Selector,
55 /// The SCALE encoded parameters that are passed to the called function.
56 pub input: Vec<u8>,
57}
58
59/// Trait to be implemented in the contract which holds the diamond storage
60#[openbrush::trait_definition]
61pub trait Diamond {
62 /// This function is used to add, replace and remove facets from the diamond
63 ///
64 /// `cuts` vector of facet cuts, each cut contains the code hash of the facet
65 /// as well as the selectors of functions.
66 /// If `cuts` is empty, we will remove this facet from diamond
67 /// If `cuts` contains a selector which already exists for a different facet we will return an error (user should remove this facet first)
68 /// If `cuts` does not contain some selectors which are already registered for this facet, those selectors will be removed from diamond
69 /// `init` optional struct which identifies a call to be executed, this struct contains the code hash
70 /// of the executed contract, selector of the executed function and input data to be passed to the called
71 #[ink(message)]
72 fn diamond_cut(&mut self, cuts: Vec<FacetCut>, init: Option<InitCall>) -> Result<(), DiamondError>;
73}