Skip to content

Commit

Permalink
Extract redundant trait functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed Oct 23, 2024
1 parent 2393856 commit 4a9f0fd
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 135 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

#### Added

- Add Voyager API support for `verify` subcommand [Read more here](https://foundry-rs.github.io/starknet-foundry/appendix/sncast/verify.html).
- Voyager API support for `verify` subcommand [Read more here](https://foundry-rs.github.io/starknet-foundry/appendix/sncast/verify.html).

## [0.32.0] - 2024-10-16

Expand Down
4 changes: 2 additions & 2 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use starknet::accounts::ConnectedAccount;
use starknet::core::utils::get_selector_from_name;
use starknet::providers::Provider;
use starknet_commands::account::list::print_account_list;
use starknet_commands::verification::verify::Verify;
use starknet_commands::verify::Verify;
use tokio::runtime::Runtime;

mod starknet_commands;
Expand Down Expand Up @@ -565,7 +565,7 @@ async fn run_async_command(
false,
)
.expect("Failed to build contract");
let result = starknet_commands::verification::verify::verify(
let result = starknet_commands::verify::verify(
&config,
verify.contract_address,
verify.class_hash,
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/starknet_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pub mod multicall;
pub mod script;
pub mod show_config;
pub mod tx_status;
pub mod verification;
pub mod verify;
97 changes: 0 additions & 97 deletions crates/sncast/src/starknet_commands/verification/base.rs

This file was deleted.

4 changes: 0 additions & 4 deletions crates/sncast/src/starknet_commands/verification/mod.rs

This file was deleted.

96 changes: 96 additions & 0 deletions crates/sncast/src/starknet_commands/verify/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use camino::Utf8PathBuf;
use reqwest::StatusCode;
use serde::Serialize;
use sncast::{helpers::configuration::CastConfig, response::structs::VerifyResponse};
use starknet::core::types::Felt;
use std::ffi::OsStr;
use walkdir::WalkDir;

fn read_workspace_files(
workspace_dir: Utf8PathBuf,
) -> Result<serde_json::Map<String, serde_json::Value>> {
// Read all files name along with their contents in a JSON format
// in the workspace dir recursively
// key is the file name and value is the file content
let mut file_data = serde_json::Map::new();

// Recursively read files and their contents in workspace directory
for entry in WalkDir::new(workspace_dir.clone()).follow_links(true) {
let entry = entry?;
let path = entry.path();
if path.is_file() {
if let Some(extension) = path.extension() {
if extension == OsStr::new("cairo") || extension == OsStr::new("toml") {
let relative_path = path.strip_prefix(workspace_dir.clone())?;
let file_content = std::fs::read_to_string(path)?;
file_data.insert(
relative_path.to_string_lossy().into_owned(),
serde_json::Value::String(file_content),
);
}
}
}
}
Ok(file_data)
}

async fn send_verification_request(
url: String,
payload: VerificationPayload,
) -> Result<VerifyResponse> {
let json_payload = serde_json::to_string(&payload)?;
let client = reqwest::Client::new();
let api_res = client
.post(url)
.header("Content-Type", "application/json")
.body(json_payload)
.send()
.await
.context("Failed to send request to verifier API")?;

if api_res.status() == StatusCode::OK {
let message = api_res
.text()
.await
.context("Failed to read verifier API response")?;
Ok(VerifyResponse { message })
} else {
let message = api_res.text().await.context("Failed to verify contract")?;
Err(anyhow!(message))
}
}

#[async_trait]
pub trait VerificationInterface {
fn gen_explorer_url(&self, config: CastConfig) -> String;

async fn verify(
&self,
config: &CastConfig,
workspace_dir: Utf8PathBuf,
contract_address: Option<Felt>,
class_hash: Option<Felt>,
class_name: String,
) -> Result<VerifyResponse> {
let file_data = read_workspace_files(workspace_dir)?;
let source_code = serde_json::Value::Object(file_data);
let payload = VerificationPayload {
class_name,
contract_address,
class_hash,
source_code,
};
let url = self.gen_explorer_url(config.clone());
send_verification_request(url, payload).await
}
}

#[derive(Serialize, Debug)]
pub struct VerificationPayload {
pub class_name: String,
pub contract_address: Option<Felt>,
pub class_hash: Option<Felt>,
pub source_code: serde_json::Value,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::base::VerificationInterface;
use super::voyager::VoyagerVerificationInterface;
use super::walnut::WalnutVerificationInterface;
use anyhow::{anyhow, bail, Result};
use base::VerificationInterface;
use camino::Utf8PathBuf;
use clap::{Parser, ValueEnum};
use promptly::prompt;
Expand All @@ -12,6 +10,12 @@ use sncast::Network;
use starknet::core::types::Felt;
use std::collections::HashMap;
use std::fmt;
use voyager::VoyagerVerificationInterface;
use walnut::WalnutVerificationInterface;

pub mod base;
mod voyager;
mod walnut;

#[derive(Parser)]
#[command(about = "Verify a contract through a block explorer")]
Expand Down Expand Up @@ -102,15 +106,27 @@ pub async fn verify(

match verifier {
Verifier::Walnut => {
let walnut = WalnutVerificationInterface::new(network, workspace_dir.to_path_buf());
let walnut = WalnutVerificationInterface::new(network);
walnut
.verify(cast_config, contract_address, class_hash, class_name)
.verify(
cast_config,
workspace_dir.to_path_buf(),
contract_address,
class_hash,
class_name,
)
.await
}
Verifier::Voyager => {
let voyager = VoyagerVerificationInterface::new(network, workspace_dir.to_path_buf());
let voyager = VoyagerVerificationInterface::new(network);
voyager
.verify(cast_config, contract_address, class_hash, class_name)
.verify(
cast_config,
workspace_dir.to_path_buf(),
contract_address,
class_hash,
class_name,
)
.await
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
use super::base::VerificationInterface;
use async_trait::async_trait;
use camino::Utf8PathBuf;
use sncast::{helpers::configuration::CastConfig, Network};

pub struct VoyagerVerificationInterface {
pub network: Network,
pub workspace_dir: Utf8PathBuf,
}

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

#[async_trait]
impl VerificationInterface for VoyagerVerificationInterface {
fn get_workspace_dir(&self) -> Utf8PathBuf {
self.workspace_dir.clone()
}

fn gen_explorer_url(&self, config: CastConfig) -> String {
let base_api_url = match config.verification_base_url {
Some(custom_base_api_url) => custom_base_api_url.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
use super::base::VerificationInterface;
use async_trait::async_trait;
use camino::Utf8PathBuf;
use sncast::{helpers::configuration::CastConfig, Network};

pub struct WalnutVerificationInterface {
pub network: Network,
pub workspace_dir: Utf8PathBuf,
}

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

#[async_trait]
impl VerificationInterface for WalnutVerificationInterface {
fn get_workspace_dir(&self) -> Utf8PathBuf {
self.workspace_dir.clone()
}

fn gen_explorer_url(&self, config: CastConfig) -> String {
let api_base_url = match config.verification_base_url {
Some(custom_base_api_url) => custom_base_api_url.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mod multicall;
mod script;
mod show_config;
mod tx_status;
mod verification;
mod verify;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4a9f0fd

Please sign in to comment.