sb_starknet_abigen_parser/
cairo_function.rs

1use starknet::core::types::contract::{AbiNamedMember, AbiOutput, StateMutability};
2
3use super::abi_types::AbiTypeAny;
4
5#[derive(Debug, Clone)]
6pub struct CairoFunction {
7    pub name: String,
8    pub state_mutability: StateMutability,
9    pub inputs: Vec<(String, AbiTypeAny)>,
10    // For now, only one output type is supported (or none).
11    // TODO: investigate the cases where more than one output is
12    // present in the ABI.
13    pub output: Option<AbiTypeAny>,
14}
15
16impl CairoFunction {
17    /// Initializes a new instance from the abi name and it's members.
18    pub fn new(
19        abi_name: &str,
20        state_mutability: StateMutability,
21        inputs: &[AbiNamedMember],
22        outputs: &Vec<AbiOutput>,
23    ) -> CairoFunction {
24        let name = abi_name.to_string();
25
26        let output = if !outputs.is_empty() {
27            // For now, only first output is considered.
28            // TODO: investigate when we can have several outputs.
29            Some(AbiTypeAny::from_string(&outputs[0].r#type))
30        } else {
31            None
32        };
33
34        let inputs = inputs
35            .iter()
36            .map(|i| (i.name.clone(), AbiTypeAny::from_string(&i.r#type)))
37            .collect();
38
39        CairoFunction {
40            name,
41            state_mutability,
42            inputs,
43            output,
44        }
45    }
46}