8000 feat: deploy and use global contracts from UI excluding action builder. by akorchyn · Pull Request #456 · near/near-cli-rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: deploy and use global contracts from UI excluding action builder. #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use inquire::CustomType;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::super::ContractFileContext)]
#[interactive_clap(input_context = super::super::GenericDeployContext)]
#[interactive_clap(output_context = CallFunctionActionContext)]
pub struct CallFunctionAction {
/// What is the name of the function?
Expand All @@ -23,14 +23,14 @@ pub struct CallFunctionActionContext {
global_context: crate::GlobalContext,
receiver_account_id: near_primitives::types::AccountId,
signer_account_id: near_primitives::types::AccountId,
code: Vec<u8>,
deploy_action: near_primitives::transaction::Action,
function_name: String,
function_args: Vec<u8>,
}

impl CallFunctionActionContext {
pub fn from_previous_context(
previous_context: super::super::ContractFileContext,
previous_context: super::super::GenericDeployContext,
scope: &<CallFunctionAction as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
let function_args =
Expand All @@ -42,7 +42,7 @@ impl CallFunctionActionContext {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
signer_account_id: previous_context.signer_account_id,
code: previous_context.code,
deploy_action: previous_context.deploy_action,
function_name: scope.function_name.clone(),
function_args,
})
Expand All @@ -51,7 +51,7 @@ impl CallFunctionActionContext {

impl CallFunctionAction {
fn input_function_args_type(
_context: &super::super::ContractFileContext,
_context: &super::super::GenericDeployContext,
) -> color_eyre::eyre::Result<
Option<super::super::super::call_function::call_function_args_type::FunctionArgsType>,
> {
Expand All @@ -76,7 +76,7 @@ pub struct PrepaidGasContext {
global_context: crate::GlobalContext,
receiver_account_id: near_primitives::types::AccountId,
signer_account_id: near_primitives::types::AccountId,
code: Vec<u8>,
deploy_action: near_primitives::transaction::Action,
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
Expand All @@ -91,7 +91,7 @@ impl PrepaidGasContext {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
signer_account_id: previous_context.signer_account_id,
code: previous_context.code,
deploy_action: previous_context.deploy_action,
function_name: previous_context.function_name,
function_args: previous_context.function_args,
gas: scope.gas,
Expand Down Expand Up @@ -155,11 +155,7 @@ impl DepositContext {
signer_id: signer_account_id.clone(),
receiver_id: receiver_account_id.clone(),
actions: vec![
near_primitives::transaction::Action::DeployContract(
near_primitives::transaction::DeployContractAction {
code: previous_context.code.clone(),
},
),
previous_context.deploy_action.clone(),
near_primitives::transaction::Action::FunctionCall(Box::new(
near_primitives::transaction::FunctionCallAction {
method_name: previous_context.function_name.clone(),
Expand Down
18 changes: 7 additions & 11 deletions src/commands/contract/deploy/initialize_mode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage};
mod call_function_type;

#[derive(Debug, Clone, EnumDiscriminants, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(context = super::ContractFileContext)]
#[interactive_clap(context = super::GenericDeployContext)]
#[strum_discriminants(derive(EnumMessage, EnumIter))]
/// Select the need for initialization:
pub enum InitializeMode {
Expand All @@ -16,7 +16,7 @@ pub enum InitializeMode {
}

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::ContractFileContext)]
#[interactive_clap(input_context = super::GenericDeployContext)]
#[interactive_clap(output_context = NoInitializeContext)]
pub struct NoInitialize {
#[interactive_clap(named_arg)]
Expand All @@ -25,18 +25,18 @@ pub struct NoInitialize {
}

#[derive(Debug, Clone)]
pub struct NoInitializeContext(super::ContractFileContext);
pub struct NoInitializeContext(super::GenericDeployContext);

impl NoInitializeContext {
pub fn from_previous_context(
previous_context: super::ContractFileContext,
previous_context: super::GenericDeployContext,
_scope: &<NoInitialize as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self(super::ContractFileContext {
Ok(Self(super::GenericDeployContext {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
signer_account_id: previous_context.signer_account_id,
code: previous_context.code,
deploy_action: previous_context.deploy_action,
}))
}
}
Expand All @@ -52,11 +52,7 @@ impl From<NoInitializeContext> for crate::commands::ActionContext {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: receiver_account_id.clone(),
actions: vec![near_primitives::transaction::Action::DeployContract(
near_primitives::transaction::DeployContractAction {
code: item.0.code.clone(),
},
)],
actions: vec![item.0.deploy_action.clone()],
})
}
});
Expand Down
150 changes: 138 additions & 12 deletions src/commands/contract/deploy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use color_eyre::eyre::Context;
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

pub mod initialize_mode;

Expand All @@ -9,9 +10,34 @@ pub struct Contract {
#[interactive_clap(skip_default_input_arg)]
/// What is the contract account ID?
account_id: crate::types::account_id::AccountId,
#[interactive_clap(named_arg)]
/// Specify a path to wasm file
use_file: ContractFile,
#[interactive_clap(subcommand)]
/// Specify a deploy mode
deploy_mode: DeployModes,
}

#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = ContractContext)]
#[strum_discriminants(derive(EnumMessage, EnumIter))]
#[non_exhaustive]
/// Choose a contract action:
pub enum DeployModes {
#[strum_discriminants(strum(
message = "use-file - Deploy a contract using a wasm file"
))]
/// Deploy a contract using a wasm file
UseFile(ContractFile),

#[strum_discriminants(strum(
message = "use-global-hash - Deploy a contract using a global contract code hash"
))]
/// Deploy a contract using a global contract code hash
UseGlobalHash(ContractHash),

#[strum_discriminants(strum(
message = "use-global-account-id - Deploy a contract using an account ID"
))]
/// Deploy a contract using an global contract account ID
UseGlobalAccountId(ContractAccountId),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -45,6 +71,14 @@ impl Contract {
}
}

