leo_lang/cli/commands/common/
interactive.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use super::*;
18use leo_ast::NetworkName;
19
20/// Asks the user to confirm an action, with an optional `--yes` override.
21pub fn confirm(prompt: &str, skip_confirmation: bool) -> Result<bool> {
22    if skip_confirmation {
23        return Ok(true);
24    }
25
26    let result = Confirm::with_theme(&ColorfulTheme::default())
27        .with_prompt(prompt)
28        .default(false)
29        .interact()
30        .map_err(|e| CliError::custom(format!("Failed to prompt user: {e}")).into());
31
32    // Print a newline for better formatting.
33    println!();
34
35    result
36}
37
38/// Asks the user to confirm a fee.
39pub fn confirm_fee<N: Network>(
40    fee: &snarkvm::prelude::Fee<N>,
41    private_key: &PrivateKey<N>,
42    address: &Address<N>,
43    endpoint: &str,
44    network: NetworkName,
45    context: &Context,
46    skip: bool,
47) -> Result<bool> {
48    // Get the fee amount.
49    let total_cost = (*fee.amount()? as f64) / 1_000_000.0;
50    if fee.is_fee_public() {
51        let public_balance = get_public_balance(private_key, endpoint, network, context)? as f64 / 1_000_000.0;
52        println!("💰Your current public balance is {public_balance} credits.\n");
53        if public_balance < total_cost {
54            return Err(PackageError::insufficient_balance(address, public_balance, total_cost).into());
55        }
56    }
57    // Confirm the transaction.
58    confirm(&format!("This transaction will cost you {total_cost} credits. Do you want to proceed?"), skip)
59}