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 124 125 126 127 128 129 130 131 132 133 134 135 136 137
//! Deploy contains types for deploying contracts.
//!
//! Contracts are assigned an ID that is derived from a set of arguments. A
//! contract may choose which set of arguments to use to deploy with:
//!
//! - [Deployer::with_current_contract] – A contract deployed by the currently
//! executing contract will have an ID derived from the currently executing
//! contract's ID.
//!
//! The deployer can be created using [Env::deployer].
//!
//! ### Examples
//!
//! ```
//! # use soroban_sdk::{contractimpl, BytesN, Env, Symbol};
//! #
//! # pub struct Contract;
//! #
//! # #[contractimpl]
//! # impl Contract {
//! # pub fn f(env: Env, wasm_hash: BytesN<32>) {
//! # let salt = [0u8; 32];
//! let deployer = env.deployer().with_current_contract(&salt);
//! let contract_id = deployer.deploy(&wasm_hash);
//! # }
//! # }
//! #
//! # #[cfg(feature = "testutils")]
//! # fn main() {
//! # let env = Env::default();
//! # let contract_id = env.register_contract(None, Contract);
//! # // Install the contract code before deploying its instance.
//! # let wasm_hash = env.install_contract_wasm(&[0u8; 100]);
//! # ContractClient::new(&env, &contract_id).f(&wasm_hash);
//! # }
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
use crate::{env::internal::Env as _, unwrap::UnwrapInfallible, Address, BytesN, Env, IntoVal};
/// Deployer provides access to deploying contracts.
pub struct Deployer {
env: Env,
}
impl Deployer {
pub(crate) fn new(env: &Env) -> Deployer {
Deployer { env: env.clone() }
}
pub fn env(&self) -> &Env {
&self.env
}
/// Get a deployer that deploys contracts that derive their contract IDs
/// from the current contract and the provided salt.
pub fn with_current_contract(
&self,
salt: &impl IntoVal<Env, BytesN<32>>,
) -> DeployerWithCurrentContract {
let env = self.env();
DeployerWithCurrentContract {
env: env.clone(),
salt: salt.into_val(env),
}
}
#[doc(hidden)]
/// Get a deployer for contracts that derive their contract IDs from the
/// given contract ID and the provided salt.
pub fn with_other_contract(
&self,
contract_id: &Address,
salt: &impl IntoVal<Env, BytesN<32>>,
) -> DeployerWithOtherContract {
let env = self.env();
DeployerWithOtherContract {
env: env.clone(),
contract_id: contract_id.clone(),
salt: salt.into_val(env),
}
}
}
/// A deployer that deploys a contract that has its ID derived from the current
/// contract ID and the provided salt.
pub struct DeployerWithCurrentContract {
env: Env,
salt: BytesN<32>,
}
impl DeployerWithCurrentContract {
/// Return the ID of the contract defined by the deployer.
#[doc(hidden)]
pub fn id(&self) -> Address {
todo!()
}
/// Deploy a contract.
///
/// The contract ID from the currently executing contract and the given salt
/// will be used to derive a contract ID for the deployed contract.
///
/// Returns the deployed contract's ID.
pub fn deploy(&self, wasm_hash: &impl IntoVal<Env, BytesN<32>>) -> Address {
let env = &self.env;
let id = env
.create_contract_from_contract(
wasm_hash.into_val(env).to_object(),
self.salt.to_object(),
)
.unwrap_infallible();
let id = unsafe { BytesN::<32>::unchecked_new(env.clone(), id) };
Address::from_contract_id(&id)
}
}
#[doc(hidden)]
/// A deployer for contracts that derive their contract IDs from the
/// given contract ID and the provided salt.
///
/// This deployer is unable to actually deploy contracts because the currently
/// executing contract can only deploy contracts with IDs derived from its own
/// contract ID or an ed25519 public key.
pub struct DeployerWithOtherContract {
env: Env,
contract_id: Address,
salt: BytesN<32>,
}
impl DeployerWithOtherContract {
#[doc(hidden)]
/// Return the ID of the contract defined by the deployer.
pub fn id(&self) -> Address {
todo!()
}
}