Skip to main content

multiversx_sc/abi/
endpoint_abi.rs

1use super::*;
2use alloc::{
3    borrow::ToOwned,
4    string::{String, ToString},
5    vec::Vec,
6};
7
8#[derive(Clone, Debug)]
9pub struct InputAbi {
10    pub arg_name: String,
11    pub type_names: TypeNames,
12    pub multi_arg: bool,
13}
14
15#[derive(Clone, Debug)]
16pub struct OutputAbi {
17    pub output_name: String,
18    pub type_names: TypeNames,
19    pub multi_result: bool,
20}
21
22pub type OutputAbis = Vec<OutputAbi>;
23
24#[derive(Clone, Default, Debug)]
25pub enum EndpointMutabilityAbi {
26    #[default]
27    Mutable,
28    Readonly,
29    Pure,
30}
31
32#[derive(Clone, Default, Debug)]
33pub enum EndpointTypeAbi {
34    #[default]
35    Init,
36    Upgrade,
37    Endpoint,
38    PromisesCallback,
39}
40
41#[derive(Clone, Default, Debug)]
42pub struct EndpointAbi {
43    pub docs: Vec<String>,
44    pub name: String,
45    pub rust_method_name: String,
46    pub title: Option<String>,
47    pub only_owner: bool,
48    pub only_admin: bool,
49    pub labels: Vec<String>,
50    pub endpoint_type: EndpointTypeAbi,
51    pub mutability: EndpointMutabilityAbi,
52    pub payable_in_tokens: Vec<String>,
53    pub inputs: Vec<InputAbi>,
54    pub outputs: OutputAbis,
55    pub allow_multiple_var_args: bool,
56}
57
58impl EndpointAbi {
59    /// Used in code generation.
60    pub fn new(
61        name: &str,
62        rust_method_name: &str,
63        mutability: EndpointMutabilityAbi,
64        endpoint_type: EndpointTypeAbi,
65    ) -> Self {
66        EndpointAbi {
67            docs: Vec::new(),
68            name: name.to_string(),
69            rust_method_name: rust_method_name.to_string(),
70            only_owner: false,
71            only_admin: false,
72            labels: Vec::new(),
73            endpoint_type,
74            mutability,
75            payable_in_tokens: Vec::new(),
76            title: None,
77            inputs: Vec::new(),
78            outputs: Vec::new(),
79            allow_multiple_var_args: false,
80        }
81    }
82
83    pub fn with_docs(mut self, doc_line: &str) -> Self {
84        self.docs.push(doc_line.to_owned());
85        self
86    }
87
88    pub fn with_title(mut self, title: &str) -> Self {
89        self.title = Some(title.to_owned());
90        self
91    }
92
93    pub fn with_only_owner(mut self) -> Self {
94        self.only_owner = true;
95        self
96    }
97
98    pub fn with_only_admin(mut self) -> Self {
99        self.only_admin = true;
100        self
101    }
102
103    pub fn with_allow_multiple_var_args(mut self) -> Self {
104        self.allow_multiple_var_args = true;
105        self
106    }
107
108    pub fn with_label(mut self, label: &str) -> Self {
109        self.labels.push(label.to_owned());
110        self
111    }
112
113    pub fn with_payable_token(mut self, token: &str) -> Self {
114        self.payable_in_tokens.push(token.to_owned());
115        self
116    }
117
118    pub fn add_input<T: TypeAbi>(&mut self, arg_name: &str) {
119        self.inputs.push(InputAbi {
120            arg_name: arg_name.to_string(),
121            type_names: T::type_names(),
122            multi_arg: T::is_variadic(),
123        });
124    }
125
126    pub fn add_output<T: TypeAbi>(&mut self, output_names: &[&'static str]) {
127        self.outputs
128            .extend_from_slice(T::output_abis(output_names).as_slice());
129    }
130
131    pub fn endpoint_with_name_and_labels(
132        name: &'static str,
133        labels: &'static [&'static str],
134    ) -> Self {
135        EndpointAbi {
136            name: name.to_string(),
137            labels: labels.iter().map(|s| s.to_string()).collect(),
138            endpoint_type: EndpointTypeAbi::Endpoint,
139            ..Default::default()
140        }
141    }
142}