gcli/cmd/create.rs
1// This file is part of Gear.
2//
3// Copyright (C) 2021-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! command `create`
20use crate::{App, result::Result, utils::Hex};
21use clap::Parser;
22
23/// Deploy program to gear node
24#[derive(Clone, Debug, Parser)]
25pub struct Create {
26 /// gear program code id
27 code_id: String,
28 /// gear program salt ( hex encoding )
29 #[arg(short, long, default_value = "0x")]
30 salt: String,
31 /// gear program init payload ( hex encoding )
32 #[arg(short, long, default_value = "0x")]
33 init_payload: String,
34 /// gear program gas limit
35 ///
36 /// Use estimated gas limit automatically if not set.
37 #[arg(short, long)]
38 gas_limit: Option<u64>,
39 /// gear program balance
40 #[arg(short, long, default_value = "0")]
41 value: u128,
42}
43
44impl Create {
45 /// Exec command submit
46 pub async fn exec(&self, app: &impl App) -> Result<()> {
47 let code_id = self.code_id.to_hash()?.into();
48 let payload = self.init_payload.to_vec()?;
49 let signer = app.signer().await?;
50
51 // estimate gas
52 let gas_limit = if let Some(gas_limit) = self.gas_limit {
53 gas_limit
54 } else {
55 signer
56 .calculate_create_gas(None, code_id, payload.clone(), self.value, false)
57 .await?
58 .min_limit
59 };
60
61 // create program
62 signer
63 .create_program(code_id, self.salt.to_vec()?, payload, gas_limit, self.value)
64 .await?;
65
66 Ok(())
67 }
68}