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