unc_cli_rs/commands/contract/deploy/
mod.rs

1use color_eyre::eyre::Context;
2
3pub mod initialize_mode;
4
5#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
6#[interactive_clap(input_context = crate::GlobalContext)]
7#[interactive_clap(output_context = ContractContext)]
8pub struct Contract {
9    #[interactive_clap(skip_default_input_arg)]
10    /// What is the contract account ID?
11    account_id: crate::types::account_id::AccountId,
12    #[interactive_clap(named_arg)]
13    /// Specify a path to wasm file
14    use_file: ContractFile,
15}
16
17#[derive(Debug, Clone)]
18pub struct ContractContext {
19    global_context: crate::GlobalContext,
20    receiver_account_id: unc_primitives::types::AccountId,
21    signer_account_id: unc_primitives::types::AccountId,
22}
23
24impl ContractContext {
25    pub fn from_previous_context(
26        previous_context: crate::GlobalContext,
27        scope: &<Contract as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
28    ) -> color_eyre::eyre::Result<Self> {
29        Ok(Self {
30            global_context: previous_context,
31            receiver_account_id: scope.account_id.clone().into(),
32            signer_account_id: scope.account_id.clone().into(),
33        })
34    }
35}
36
37impl Contract {
38    pub fn input_account_id(
39        context: &crate::GlobalContext,
40    ) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
41        crate::common::input_signer_account_id_from_used_account_list(
42            &context.config.credentials_home_dir,
43            "What is the contract account ID?",
44        )
45    }
46}
47
48#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
49#[interactive_clap(input_context = ContractContext)]
50#[interactive_clap(output_context = ContractFileContext)]
51pub struct ContractFile {
52    /// What is a file location of the contract?
53    pub file_path: crate::types::path_buf::PathBuf,
54    #[interactive_clap(subcommand)]
55    initialize: self::initialize_mode::InitializeMode,
56}
57
58#[derive(Debug, Clone)]
59pub struct ContractFileContext {
60    pub global_context: crate::GlobalContext,
61    pub receiver_account_id: unc_primitives::types::AccountId,
62    pub signer_account_id: unc_primitives::types::AccountId,
63    pub code: Vec<u8>,
64}
65
66impl ContractFileContext {
67    pub fn from_previous_context(
68        previous_context: ContractContext,
69        scope: &<ContractFile as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
70    ) -> color_eyre::eyre::Result<Self> {
71        let code = std::fs::read(&scope.file_path).wrap_err_with(|| {
72            format!("Failed to open or read the file: {:?}.", &scope.file_path.0,)
73        })?;
74        Ok(Self {
75            global_context: previous_context.global_context,
76            receiver_account_id: previous_context.receiver_account_id,
77            signer_account_id: previous_context.signer_account_id,
78            code,
79        })
80    }
81}