1use odra::{
2 contract_def::HasIdent,
3 host::{Deployer, HostEnv},
4 prelude::Addressable,
5 OdraContract
6};
7
8use crate::{ContractProvider, DeployedContractsContainer};
9
10pub fn log<T: ToString>(msg: T) {
12 prettycli::info(&msg.to_string());
13}
14
15pub trait DeployerExt: Sized {
17 type Contract: OdraContract + 'static + Deployer<Self::Contract>;
19
20 fn load_or_deploy(
22 env: &HostEnv,
23 args: <<Self as DeployerExt>::Contract as OdraContract>::InitArgs,
24 container: &mut DeployedContractsContainer,
25 gas: u64
26 ) -> Result<<<Self as DeployerExt>::Contract as OdraContract>::HostRef, crate::deploy::Error>
27 {
28 if let Ok(contract) = container.contract_ref::<Self::Contract>(env) {
29 prettycli::info(&format!(
30 "Using existing contract {} at address {:?}",
31 <Self::Contract as OdraContract>::HostRef::ident(),
32 contract.address()
33 ));
34 Ok(contract)
35 } else {
36 env.set_gas(gas);
37 let contract = Self::Contract::try_deploy(env, args)?;
38 container.add_contract(&contract)?;
39 Ok(contract)
40 }
41 }
42}
43
44impl<T: OdraContract + Deployer<T> + 'static> DeployerExt for T {
45 type Contract = T;
46}