use odra::{
contract_def::HasIdent,
host::{Deployer, HostEnv, InstallConfig},
prelude::Addressable,
OdraContract
};
use crate::{ContractProvider, DeployedContractsContainer};
const DEFAULT_CONTRACTS_FILE: &str = "resources/contracts.toml";
pub fn log<T: ToString>(msg: T) {
prettycli::info(&msg.to_string());
}
pub(crate) fn get_default_contracts_file() -> String {
if let Ok(network_name) = std::env::var("ODRA_CASPER_LIVENET_CHAIN_NAME") {
format!("resources/{}-contracts.toml", network_name)
} else {
DEFAULT_CONTRACTS_FILE.to_string()
}
}
pub trait DeployerExt: Sized {
type Contract: OdraContract + 'static + Deployer<Self::Contract>;
fn load_or_deploy(
env: &HostEnv,
args: <<Self as DeployerExt>::Contract as OdraContract>::InitArgs,
container: &mut DeployedContractsContainer,
gas: u64
) -> Result<<<Self as DeployerExt>::Contract as OdraContract>::HostRef, crate::deploy::Error>
{
if let Ok(contract) = container.contract_ref::<Self::Contract>(env) {
prettycli::info(&format!(
"Using existing contract {} at address {}",
<Self::Contract as OdraContract>::HostRef::ident(),
contract.address().to_string()
));
Ok(contract)
} else {
env.set_gas(gas);
let contract = Self::Contract::try_deploy(env, args)?;
container.add_contract(&contract)?;
Ok(contract)
}
}
fn load_or_deploy_with_cfg(
env: &HostEnv,
package_name: Option<String>,
args: <<Self as DeployerExt>::Contract as OdraContract>::InitArgs,
cfg: InstallConfig,
container: &mut DeployedContractsContainer,
gas: u64
) -> Result<<<Self as DeployerExt>::Contract as OdraContract>::HostRef, crate::deploy::Error>
{
if let Ok(contract) =
container.contract_ref_named::<Self::Contract>(env, package_name.clone())
{
prettycli::info(&format!(
"Using existing contract {} at address {}",
<Self::Contract as OdraContract>::HostRef::ident(),
contract.address().to_string()
));
Ok(contract)
} else {
env.set_gas(gas);
let contract = Self::Contract::try_deploy_with_cfg(env, args, cfg)?;
container.add_contract_named(&contract, package_name)?;
Ok(contract)
}
}
}
impl<T: OdraContract + Deployer<T> + 'static> DeployerExt for T {
type Contract = T;
}