Skip to content

Commit

Permalink
feat: Added ability to select contract function from NEAR ABI functio…
Browse files Browse the repository at this point in the history
…ns (#314)
  • Loading branch information
FroVolod authored Mar 26, 2024
1 parent ae25ded commit 7fca81b
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 57 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ near-abi = "0.4.2"
zstd = "0.11"

keyring = "2.0.5"
interactive-clap = "0.2.8"
interactive-clap-derive = "0.2.8"
interactive-clap = "0.2.9"
interactive-clap-derive = "0.2.9"

[features]
default = ["ledger", "self-update"]
Expand Down
72 changes: 54 additions & 18 deletions src/commands/contract/call_function/as_read_only/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@ pub struct CallFunctionView {
#[interactive_clap(skip_default_input_arg)]
/// What is the contract account ID?
contract_account_id: crate::types::account_id::AccountId,
#[interactive_clap(flatten)]
/// Select function
function: Function,
}

#[derive(Clone)]
pub struct CallFunctionViewContext {
global_context: crate::GlobalContext,
contract_account_id: near_primitives::types::AccountId,
}

impl CallFunctionViewContext {
pub fn from_previous_context(
previous_context: crate::GlobalContext,
scope: &<CallFunctionView as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context,
contract_account_id: scope.contract_account_id.clone().into(),
})
}
}

