generated from Uniswap/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathexecute_deploy_script.rs
267 lines (243 loc) · 9.77 KB
/
execute_deploy_script.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::constants;
use crate::libs::explorer::{ExplorerApiLib, SupportedExplorerType};
use crate::screens::screen_manager::{Screen, ScreenResult};
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};
pub struct ExecuteDeployScriptScreen {
execution_status: Arc<Mutex<ExecutionStatus>>,
execution_error_message: Arc<Mutex<String>>,
}
#[derive(PartialEq, Debug, PartialOrd)]
enum ExecutionStatus {
Failed = -1,
Pending = 0,
DryRunCompleted = 1,
DeploymentCompleted = 2,
Success = 3,
}
impl ExecuteDeployScriptScreen {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let screen = ExecuteDeployScriptScreen {
execution_status: Arc::new(Mutex::new(ExecutionStatus::Pending)),
execution_error_message: Arc::new(Mutex::new(String::new())),
};
let working_dir = STATE_MANAGER.working_directory.clone();
let execution_status = Arc::clone(&screen.execution_status);
let execution_error_message = Arc::clone(&screen.execution_error_message);
let chain_id = STATE_MANAGER
.workflow_state
.lock()?
.chain_id
.clone()
.unwrap();
let rpc_url = STATE_MANAGER
.workflow_state
.lock()?
.web3
.clone()
.unwrap()
.rpc_url;
let private_key = STATE_MANAGER
.workflow_state
.lock()?
.private_key
.clone()
.unwrap();
let explorer = STATE_MANAGER.workflow_state.lock()?.block_explorer.clone();
let explorer_api_key = STATE_MANAGER
.workflow_state
.lock()?
.explorer_api_key
.clone();
tokio::spawn(async move {
let mut command = &mut Command::new("forge");
command = command
.arg("script")
.arg(
working_dir
.join("script")
.join("deploy")
.join("Deploy-all.s.sol"),
)
.arg(format!("--rpc-url={}", rpc_url))
.arg("-vvvv")
.arg(format!("--private-key={}", private_key));
match execute_command(command) {
Ok(result) => {
*execution_status.lock().unwrap() = ExecutionStatus::DryRunCompleted;
if let Some(error_message) = result {
*execution_error_message.lock().unwrap() = error_message;
}
}
Err(e) => {
*execution_status.lock().unwrap() = ExecutionStatus::Failed;
*execution_error_message.lock().unwrap() = e.to_string();
return;
}
}
if explorer.is_some() && explorer_api_key.is_some() {
let explorer_api =
ExplorerApiLib::new(explorer.clone().unwrap(), explorer_api_key.unwrap())
.unwrap();
command = command
.arg("--verify")
.arg(format!(
"--verifier={}",
if explorer_api.explorer.explorer_type == SupportedExplorerType::Blockscout
{
"blockscout"
} else {
// custom also works for etherscan
"custom"
}
))
.arg(format!("--verifier-url={}", explorer_api.api_url))
.arg(format!("--verifier-api-key={}", explorer_api.api_key));
}
match execute_command(command.arg("--broadcast").arg("--skip-simulation")) {
Ok(result) => {
*execution_status.lock().unwrap() = ExecutionStatus::DeploymentCompleted;
if let Some(error_message) = result {
*execution_error_message.lock().unwrap() = error_message;
}
}
Err(e) => {
*execution_status.lock().unwrap() = ExecutionStatus::Failed;
*execution_error_message.lock().unwrap() = e.to_string();
return;
}
}
let _ =
std::fs::remove_file(get_config_dir(chain_id.clone()).join("task-pending.json"));
let mut command = &mut Command::new("node");
command = command
.arg(working_dir.join("lib").join("forge-chronicles"))
.arg("Deploy-all.s.sol")
.arg("-c")
.arg(chain_id.clone())
.arg("--force");
if explorer.is_some() {
command = command.arg("-e").arg(explorer.unwrap().url);
}
match execute_command(command) {
Ok(result) => {
*execution_status.lock().unwrap() = ExecutionStatus::Success;
if let Some(error_message) = result {
*execution_error_message.lock().unwrap() = error_message;
}
}
Err(e) => {
*execution_status.lock().unwrap() = ExecutionStatus::Failed;
*execution_error_message.lock().unwrap() = e.to_string();
}
}
});
Ok(screen)
}
fn get_status_mark(&self, target_status: ExecutionStatus) -> char {
if *self.execution_status.lock().unwrap() == target_status {
get_spinner_frame()
} else if *self.execution_status.lock().unwrap() < target_status {
' '
} else {
'✓'
}
}
}
fn execute_command(command: &mut Command) -> Result<Option<String>, Box<dyn std::error::Error>> {
let cmd_str = format!("{:?}", command);
let re = Regex::new(r"--private-key=0x[a-fA-F0-9]+").unwrap();
let masked_cmd = re.replace_all(&cmd_str, "--private-key=***");
crate::errors::log(format!("Executing command: {}", masked_cmd));
let mut result = command
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()?;
// Handle stdout
let stdout = result.stdout.take().expect("Failed to capture stdout");
let stdout_reader = std::io::BufReader::new(stdout);
for line in stdout_reader.lines() {
let line = line?;
crate::errors::log(line);
}
// Handle stderr
let stderr = result.stderr.take().expect("Failed to capture stderr");
let stderr_reader = std::io::BufReader::new(stderr);
let mut error_message = String::new();
for line in stderr_reader.lines() {
let line = line?;
crate::errors::log(line.clone());
error_message.push_str(&line);
error_message.push('\n');
}
match result.wait() {
Ok(status) => {
if !status.success() {
if error_message.contains("verify") || error_message.contains("verification") {
return Ok(Some("Verification of one or more contracts failed. Check the debug screen for more information".to_string()));
} else {
return Err(error_message.into());
}
}
Ok(None)
}
Err(e) => Err(e.to_string().into()),
}
}
impl Screen for ExecuteDeployScriptScreen {
fn render_content(&self, buffer: &mut Buffer) -> Result<(), Box<dyn std::error::Error>> {
if *self.execution_status.lock().unwrap() == ExecutionStatus::Failed {
buffer.append_row_text(&format!(
"Deployment failed: {}\n",
self.execution_error_message.lock().unwrap()
));
buffer.append_row_text_color("> Press any key to continue", constants::SELECTION_COLOR);
} else {
buffer.append_row_text(&format!(
"{} Executing dry run\n",
self.get_status_mark(ExecutionStatus::Pending)
));
buffer.append_row_text(&format!(
"{} Deploying contracts\n",
self.get_status_mark(ExecutionStatus::DryRunCompleted)
));
buffer.append_row_text(&format!(
"{} Generating deployment logs\n",
self.get_status_mark(ExecutionStatus::DeploymentCompleted)
));
if *self.execution_status.lock().unwrap() == ExecutionStatus::Success {
buffer.append_row_text("Deployment successful\n");
let error_message = self.execution_error_message.lock().unwrap();
if !error_message.is_empty() {
buffer.append_row_text(&error_message);
}
buffer.append_row_text_color(
"\n> Press any key to continue",
constants::SELECTION_COLOR,
);
}
buffer.append_row_text_color(
"\nUse CTRL+D to toggle debug mode and view console output.",
constants::INSTRUCTIONS_COLOR,
);
}
Ok(())
}
fn handle_input(&mut self, _: Event) -> Result<ScreenResult, Box<dyn std::error::Error>> {
if *self.execution_status.lock().unwrap() == ExecutionStatus::Success {
return Ok(ScreenResult::NextScreen(None));
} else if *self.execution_status.lock().unwrap() == ExecutionStatus::Failed {
return Ok(ScreenResult::Reset);
}
Ok(ScreenResult::Continue)
}
fn execute(&mut self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
}