Skip to content

Commit

Permalink
Refactor verifier verify fn
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed Nov 5, 2024
1 parent cff6f3e commit 8985d95
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
4 changes: 1 addition & 3 deletions crates/sncast/src/starknet_commands/verify/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_trait::async_trait;
use camino::Utf8PathBuf;
use reqwest::StatusCode;
use serde::Serialize;
use sncast::{response::structs::VerifyResponse, Network};
use sncast::response::structs::VerifyResponse;
use starknet::core::types::Felt;
use std::ffi::OsStr;
use walkdir::WalkDir;
Expand Down Expand Up @@ -64,8 +64,6 @@ async fn send_verification_request(

#[async_trait]
pub trait VerificationInterface {
fn new(network: Network, base_url: Option<String>) -> Self;

fn explorer_url(&self) -> String;

async fn verify(
Expand Down
50 changes: 26 additions & 24 deletions crates/sncast/src/starknet_commands/verify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ impl fmt::Display for Verifier {
}
}

fn get_verifier(
verifier: Verifier,
network: Network,
custom_base_api_url: Option<String>,
) -> Box<dyn VerificationInterface + Sync> {
match verifier {
Verifier::Walnut => Box::new(WalnutVerificationInterface::new(
network,
custom_base_api_url,
)),
Verifier::Voyager => Box::new(VoyagerVerificationInterface::new(
network,
custom_base_api_url,
)),
}
}

// disable too many arguments clippy warning
#[allow(clippy::too_many_arguments)]
pub async fn verify(
Expand Down Expand Up @@ -109,28 +126,13 @@ pub async fn verify(
.parent()
.ok_or(anyhow!("Failed to obtain workspace dir"))?;

match verifier {
Verifier::Walnut => {
let walnut = WalnutVerificationInterface::new(network, custom_base_api_url);
walnut
.verify(
workspace_dir.to_path_buf(),
contract_address,
class_hash,
class_name,
)
.await
}
Verifier::Voyager => {
let voyager = VoyagerVerificationInterface::new(network, custom_base_api_url);
voyager
.verify(
workspace_dir.to_path_buf(),
contract_address,
class_hash,
class_name,
)
.await
}
}
let verifier = get_verifier(verifier, network, custom_base_api_url);
verifier
.verify(
workspace_dir.to_path_buf(),
contract_address,
class_hash,
class_name,
)
.await
}
8 changes: 5 additions & 3 deletions crates/sncast/src/starknet_commands/verify/voyager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ pub struct VoyagerVerificationInterface {
pub base_url: String,
}

#[async_trait]
impl VerificationInterface for VoyagerVerificationInterface {
fn new(network: Network, base_url: Option<String>) -> Self {
impl VoyagerVerificationInterface {
pub fn new(network: Network, base_url: Option<String>) -> Self {
let base_url = match base_url {
Some(custom_base_api_url) => custom_base_api_url.clone(),
None => match network {
Expand All @@ -19,7 +18,10 @@ impl VerificationInterface for VoyagerVerificationInterface {

VoyagerVerificationInterface { base_url }
}
}

#[async_trait]
impl VerificationInterface for VoyagerVerificationInterface {
fn explorer_url(&self) -> String {
format!("{}/class-verify-v2", self.base_url)
}
Expand Down
8 changes: 5 additions & 3 deletions crates/sncast/src/starknet_commands/verify/walnut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ pub struct WalnutVerificationInterface {
pub network: Network,
}

#[async_trait]
impl VerificationInterface for WalnutVerificationInterface {
fn new(network: Network, base_url: Option<String>) -> Self {
impl WalnutVerificationInterface {
pub fn new(network: Network, base_url: Option<String>) -> Self {
let base_url = match base_url {
Some(custom_base_api_url) => custom_base_api_url.clone(),
None => "https://api.walnut.dev".to_string(),
};

WalnutVerificationInterface { base_url, network }
}
}

#[async_trait]
impl VerificationInterface for WalnutVerificationInterface {
fn explorer_url(&self) -> String {
let path = match self.network {
Network::Mainnet => "/v1/sn_main/verify",
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/tests/e2e/verify/voyager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async fn test_no_class_hash_or_contract_address_provided() {
error: the following required arguments were not provided:
<--contract-address <CONTRACT_ADDRESS>|--class-hash <CLASS_HASH>>
Usage: sncast verify --class-name <CLASS_NAME> --network <NETWORK> --verifier <VERIFIER> <--contract-address <CONTRACT_ADDRESS>|--class-hash <CLASS_HASH>>
Usage: sncast verify --class-name <CLASS_NAME> --verifier <VERIFIER> --network <NETWORK> <--contract-address <CONTRACT_ADDRESS>|--class-hash <CLASS_HASH>>
For more information, try '--help'."
),
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/tests/e2e/verify/walnut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async fn test_no_class_hash_or_contract_address_provided() {
error: the following required arguments were not provided:
<--contract-address <CONTRACT_ADDRESS>|--class-hash <CLASS_HASH>>
Usage: sncast verify --class-name <CLASS_NAME> --network <NETWORK> --verifier <VERIFIER> <--contract-address <CONTRACT_ADDRESS>|--class-hash <CLASS_HASH>>
Usage: sncast verify --class-name <CLASS_NAME> --verifier <VERIFIER> --network <NETWORK> <--contract-address <CONTRACT_ADDRESS>|--class-hash <CLASS_HASH>>
For more information, try '--help'."
),
Expand Down

0 comments on commit 8985d95

Please sign in to comment.