Skip to content

Commit

Permalink
Make rpc url optional for show-config command (#2701)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2644 

## Introduced changes

<!-- A brief description of the changes -->

- Do not require rpc url for `show-config` command to work
- Added missing `show_explorer_links` field to the `ShowConfigResponse`

## 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`
  • Loading branch information
ddoktorski authored Nov 25, 2024
1 parent 28e4d8b commit 6578e28
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Changed

- Changed return type of `declare` in Cairo Deployment Scripts so it can handle already declared contracts without failing
- Allow using `show-config` command without providing rpc url

## [0.33.0] - 2024-11-04

Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ async fn run_async_command(
},

Commands::ShowConfig(show) => {
let provider = show.rpc.get_provider(&config).await?;
let provider = show.rpc.get_provider(&config).await.ok();

let result =
starknet_commands::show_config::show_config(&show, &provider, config, cli.profile)
Expand Down
1 change: 1 addition & 0 deletions crates/sncast/src/response/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl From<Value> for OutputValue {
.collect(),
),
Value::String(s) => OutputValue::String(s.to_string()),
Value::Bool(b) => OutputValue::String(b.to_string()),
s => panic!("{s:?} cannot be auto-serialized to output"),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/sncast/src/response/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ impl CommandResponse for MulticallNewResponse {}
#[derive(Serialize)]
pub struct ShowConfigResponse {
pub profile: Option<String>,
pub chain_id: String,
pub chain_id: Option<String>,
pub rpc_url: Option<String>,
pub account: Option<String>,
pub accounts_file_path: Option<Utf8PathBuf>,
pub keystore: Option<Utf8PathBuf>,
pub wait_timeout: Option<Decimal>,
pub wait_retry_interval: Option<Decimal>,
pub show_explorer_links: bool,
}
impl CommandResponse for ShowConfigResponse {}

Expand Down
12 changes: 9 additions & 3 deletions crates/sncast/src/starknet_commands/show_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ pub struct ShowConfig {
#[allow(clippy::ptr_arg)]
pub async fn show_config(
show: &ShowConfig,
provider: &JsonRpcClient<HttpTransport>,
provider: &Option<JsonRpcClient<HttpTransport>>,
cast_config: CastConfig,
profile: Option<String>,
) -> Result<ShowConfigResponse> {
let chain_id_field = get_chain_id(provider).await?;
let chain_id = chain_id_to_network_name(chain_id_field);
let chain_id = if let Some(provider) = provider {
let chain_id_field = get_chain_id(provider).await?;
Some(chain_id_to_network_name(chain_id_field))
} else {
None
};

let rpc_url = Some(show.rpc.url.clone().unwrap_or(cast_config.url)).filter(|p| !p.is_empty());
let account = Some(cast_config.account).filter(|p| !p.is_empty());
let mut accounts_file_path =
Expand All @@ -44,5 +49,6 @@ pub async fn show_config(
keystore,
wait_timeout: wait_timeout.map(|x| Decimal(u64::from(x))),
wait_retry_interval: wait_retry_interval.map(|x| Decimal(u64::from(x))),
show_explorer_links: cast_config.show_explorer_links,
})
}
6 changes: 6 additions & 0 deletions crates/sncast/tests/data/files/correct_snfoundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ account = "user3"
[sncast.profile5]
url = "http://127.0.0.1:5055/rpc"
account = "user8"

[sncast.profile6]
accounts-file = "/path/to/account.json"
account = "user1"
wait-params = { timeout = 500, retry-interval = 10 }
show-explorer-links = false
23 changes: 23 additions & 0 deletions crates/sncast/tests/e2e/show_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async fn test_show_config_from_snfoundry_toml() {
accounts_file_path: ../account-file
chain_id: alpha-sepolia
rpc_url: {}
show_explorer_links: true
wait_retry_interval: 5
wait_timeout: 300
", URL});
Expand Down Expand Up @@ -44,6 +45,7 @@ async fn test_show_config_from_cli() {
chain_id: alpha-sepolia
keystore: ../keystore
rpc_url: {}
show_explorer_links: true
wait_retry_interval: 1
wait_timeout: 2
", URL});
Expand All @@ -63,6 +65,7 @@ async fn test_show_config_from_cli_and_snfoundry_toml() {
chain_id: alpha-sepolia
profile: profile2
rpc_url: {}
show_explorer_links: true
wait_retry_interval: 5
wait_timeout: 300
", URL});
Expand All @@ -82,6 +85,7 @@ async fn test_show_config_when_no_keystore() {
chain_id: alpha-sepolia
profile: profile4
rpc_url: {}
show_explorer_links: true
wait_retry_interval: 5
wait_timeout: 300
", URL});
Expand All @@ -101,7 +105,26 @@ async fn test_show_config_when_keystore() {
keystore: ../keystore
profile: profile3
rpc_url: {}
show_explorer_links: true
wait_retry_interval: 5
wait_timeout: 300
", URL});
}

#[tokio::test]
async fn test_show_config_no_url() {
let tempdir = copy_config_to_tempdir("tests/data/files/correct_snfoundry.toml", None).unwrap();
let args = vec!["--profile", "profile6", "show-config"];

let snapbox = runner(&args).current_dir(tempdir.path());

snapbox.assert().success().stdout_eq(formatdoc! {r"
command: show-config
account: user1
accounts_file_path: /path/to/account.json
profile: profile6
show_explorer_links: false
wait_retry_interval: 10
wait_timeout: 500
"});
}

0 comments on commit 6578e28

Please sign in to comment.