Skip to content

Commit

Permalink
Move config to verify level & utilize ArgGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed Oct 24, 2024
1 parent 4a9f0fd commit 34522c8
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 261 deletions.
11 changes: 0 additions & 11 deletions crates/sncast/src/helpers/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ pub struct CastConfig {
)]
/// Print links pointing to pages with transaction details in the chosen block explorer
pub show_explorer_links: bool,

#[serde(
default,
rename(
serialize = "verification-base-url",
deserialize = "verification-base-url"
)
)]
/// Custom base url to be used for verification
pub verification_base_url: Option<String>,
}

impl Default for CastConfig {
Expand All @@ -68,7 +58,6 @@ impl Default for CastConfig {
wait_params: ValidatedWaitParams::default(),
block_explorer: Some(block_explorer::Service::default()),
show_explorer_links: true,
verification_base_url: None,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,13 @@ async fn run_async_command(
)
.expect("Failed to build contract");
let result = starknet_commands::verify::verify(
&config,
verify.contract_address,
verify.class_hash,
verify.contract_address_or_class_hash.contract_address,
verify.contract_address_or_class_hash.class_hash,
verify.class_name,
verify.verifier,
verify.network,
verify.confirm_verification,
verify.custom_base_api_url,
&package_metadata.manifest_path,
&artifacts,
)
Expand Down
9 changes: 5 additions & 4 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::{helpers::configuration::CastConfig, response::structs::VerifyResponse};
use sncast::{response::structs::VerifyResponse, Network};
use starknet::core::types::Felt;
use std::ffi::OsStr;
use walkdir::WalkDir;
Expand Down Expand Up @@ -64,11 +64,12 @@ async fn send_verification_request(

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

fn gen_explorer_url(&self) -> String;

async fn verify(
&self,
config: &CastConfig,
workspace_dir: Utf8PathBuf,
contract_address: Option<Felt>,
class_hash: Option<Felt>,
Expand All @@ -82,7 +83,7 @@ pub trait VerificationInterface {
class_hash,
source_code,
};
let url = self.gen_explorer_url(config.clone());
let url = self.gen_explorer_url();
send_verification_request(url, payload).await
}
}
Expand Down
37 changes: 20 additions & 17 deletions crates/sncast/src/starknet_commands/verify/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::{anyhow, bail, Result};
use base::VerificationInterface;
use camino::Utf8PathBuf;
use clap::{Parser, ValueEnum};
use clap::{Args, Parser, ValueEnum};
use promptly::prompt;
use scarb_api::StarknetContractArtifacts;
use sncast::helpers::configuration::CastConfig;
use sncast::response::structs::VerifyResponse;
use sncast::Network;
use starknet::core::types::Felt;
Expand All @@ -17,18 +16,25 @@ pub mod base;
mod voyager;
mod walnut;

#[derive(Parser)]
#[command(about = "Verify a contract through a block explorer")]
pub struct Verify {
#[derive(Args, Debug, Clone)]
#[group(required = true, multiple = false)]
pub struct ContractAddressOrClassHashGroup {
/// Contract address of the contract. Either this or class hash should be provided.
#[clap(short = 'd', long)]
pub contract_address: Option<Felt>,

/// Class hash of the contract. Either this or contract address should be provided
/// Class hash of the contract. Either this or contract address should be provided.
#[clap(short = 'x', long)]
pub class_hash: Option<Felt>,
}

#[derive(Parser)]
#[command(about = "Verify a contract through a block explorer")]
pub struct Verify {
#[clap(flatten)]
pub contract_address_or_class_hash: ContractAddressOrClassHashGroup,

/// Class name of the contract to be verified
/// Class name of the contract to be verified. Either this or class hash should be provided.
#[clap(short, long)]
pub class_name: String,

Expand All @@ -47,6 +53,10 @@ pub struct Verify {
/// Optionally specify package with the contract to be verified
#[clap(long)]
pub package: Option<String>,

// Custom api to be used as a verifier's base url.
#[clap(long)]
pub custom_base_api_url: Option<String>,
}

#[derive(ValueEnum, Clone, Debug)]
Expand All @@ -67,13 +77,13 @@ impl fmt::Display for Verifier {
// disable too many arguments clippy warning
#[allow(clippy::too_many_arguments)]
pub async fn verify(
cast_config: &CastConfig,
contract_address: Option<Felt>,
class_hash: Option<Felt>,
class_name: String,
verifier: Verifier,
network: Network,
confirm_verification: bool,
custom_base_api_url: Option<String>,
manifest_path: &Utf8PathBuf,
artifacts: &HashMap<String, StarknetContractArtifacts>,
) -> Result<VerifyResponse> {
Expand All @@ -93,11 +103,6 @@ pub async fn verify(
return Err(anyhow!("Contract named '{class_name}' was not found"));
}

// ensure that either contract_address or class_hash is provided
if contract_address.is_none() && class_hash.is_none() {
bail!("Either contract_address or class_hash must be provided");
}

// Build JSON Payload for the verification request
// get the parent dir of the manifest path
let workspace_dir = manifest_path
Expand All @@ -106,10 +111,9 @@ pub async fn verify(

match verifier {
Verifier::Walnut => {
let walnut = WalnutVerificationInterface::new(network);
let walnut = WalnutVerificationInterface::new(network, custom_base_api_url);
walnut
.verify(
cast_config,
workspace_dir.to_path_buf(),
contract_address,
class_hash,
Expand All @@ -118,10 +122,9 @@ pub async fn verify(
.await
}
Verifier::Voyager => {
let voyager = VoyagerVerificationInterface::new(network);
let voyager = VoyagerVerificationInterface::new(network, custom_base_api_url);
voyager
.verify(
cast_config,
workspace_dir.to_path_buf(),
contract_address,
class_hash,
Expand Down
22 changes: 10 additions & 12 deletions crates/sncast/src/starknet_commands/verify/voyager.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
use super::base::VerificationInterface;
use async_trait::async_trait;
use sncast::{helpers::configuration::CastConfig, Network};
use sncast::Network;

pub struct VoyagerVerificationInterface {
pub network: Network,
}

impl VoyagerVerificationInterface {
pub fn new(network: Network) -> Self {
VoyagerVerificationInterface { network }
}
pub base_url: String,
}

#[async_trait]
impl VerificationInterface for VoyagerVerificationInterface {
fn gen_explorer_url(&self, config: CastConfig) -> String {
let base_api_url = match config.verification_base_url {
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 self.network {
None => match network {
Network::Mainnet => "https://api.voyager.online/beta".to_string(),
Network::Sepolia => "https://sepolia-api.voyager.online/beta".to_string(),
},
};

format!("{base_api_url}/class-verify-v2")
VoyagerVerificationInterface { base_url }
}

fn gen_explorer_url(&self) -> String {
format!("{}/class-verify-v2", self.base_url)
}
}
19 changes: 9 additions & 10 deletions crates/sncast/src/starknet_commands/verify/walnut.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
use super::base::VerificationInterface;
use async_trait::async_trait;
use sncast::{helpers::configuration::CastConfig, Network};
use sncast::Network;

pub struct WalnutVerificationInterface {
pub base_url: String,
pub network: Network,
}

impl WalnutVerificationInterface {
pub fn new(network: Network) -> Self {
WalnutVerificationInterface { network }
}
}

#[async_trait]
impl VerificationInterface for WalnutVerificationInterface {
fn gen_explorer_url(&self, config: CastConfig) -> String {
let api_base_url = match config.verification_base_url {
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 }
}

fn gen_explorer_url(&self) -> String {
let path = match self.network {
Network::Mainnet => "/v1/sn_main/verify",
Network::Sepolia => "/v1/sn_sepolia/verify",
};
format!("{api_base_url}{path}")
format!("{}{}", self.base_url, path)
}
}
24 changes: 0 additions & 24 deletions crates/sncast/tests/e2e/verify/helpers.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/sncast/tests/e2e/verify/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod helpers;
mod voyager;
mod walnut;
Loading

0 comments on commit 34522c8

Please sign in to comment.