soroban_sdk/deploy.rs
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
//! 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
//!
//! #### Deploy a contract without constructor (or 0-argument constructor)
//!
//! ```
//! use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol};
//!
//! const DEPLOYED_WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm");
//!
//! #[contract]
//! pub struct Contract;
//!
//! #[contractimpl]
//! impl Contract {
//! pub fn deploy(env: Env, wasm_hash: BytesN<32>) {
//! let salt = [0u8; 32];
//! let deployer = env.deployer().with_current_contract(salt);
//! let contract_address = deployer.deploy_v2(wasm_hash, ());
//! // ...
//! }
//! }
//!
//! #[test]
//! fn test() {
//! # }
//! # #[cfg(feature = "testutils")]
//! # fn main() {
//! let env = Env::default();
//! let contract_address = env.register(Contract, ());
//! let contract = ContractClient::new(&env, &contract_address);
//! // Upload the contract code before deploying its instance.
//! let wasm_hash = env.deployer().upload_contract_wasm(DEPLOYED_WASM);
//! contract.deploy(&wasm_hash);
//! }
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
//!
//! #### Deploy a contract with a multi-argument constructor
//!
//! ```
//! use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol, IntoVal};
//!
//! const DEPLOYED_WASM_WITH_CTOR: &[u8] = include_bytes!("../doctest_fixtures/contract_with_constructor.wasm");
//!
//! #[contract]
//! pub struct Contract;
//!
//! #[contractimpl]
//! impl Contract {
//! pub fn deploy_with_constructor(env: Env, wasm_hash: BytesN<32>) {
//! let salt = [1u8; 32];
//! let deployer = env.deployer().with_current_contract(salt);
//! let contract_address = deployer.deploy_v2(
//! wasm_hash,
//! (1_u32, 2_i64),
//! );
//! // ...
//! }
//! }
//!
//! #[test]
//! fn test() {
//! # }
//! # #[cfg(feature = "testutils")]
//! # fn main() {
//! let env = Env::default();
//! let contract_address = env.register(Contract, ());
//! let contract = ContractClient::new(&env, &contract_address);
//! // Upload the contract code before deploying its instance.
//! let wasm_hash = env.deployer().upload_contract_wasm(DEPLOYED_WASM_WITH_CTOR);
//! contract.deploy_with_constructor(&wasm_hash);
//! }
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
//!
//! #### Derive before deployment what the address of a contract will be
//!
//! ```
//! use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, Symbol, IntoVal};
//!
//! #[contract]
//! pub struct Contract;
//!
//! #[contractimpl]
//! impl Contract {
//! pub fn deploy_contract_address(env: Env) -> Address {
//! let salt = [1u8; 32];
//! let deployer = env.deployer().with_current_contract(salt);
//! // Deployed contract address is deterministic and can be accessed
//! // before deploying the contract. It is derived from the deployer
//! // (the current contract's address) and the salt passed in above.
//! deployer.deployed_address()
//! }
//! }
//!
//! #[test]
//! fn test() {
//! # }
//! # #[cfg(feature = "testutils")]
//! # fn main() {
//! let env = Env::default();
//! let contract_address = env.register(Contract, ());
//! let contract = ContractClient::new(&env, &contract_address);
//! assert_eq!(
//! contract.deploy_contract_address(),
//! Address::from_str(&env, "CBESJIMX7J53SWJGJ7WQ6QTLJI4S5LPPJNC2BNVD63GIKAYCDTDOO322"),
//! );
//! }
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
use crate::{
env::internal::Env as _, unwrap::UnwrapInfallible, Address, Bytes, BytesN, ConstructorArgs,
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 contract that derive the contract IDs
/// from the current contract and provided salt.
pub fn with_current_contract(
&self,
salt: impl IntoVal<Env, BytesN<32>>,
) -> DeployerWithAddress {
DeployerWithAddress {
env: self.env.clone(),
address: self.env.current_contract_address(),
salt: salt.into_val(&self.env),
}
}
/// Get a deployer that deploys contracts that derive the contract ID
/// from the provided address and salt.
///
/// The deployer address must authorize all the deployments.
pub fn with_address(
&self,
address: Address,
salt: impl IntoVal<Env, BytesN<32>>,
) -> DeployerWithAddress {
DeployerWithAddress {
env: self.env.clone(),
address,
salt: salt.into_val(&self.env),
}
}
/// Get a deployer that deploys an instance of Stellar Asset Contract
/// corresponding to the provided serialized asset.
///
/// `serialized_asset` is the Stellar `Asset` XDR serialized to bytes. Refer
/// to `[soroban_sdk::xdr::Asset]`
pub fn with_stellar_asset(
&self,
serialized_asset: impl IntoVal<Env, Bytes>,
) -> DeployerWithAsset {
DeployerWithAsset {
env: self.env.clone(),
serialized_asset: serialized_asset.into_val(&self.env),
}
}
/// Upload the contract Wasm code to the network.
///
/// Returns the hash of the uploaded Wasm that can be then used for
/// the contract deployment.
/// ### Examples
/// ```
/// use soroban_sdk::{BytesN, Env};
///
/// const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm");
///
/// #[test]
/// fn test() {
/// # }
/// # fn main() {
/// let env = Env::default();
/// env.deployer().upload_contract_wasm(WASM);
/// }
/// ```
pub fn upload_contract_wasm(&self, contract_wasm: impl IntoVal<Env, Bytes>) -> BytesN<32> {
self.env
.upload_wasm(contract_wasm.into_val(&self.env).to_object())
.unwrap_infallible()
.into_val(&self.env)
}
/// Replaces the executable of the current contract with the provided Wasm.
///
/// The Wasm blob identified by the `wasm_hash` has to be already present
/// in the ledger (uploaded via `[Deployer::upload_contract_wasm]`).
///
/// The function won't do anything immediately. The contract executable
/// will only be updated after the invocation has successfully finished.
pub fn update_current_contract_wasm(&self, wasm_hash: impl IntoVal<Env, BytesN<32>>) {
self.env
.update_current_contract_wasm(wasm_hash.into_val(&self.env).to_object())
.unwrap_infallible();
}
/// Extend the TTL of the contract instance and code.
///
/// Extends the TTL of the instance and code only if the TTL for the provided contract is below `threshold` ledgers.
/// The TTL will then become `extend_to`. Note that the `threshold` check and TTL extensions are done for both the
/// contract code and contract instance, so it's possible that one is bumped but not the other depending on what the
/// current TTL's are.
///
/// The TTL is the number of ledgers between the current ledger and the final ledger the data can still be accessed.
pub fn extend_ttl(&self, contract_address: Address, threshold: u32, extend_to: u32) {
self.env
.extend_contract_instance_and_code_ttl(
contract_address.to_object(),
threshold.into(),
extend_to.into(),
)
.unwrap_infallible();
}
/// Extend the TTL of the contract instance.
///
/// Same as [`extend_ttl`](Self::extend_ttl) but only for contract instance.
pub fn extend_ttl_for_contract_instance(
&self,
contract_address: Address,
threshold: u32,
extend_to: u32,
) {
self.env
.extend_contract_instance_ttl(
contract_address.to_object(),
threshold.into(),
extend_to.into(),
)
.unwrap_infallible();
}
/// Extend the TTL of the contract code.
///
/// Same as [`extend_ttl`](Self::extend_ttl) but only for contract code.
pub fn extend_ttl_for_code(&self, contract_address: Address, threshold: u32, extend_to: u32) {
self.env
.extend_contract_code_ttl(
contract_address.to_object(),
threshold.into(),
extend_to.into(),
)
.unwrap_infallible();
}
}
/// A deployer that deploys a contract that has its ID derived from the provided
/// address and salt.
pub struct DeployerWithAddress {
env: Env,
address: Address,
salt: BytesN<32>,
}
impl DeployerWithAddress {
/// Return the address of the contract defined by the deployer.
///
/// This function can be called at anytime, before or after the contract is
/// deployed, because contract addresses are deterministic.
pub fn deployed_address(&self) -> Address {
self.env
.get_contract_id(self.address.to_object(), self.salt.to_object())
.unwrap_infallible()
.into_val(&self.env)
}
/// Deploy a contract that uses Wasm executable with provided hash.
///
/// The address of the deployed contract is defined by the deployer address
/// and provided salt.
///
/// Returns the deployed contract's address.
#[deprecated(note = "use deploy_v2")]
pub fn deploy(&self, wasm_hash: impl IntoVal<Env, BytesN<32>>) -> Address {
let env = &self.env;
let address_obj = env
.create_contract(
self.address.to_object(),
wasm_hash.into_val(env).to_object(),
self.salt.to_object(),
)
.unwrap_infallible();
unsafe { Address::unchecked_new(env.clone(), address_obj) }
}
/// Deploy a contract that uses Wasm executable with provided hash.
///
/// The constructor args will be passed to the contract's constructor. Pass
/// `()` for contract's with no constructor or a constructor with zero
/// arguments.
///
/// The address of the deployed contract is defined by the deployer address
/// and provided salt.
///
/// Returns the deployed contract's address.
pub fn deploy_v2<A>(
&self,
wasm_hash: impl IntoVal<Env, BytesN<32>>,
constructor_args: A,
) -> Address
where
A: ConstructorArgs,
{
let env = &self.env;
let address_obj = env
.create_contract_with_constructor(
self.address.to_object(),
wasm_hash.into_val(env).to_object(),
self.salt.to_object(),
constructor_args.into_val(env).to_object(),
)
.unwrap_infallible();
unsafe { Address::unchecked_new(env.clone(), address_obj) }
}
}
pub struct DeployerWithAsset {
env: Env,
serialized_asset: Bytes,
}
impl DeployerWithAsset {
/// Return the address of the contract defined by the deployer.
///
/// This function can be called at anytime, before or after the contract is
/// deployed, because contract addresses are deterministic.
pub fn deployed_address(&self) -> Address {
self.env
.get_asset_contract_id(self.serialized_asset.to_object())
.unwrap_infallible()
.into_val(&self.env)
}
pub fn deploy(&self) -> Address {
self.env
.create_asset_contract(self.serialized_asset.to_object())
.unwrap_infallible()
.into_val(&self.env)
}
}
#[cfg(any(test, feature = "testutils"))]
#[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
mod testutils {
use crate::deploy::Deployer;
use crate::Address;
impl crate::testutils::Deployer for Deployer {
fn get_contract_instance_ttl(&self, contract: &Address) -> u32 {
self.env
.host()
.get_contract_instance_live_until_ledger(contract.to_object())
.unwrap()
.checked_sub(self.env.ledger().sequence())
.unwrap()
}
fn get_contract_code_ttl(&self, contract: &Address) -> u32 {
self.env
.host()
.get_contract_code_live_until_ledger(contract.to_object())
.unwrap()
.checked_sub(self.env.ledger().sequence())
.unwrap()
}
}
}