Skip to content
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

feat: Added the ability to use the TEACH-ME mode #360

Merged
merged 18 commits into from
Jul 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ impl AccountContext {
move |network_config, block_reference| {
let contract_account_id = (previous_context.get_contract_account_id)(network_config)?;

let storage_balance = get_storage_balance(network_config, &contract_account_id, &account_id, block_reference)?;
let storage_balance =
get_storage_balance(
network_config,
&contract_account_id,
&account_id,
block_reference
)?;
eprintln!("storage balance for <{account_id}>:");
eprintln!(" {:<13} {:>10} ({} [{:>28} yoctoNEAR])",
"available:",
Expand Down
7 changes: 6 additions & 1 deletion src/commands/account/update_social_profile/sign_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ impl From<SignerContext> for crate::commands::ActionContext {
let get_prepopulated_transaction_after_getting_network_callback: crate::commands::GetPrepopulatedTransactionAfterGettingNetworkCallback =
Arc::new({
move |network_config| {
get_prepopulated_transaction(network_config, &account_id, &signer_id, &data)
get_prepopulated_transaction(
network_config,
&account_id,
&signer_id,
&data
)
}
});

Expand Down
5 changes: 4 additions & 1 deletion src/commands/account/view_account_summary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ impl ViewAccountSummaryContext {
let account_id: near_primitives::types::AccountId = scope.account_id.clone().into();

move |network_config, block_reference| {
get_account_inquiry(&account_id, network_config, block_reference)
get_account_inquiry(
&account_id, network_config,
block_reference
)
}
});
Ok(Self(crate::network_view_at_block::ArgsForViewContext {
Expand Down
9 changes: 8 additions & 1 deletion src/commands/contract/call_function/as_read_only/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ impl FunctionContext {
let function_name = scope.function_name.clone();

move |network_config, block_reference| {
call_view_function(network_config, &account_id, &function_name, function_args.clone(), function_args_type.clone(), block_reference)
call_view_function(
network_config,
&account_id,
&function_name,
function_args.clone(),
function_args_type.clone(),
block_reference
)
}
});

Expand Down
7 changes: 6 additions & 1 deletion src/commands/staking/delegate/view_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ impl ViewBalanceContext {
let on_after_getting_block_reference_callback: crate::network_view_at_block::OnAfterGettingBlockReferenceCallback = std::sync::Arc::new({

move |network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference| {
calculation_delegated_stake_balance(&account_id, &validator_account_id, network_config, block_reference)
calculation_delegated_stake_balance(
&account_id,
&validator_account_id,
network_config,
block_reference
)
}
});
Ok(Self(crate::network_view_at_block::ArgsForViewContext {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/tokens/send_ft/amount_ft.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::too_many_arguments)]

use color_eyre::eyre::{Context, ContextCompat};
use inquire::CustomType;
use serde_json::{json, Value};
Expand Down
8 changes: 7 additions & 1 deletion src/commands/tokens/view_ft_balance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ impl ViewFtBalanceContext {
let args = serde_json::to_vec(&json!({
"account_id": owner_account_id.to_string(),
}))?;
let call_result = get_ft_balance(network_config, &ft_contract_account_id, args, block_reference.clone())?;
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
8 changes: 7 additions & 1 deletion src/commands/tokens/view_nft_assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ impl ViewNftAssetsContext {
let args = serde_json::to_vec(&json!({
"account_id": owner_account_id.to_string(),
}))?;
let call_result = get_nft_balance(network_config, &nft_contract_account_id, args, block_reference.clone())?;
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
155 changes: 141 additions & 14 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2076,16 +2076,41 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
tracing::Span::current().pb_set_message(&format!(
"the '{method_name}' method of the <{account_id}> contract ..."
));
let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::CallFunction {
account_id: account_id.clone(),
method_name: method_name.to_owned(),
args: near_primitives::types::FunctionArgs::from(args),
},
};
let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request)?;
tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nHTTP POST {}\nJSON-BODY:\n{:#}",
method_name,
account_id,
self.server_addr(),
request_payload,
);

let query_view_method_response = self
.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::CallFunction {
account_id: account_id.clone(),
method_name: method_name.to_owned(),
args: near_primitives::types::FunctionArgs::from(args),
},
})
.blocking_call(query_view_method_request)
.wrap_err("Failed to make a view-function call")?;

let call_result = query_view_method_response.call_result()?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nRESPONSE:\n{{\n \"block_hash\": {}\n \"block_height\": {}\n \"logs\": {:?}\n \"result\": {:?}\n}}",
method_name,
account_id,
query_view_method_response.block_hash,
query_view_method_response.block_height,
call_result.logs,
call_result.result
);

query_view_method_response.call_result()
}

Expand All @@ -2102,13 +2127,37 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
>,
> {
tracing::Span::current().pb_set_message(&format!("{public_key} ..."));
self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {

let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::ViewAccessKey {
account_id: account_id.clone(),
public_key: public_key.clone(),
},
})
};
let request_payload = request_payload(&query_view_method_request)?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting access key information for public_key: {}\nHTTP POST {}\nJSON-BODY:\n{:#}",
public_key,
self.server_addr(),
request_payload,
);

let query_view_method_response: near_jsonrpc_client::methods::query::RpcQueryResponse =
self.blocking_call(query_view_method_request)?;

let response_payload = response_payload(&query_view_method_response)?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting access key information for public_key: {}\nRESPONSE:\n{:#}",
public_key,
response_payload
);

Ok(query_view_method_response)
}

#[tracing::instrument(name = "Getting a list of", skip_all)]
Expand All @@ -2123,12 +2172,35 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
>,
> {
tracing::Span::current().pb_set_message(&format!("{account_id} access keys ..."));
self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {

let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::ViewAccessKeyList {
account_id: account_id.clone(),
},
})
};
let request_payload = request_payload(&query_view_method_request)?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting a list of {} access keys\nHTTP POST {}\nJSON-BODY:\n{:#}",
account_id,
self.server_addr(),
request_payload,
);

let query_view_method_response = self.blocking_call(query_view_method_request)?;

let response_payload = response_payload(&query_view_method_response)?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting a list of {} access keys\nRESPONSE:\n{:#}",
account_id,
response_payload
);

Ok(query_view_method_response)
}

#[tracing::instrument(name = "Getting information about", skip_all)]
Expand All @@ -2143,15 +2215,70 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
>,
> {
tracing::Span::current().pb_set_message(&format!("{account_id} ..."));
self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {

let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::ViewAccount {
account_id: account_id.clone(),
},
})
};
let request_payload = request_payload(&query_view_method_request)?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting information about {}\nHTTP POST {}\nJSON-BODY:\n{:#}",
account_id,
self.server_addr(),
request_payload,
);

