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
// This file is part of Gear.

// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{ActorId, CodeId, MessageId};
use gcore::errors::Result;
use gstd_codegen::wait_create_program_for_reply;

/// Create a new program from the already existing on-chain code identified by
/// [`CodeId`].
///
/// The function returns an initial message identifier and a newly created
/// program identifier.
///
/// The first argument is the code identifier (see [`CodeId`] for details). The
/// second argument is an arbitrary byte sequence (also known as `salt`) that
/// allows the creation of multiple programs from the same code. The third and
/// last arguments are the initialization message's payload and value to be
/// transferred to the newly created program.
///
/// # Examples
///
/// Create a new program from the provided code identifier:
///
/// ```
/// use gstd::{msg, prog, CodeId};
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     let code_id: CodeId = msg::load().expect("Unable to load");
///     let (init_message_id, new_program_id) =
///         prog::create_program_bytes(code_id, "salt", b"INIT", 0)
///             .expect("Unable to create a program");
///     msg::send_bytes(new_program_id, b"PING", 0).expect("Unable to send");
/// }
/// ```
#[wait_create_program_for_reply]
pub fn create_program_bytes(
    code_id: CodeId,
    salt: impl AsRef<[u8]>,
    payload: impl AsRef<[u8]>,
    value: u128,
) -> Result<(MessageId, ActorId)> {
    create_program_bytes_delayed(code_id, salt, payload, value, 0)
}

/// Same as [`create_program_bytes`], but creates a new program after the
/// `delay` expressed in block count.
///
/// # Examples
///
/// Create a new program from the provided code identifier after 100 blocks and
/// send a message to it after 200 blocks:
///
/// ```
/// use gstd::{msg, prog, CodeId};
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     let code_id: CodeId = msg::load().expect("Unable to load");
///     let (init_message_id, new_program_id) =
///         prog::create_program_bytes_delayed(code_id, "salt", b"INIT", 0, 100)
///             .expect("Unable to create a program");
///     msg::send_bytes_delayed(new_program_id, b"PING", 0, 200).expect("Unable to send");
/// }
/// ```
pub fn create_program_bytes_delayed(
    code_id: CodeId,
    salt: impl AsRef<[u8]>,
    payload: impl AsRef<[u8]>,
    value: u128,
    delay: u32,
) -> Result<(MessageId, ActorId)> {
    gcore::prog::create_program_delayed(code_id, salt.as_ref(), payload.as_ref(), value, delay)
}

/// Same as [`create_program_bytes`], but with an explicit gas limit.
#[cfg(not(feature = "ethexe"))]
#[wait_create_program_for_reply]
pub fn create_program_bytes_with_gas(
    code_id: CodeId,
    salt: impl AsRef<[u8]>,
    payload: impl AsRef<[u8]>,
    gas_limit: u64,
    value: u128,
) -> Result<(MessageId, ActorId)> {
    create_program_bytes_with_gas_delayed(code_id, salt, payload, gas_limit, value, 0)
}

/// Same as [`create_program_bytes_with_gas`], but creates a new program after
/// the `delay` expressed in block count.
#[cfg(not(feature = "ethexe"))]
pub fn create_program_bytes_with_gas_delayed(
    code_id: CodeId,
    salt: impl AsRef<[u8]>,
    payload: impl AsRef<[u8]>,
    gas_limit: u64,
    value: u128,
    delay: u32,
) -> Result<(MessageId, ActorId)> {
    gcore::prog::create_program_with_gas_delayed(
        code_id,
        salt.as_ref(),
        payload.as_ref(),
        gas_limit,
        value,
        delay,
    )
}