abstract_std/objects/
registry.rs

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use cosmwasm_std::{Addr, Deps, QuerierWrapper};
use thiserror::Error;

use super::{
    module::{Module, ModuleInfo},
    module_reference::ModuleReference,
    namespace::Namespace,
    AccountId,
};
use crate::{
    account::state::ACCOUNT_ID,
    native_addrs,
    registry::{
        state::{
            ACCOUNT_ADDRESSES, CONFIG, NAMESPACES, REGISTERED_MODULES, SERVICE_INFOS,
            STANDALONE_INFOS,
        },
        Account, ModuleConfiguration, ModuleResponse, ModulesResponse, NamespaceResponse,
        NamespacesResponse, QueryMsg,
    },
    AbstractResult,
};

#[derive(Error, Debug, PartialEq)]
pub enum RegistryError {
    // 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 an Account 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 registry {registry_addr}. Please ensure that you are using the correct Account id and registry address.")]
    UnknownAccountId {
        account_id: AccountId,
        registry_addr: Addr,
    },

    // Caller is not a valid account
    #[error("Address {0} is not the valid account address of {1}.")]
    NotAccount(Addr, AccountId),

    // Query method failed
    #[error("Query during '{method_name}' failed: {error}")]
    QueryFailed {
        method_name: String,
        error: cosmwasm_std::StdError,
    },

    // Service module not found in version registry
    #[error("Service {service_addr} not found in version registry {registry_addr}.")]
    ServiceNotFound {
        service_addr: Addr,
        registry_addr: Addr,
    },

    #[error("The provided module {0} has an invalid module reference.")]
    InvalidReference(ModuleInfo),
}

pub type RegistryResult<T> = Result<T, RegistryError>;

/// Store the Registry contract.
#[allow(rustdoc::broken_intra_doc_links)]
/// Implements [`AbstractRegistryAccess`] (defined in abstract-sdk)
#[cosmwasm_schema::cw_serde]
pub struct RegistryContract {
    /// Address of the registry contract
    pub address: Addr,
}

impl RegistryContract {
    /// Retrieve address of the Registry
    pub fn new(deps: Deps, abstract_code_id: u64) -> AbstractResult<Self> {
        let address = deps
            .api
            .addr_humanize(&native_addrs::registry_address(deps, abstract_code_id)?)?;
        Ok(Self { address })
    }

    // Module registry

    /// Raw query for a module reference
    #[function_name::named]
    pub fn query_module_reference_raw(
        &self,
        module_info: &ModuleInfo,
        querier: &QuerierWrapper,
    ) -> RegistryResult<ModuleReference> {
        let module_reference = REGISTERED_MODULES
            .query(querier, self.address.clone(), module_info)
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;

        module_reference.ok_or_else(|| RegistryError::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,
    ) -> RegistryResult<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,
    ) -> RegistryResult<ModuleConfiguration> {
        Ok(self
            .query_modules_configs(vec![module_info], querier)?
            .swap_remove(0)
            .config)
    }

    /// Smart query for a modules and its configurations
    #[function_name::named]
    pub fn query_modules_configs(
        &self,
        infos: Vec<ModuleInfo>,
        querier: &QuerierWrapper,
    ) -> RegistryResult<Vec<ModuleResponse>> {
        let ModulesResponse { modules } = querier
            .query_wasm_smart(self.address.to_string(), &QueryMsg::Modules { infos })
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        Ok(modules)
    }

    /// Queries the account that owns the namespace
    /// Is also returns the base modules of that account (Account)
    #[function_name::named]
    pub fn query_namespace(
        &self,
        namespace: Namespace,
        querier: &QuerierWrapper,
    ) -> RegistryResult<NamespaceResponse> {
        let namespace_response: NamespaceResponse = querier
            .query_wasm_smart(self.address.to_string(), &QueryMsg::Namespace { namespace })
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        Ok(namespace_response)
    }

    /// Queries the account id that owns the namespace
    #[function_name::named]
    pub fn query_namespace_raw(
        &self,
        namespace: Namespace,
        querier: &QuerierWrapper,
    ) -> RegistryResult<Option<AccountId>> {
        let namespace_response = NAMESPACES
            .query(querier, self.address.clone(), &namespace)
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        Ok(namespace_response)
    }