let query_view_method_response = self.blocking_call(query_view_method_request)?;

let response_payload = response_payload(&query_view_method_response)?;

tracing::info!(
target: "near_teach_me",
"\nTEACH-ME: Getting information about {}\nRESPONSE:\n{:#}",
account_id,
response_payload
);

Ok(query_view_method_response)
}
}

fn request_payload(
query_view_method_request: &near_jsonrpc_client::methods::query::RpcQueryRequest,
) -> Result<
serde_json::Value,
near_jsonrpc_client::errors::JsonRpcError<near_jsonrpc_primitives::types::query::RpcQueryError>,
> {
near_jsonrpc_client::methods::to_json(query_view_method_request).map_err(|err| {
near_jsonrpc_client::errors::JsonRpcError::TransportError(
near_jsonrpc_client::errors::RpcTransportError::SendError(
near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(err),
),
)
})
}

fn response_payload(
query_view_method_response: &near_jsonrpc_client::methods::query::RpcQueryResponse,
) -> Result<
serde_json::Value,
near_jsonrpc_client::errors::JsonRpcError<near_jsonrpc_primitives::types::query::RpcQueryError>,
> {
serde_json::to_value(query_view_method_response).map_err(|err| {
near_jsonrpc_client::errors::JsonRpcError::TransportError(
near_jsonrpc_client::errors::RpcTransportError::SendError(
near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(
err.into(),
),
),
)
})
}

#[easy_ext::ext(RpcQueryResponseExt)]
pub impl near_jsonrpc_primitives::types::query::RpcQueryResponse {
fn access_key_view(&self) -> color_eyre::eyre::Result<near_primitives::views::AccessKeyView> {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pub mod utils_command;
pub struct GlobalContext {
pub config: crate::config::Config,
pub offline: bool,
pub teach_me: bool,
}
Loading
Loading