stacksdapp-deployer 0.2.0

A specialized deployment utility for broadcasting Clarity smart contracts to Stacks devnet, testnet, and mainnet.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Clean  -style deploy terminal UI.

use colored::Colorize;
use std::io::{self, Write};
use std::net::{SocketAddr, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

const SPINNER: &[&str] = &["", "", "", "", "", "", "", "", "", ""];

fn human_output_enabled() -> bool {
    !stacksdapp_shell::is_quiet()
}

fn println_human(line: impl std::fmt::Display) {
    if human_output_enabled() {
        println!("{line}");
    }
}

pub struct DeployUi {
    start: Instant,
    network: String,
    rpc: String,
    project: String,
    bar_finalized: AtomicBool,
}

/// In-place spinner that occupies the checkmark column until [`LiveStep::finish`].
pub struct LiveStep {
    label: String,
    stop: Arc<AtomicBool>,
    handle: Option<JoinHandle<()>>,
    finished: bool,
}

impl LiveStep {
    pub fn finish(mut self) {
        self.complete(true);
    }

    pub fn fail(mut self) {
        self.complete(false);
    }

    fn complete(&mut self, ok: bool) {
        if self.finished {
            return;
        }
        self.finished = true;
        self.stop.store(true, Ordering::SeqCst);
        if let Some(h) = self.handle.take() {
            let _ = h.join();
        }
        if !human_output_enabled() {
            return;
        }
        // Clear spinner line, then print final status.
        print!("\r\x1b[2K");
        if ok {
            println!(
                "{} {}",
                "".truecolor(52, 211, 153).bold(),
                self.label.white()
            );
        } else {
            println!(
                "{} {}",
                "".truecolor(239, 68, 68).bold(),
                self.label.white()
            );
        }
        let _ = io::stdout().flush();
    }
}

impl Drop for LiveStep {
    fn drop(&mut self) {
        if !self.finished {
            self.complete(false);
        }
    }
}

impl DeployUi {
    pub fn start(network: &str, rpc: &str) -> Self {
        let project = std::env::current_dir()
            .ok()
            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
            .unwrap_or_else(|| ".".into());

        let title = match network {
            "mainnet" => "Deploying to Stacks Mainnet 🚀",
            "devnet" => "Deploying to Local Devnet 🚀",
            _ => "Deploying to Stacks Testnet 🚀",
        };

        if human_output_enabled() {
            println!();
            println!("{}", "".repeat(46).truecolor(75, 85, 99));
            println!("{:^46}", title.bold().white());
            println!("{}", "".repeat(46).truecolor(75, 85, 99));
            println!();
            kv("Network", network);
            kv("RPC", rpc);
            kv("Project", &project);
            println!();
        }

        Self {
            start: Instant::now(),
            network: network.to_string(),
            rpc: rpc.to_string(),
            project,
            bar_finalized: AtomicBool::new(false),
        }
    }

    /// Start a live spinner on the current line; call [`LiveStep::finish`] when done.
    pub fn begin_step(&self, label: &str) -> LiveStep {
        if stacksdapp_shell::is_quiet() {
            return LiveStep {
                label: label.to_string(),
                stop: Arc::new(AtomicBool::new(true)),
                handle: None,
                finished: true,
            };
        }

        let stop = Arc::new(AtomicBool::new(false));
        let stop_c = Arc::clone(&stop);
        let label_c = label.to_string();

        // Seed the line immediately so the screen isn't blank.
        print!(
            "\r{} {}",
            SPINNER[0].truecolor(167, 139, 250),
            label.truecolor(156, 163, 175)
        );
        let _ = io::stdout().flush();

        let handle = thread::spawn(move || {
            let mut i = 0usize;
            while !stop_c.load(Ordering::Relaxed) {
                print!(
                    "\r{} {}",
                    SPINNER[i % SPINNER.len()].truecolor(167, 139, 250),
                    label_c.truecolor(156, 163, 175)
                );
                let _ = io::stdout().flush();
                i = i.wrapping_add(1);
                thread::sleep(Duration::from_millis(80));
            }
        });

        LiveStep {
            label: label.to_string(),
            stop,
            handle: Some(handle),
            finished: false,
        }
    }

    pub fn step_ok(&self, label: &str) {
        if !human_output_enabled() {
            return;
        }
        println!("{} {}", "".truecolor(52, 211, 153).bold(), label.white());
    }

    pub fn step_detail(&self, text: &str) {
        if !human_output_enabled() {
            return;
        }
        println!(
            "  {} {}",
            "".truecolor(156, 163, 175),
            text.truecolor(156, 163, 175)
        );
    }

    pub fn print_summary(&self, deployer: &str, contracts: &[String], fee_micro: u64) {
        if !human_output_enabled() {
            return;
        }
        println!();
        println!("{}", "".repeat(46).truecolor(75, 85, 99));
        println!();
        println!("{}", "Deployment Summary".bold().white());
        println!();
        kv("Deployer", &short_addr(deployer));
        kv("Contracts", &contracts.len().to_string());
        if fee_micro > 0 {
            kv("Fee", &format!("{:.6} STX", fee_micro as f64 / 1_000_000.0));
        }
        println!();
        for name in contracts {
            println!("  {}", name.truecolor(52, 211, 153));
        }
        println!();
        println!("{}", "".repeat(46).truecolor(75, 85, 99));
        println!();
    }

    pub fn confirm_continue(&self, yes: bool) -> anyhow::Result<bool> {
        use std::io::IsTerminal;

        if yes {
            return Ok(true);
        }
        if !std::io::stdin().is_terminal() {
            anyhow::bail!(
                "Refusing to deploy to {} without confirmation in a non-interactive terminal.\n\
                 Re-run with: stacksdapp deploy --network {} --yes",
                self.network,
                self.network
            );
        }
        if self.network == "mainnet" {
            eprintln!(
                "{}",
                "Mainnet broadcast — real funds will be spent."
                    .yellow()
                    .bold()
            );
        }
        print!("{} ", "Continue? (Y/n)".bold().white());
        io::stdout().flush()?;
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let t = input.trim();
        if t.is_empty() || t.eq_ignore_ascii_case("y") || t.eq_ignore_ascii_case("yes") {
            Ok(true)
        } else {
            Ok(false)
        }
    }

    pub fn broadcasting_start(&self) {
        if !human_output_enabled() {
            return;
        }
        self.bar_finalized.store(false, Ordering::SeqCst);
        println!();
        println!("{}", "Broadcasting...".bold().white());
        println!();
    }

    /// Update the single in-place progress bar. Finalizes (newline) only once at 100%.
    pub fn render_bar(&self, done: usize, total: usize) {
        if !human_output_enabled() {
            if done >= total {
                self.bar_finalized.store(true, Ordering::SeqCst);
            }
            return;
        }
        if self.bar_finalized.load(Ordering::SeqCst) {
            return;
        }
        let total = total.max(1);
        let pct = ((done * 100) / total).min(100);
        let width = 32usize;
        let filled = (pct * width) / 100;
        let bar: String = "".repeat(filled) + &"".repeat(width.saturating_sub(filled));
        print!("\r[{}] {pct}%   ", bar.truecolor(52, 211, 153));
        let _ = io::stdout().flush();
        if done >= total {
            self.bar_finalized.store(true, Ordering::SeqCst);
            println!();
            println!();
        }
    }

    pub fn contract_broadcast_ok(&self, name: &str, txid: &str) {
        if !human_output_enabled() {
            return;
        }
        println!("{} {}", "".truecolor(52, 211, 153).bold(), name.white());
        println!(
            "  {:<8} {}",
            "txid".truecolor(156, 163, 175),
            short_txid(txid).truecolor(156, 163, 175)
        );
        println!();
    }

    pub fn waiting_confirmation(&self) {
        println_human("Waiting for node confirmation...");
        if human_output_enabled() {
            println!();
        }
    }

    pub fn success(
        &self,
        entries: &[(String, String, String)], // name, full_contract_id, full_txid
        deploy_status: &str,
    ) {
        if !human_output_enabled() {
            return;
        }
        println!(
            "{} {}",
            "".truecolor(52, 211, 153).bold(),
            "Deployment complete.".white()
        );
        println!();
        println!("{}", "".repeat(46).truecolor(75, 85, 99));
        println!("{:^46}", "Success 🎉".bold().truecolor(52, 211, 153));
        println!("{}", "".repeat(46).truecolor(75, 85, 99));
        println!();

        println!("{}", "Contract".bold().white());
        println!();
        for (name, id, _) in entries {
            let _ = name;
            println!("{}", id.truecolor(52, 211, 153));
        }
        println!();

        println!("{}", "Transaction".bold().white());
        println!();
        for (_, _, txid) in entries {
            if txid.is_empty() {
                if deploy_status == "broadcast" {
                    println!(
                        "{}",
                        "(submitted to mempool — txid not captured; re-run with -vv or check explorer)"
                            .truecolor(156, 163, 175)
                    );
                } else {
                    println!("{}", "(pending)".truecolor(156, 163, 175));
                }
            } else {
                println!("{}", txid.white());
            }
        }
        println!();

        if deploy_status == "broadcast" && self.network != "devnet" {
            println!("{}", "Status".bold().white());
            println!();
            let timing = if self.network == "mainnet" {
                "Mainnet blocks typically confirm within 10–30 minutes."
            } else {
                "Testnet blocks typically confirm within 1–10 minutes."
            };
            println!(
                "{}",
                format!("Broadcast to mempool — {timing}").truecolor(156, 163, 175)
            );
            println!(
                "{}",
                "Use --wait-confirm to block until contracts appear on chain."
                    .truecolor(156, 163, 175)
            );
            println!();
        } else if deploy_status == "confirmed" && self.network == "devnet" {
            println!("{}", "Status".bold().white());
            println!();
            println!(
                "{}",
                "Confirmed on local devnet — contract source is live on stacks-core."
                    .truecolor(156, 163, 175)
            );
            println!();
        }

        println!("{}", "Generated".bold().white());
        println!();
        for f in [
            "frontend/src/generated/contracts.ts",
            "frontend/src/generated/hooks.ts",
            "frontend/src/generated/deployments.json",
        ] {
            println!(
                "{} {}",
                "".truecolor(52, 211, 153),
                f.truecolor(156, 163, 175)
            );
        }
        println!();

        println!("{}", "Next".bold().white());
        println!();
        if self.network == "devnet" {
            let local_url = detect_local_frontend_url();
            println!(
                "{}",
                format!("Open {local_url} in your browser")
                    .truecolor(52, 211, 153)
                    .bold()
            );
            println!(
                "{}",
                "(Devnet is already running from `stacksdapp dev` — use the Debug Contracts panel to interact.)"
                    .truecolor(156, 163, 175)
            );
        } else {
            println!(
                "{}",
                format!("stacksdapp dev --network {}", self.network)
                    .truecolor(52, 211, 153)
                    .bold()
            );
        }
        println!();

        if self.network != "devnet" {
            let chain = match self.network.as_str() {
                "mainnet" => "mainnet",
                _ => "testnet",
            };
            let explorer_urls: Vec<String> = entries
                .iter()
                .filter(|(_, _, txid)| !txid.is_empty() && *txid != "already-deployed")
                .map(|(_, _, txid)| {
                    format!(
                        "https://explorer.hiro.so/txid/{}?chain={chain}",
                        txid.trim_start_matches("0x")
                    )
                })
                .collect();

            if !explorer_urls.is_empty() {
                println!("{}", "Explorer".bold().white());
                println!();
                for url in explorer_urls {
                    println!("{}", url.truecolor(167, 139, 250));
                }
                println!();
                if deploy_status == "broadcast" {
                    println!(
                        "{}",
                        "Explorer pages may take 10–30 seconds to appear after broadcast."
                            .truecolor(156, 163, 175)
                    );
                } else {
                    println!(
                        "{}",
                        "Note: the explorer link may take 10–15 seconds to show the transaction while it indexes."
                            .truecolor(156, 163, 175)
                    );
                }
                println!();
            }
        }

        let secs = self.start.elapsed().as_secs_f64();
        println!("{} {:.1}s", "Done in".truecolor(156, 163, 175), secs);
        println!();
        let _ = (&self.rpc, &self.project);
    }

    pub fn dry_run_done(&self, contracts: &[String], fee_micro: u64) {
        if !human_output_enabled() {
            return;
        }
        println!();
        println!("{}", "Dry run complete — nothing broadcast.".bold().white());
        if fee_micro > 0 {
            println!("Estimated fee: {:.6} STX", fee_micro as f64 / 1_000_000.0);
        }
        println!("Contracts: {}", contracts.join(", "));
        println!(
            "{}",
            "Re-run without --dry-run to apply.".truecolor(156, 163, 175)
        );
        println!();
    }
}

fn kv(key: &str, value: &str) {
    if !human_output_enabled() {
        return;
    }
    println!("{:<12} {}", soft_grey(key), value.white());
}

fn soft_grey(s: &str) -> colored::ColoredString {
    s.truecolor(156, 163, 175)
}

pub fn short_addr(addr: &str) -> String {
    if addr.len() <= 14 {
        return addr.to_string();
    }
    format!("{}...{}", &addr[..8], &addr[addr.len().saturating_sub(6)..])
}

pub fn short_txid(txid: &str) -> String {
    let t = txid.trim_start_matches("0x");
    if t.len() <= 16 {
        return txid.to_string();
    }
    format!("{}...{}", &t[..8], &t[t.len().saturating_sub(7)..])
}

/// Best-effort detect where `stacksdapp dev` is serving the Next.js app.
fn detect_local_frontend_url() -> String {
    for port in [3000u16, 3001] {
        let Ok(addr) = format!("127.0.0.1:{port}").parse::<SocketAddr>() else {
            continue;
        };
        if TcpStream::connect_timeout(&addr, Duration::from_millis(250)).is_ok() {
            return format!("http://localhost:{port}");
        }
    }
    "http://localhost:3000".to_string()
}

#[cfg(test)]
mod tests {
    use super::{human_output_enabled, DeployUi};
    use stacksdapp_shell::{init, ColorMode, Format, Shell};

    #[test]
    fn deploy_ui_respects_quiet_mode() {
        init(Shell {
            verbosity: 0,
            quiet: true,
            format: Format::Human,
            color: ColorMode::Never,
        });
        assert!(!human_output_enabled());
        let ui = DeployUi::start("devnet", "http://localhost:3999");
        ui.step_detail("hidden");
        ui.print_summary("ST1PQ", &["counter".into()], 1000);
        ui.dry_run_done(&["counter".into()], 1000);
        ui.success(
            &[("counter".into(), "ST1PQ.counter".into(), "0xabc".into())],
            "confirmed",
        );
        ui.begin_step("quiet step").finish();
    }
}