1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use cosmwasm_std::{Addr, QuerierWrapper};
use thiserror::Error;

use crate::version_control::{
    state::{ACCOUNT_ADDRESSES, CONFIG, REGISTERED_MODULES, STANDALONE_INFOS},
    AccountBase, ModuleConfiguration, ModuleResponse, ModulesResponse, NamespaceResponse,
    NamespacesResponse, QueryMsg,
};

use super::{
    account::ACCOUNT_ID,
    module::{Module, ModuleInfo},
    module_reference::ModuleReference,
    namespace::Namespace,
    AccountId,
};

#[derive(Error, Debug, PartialEq)]
pub enum VersionControlError {
    #[error(transparent)]
    StdError(#[from] cosmwasm_std::StdError),

    // module not found in version registry
    #[error("Module {module} not found in version registry {registry_addr}.")]
    ModuleNotFound { module: String, registry_addr: Addr },

    // failed to query account id
    #[error("Failed to query Account id on contract {contract_addr}. Please ensure that the contract is a Manager or Proxy contract.")]
    FailedToQueryAccountId { contract_addr: Addr },

    // standalone module not found in version registry
    #[error("Standalone {code_id} not found in version registry {registry_addr}.")]
    StandaloneNotFound { code_id: u64, registry_addr: Addr },

    // unknown Account id error
    #[error("Unknown Account id {account_id} on version control {registry_addr}. Please ensure that you are using the correct Account id and version control address.")]
    UnknownAccountId {
        account_id: AccountId,
        registry_addr: Addr,
    },

    // caller not Manager error
    #[error("Address {0} is not the Manager of Account {1}.")]
    NotManager(Addr, AccountId),

    // caller not Proxy error
    #[error("Address {0} is not the Proxy of Account {1}.")]
    NotProxy(Addr, AccountId),
}

pub type VersionControlResult<T> = Result<T, VersionControlError>;

/// Store the Version Control contract.
/// Implements [`AbstractRegistryAccess`]
#[cosmwasm_schema::cw_serde]
pub struct VersionControlContract {
    /// Address of the version control contract
    pub address: Addr,
}

impl VersionControlContract {
    /// Construct a new version control feature object.
    pub fn new(address: Addr) -> Self {
        Self { address }
    }

    // Module registry

    /// Raw query for a module reference
    pub fn query_module_reference_raw(
        &self,
        module_info: &ModuleInfo,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<ModuleReference> {
        let module_reference =
            REGISTERED_MODULES.query(querier, self.address.clone(), module_info)?;
        module_reference.ok_or_else(|| VersionControlError::ModuleNotFound {
            module: module_info.to_string(),
            registry_addr: self.address.clone(),
        })
    }

    /// Smart query for a module
    pub fn query_module(
        &self,
        module_info: ModuleInfo,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<Module> {
        Ok(self
            .query_modules_configs(vec![module_info], querier)?
            .swap_remove(0)
            .module)
    }

    /// Smart query for a module config
    pub fn query_config(
        &self,
        module_info: ModuleInfo,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<ModuleConfiguration> {
        Ok(self
            .query_modules_configs(vec![module_info], querier)?
            .swap_remove(0)
            .config)
    }

    /// Smart query for a modules and its configurations
    pub fn query_modules_configs(
        &self,
        infos: Vec<ModuleInfo>,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<Vec<ModuleResponse>> {
        let ModulesResponse { modules } =
            querier.query_wasm_smart(self.address.to_string(), &QueryMsg::Modules { infos })?;
        Ok(modules)
    }

    /// Queries the account that owns the namespace
    /// Is also returns the base modules of that account (AccountBase)
    pub fn query_namespace(
        &self,
        namespace: Namespace,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<NamespaceResponse> {
        let namespace_response: NamespaceResponse = querier
            .query_wasm_smart(self.address.to_string(), &QueryMsg::Namespace { namespace })?;
        Ok(namespace_response)
    }

    /// Queries the namespaces owned by accounts
    pub fn query_namespaces(
        &self,
        accounts: Vec<AccountId>,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<NamespacesResponse> {
        let namespaces_response: NamespacesResponse = querier
            .query_wasm_smart(self.address.to_string(), &QueryMsg::Namespaces { accounts })?;
        Ok(namespaces_response)
    }

    /// Queries the module info of the standalone code id
    pub fn query_standalone_info_raw(
        &self,
        code_id: u64,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<ModuleInfo> {
        let module_info = STANDALONE_INFOS.query(querier, self.address.clone(), code_id)?;
        module_info.ok_or_else(|| VersionControlError::StandaloneNotFound {
            code_id,
            registry_addr: self.address.clone(),
        })
    }

    // AccountRegistry

    /// Get AccountId for given manager or proxy address.
    pub fn account_id(
        &self,
        maybe_core_contract_addr: &Addr,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<AccountId> {
        ACCOUNT_ID
            .query(querier, maybe_core_contract_addr.clone())
            .map_err(|_| VersionControlError::FailedToQueryAccountId {
                contract_addr: maybe_core_contract_addr.clone(),
            })
    }

    /// Get the account base for a given account id.
    pub fn account_base(
        &self,
        account_id: &AccountId,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<AccountBase> {
        let maybe_account = ACCOUNT_ADDRESSES.query(querier, self.address.clone(), account_id)?;
        maybe_account.ok_or_else(|| VersionControlError::UnknownAccountId {
            account_id: account_id.clone(),
            registry_addr: self.address.clone(),
        })
    }

    /// Get namespace registration fee
    pub fn namespace_registration_fee(
        &self,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<Option<cosmwasm_std::Coin>> {
        let config = CONFIG.query(querier, self.address.clone())?;
        Ok(config.namespace_registration_fee)
    }

    /// Verify if the provided manager address is indeed a user.
    pub fn assert_manager(
        &self,
        maybe_manager: &Addr,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<AccountBase> {
        let account_id = self.account_id(maybe_manager, querier)?;
        let account_base = self.account_base(&account_id, querier)?;
        if account_base.manager.ne(maybe_manager) {
            Err(VersionControlError::NotManager(
                maybe_manager.clone(),
                account_id,
            ))
        } else {
            Ok(account_base)
        }
    }

    /// Verify if the provided proxy address is indeed a user.
    pub fn assert_proxy(
        &self,
        maybe_proxy: &Addr,
        querier: &QuerierWrapper,
    ) -> VersionControlResult<AccountBase> {
        let account_id = self.account_id(maybe_proxy, querier)?;
        let account_base = self.account_base(&account_id, querier)?;
        if account_base.proxy.ne(maybe_proxy) {
            Err(VersionControlError::NotProxy(
                maybe_proxy.clone(),
                account_id,
            ))
        } else {
            Ok(account_base)
        }
    }
}