Skip to content

Commit

Permalink
feat: Added loading indicators for "tokens" group commands (near#355)
Browse files Browse the repository at this point in the history
Co-authored-by: FroVolod <[email protected]>
  • Loading branch information
FroVolod and FroVolod authored Jun 21, 2024
1 parent dc6b82c commit fdb92bf
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 81 deletions.
126 changes: 74 additions & 52 deletions src/commands/tokens/send_ft/amount_ft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,60 +187,17 @@ impl DepositContext {
let receiver_account_id = previous_context.receiver_account_id.clone();
let deposit = scope.deposit;
let amount_ft = previous_context.amount_ft.clone();
let action_ft_transfer = near_primitives::transaction::Action::FunctionCall(
Box::new(near_primitives::transaction::FunctionCallAction {
method_name: "ft_transfer".to_string(),
args: serde_json::to_vec(&json!({
"receiver_id": receiver_account_id.to_string(),
"amount": amount_ft.amount().to_string()
}))?,
gas: previous_context.gas.as_gas(),
deposit: deposit.as_yoctonear(),
}),
);

move |network_config| {
let args = serde_json::to_vec(&json!({
"account_id": receiver_account_id.to_string(),
}))?;
let call_result = network_config
.json_rpc_client()
.blocking_call_view_function(
&ft_contract_account_id,
"storage_balance_of",
args.clone(),
near_primitives::types::Finality::Final.into(),
)
.wrap_err_with(||{
format!("Failed to fetch query for view method: 'storage_balance_of' (contract <{}> on network <{}>)",
ft_contract_account_id,
network_config.network_name
)
})?;

if call_result.parse_result_from_json::<Value>()?.is_null() {
let action_storage_deposit =
near_primitives::transaction::Action::FunctionCall(Box::new(
near_primitives::transaction::FunctionCallAction {
method_name: "storage_deposit".to_string(),
args,
gas: previous_context.gas.as_gas(),
deposit: near_token::NearToken::from_millinear(100)
.as_yoctonear(),
},
));
return Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: ft_contract_account_id.clone(),
actions: vec![action_storage_deposit, action_ft_transfer.clone()],
});
}

Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: ft_contract_account_id.clone(),
actions: vec![action_ft_transfer.clone()],
})
get_prepopulated_transaction(
network_config,
&ft_contract_account_id,
&receiver_account_id,
&signer_account_id,
&amount_ft,
&deposit,
&previous_context.gas
)
}
});

Expand Down Expand Up @@ -297,3 +254,68 @@ impl Deposit {
))
}
}

#[tracing::instrument(
name = "Creating a pre-populated transaction for signature ...",
skip_all
)]
fn get_prepopulated_transaction(
network_config: &crate::config::NetworkConfig,
ft_contract_account_id: &near_primitives::types::AccountId,
receiver_account_id: &near_primitives::types::AccountId,
signer_id: &near_primitives::types::AccountId,
amount_ft: &crate::types::ft_properties::FungibleToken,
deposit: &crate::types::near_token::NearToken,
gas: &crate::common::NearGas,
) -> color_eyre::eyre::Result<crate::commands::PrepopulatedTransaction> {
let action_ft_transfer = near_primitives::transaction::Action::FunctionCall(Box::new(
near_primitives::transaction::FunctionCallAction {
method_name: "ft_transfer".to_string(),
args: serde_json::to_vec(&json!({
"receiver_id": receiver_account_id.to_string(),
"amount": amount_ft.amount().to_string()
}))?,
gas: gas.as_gas(),
deposit: deposit.as_yoctonear(),
},
));

let args = serde_json::to_vec(&json!({"account_id": receiver_account_id.to_string()}))?;

let call_result = network_config
.json_rpc_client()
.blocking_call_view_function(
ft_contract_account_id,
"storage_balance_of",
args.clone(),
near_primitives::types::Finality::Final.into(),
)
.wrap_err_with(||{
format!("Failed to fetch query for view method: 'storage_balance_of' (contract <{}> on network <{}>)",
ft_contract_account_id,
network_config.network_name
)
})?;

if call_result.parse_result_from_json::<Value>()?.is_null() {
let action_storage_deposit = near_primitives::transaction::Action::FunctionCall(Box::new(
near_primitives::transaction::FunctionCallAction {
method_name: "storage_deposit".to_string(),
args,
gas: gas.as_gas(),
deposit: near_token::NearToken::from_millinear(100).as_yoctonear(),
},
));
return Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_id.clone(),
receiver_id: ft_contract_account_id.clone(),
actions: vec![action_storage_deposit, action_ft_transfer.clone()],
});
}

Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_id.clone(),
receiver_id: ft_contract_account_id.clone(),
actions: vec![action_ft_transfer.clone()],
})
}
38 changes: 24 additions & 14 deletions src/commands/tokens/view_ft_balance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,7 @@ impl ViewFtBalanceContext {
let args = serde_json::to_vec(&json!({
"account_id": owner_account_id.to_string(),
}))?;
let call_result = network_config
.json_rpc_client()
.blocking_call_view_function(
&ft_contract_account_id,
"ft_balance_of",
args,
block_reference.clone(),
)
.wrap_err_with(||{
format!("Failed to fetch query for view method: 'ft_balance_of' (contract <{}> on network <{}>)",
ft_contract_account_id,
network_config.network_name
)
})?;
let call_result = get_ft_balance(network_config, &ft_contract_account_id, args, block_reference.clone())?;
call_result.print_logs();
let amount: String = call_result.parse_result_from_json()?;
let fungible_token = crate::types::ft_properties::FungibleToken::from_params_ft(
Expand Down Expand Up @@ -93,3 +80,26 @@ impl ViewFtBalance {
)
}
}