    /// Queries the namespaces owned by accounts
    #[function_name::named]
    pub fn query_namespaces(
        &self,
        accounts: Vec<AccountId>,
        querier: &QuerierWrapper,
    ) -> RegistryResult<NamespacesResponse> {
        let namespaces_response: NamespacesResponse = querier
            .query_wasm_smart(self.address.to_string(), &QueryMsg::Namespaces { accounts })
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        Ok(namespaces_response)
    }

    /// Queries the module info of the standalone code id
    #[function_name::named]
    pub fn query_standalone_info_raw(
        &self,
        code_id: u64,
        querier: &QuerierWrapper,
    ) -> RegistryResult<ModuleInfo> {
        let module_info = STANDALONE_INFOS
            .query(querier, self.address.clone(), code_id)
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        module_info.ok_or_else(|| RegistryError::StandaloneNotFound {
            code_id,
            registry_addr: self.address.clone(),
        })
    }

    /// Queries the module info of the standalone code id
    #[function_name::named]
    pub fn query_service_info_raw(
        &self,
        service_addr: &Addr,
        querier: &QuerierWrapper,
    ) -> RegistryResult<ModuleInfo> {
        let module_info = SERVICE_INFOS
            .query(querier, self.address.clone(), service_addr)
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        module_info.ok_or_else(|| RegistryError::ServiceNotFound {
            service_addr: service_addr.clone(),
            registry_addr: self.address.clone(),
        })
    }

    // AccountRegistry

    /// Get self reported Account id, for checked use
    /// [`RegistryContract::account_id`]
    pub fn unchecked_account_id(
        &self,
        maybe_core_contract_addr: &Addr,
        querier: &QuerierWrapper,
    ) -> RegistryResult<AccountId> {
        ACCOUNT_ID
            .query(querier, maybe_core_contract_addr.clone())
            .map_err(|_| RegistryError::FailedToQueryAccountId {
                contract_addr: maybe_core_contract_addr.clone(),
            })
    }

    /// Get AccountId for given account address.
    /// Also verifies that that address is indeed an account.
    pub fn account_id(
        &self,
        maybe_account_addr: &Addr,
        querier: &QuerierWrapper,
    ) -> RegistryResult<AccountId> {
        let self_reported_account_id = self.unchecked_account_id(maybe_account_addr, querier)?;
        // now we need to verify that the account id is indeed correct
        let account = self.account(&self_reported_account_id, querier)?;
        if account.addr().ne(maybe_account_addr) {
            Err(RegistryError::FailedToQueryAccountId {
                contract_addr: maybe_account_addr.clone(),
            })
        } else {
            Ok(self_reported_account_id)
        }
    }

    /// Get the account for a given account id.
    #[function_name::named]
    pub fn account(
        &self,
        account_id: &AccountId,
        querier: &QuerierWrapper,
    ) -> RegistryResult<Account> {
        let maybe_account = ACCOUNT_ADDRESSES
            .query(querier, self.address.clone(), account_id)
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        maybe_account.ok_or_else(|| RegistryError::UnknownAccountId {
            account_id: account_id.clone(),
            registry_addr: self.address.clone(),
        })
    }

    /// Get namespace registration fee
    #[function_name::named]
    pub fn namespace_registration_fee(
        &self,
        querier: &QuerierWrapper,
    ) -> RegistryResult<Option<cosmwasm_std::Coin>> {
        let config = CONFIG
            .query(querier, self.address.clone())
            .map_err(|error| RegistryError::QueryFailed {
                method_name: function_name!().to_owned(),
                error,
            })?;
        Ok(config.namespace_registration_fee)
    }

    /// Verify if the provided account address is indeed a user.
    pub fn assert_account(
        &self,
        maybe_account: &Addr,
        querier: &QuerierWrapper,
    ) -> RegistryResult<Account> {
        let account_id = self.unchecked_account_id(maybe_account, querier)?;
        let account = self.account(&account_id, querier)?;
        if account.addr().ne(maybe_account) {
            Err(RegistryError::NotAccount(maybe_account.clone(), account_id))
        } else {
            Ok(account)
        }
    }
}