odra_cli/cmd/
deploy.rs

1use crate::{
2    container::ContractError, CustomTypeSet, DeployedContractsContainer, DEPLOY_SUBCOMMAND
3};
4use anyhow::Result;
5use clap::ArgMatches;
6use odra::{host::HostEnv, prelude::OdraError};
7use thiserror::Error;
8
9use super::OdraCommand;
10
11/// DeployCmd is a struct that represents the deploy command in the Odra CLI.
12///
13/// The deploy command runs the [DeployScript].
14pub(crate) struct DeployCmd {
15    pub script: Box<dyn DeployScript>
16}
17
18impl OdraCommand for DeployCmd {
19    fn name(&self) -> &str {
20        DEPLOY_SUBCOMMAND
21    }
22
23    fn run(&self, env: &HostEnv, _args: &ArgMatches, _types: &CustomTypeSet) -> Result<()> {
24        let mut container = DeployedContractsContainer::new()?;
25        self.script.deploy(env, &mut container)?;
26        Ok(())
27    }
28}
29
30/// Script that deploys contracts to the blockchain and stores contract data for further use.
31///
32/// In a deploy script, you can define the contracts that you want to deploy to the blockchain
33/// and write metadata to the container.
34pub trait DeployScript {
35    fn deploy(
36        &self,
37        env: &HostEnv,
38        container: &mut DeployedContractsContainer
39    ) -> core::result::Result<(), DeployError>;
40}
41
42/// Error that occurs during contract deployment.
43#[derive(Debug, Error)]
44pub enum DeployError {
45    #[error("Deploy error: {message}")]
46    OdraError { message: String },
47    #[error("Contract read error: {0}")]
48    ContractReadError(#[from] ContractError)
49}
50
51impl From<OdraError> for DeployError {
52    fn from(err: OdraError) -> Self {
53        DeployError::OdraError {
54            message: format!("{:?}", err)
55        }
56    }
57}