Skip to content

Commit 701d059

Browse files
Move transform_response to reduce cast main.rs (#3532)
<!-- Reference any GitHub issues resolved by this PR --> Closes # ## Introduced changes <!-- A brief description of the changes --> - ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
1 parent 004a69e commit 701d059

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

crates/sncast/src/main.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use anyhow::{Context, Result, bail};
1010
use camino::Utf8PathBuf;
1111
use clap::{CommandFactory, Parser, Subcommand};
1212
use configuration::load_config;
13-
use data_transformer::{reverse_transform_output, transform};
13+
use data_transformer::transform;
1414
use foundry_ui::{Message, UI};
1515
use shared::auto_completions::{Completion, generate_completions};
1616
use sncast::helpers::config::{combine_cast_configs, get_global_config_path};
@@ -20,14 +20,13 @@ use sncast::helpers::output_format::output_format_from_json_flag;
2020
use sncast::helpers::scarb_utils::{
2121
BuildConfig, assert_manifest_path_exists, build_and_load_artifacts, get_package_metadata,
2222
};
23-
use sncast::response::call::CallResponse;
2423
use sncast::response::cast_message::SncastMessage;
2524
use sncast::response::command::CommandResponse;
2625
use sncast::response::declare::DeclareResponse;
2726
use sncast::response::errors::ResponseError;
2827
use sncast::response::errors::handle_starknet_command_error;
2928
use sncast::response::explorer_link::{ExplorerLinksMessage, block_explorer_link_if_allowed};
30-
use sncast::response::transformed_call::TransformedCallResponse;
29+
use sncast::response::transformed_call::transform_response;
3130
use sncast::{
3231
ValidatedWaitParams, WaitForTx, get_account, get_block_id, get_class_hash_by_address,
3332
get_contract_class,
@@ -546,33 +545,6 @@ fn get_cast_config(cli: &Cli, ui: &UI) -> Result<CastConfig> {
546545
Ok(combined_config)
547546
}
548547

549-
fn transform_response(
550-
result: &Result<CallResponse>,
551-
contract_class: &ContractClass,
552-
selector: &Felt,
553-
) -> Option<TransformedCallResponse> {
554-
let Ok(CallResponse { response, .. }) = result else {
555-
return None;
556-
};
557-
558-
if response.is_empty() {
559-
return None;
560-
}
561-
562-
let ContractClass::Sierra(sierra_class) = contract_class else {
563-
return None;
564-
};
565-
566-
let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str()).ok()?;
567-
568-
let transformed_response = reverse_transform_output(response, &abi, selector).ok()?;
569-
570-
Some(TransformedCallResponse {
571-
response_raw: response.clone(),
572-
response: transformed_response,
573-
})
574-
}
575-
576548
fn process_command_result<T>(
577549
command: &str,
578550
result: Result<T>,

crates/sncast/src/response/transformed_call.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use super::command::CommandResponse;
2+
use crate::response::call::CallResponse;
23
use crate::response::cast_message::SncastMessage;
4+
use anyhow::Result;
35
use conversions::string::IntoHexStr;
6+
use data_transformer::reverse_transform_output;
47
use foundry_ui::Message;
58
use foundry_ui::styling;
69
use serde::Serialize;
710
use serde_json::Value;
811
use serde_json::json;
12+
use starknet::core::types::{ContractClass, contract::AbiEntry};
913
use starknet_types_core::felt::Felt;
1014

1115
#[derive(Serialize, Clone)]
@@ -44,3 +48,31 @@ impl Message for SncastMessage<TransformedCallResponse> {
4448
})
4549
}
4650
}
51+
52+
#[must_use]
53+
pub fn transform_response(
54+
result: &Result<CallResponse>,
55+
contract_class: &ContractClass,
56+
selector: &Felt,
57+
) -> Option<TransformedCallResponse> {
58+
let Ok(CallResponse { response, .. }) = result else {
59+
return None;
60+
};
61+
62+
if response.is_empty() {
63+
return None;
64+
}
65+
66+
let ContractClass::Sierra(sierra_class) = contract_class else {
67+
return None;
68+
};
69+
70+
let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str()).ok()?;
71+
72+
let transformed_response = reverse_transform_output(response, &abi, selector).ok()?;
73+
74+
Some(TransformedCallResponse {
75+
response_raw: response.clone(),
76+
response: transformed_response,
77+
})
78+
}

0 commit comments

Comments
 (0)