use crate::{
env::internal::Env as _, unwrap::UnwrapInfallible, Address, Bytes, BytesN, ConstructorArgs,
Env, IntoVal,
};
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
}
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),
}
}
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),
}
}
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),
}
}
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)
}
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();
}
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();
}
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();
}
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();
}
}
pub struct DeployerWithAddress {
env: Env,
address: Address,
salt: BytesN<32>,
}
impl DeployerWithAddress {
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)
}
#[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) }
}
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 {
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()
}
}
}