#[tracing::instrument(name = "Getting FT balance ...", skip_all)]
fn get_ft_balance(
network_config: &crate::config::NetworkConfig,
ft_contract_account_id: &near_primitives::types::AccountId,
args: Vec<u8>,
block_reference: near_primitives::types::BlockReference,
) -> color_eyre::eyre::Result<near_primitives::views::CallResult> {
network_config
.json_rpc_client()
.blocking_call_view_function(
ft_contract_account_id,
"ft_balance_of",
args,
block_reference,
)
.wrap_err_with(||{
format!("Failed to fetch query for view method: 'ft_balance_of' (contract <{}> on network <{}>)",
ft_contract_account_id,
network_config.network_name
)
})
}
1 change: 0 additions & 1 deletion src/commands/tokens/view_near_balance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct ViewNearBalance {
pub struct ViewNearBalanceContext(crate::network_view_at_block::ArgsForViewContext);

impl ViewNearBalanceContext {
#[tracing::instrument(name = "Getting a NEAR balance for your account ...", skip_all)]
pub fn from_previous_context(
previous_context: super::TokensCommandsContext,
_scope: &<ViewNearBalance as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
Expand Down
38 changes: 24 additions & 14 deletions src/commands/tokens/view_nft_assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,7 @@ impl ViewNftAssetsContext {
let args = serde_json::to_vec(&json!({
"account_id": owner_account_id.to_string(),
}))?;
let call_result = network_config
.json_rpc_client()
.blocking_call_view_function(
&nft_contract_account_id,
"nft_tokens_for_owner",
args,
block_reference.clone(),
)
.wrap_err_with(||{
format!("Failed to fetch query for view method: 'nft_tokens_for_owner' (contract <{}> on network <{}>)",
nft_contract_account_id,
network_config.network_name
)
})?;
let call_result = get_nft_balance(network_config, &nft_contract_account_id, args, block_reference.clone())?;
call_result.print_logs();
let serde_call_result: serde_json::Value = call_result.parse_result_from_json()?;

Expand Down Expand Up @@ -82,3 +69,26 @@ impl ViewNftAssets {
)
}
}

#[tracing::instrument(name = "Getting NFT balance ...", skip_all)]
fn get_nft_balance(
network_config: &crate::config::NetworkConfig,
nft_contract_account_id: &near_primitives::types::AccountId,
args: Vec<u8>,
block_reference: near_primitives::types::BlockReference,
) -> color_eyre::eyre::Result<near_primitives::views::CallResult> {
network_config
.json_rpc_client()
.blocking_call_view_function(
nft_contract_account_id,
"nft_tokens_for_owner",
args,
block_reference,
)
.wrap_err_with(||{
format!("Failed to fetch query for view method: 'nft_tokens_for_owner' (contract <{}> on network <{}>)",
nft_contract_account_id,
network_config.network_name
)
})
}
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,12 @@ pub fn is_account_exist(
false
}

#[tracing::instrument(name = "Searching for a network where an account exists for", skip_all)]
pub fn find_network_where_account_exist(
context: &crate::GlobalContext,
new_account_id: near_primitives::types::AccountId,
) -> Option<crate::config::NetworkConfig> {
tracing::Span::current().pb_set_message(new_account_id.as_str());
for (_, network_config) in context.config.network_connection.iter() {
if tokio::runtime::Runtime::new()
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions src/types/ft_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub struct FtMetadata {
pub decimals: u8,
}

#[tracing::instrument(name = "Getting FT metadata ...", skip_all)]
pub fn params_ft_metadata(
ft_contract_account_id: near_primitives::types::AccountId,
network_config: &crate::config::NetworkConfig,
Expand Down

0 comments on commit fdb92bf

Please sign in to comment.