snarkos_cli/commands/developer/
deploy.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::Developer;
17use snarkvm::{
18    circuit::{Aleo, AleoCanaryV0, AleoTestnetV0, AleoV0},
19    console::{
20        network::{CanaryV0, MainnetV0, Network, TestnetV0},
21        program::ProgramOwner,
22    },
23    prelude::{
24        PrivateKey,
25        ProgramID,
26        VM,
27        block::Transaction,
28        deployment_cost,
29        query::Query,
30        store::{ConsensusStore, helpers::memory::ConsensusMemory},
31    },
32};
33
34use aleo_std::StorageMode;
35use anyhow::{Result, bail};
36use clap::Parser;
37use colored::Colorize;
38use std::{path::PathBuf, str::FromStr};
39use zeroize::Zeroize;
40
41/// Deploys an Aleo program.
42#[derive(Debug, Parser)]
43pub struct Deploy {
44    /// The name of the program to deploy.
45    program_id: String,
46    /// Specify the network to create a deployment for.
47    #[clap(default_value = "0", long = "network")]
48    pub network: u16,
49    /// A path to a directory containing a manifest file. Defaults to the current working directory.
50    #[clap(long)]
51    path: Option<String>,
52    /// The private key used to generate the deployment.
53    #[clap(short, long)]
54    private_key: String,
55    /// The endpoint to query node state from.
56    #[clap(short, long)]
57    query: String,
58    /// The priority fee in microcredits.
59    #[clap(long)]
60    priority_fee: u64,
61    /// The record to spend the fee from.
62    #[clap(short, long)]
63    record: Option<String>,
64    /// The endpoint used to broadcast the generated transaction.
65    #[clap(short, long, conflicts_with = "dry_run")]
66    broadcast: Option<String>,
67    /// Performs a dry-run of transaction generation.
68    #[clap(short, long, conflicts_with = "broadcast")]
69    dry_run: bool,
70    /// Store generated deployment transaction to a local file.
71    #[clap(long)]
72    store: Option<String>,
73    /// Specify the path to a directory containing the ledger. Overrides the default path (also for
74    /// dev).
75    #[clap(long = "storage_path")]
76    storage_path: Option<PathBuf>,
77}
78
79impl Drop for Deploy {
80    /// Zeroize the private key when the `Deploy` struct goes out of scope.
81    fn drop(&mut self) {
82        self.private_key.zeroize();
83    }
84}
85
86impl Deploy {
87    /// Deploys an Aleo program.
88    pub fn parse(self) -> Result<String> {
89        // Ensure that the user has specified an action.
90        if !self.dry_run && self.broadcast.is_none() && self.store.is_none() {
91            bail!("❌ Please specify one of the following actions: --broadcast, --dry-run, --store");
92        }
93
94        // Construct the deployment for the specified network.
95        match self.network {
96            MainnetV0::ID => self.construct_deployment::<MainnetV0, AleoV0>(),
97            TestnetV0::ID => self.construct_deployment::<TestnetV0, AleoTestnetV0>(),
98            CanaryV0::ID => self.construct_deployment::<CanaryV0, AleoCanaryV0>(),
99            unknown_id => bail!("Unknown network ID ({unknown_id})"),
100        }
101    }
102
103    /// Construct and process the deployment transaction.
104    fn construct_deployment<N: Network, A: Aleo<Network = N, BaseField = N::Field>>(&self) -> Result<String> {
105        // Specify the query
106        let query = Query::from(&self.query);
107
108        // Retrieve the private key.
109        let private_key = PrivateKey::from_str(&self.private_key)?;
110
111        // Retrieve the program ID.
112        let program_id = ProgramID::from_str(&self.program_id)?;
113
114        // Fetch the package from the directory.
115        let package = Developer::parse_package(program_id, &self.path)?;
116
117        println!("📦 Creating deployment transaction for '{}'...\n", &program_id.to_string().bold());
118
119        // Generate the deployment
120        let deployment = package.deploy::<A>(None)?;
121        let deployment_id = deployment.to_deployment_id()?;
122
123        // Generate the deployment transaction.
124        let transaction = {
125            // Initialize an RNG.
126            let rng = &mut rand::thread_rng();
127
128            // Initialize the storage.
129            let storage_mode = match &self.storage_path {
130                Some(path) => StorageMode::Custom(path.clone()),
131                None => StorageMode::Production,
132            };
133            let store = ConsensusStore::<N, ConsensusMemory<N>>::open(storage_mode)?;
134
135            // Initialize the VM.
136            let vm = VM::from(store)?;
137
138            // Compute the minimum deployment cost.
139            let (minimum_deployment_cost, (_, _, _)) = deployment_cost(&deployment)?;
140
141            // Prepare the fees.
142            let fee = match &self.record {
143                Some(record) => {
144                    let fee_record = Developer::parse_record(&private_key, record)?;
145                    let fee_authorization = vm.authorize_fee_private(
146                        &private_key,
147                        fee_record,
148                        minimum_deployment_cost,
149                        self.priority_fee,
150                        deployment_id,
151                        rng,
152                    )?;
153                    vm.execute_fee_authorization(fee_authorization, Some(query), rng)?
154                }
155                None => {
156                    let fee_authorization = vm.authorize_fee_public(
157                        &private_key,
158                        minimum_deployment_cost,
159                        self.priority_fee,
160                        deployment_id,
161                        rng,
162                    )?;
163                    vm.execute_fee_authorization(fee_authorization, Some(query), rng)?
164                }
165            };
166            // Construct the owner.
167            let owner = ProgramOwner::new(&private_key, deployment_id, rng)?;
168
169            // Create a new transaction.
170            Transaction::from_deployment(owner, deployment, fee)?
171        };
172        println!("✅ Created deployment transaction for '{}'", program_id.to_string().bold());
173
174        // Determine if the transaction should be broadcast, stored, or displayed to the user.
175        Developer::handle_transaction(&self.broadcast, self.dry_run, &self.store, transaction, program_id.to_string())
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182    use crate::commands::{CLI, Command};
183
184    #[test]
185    fn clap_snarkos_deploy() {
186        let arg_vec = vec![
187            "snarkos",
188            "developer",
189            "deploy",
190            "--private-key",
191            "PRIVATE_KEY",
192            "--query",
193            "QUERY",
194            "--priority-fee",
195            "77",
196            "--record",
197            "RECORD",
198            "hello.aleo",
199        ];
200        let cli = CLI::parse_from(arg_vec);
201
202        if let Command::Developer(Developer::Deploy(deploy)) = cli.command {
203            assert_eq!(deploy.network, 0);
204            assert_eq!(deploy.program_id, "hello.aleo");
205            assert_eq!(deploy.private_key, "PRIVATE_KEY");
206            assert_eq!(deploy.query, "QUERY");
207            assert_eq!(deploy.priority_fee, 77);
208            assert_eq!(deploy.record, Some("RECORD".to_string()));
209        } else {
210            panic!("Unexpected result of clap parsing!");
211        }
212    }
213}