odra_types/
contract_def.rs

1//! Encapsulates a set of structures that abstract out a smart contract layout.
2
3use alloc::{string::String, vec::Vec};
4use casper_types::CLType;
5
6/// Contract's entrypoint.
7#[derive(Debug, Clone)]
8pub struct Entrypoint {
9    pub ident: String,
10    pub args: Vec<Argument>,
11    pub is_mut: bool,
12    pub ret: CLType,
13    pub ty: EntrypointType
14}
15
16/// Defines an argument passed to an entrypoint.
17#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
18pub struct Argument {
19    pub ident: String,
20    pub ty: CLType,
21    pub is_ref: bool,
22    pub is_slice: bool
23}
24
25/// Defines an entrypoint type.
26#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
27pub enum EntrypointType {
28    /// A special entrypoint that can be called just once on the contract initialization.
29    Constructor { non_reentrant: bool },
30    /// A regular entrypoint.
31    Public { non_reentrant: bool },
32    /// A payable entrypoint.
33    PublicPayable { non_reentrant: bool }
34}
35
36impl EntrypointType {
37    pub fn is_non_reentrant(&self) -> bool {
38        match self {
39            EntrypointType::Constructor { non_reentrant } => *non_reentrant,
40            EntrypointType::Public { non_reentrant } => *non_reentrant,
41            EntrypointType::PublicPayable { non_reentrant } => *non_reentrant
42        }
43    }
44}
45
46/// Defines an event.
47#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
48pub struct Event {
49    pub ident: String,
50    pub args: Vec<Argument>
51}
52
53impl Event {
54    pub fn has_any(&self) -> bool {
55        self.args.iter().any(|arg| arg.ty == CLType::Any)
56    }
57}
58
59/// A trait that should be implemented by each smart contract to allow the backend
60/// to generate blockchain-specific code.
61pub trait HasEntrypoints {
62    /// Returns an abstract contract definition.
63    fn entrypoints() -> Vec<Entrypoint>;
64}
65
66/// A trait that should be implemented by each smart contract to allow the backend.
67pub trait HasIdent {
68    fn ident() -> String;
69}
70/// A trait that should be implemented by each smart contract to allow the backend.
71pub trait HasEvents {
72    fn events() -> Vec<Event>;
73}
74
75pub trait Node {
76    const IS_LEAF: bool = true;
77    const COUNT: u32;
78
79    fn __keys() -> Vec<String> {
80        Vec::new()
81    }
82}
83
84#[derive(Debug, Clone)]
85pub struct ContractBlueprint {
86    pub keys: Vec<String>,
87    pub keys_count: u32,
88    pub events: Vec<Event>,
89    pub entrypoints: Vec<Entrypoint>,
90    pub fqn: &'static str
91}
92
93#[cfg(test)]
94#[allow(dead_code)]
95mod test {
96    use super::Node;
97    use alloc::{string::String, vec, vec::Vec};
98    use core::marker::PhantomData;
99
100    #[test]
101    fn key_collection_works() {
102        struct Variable<T> {
103            ty: PhantomData<T>
104        }
105
106        impl<T> Node for Variable<T> {
107            const COUNT: u32 = 1;
108        }
109
110        struct Contract {
111            pub value: Variable<u32>,
112            pub submodule: Submodule
113        }
114
115        impl Node for Contract {
116            const COUNT: u32 = <Variable<u32> as Node>::COUNT + <Submodule as Node>::COUNT;
117            const IS_LEAF: bool = false;
118
119            fn __keys() -> Vec<String> {
120                let mut result = vec![];
121                // The same would be generated by the [odra::module] macro.
122                if <Variable<u32> as Node>::IS_LEAF {
123                    result.push(String::from("value"));
124                } else {
125                    result.extend(
126                        <Variable<u32> as Node>::__keys()
127                            .iter()
128                            .map(|k| odra_utils::create_key("value", k))
129                    )
130                }
131                if <Submodule as Node>::IS_LEAF {
132                    result.push(String::from("submodule"));
133                } else {
134                    result.extend(
135                        <Submodule as Node>::__keys()
136                            .iter()
137                            .map(|k| odra_utils::create_key("submodule", k))
138                    )
139                }
140                result
141            }
142        }
143
144        struct Submodule {
145            abc: Variable<u32>
146        }
147
148        impl Node for Submodule {
149            const COUNT: u32 = <Variable<u32> as Node>::COUNT;
150            const IS_LEAF: bool = false;
151
152            fn __keys() -> Vec<String> {
153                let mut result = vec![];
154                if <Variable<u32> as Node>::IS_LEAF {
155                    result.push(String::from("abc"));
156                } else {
157                    result.extend(
158                        <Variable<u32> as Node>::__keys()
159                            .iter()
160                            .map(|k| odra_utils::create_key("abc", k))
161                    )
162                }
163                result
164            }
165        }
166
167        assert_eq!(Contract::__keys(), ["value", "submodule#abc"])
168    }
169}