1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use inquire::{Select, Text};
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod as_read_only;
mod as_transaction;
pub mod call_function_args_type;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = crate::GlobalContext)]
pub struct CallFunctionCommands {
    #[interactive_clap(subcommand)]
    function_call_actions: CallFunctionActions,
}

#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = crate::GlobalContext)]
#[strum_discriminants(derive(EnumMessage, EnumIter))]
/// Сhoose action for account:
pub enum CallFunctionActions {
    #[strum_discriminants(strum(message = "as-read-only    - Calling a view method"))]
    /// Calling a view method
    AsReadOnly(self::as_read_only::CallFunctionView),
    #[strum_discriminants(strum(message = "as-transaction  - Calling a change method"))]
    /// Calling a change method
    AsTransaction(self::as_transaction::CallFunction),
}

pub fn input_call_function_name(
    global_context: &crate::GlobalContext,
    contract_account_id: &near_primitives::types::AccountId,
) -> color_eyre::eyre::Result<Option<String>> {
    input_function_name(
        global_context,
        contract_account_id,
        near_abi::AbiFunctionKind::Call,
        "Select the as-transaction function for your contract:",
    )
}

pub fn input_view_function_name(
    global_context: &crate::GlobalContext,
    contract_account_id: &near_primitives::types::AccountId,
) -> color_eyre::eyre::Result<Option<String>> {
    input_function_name(
        global_context,
        contract_account_id,
        near_abi::AbiFunctionKind::View,
        "Select the viewing function for your contract:",
    )
}

fn input_function_name(
    global_context: &crate::GlobalContext,
    contract_account_id: &near_primitives::types::AccountId,
    function_kind: near_abi::AbiFunctionKind,
    message: &str,
) -> color_eyre::eyre::Result<Option<String>> {
    let network_config = crate::common::find_network_where_account_exist(
        global_context,
        contract_account_id.clone(),
    );

    if let Some(network_config) = network_config {
        let json_rpc_client = network_config.json_rpc_client();
        if let Ok(contract_abi) =
            tokio::runtime::Runtime::new()
                .unwrap()
                .block_on(super::inspect::get_contract_abi(
                    &json_rpc_client,
                    &near_primitives::types::Finality::Final.into(),
                    contract_account_id,
                ))
        {
            let function_names = contract_abi
                .body
                .functions
                .into_iter()
                .filter(|function| function_kind == function.kind)
                .map(|function| function.name)
                .collect::<Vec<String>>();
            if !function_names.is_empty() {
                return Ok(Some(
                    Select::new(message, function_names).prompt()?.to_string(),
                ));
            }
        }
    }

    Ok(Some(
        Text::new("What is the name of the function?").prompt()?,
    ))
}