1use odra::{
2 contract_def::HasIdent,
3 host::{Deployer, HostEnv, InstallConfig},
4 prelude::Addressable,
5 OdraContract
6};
7
8use crate::{ContractProvider, DeployedContractsContainer};
9
10const DEFAULT_CONTRACTS_FILE: &str = "resources/contracts.toml";
11
12pub fn log<T: ToString>(msg: T) {
14 prettycli::info(&msg.to_string());
15}
16
17pub(crate) fn get_default_contracts_file() -> String {
21 if let Ok(network_name) = std::env::var("ODRA_CASPER_LIVENET_CHAIN_NAME") {
22 format!("resources/{}-contracts.toml", network_name)
23 } else {
24 DEFAULT_CONTRACTS_FILE.to_string()
25 }
26}
27
28pub trait DeployerExt: Sized {
30 type Contract: OdraContract + 'static + Deployer<Self::Contract>;
32
33 fn load_or_deploy(
35 env: &HostEnv,
36 args: <<Self as DeployerExt>::Contract as OdraContract>::InitArgs,
37 container: &mut DeployedContractsContainer,
38 gas: u64
39 ) -> Result<<<Self as DeployerExt>::Contract as OdraContract>::HostRef, crate::deploy::Error>
40 {
41 if let Ok(contract) = container.contract_ref::<Self::Contract>(env) {
42 prettycli::info(&format!(
43 "Using existing contract {} at address {}",
44 <Self::Contract as OdraContract>::HostRef::ident(),
45 contract.address().to_string()
46 ));
47 Ok(contract)
48 } else {
49 env.set_gas(gas);
50 let contract = Self::Contract::try_deploy(env, args)?;
51 env.set_gas(0);
53 container.add_contract(&contract)?;
54 Ok(contract)
55 }
56 }
57
58 fn load_or_deploy_with_cfg(
60 env: &HostEnv,
61 package_name: Option<String>,
62 args: <<Self as DeployerExt>::Contract as OdraContract>::InitArgs,
63 cfg: InstallConfig,
64 container: &mut DeployedContractsContainer,
65 gas: u64
66 ) -> Result<<<Self as DeployerExt>::Contract as OdraContract>::HostRef, crate::deploy::Error>
67 {
68 if let Ok(contract) =
69 container.contract_ref_named::<Self::Contract>(env, package_name.clone())
70 {
71 prettycli::info(&format!(
72 "Using existing contract {} at address {}",
73 <Self::Contract as OdraContract>::HostRef::ident(),
74 contract.address().to_string()
75 ));
76 Ok(contract)
77 } else {
78 env.set_gas(gas);
79 let contract = Self::Contract::try_deploy_with_cfg(env, args, cfg)?;
80 env.set_gas(0);
82 container.add_contract_named(&contract, package_name)?;
83 Ok(contract)
84 }
85 }
86}
87
88impl<T: OdraContract + Deployer<T> + 'static> DeployerExt for T {
89 type Contract = T;
90}