#[derive(Debug, Clone)]
pub struct GenericDeployContext {
pub global_context: crate::GlobalContext,
pub receiver_account_id: near_primitives::types::AccountId,
pub signer_account_id: near_primitives::types::AccountId,
pub deploy_action: near_primitives::transaction::Action,
}

#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(input_context = ContractContext)]
#[interactive_clap(output_context = ContractFileContext)]
Expand All @@ -55,13 +89,7 @@ pub struct ContractFile {
initialize: self::initialize_mode::InitializeMode,
}

#[derive(Debug, Clone)]
pub struct ContractFileContext {
pub global_context: crate::GlobalContext,
pub receiver_account_id: near_primitives::types::AccountId,
pub signer_account_id: near_primitives::types::AccountId,
pub code: Vec<u8>,
}
pub struct ContractFileContext(GenericDeployContext);

impl ContractFileContext {
pub fn from_previous_context(
Expand All @@ -71,11 +99,109 @@ impl ContractFileContext {
let code = std::fs::read(&scope.file_path).wrap_err_with(|| {
format!("Failed to open or read the file: {:?}.", &scope.file_path.0,)
})?;
Ok(Self {
Ok(Self(GenericDeployContext {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
signer_account_id: previous_context.signer_account_id,
code,
deploy_action: near_primitives::transaction::Action::DeployContract(
near_primitives::action::DeployContractAction { code },
),
}))
}
}

impl From<ContractFileContext> for GenericDeployContext {
fn from(item: ContractFileContext) -> Self {
item.0
}
}

#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(input_context = ContractContext)]
#[interactive_clap(output_context = ContractHashContext)]
pub struct ContractHash {
/// What is a global contract code hash?
pub hash: crate::types::crypto_hash::CryptoHash,
#[interactive_clap(subcommand)]
initialize: self::initialize_mode::InitializeMode,
}

pub struct ContractHashContext(GenericDeployContext);

impl ContractHashContext {
pub fn from_previous_context(
previous_context: ContractContext,
scope: &<ContractHash as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self(GenericDeployContext {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
signer_account_id: previous_context.signer_account_id,
deploy_action: near_primitives::transaction::Action::UseGlobalContract(Box::new(
near_primitives::action::UseGlobalContractAction {
contract_identifier:
near_primitives::action::GlobalContractIdentifier::CodeHash(
scope.hash.into(),
),
},
)),
}))
}
}

impl From<ContractHashContext> for GenericDeployContext {
fn from(item: ContractHashContext) -> Self {
item.0
}
}

#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(input_context = ContractContext)]
#[interactive_clap(output_context = ContractAccountIdContext)]
pub struct ContractAccountId {
#[interactive_clap(skip_default_input_arg)]
/// What is a global contract account ID?
pub global_contract_account_id: crate::types::account_id::AccountId,
#[interactive_clap(subcommand)]
initialize: self::initialize_mode::InitializeMode,
}

impl ContractAccountId {
pub fn input_global_contract_account_id(
context: &ContractContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
crate::common::input_non_signer_account_id_from_used_account_list(
&context.global_context.config.credentials_home_dir,
"What is a global contract account ID?",
)
}
}

pub struct ContractAccountIdContext(GenericDeployContext);

impl ContractAccountIdContext {
pub fn from_previous_context(
previous_context: ContractContext,
scope: &<ContractAccountId as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<GenericDeployContext> {
Ok(GenericDeployContext {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
signer_account_id: previous_context.signer_account_id,
deploy_action: near_primitives::transaction::Action::UseGlobalContract(Box::new(
near_primitives::action::UseGlobalContractAction {
contract_identifier:
near_primitives::action::GlobalContractIdentifier::AccountId(
scope.global_contract_account_id.clone().into(),
),
},
)),
})
}
}

impl From<ContractAccountIdContext> for GenericDeployContext {
fn from(item: ContractAccountIdContext) -> Self {
item.0
}
}
Loading
Loading
0