Skip to main content

quartz_contract_core/msg/
instantiate.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::StdError;
3use sha2::{Digest, Sha256};
4
5use crate::{
6    msg::{
7        execute::attested::{
8            Attested, DefaultAttestation, HasUserData, RawAttested, RawDefaultAttestation,
9        },
10        HasDomainType,
11    },
12    state::{Config, RawConfig, UserData},
13};
14
15#[derive(Clone, Debug, PartialEq)]
16pub struct Instantiate<A = DefaultAttestation>(pub Attested<CoreInstantiate, A>);
17
18#[cw_serde]
19pub struct RawInstantiate<RA = RawDefaultAttestation>(RawAttested<RawCoreInstantiate, RA>);
20
21impl<RA> TryFrom<RawInstantiate<RA>> for Instantiate<RA::DomainType>
22where
23    RA: HasDomainType,
24{
25    type Error = StdError;
26
27    fn try_from(value: RawInstantiate<RA>) -> Result<Self, Self::Error> {
28        Ok(Self(TryFrom::try_from(value.0)?))
29    }
30}
31
32impl<RA> From<Instantiate<RA::DomainType>> for RawInstantiate<RA>
33where
34    RA: HasDomainType,
35{
36    fn from(value: Instantiate<RA::DomainType>) -> Self {
37        Self(From::from(value.0))
38    }
39}
40
41impl<RA> HasDomainType for RawInstantiate<RA>
42where
43    RA: HasDomainType,
44{
45    type DomainType = Instantiate<RA::DomainType>;
46}
47
48#[derive(Clone, Debug, PartialEq)]
49pub struct CoreInstantiate {
50    config: Config,
51}
52
53impl CoreInstantiate {
54    pub fn new(config: Config) -> Self {
55        Self { config }
56    }
57
58    pub fn config(&self) -> &Config {
59        &self.config
60    }
61}
62
63#[cw_serde]
64pub struct RawCoreInstantiate {
65    config: RawConfig,
66}
67
68impl TryFrom<RawCoreInstantiate> for CoreInstantiate {
69    type Error = StdError;
70
71    fn try_from(value: RawCoreInstantiate) -> Result<Self, Self::Error> {
72        Ok(Self {
73            config: value.config.try_into()?,
74        })
75    }
76}
77
78impl From<CoreInstantiate> for RawCoreInstantiate {
79    fn from(value: CoreInstantiate) -> Self {
80        Self {
81            config: value.config.into(),
82        }
83    }
84}
85
86impl HasDomainType for RawCoreInstantiate {
87    type DomainType = CoreInstantiate;
88}
89
90impl HasUserData for CoreInstantiate {
91    fn user_data(&self) -> UserData {
92        let mut hasher = Sha256::new();
93        hasher.update(
94            serde_json::to_string(&RawCoreInstantiate::from(self.clone()))
95                .expect("infallible serializer"),
96        );
97        let digest: [u8; 32] = hasher.finalize().into();
98
99        let mut user_data = [0u8; 64];
100        user_data[0..32].copy_from_slice(&digest);
101        user_data
102    }
103}