Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp cli compilation #49

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ script/deploy/tasks/1337/**
script/deploy/tasks/31337/**

debug/
Cargo.lock
Cargo.lock
deploy-cli
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ Follow these steps to set up your local environment:

## Deployment

- Make sure node `>18.0` is installed
- Follow the [setup](#setup) instructions
- run `./deploy-cli` to generate deployment tasks and execute them
- Make sure node `>18.0` is installed
- Install `just` and `cargo`
- Install deploy-cli, `cd script/cli` and either
- run `just install` to install the cli to your local bin and run `deploy-cli`
- or run `just build` to build the cli, copy it to the project root, and run `./deploy-cli` from the project root

## Contributing

Expand Down
Binary file removed deploy-cli
Binary file not shown.
3 changes: 2 additions & 1 deletion script/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ lazy_static = "1.4.0"
signal-hook = "0.3"
regex = "1.11.0"
tokio = { version = "1.40.0", features = ["full"] }
alloy = { version = "0.4.2", features = ["full", "rpc-types-trace", "rpc-types-debug"] }
alloy = { version = "0.4.2", features = ["default", "json-abi", "transports", "providers", "dyn-abi", "rpc-types-trace", "rpc-types-debug"] }
eyre = "0.6.12"
reqwest = "0.12.8"
openssl = { version = "0.10.35", features = ["vendored"] }

[build-dependencies]
5 changes: 0 additions & 5 deletions script/cli/build.sh

This file was deleted.

36 changes: 36 additions & 0 deletions script/cli/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Default recipe to run when just is called without arguments
default:
@just --list

# Build the project and copy the binary to the project root
build:
cargo build --release
cp ./target/release/deploy-cli ../../deploy-cli

# Run tests
test:
cargo test

# Format code
fmt:
cargo fmt

# Run clippy lints
lint:
cargo clippy

# Clean build artifacts
clean:
cargo clean

# Build and run the project
run:
cargo run -- --dir ../..

# Watch the project and run it when the code changes
watch:
cargo watch -x 'run -- --dir ../..'

# Install the project
install:
cargo install --path .
2 changes: 1 addition & 1 deletion script/cli/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// this allows to run test code during development so it's not necessary to click through a bunch of screens over and over again. Return false to continue with the normal program flow and show the main menu. Return true to abort the program after the code from this function has been executed. On errors this function will also automatically exit the program. By default this function should be empty and just return false.
pub async fn run() -> Result<bool, Box<dyn std::error::Error>> {
return Ok(false);
Ok(false)
}

// async fn pretty_print_response(url: &str) -> Result<(), Box<dyn std::error::Error>> {
Expand Down
42 changes: 20 additions & 22 deletions script/cli/src/libs/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ impl ExplorerApiLib {
pub fn new(explorer: Explorer, api_key: String) -> Result<Self, Box<dyn std::error::Error>> {
if explorer.name.to_lowercase().contains("blockscout") {
// blockscout just appends /api to their explorer url
return Ok(ExplorerApiLib {
Ok(ExplorerApiLib {
name: explorer.name.to_string(),
url: explorer.url.to_string(),
standard: explorer.standard.to_string(),
api_key: api_key.to_string(),
api_url: format!("{}/api?", explorer.url),
explorer_type: SupportedExplorerType::Blockscout,
});
})
} else if explorer.name.to_lowercase().contains("scan") {
let chain_id = STATE_MANAGER
.workflow_state
Expand Down Expand Up @@ -66,7 +66,7 @@ impl ExplorerApiLib {
url: explorer.url.to_string(),
standard: explorer.standard.to_string(),
api_key: api_key.to_string(),
api_url: format!("{}", explorer.url.replace("https://", "https://api.")),
api_url: explorer.url.replace("https://", "https://api.").to_string(),
explorer_type: SupportedExplorerType::Etherscan,
});
} else if slices == 3 {
Expand All @@ -76,23 +76,23 @@ impl ExplorerApiLib {
url: explorer.url.to_string(),
standard: explorer.standard.to_string(),
api_key: api_key.to_string(),
api_url: format!("{}", explorer.url.replace("https://", "https://api-")),
api_url: explorer.url.replace("https://", "https://api-").to_string(),
explorer_type: SupportedExplorerType::Etherscan,
});
} else {
return Err(format!(
"Invalid etherscan url: {} ({})",
explorer.name.to_string(),
explorer.url.to_string(),
explorer.name,
explorer.url,
)
.into());
}
}
} else {
return Err(format!(
"Unsupported explorer: {} ({})",
explorer.name.to_string(),
explorer.url.to_string(),
explorer.name,
explorer.url,
)
.into());
}
Expand Down Expand Up @@ -124,9 +124,7 @@ impl ExplorerApiLib {
get_constructor_inputs(abi)?,
));
} else {
return Err(format!(
"Explorer Error: ContractName not found in the response from the explorer api.",
)
return Err("Explorer Error: ContractName not found in the response from the explorer api.".to_string()
.into());
}
}
Expand All @@ -135,12 +133,12 @@ impl ExplorerApiLib {
}
}
}
return Err(format!(
Err(format!(
"Unsupported explorer: {} ({})",
self.name.to_string(),
self.url.to_string(),
self.name,
self.url,
)
.into());
.into())
}

pub async fn get_creation_transaction_hash(
Expand All @@ -160,12 +158,12 @@ impl ExplorerApiLib {
.to_string();
return Ok(tx_hash);
}
return Err(format!(
Err(format!(
"Unsupported explorer: {} ({})",
self.name.to_string(),
self.url.to_string(),
self.name,
self.url,
)
.into());
.into())
}
}

Expand All @@ -184,10 +182,10 @@ async fn get_etherscan_result(url: &str) -> Result<serde_json::Value, Box<dyn st
"Invalid response from etherscan: {:?}",
json_response
));
return Err("Invalid response from etherscan".into());
Err("Invalid response from etherscan".into())
}
Err(e) => {
return Err(format!("Explorer Request Error: {}", e).into());
Err(format!("Explorer Request Error: {}", e).into())
}
}
}
Expand All @@ -197,5 +195,5 @@ fn get_constructor_inputs(abi: &str) -> Result<Option<Constructor>, Box<dyn std:
return Err("Contract source code not verified".into());
}
let parsed_abi: JsonAbi = serde_json::from_str(abi)?;
return Ok(parsed_abi.constructor);
Ok(parsed_abi.constructor)
}
32 changes: 4 additions & 28 deletions script/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,16 @@ use crossterm::{
cursor::{Hide, MoveToColumn, Show},
event::{poll, read, Event, KeyCode, KeyModifiers},
execute,
terminal::{
disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, Clear, ClearType,
EnterAlternateScreen, LeaveAlternateScreen,
},
terminal::{enable_raw_mode, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen},
Result,
};
use screens::screen_manager::ScreenManager;
use signal_hook::{
consts::{SIGINT, SIGTERM},
iterator::Signals,
};
use state_manager::STATE_MANAGER;
use std::{
io::{stdout, Write},
panic, process, thread,
time::Duration,
};
use state_manager::{clean_terminal, STATE_MANAGER};
use std::{io::stdout, panic, process, thread, time::Duration};
use ui::{render_ascii_title, Buffer};

#[tokio::main]
Expand Down Expand Up @@ -66,18 +59,6 @@ async fn main() -> Result<()> {
result
}

fn clean_terminal() -> Result<()> {
let mut stdout = stdout();
if is_raw_mode_enabled().unwrap() {
stdout.flush()?;
let result = disable_raw_mode();
execute!(stdout, LeaveAlternateScreen, Show,)?;
result
} else {
Ok(())
}
}

fn run_main_menu() -> Result<()> {
let mut screen_manager = ScreenManager::new();
let mut buffer = Buffer::new();
Expand All @@ -99,12 +80,7 @@ fn run_main_menu() -> Result<()> {
(KeyModifiers::CONTROL, KeyCode::Char('c')) => break,
(KeyModifiers::CONTROL, KeyCode::Char('d')) => {
// Toggle debug mode
let debug_mode = STATE_MANAGER
.workflow_state
.lock()
.unwrap()
.debug_mode
.clone();
let debug_mode = STATE_MANAGER.workflow_state.lock().unwrap().debug_mode;
STATE_MANAGER.workflow_state.lock().unwrap().debug_mode = !debug_mode;
if !debug_mode {
execute!(stdout(), LeaveAlternateScreen, Show, MoveToColumn(0),)?;
Expand Down
22 changes: 11 additions & 11 deletions script/cli/src/screens/deploy_contracts/execute_deploy_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::state_manager::STATE_MANAGER;
use crate::ui::{get_spinner_frame, Buffer};
use crate::util::deploy_config_lib::get_config_dir;
use crossterm::event::Event;
use regex::Regex;
use std::io::BufRead;
use std::process::Command;
use std::sync::{Arc, Mutex};
use regex::Regex;

pub struct ExecuteDeployScriptScreen {
execution_status: Arc<Mutex<ExecutionStatus>>,
Expand Down Expand Up @@ -75,9 +75,10 @@ impl ExecuteDeployScriptScreen {
.join("Deploy-all.s.sol"),
)
.arg(format!("--rpc-url={}", rpc_url))
.arg("-vvvv").arg(format!("--private-key={}", private_key));
.arg("-vvvv")
.arg(format!("--private-key={}", private_key));

match execute_command(&mut command) {
match execute_command(command) {
Ok(result) => {
*execution_status.lock().unwrap() = ExecutionStatus::DryRunCompleted;
if let Some(error_message) = result {
Expand Down Expand Up @@ -111,7 +112,7 @@ impl ExecuteDeployScriptScreen {
}
}

match execute_command(&mut command.arg("--broadcast").arg("--skip-simulation")) {
match execute_command(command.arg("--broadcast").arg("--skip-simulation")) {
Ok(result) => {
*execution_status.lock().unwrap() = ExecutionStatus::DeploymentCompleted;
if let Some(error_message) = result {
Expand Down Expand Up @@ -140,7 +141,7 @@ impl ExecuteDeployScriptScreen {
command = command.arg("-e").arg(explorer.unwrap().url);
}

match execute_command(&mut command) {
match execute_command(command) {
Ok(result) => {
*execution_status.lock().unwrap() = ExecutionStatus::Success;
if let Some(error_message) = result {
Expand All @@ -150,7 +151,6 @@ impl ExecuteDeployScriptScreen {
Err(e) => {
*execution_status.lock().unwrap() = ExecutionStatus::Failed;
*execution_error_message.lock().unwrap() = e.to_string();
return;
}
}
});
Expand Down Expand Up @@ -195,7 +195,7 @@ fn execute_command(command: &mut Command) -> Result<Option<String>, Box<dyn std:
let line = line?;
crate::errors::log(line.clone());
error_message.push_str(&line);
error_message.push_str("\n");
error_message.push('\n');
}
match result.wait() {
Ok(status) => {
Expand All @@ -206,10 +206,10 @@ fn execute_command(command: &mut Command) -> Result<Option<String>, Box<dyn std:
return Err(error_message.into());
}
}
return Ok(None);
Ok(None)
}
Err(e) => {
return Err(e.to_string().into());
Err(e.to_string().into())
}
}
}
Expand All @@ -222,7 +222,7 @@ impl Screen for ExecuteDeployScriptScreen {
self.execution_error_message.lock().unwrap()
));
buffer
.append_row_text_color(&"> Press any key to continue", constants::SELECTION_COLOR);
.append_row_text_color("> Press any key to continue", constants::SELECTION_COLOR);
} else {
buffer.append_row_text(&format!(
"{} Executing dry run\n",
Expand All @@ -243,7 +243,7 @@ impl Screen for ExecuteDeployScriptScreen {
buffer.append_row_text(&error_message);
}
buffer.append_row_text_color(
&"\n> Press any key to continue",
"\n> Press any key to continue",
constants::SELECTION_COLOR,
);
}
Expand Down
6 changes: 3 additions & 3 deletions script/cli/src/screens/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::screens::screen_manager::{Screen, ScreenResult};
use crate::screens::types::select::SelectComponent;
use crate::state_manager::STATE_MANAGER;
use crate::ui::Buffer;
use crate::workflows::deploy_contracts::deploy_contracts::DeployContractsWorkflow;
use crate::workflows::deploy::deploy_contracts::DeployContractsWorkflow;
use crate::workflows::error_workflow::ErrorWorkflow;
use crate::workflows::{
create_config::create_config::CreateConfigWorkflow,
register_contract::register_contract::RegisterContractWorkflow,
config::create_config::CreateConfigWorkflow,
register::register_contract::RegisterContractWorkflow,
};
use crossterm::event::Event;

Expand Down
6 changes: 3 additions & 3 deletions script/cli/src/screens/register_contract/get_contract_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ impl Screen for GetContractInfoScreen {
fn handle_input(&mut self, e: Event) -> Result<ScreenResult, Box<dyn std::error::Error>> {
if *self.execution_status.lock().unwrap() != ExecutionStatus::Pending {
let result = self.select.handle_input(e);
if result.is_some() {
if result.unwrap() == 0 {
if let Some(result) = result {
if result == 0 {
return Ok(ScreenResult::PreviousScreen);
} else if result.unwrap() == 1 {
} else if result == 1 {
return Ok(ScreenResult::NextScreen(None));
}
}
Expand Down
Loading
Loading