impl CallFunctionView {
pub fn input_contract_account_id(
context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
crate::common::input_non_signer_account_id_from_used_account_list(
&context.config.credentials_home_dir,
"What is the contract account ID?",
)
}
}

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = CallFunctionViewContext)]
#[interactive_clap(output_context = FunctionContext)]
pub struct Function {
#[interactive_clap(skip_default_input_arg)]
/// What is the name of the function?
function_name: String,
#[interactive_clap(value_enum)]
Expand All @@ -24,17 +63,17 @@ pub struct CallFunctionView {
}

#[derive(Clone)]
pub struct CallFunctionViewContext(crate::network_view_at_block::ArgsForViewContext);
pub struct FunctionContext(crate::network_view_at_block::ArgsForViewContext);

impl CallFunctionViewContext {
impl FunctionContext {
pub fn from_previous_context(
previous_context: crate::GlobalContext,
scope: &<CallFunctionView as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
previous_context: CallFunctionViewContext,
scope: &<Function as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
let on_after_getting_block_reference_callback: crate::network_view_at_block::OnAfterGettingBlockReferenceCallback = std::sync::Arc::new({
let function_args = scope.function_args.clone();
let function_args_type = scope.function_args_type.clone();
let account_id: near_primitives::types::AccountId = scope.contract_account_id.clone().into();
let account_id: near_primitives::types::AccountId = previous_context.contract_account_id.clone();
let function_name = scope.function_name.clone();

move |network_config, block_reference| {
Expand Down Expand Up @@ -75,32 +114,29 @@ impl CallFunctionViewContext {
});

Ok(Self(crate::network_view_at_block::ArgsForViewContext {
config: previous_context.config,
interacting_with_account_ids: vec![scope.contract_account_id.clone().into()],
config: previous_context.global_context.config,
interacting_with_account_ids: vec![previous_context.contract_account_id],
on_after_getting_block_reference_callback,
}))
}
}

impl From<CallFunctionViewContext> for crate::network_view_at_block::ArgsForViewContext {
fn from(item: CallFunctionViewContext) -> Self {
impl From<FunctionContext> for crate::network_view_at_block::ArgsForViewContext {
fn from(item: FunctionContext) -> Self {
item.0
}
}

impl CallFunctionView {
impl Function {
fn input_function_args_type(
_context: &crate::GlobalContext,
_context: &CallFunctionViewContext,
) -> color_eyre::eyre::Result<Option<super::call_function_args_type::FunctionArgsType>> {
super::call_function_args_type::input_function_args_type()
}

pub fn input_contract_account_id(
context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
crate::common::input_non_signer_account_id_from_used_account_list(
&context.config.credentials_home_dir,
"What is the contract account ID?",
)
fn input_function_name(
context: &CallFunctionViewContext,
) -> color_eyre::eyre::Result<Option<String>> {
super::input_view_function_name(&context.global_context, &context.contract_account_id)
}
}
100 changes: 68 additions & 32 deletions src/commands/contract/call_function/as_transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,50 @@ use inquire::CustomType;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = crate::GlobalContext)]
#[interactive_clap(output_context = CallFunctionPropertiesContext)]
pub struct CallFunctionProperties {
#[interactive_clap(output_context = CallFunctionContext)]
pub struct CallFunction {
#[interactive_clap(skip_default_input_arg)]
/// What is the contract account ID?
contract_account_id: crate::types::account_id::AccountId,
#[interactive_clap(flatten)]
/// Select function
function: Function,
}

#[derive(Debug, Clone)]
pub struct CallFunctionContext {
global_context: crate::GlobalContext,
contract_account_id: near_primitives::types::AccountId,
}

impl CallFunctionContext {
pub fn from_previous_context(
previous_context: crate::GlobalContext,
scope: &<CallFunction as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context,
contract_account_id: scope.contract_account_id.clone().into(),
})
}
}

impl CallFunction {
pub fn input_contract_account_id(
context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
crate::common::input_non_signer_account_id_from_used_account_list(
&context.config.credentials_home_dir,
"What is the contract account ID?",
)
}
}

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = CallFunctionContext)]
#[interactive_clap(output_context = FunctionContext)]
pub struct Function {
#[interactive_clap(skip_default_input_arg)]
/// What is the name of the function?
function_name: String,
#[interactive_clap(value_enum)]
Expand All @@ -20,51 +59,48 @@ pub struct CallFunctionProperties {
prepaid_gas: PrepaidGas,
}

#[derive(Debug, Clone)]
pub struct CallFunctionPropertiesContext {
#[derive(Clone)]
pub struct FunctionContext {
global_context: crate::GlobalContext,
receiver_account_id: near_primitives::types::AccountId,
contract_account_id: near_primitives::types::AccountId,
function_name: String,
function_args: Vec<u8>,
}

impl CallFunctionPropertiesContext {
impl FunctionContext {
pub fn from_previous_context(
previous_context: crate::GlobalContext,
scope: &<CallFunctionProperties as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
previous_context: CallFunctionContext,
scope: &<Function as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
let function_args = super::call_function_args_type::function_args(
scope.function_args.clone(),
scope.function_args_type.clone(),
)?;
Ok(Self {
global_context: previous_context,
receiver_account_id: scope.contract_account_id.clone().into(),
global_context: previous_context.global_context,
contract_account_id: previous_context.contract_account_id,
function_name: scope.function_name.clone(),
function_args,
})
}
}

impl CallFunctionProperties {
pub fn input_contract_account_id(
context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
crate::common::input_non_signer_account_id_from_used_account_list(
&context.config.credentials_home_dir,
"What is the contract account ID?",
)
}

impl Function {
fn input_function_args_type(
_context: &crate::GlobalContext,
_context: &CallFunctionContext,
) -> color_eyre::eyre::Result<Option<super::call_function_args_type::FunctionArgsType>> {
super::call_function_args_type::input_function_args_type()
}

fn input_function_name(
context: &CallFunctionContext,
) -> color_eyre::eyre::Result<Option<String>> {
super::input_call_function_name(&context.global_context, &context.contract_account_id)
}
}

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = CallFunctionPropertiesContext)]
#[interactive_clap(input_context = FunctionContext)]
#[interactive_clap(output_context = PrepaidGasContext)]
pub struct PrepaidGas {
#[interactive_clap(skip_default_input_arg)]
Expand All @@ -78,20 +114,20 @@ pub struct PrepaidGas {
#[derive(Debug, Clone)]
pub struct PrepaidGasContext {
global_context: crate::GlobalContext,
receiver_account_id: near_primitives::types::AccountId,
contract_account_id: near_primitives::types::AccountId,
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
}

impl PrepaidGasContext {
pub fn from_previous_context(
previous_context: CallFunctionPropertiesContext,
previous_context: FunctionContext,
scope: &<PrepaidGas as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
contract_account_id: previous_context.contract_account_id,
function_name: previous_context.function_name,
function_args: previous_context.function_args,
gas: scope.gas,
Expand All @@ -101,7 +137,7 @@ impl PrepaidGasContext {

impl PrepaidGas {
fn input_gas(
_context: &CallFunctionPropertiesContext,
_context: &FunctionContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearGas>> {
eprintln!();
Ok(Some(
Expand Down Expand Up @@ -138,7 +174,7 @@ pub struct Deposit {
#[derive(Debug, Clone)]
pub struct DepositContext {
global_context: crate::GlobalContext,
receiver_account_id: near_primitives::types::AccountId,
contract_account_id: near_primitives::types::AccountId,
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
Expand All @@ -152,7 +188,7 @@ impl DepositContext {
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
contract_account_id: previous_context.contract_account_id,
function_name: previous_context.function_name,
function_args: previous_context.function_args,
gas: previous_context.gas,
Expand Down Expand Up @@ -189,7 +225,7 @@ pub struct SignerAccountId {
#[derive(Debug, Clone)]
pub struct SignerAccountIdContext {
global_context: crate::GlobalContext,
receiver_account_id: near_primitives::types::AccountId,
contract_account_id: near_primitives::types::AccountId,
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
Expand All @@ -204,7 +240,7 @@ impl SignerAccountIdContext {
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context.global_context,
receiver_account_id: previous_context.receiver_account_id,
contract_account_id: previous_context.contract_account_id,
function_name: previous_context.function_name,
function_args: previous_context.function_args,
gas: previous_context.gas,
Expand All @@ -219,7 +255,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new({
let signer_account_id = item.signer_account_id.clone();
let receiver_account_id = item.receiver_account_id.clone();
let receiver_account_id = item.contract_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
Expand All @@ -239,7 +275,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.signer_account_id, item.receiver_account_id],
interacting_with_account_ids: vec![item.signer_account_id, item.contract_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
Loading

0 comments on commit 7fca81b

Please sign in to comment.