abstract_std/native/
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! # Registry
//!
//! `abstract_std::registry` stores chain-specific code-ids, addresses and an account_id map.
//!
//! ## Description
//! Code-ids and api-contract addresses are stored on this address. This data can not be changed and allows for complex factory logic.
//! Both code-ids and addresses are stored on a per-module version basis which allows users to easily upgrade their modules.
//!
//! An internal account-id store provides external verification for accounts.  

pub type ModuleMapEntry = (ModuleInfo, ModuleReference);

/// Contains configuration info of registry.
#[cosmwasm_schema::cw_serde]
pub struct Config {
    pub security_enabled: bool,
    pub namespace_registration_fee: Option<Coin>,
}

pub mod state {
    use cw_storage_plus::{Item, Map};

    use super::{Account, Config, ModuleConfiguration, ModuleDefaultConfiguration};
    use crate::objects::{
        account::{AccountId, AccountSequence},
        module::ModuleInfo,
        module_reference::ModuleReference,
        namespace::Namespace,
        storage_namespaces::{self},
    };

    pub const CONFIG: Item<Config> = Item::new(storage_namespaces::CONFIG_STORAGE_KEY);

    // Modules waiting for approvals
    pub const PENDING_MODULES: Map<&ModuleInfo, ModuleReference> =
        Map::new(storage_namespaces::registry::PENDING_MODULES);
    // We can iterate over the map giving just the prefix to get all the versions
    pub const REGISTERED_MODULES: Map<&ModuleInfo, ModuleReference> =
        Map::new(storage_namespaces::registry::REGISTERED_MODULES);
    // Reverse map for module info of standalone modules
    pub const STANDALONE_INFOS: Map<u64, ModuleInfo> =
        Map::new(storage_namespaces::registry::STANDALONE_INFOS);
    // Reverse map for module info of service modules
    pub const SERVICE_INFOS: Map<&cosmwasm_std::Addr, ModuleInfo> =
        Map::new(storage_namespaces::registry::SERVICE_INFOS);
    // Yanked Modules
    pub const YANKED_MODULES: Map<&ModuleInfo, ModuleReference> =
        Map::new(storage_namespaces::registry::YANKED_MODULES);
    // Modules Configuration
    pub const MODULE_CONFIG: Map<&ModuleInfo, ModuleConfiguration> =
        Map::new(storage_namespaces::registry::MODULE_CONFIG);
    // Modules Default Configuration
    pub const MODULE_DEFAULT_CONFIG: Map<(&Namespace, &str), ModuleDefaultConfiguration> =
        Map::new(storage_namespaces::registry::MODULE_DEFAULT_CONFIG);
    /// Maps Account ID to the address of its core contracts
    pub const ACCOUNT_ADDRESSES: Map<&AccountId, Account> =
        Map::new(storage_namespaces::registry::ACCOUNT_ADDRESSES);
    /// Account sequences
    pub const LOCAL_ACCOUNT_SEQUENCE: Item<AccountSequence> =
        Item::new(storage_namespaces::registry::LOCAL_ACCOUNT_SEQUENCE);
    pub const NAMESPACES: Map<&Namespace, AccountId> =
        Map::new(storage_namespaces::registry::NAMESPACES);
    pub const REV_NAMESPACES: Map<&AccountId, Namespace> =
        Map::new(storage_namespaces::registry::REV_NAMESPACES);
}

use cosmwasm_schema::QueryResponses;
use cosmwasm_std::{Addr, Api, Coin, Storage};
use cw_clearable::Clearable;

use self::state::{MODULE_CONFIG, MODULE_DEFAULT_CONFIG};
use crate::objects::{
    account::AccountId,
    module::{Module, ModuleInfo, ModuleMetadata, ModuleStatus, Monetization},
    module_reference::ModuleReference,
    namespace::Namespace,
};

/// Contains the minimal Abstract Account contract addresses.
#[cosmwasm_schema::cw_serde]
pub struct Account<T = Addr>(T);

impl<T> Account<T> {
    pub fn new(addr: T) -> Self {
        Self(addr)
    }
}

impl Account<String> {
    pub fn verify(self, api: &dyn Api) -> cosmwasm_std::StdResult<Account<Addr>> {
        let addr = api.addr_validate(&self.0)?;
        Ok(Account(addr))
    }
}

impl Account {
    pub fn addr(&self) -> &Addr {
        &self.0
    }

    pub fn into_addr(self) -> Addr {
        self.0
    }
}

impl From<Account<Addr>> for Account<String> {
    fn from(addr: Account<Addr>) -> Self {
        Account(addr.0.to_string())
    }
}

/// Registry Instantiate Msg
#[cosmwasm_schema::cw_serde]
pub struct InstantiateMsg {
    pub admin: String,
    /// allows users to directly register modules without going through approval
    /// Also allows them to change the module reference of an existing module
    /// Also allows to claim namespaces permisionlessly
    /// SHOULD ONLY BE `true` FOR TESTING
    pub security_enabled: Option<bool>,
    pub namespace_registration_fee: Option<Coin>,
}

/// Registry Execute Msg
#[cw_ownable::cw_ownable_execute]
#[cosmwasm_schema::cw_serde]
#[derive(cw_orch::ExecuteFns)]
pub enum ExecuteMsg {
    /// Remove some version of a module
    RemoveModule { module: ModuleInfo },
    /// Yank a version of a module so that it may not be installed
    /// Only callable by Admin
    YankModule { module: ModuleInfo },
    /// Propose new modules to the version registry
    /// Namespaces need to be claimed by the Account before proposing modules
    /// Once proposed, the modules need to be approved by the Admin via [`ExecuteMsg::ApproveOrRejectModules`]
    ProposeModules { modules: Vec<ModuleMapEntry> },
    /// Sets the metadata configuration for a module.
    /// Only callable by namespace admin
    UpdateModuleConfiguration {
        module_name: String,
        namespace: Namespace,
        update_module: UpdateModule,
    },
    /// Approve or reject modules
    /// This takes the modules in the pending_modules map and
    /// moves them to the registered_modules map or yanked_modules map
    ApproveOrRejectModules {
        approves: Vec<ModuleInfo>,
        rejects: Vec<ModuleInfo>,
    },
    /// Claim namespaces
    ClaimNamespace {
        account_id: AccountId,
        namespace: String,
    },
    /// Forgo namespace claims
    /// Only admin or root user can call this
    ForgoNamespace { namespaces: Vec<String> },
    /// Register a new Account to the deployed Accounts.
    /// Claims namespace if provided.  
    /// Only new accounts can call this.
    AddAccount {
        namespace: Option<String>,
        creator: String,
    },
    /// Updates configuration of the Registry contract
    UpdateConfig {
        /// Whether the contract allows direct module registration
        security_enabled: Option<bool>,
        /// The fee charged when registering a namespace
        namespace_registration_fee: Option<Clearable<Coin>>,
    },
}

#[non_exhaustive]
#[cosmwasm_schema::cw_serde]
pub enum UpdateModule {
    /// Updates the default metadata for the module
    Default { metadata: ModuleMetadata },
    /// Update configuration for specified version
    Versioned {
        /// Module version
        version: String,
        /// Update the metadata for this version
        metadata: Option<ModuleMetadata>,
        /// Update the monetization for this version
        monetization: Option<Monetization>,
        /// Update the init_funds for this version
        instantiation_funds: Option<Vec<Coin>>,
    },
}

/// A ModuleFilter that mirrors the [`ModuleInfo`] struct.
#[derive(Default)]
#[cosmwasm_schema::cw_serde]
pub struct ModuleFilter {
    pub namespace: Option<String>,
    pub name: Option<String>,
    pub version: Option<String>,
    pub status: Option<ModuleStatus>,
}

/// Registry Query Msg
#[cw_ownable::cw_ownable_query]
#[cosmwasm_schema::cw_serde]
#[derive(QueryResponses, cw_orch::QueryFns)]
pub enum QueryMsg {
    /// Query Core of Accounts
    /// Returns [`AccountsResponse`]
    #[returns(AccountsResponse)]
    Accounts { account_ids: Vec<AccountId> },
    /// Queries module information
    /// Modules that are yanked are not returned
    /// Returns [`ModulesResponse`]
    #[returns(ModulesResponse)]
    Modules { infos: Vec<ModuleInfo> },
    /// Queries namespaces for an account
    /// Returns [`NamespacesResponse`]
    #[returns(NamespacesResponse)]
    Namespaces { accounts: Vec<AccountId> },
    /// Queries information about the namespace
    /// Returns [`NamespaceResponse`]
    #[returns(NamespaceResponse)]
    Namespace { namespace: Namespace },
    /// Returns [`ConfigResponse`]
    #[returns(ConfigResponse)]
    Config {},
    /// Returns [`AccountListResponse`]
    #[returns(AccountListResponse)]
    AccountList {
        start_after: Option<AccountId>,
        limit: Option<u8>,
    },
    /// Returns [`ModulesListResponse`]
    #[returns(ModulesListResponse)]
    ModuleList {
        filter: Option<ModuleFilter>,
        start_after: Option<ModuleInfo>,
        limit: Option<u8>,
    },
    /// Returns [`NamespaceListResponse`]
    #[returns(NamespaceListResponse)]
    NamespaceList {
        start_after: Option<String>,
        limit: Option<u8>,
    },
}

#[cosmwasm_schema::cw_serde]
pub struct AccountsResponse {
    pub accounts: Vec<Account>,
}

#[cosmwasm_schema::cw_serde]
pub struct AccountListResponse {
    pub accounts: Vec<(AccountId, Account)>,
}

#[cosmwasm_schema::cw_serde]
pub struct ModulesResponse {
    pub modules: Vec<ModuleResponse>,
}

#[cosmwasm_schema::cw_serde]
pub struct ModuleResponse {
    pub module: Module,
    pub config: ModuleConfiguration,
}

#[non_exhaustive]
#[cosmwasm_schema::cw_serde]
#[derive(Default)]
pub struct ModuleConfiguration {
    pub monetization: Monetization,
    pub metadata: Option<ModuleMetadata>,
    pub instantiation_funds: Vec<Coin>,
}

#[non_exhaustive]
#[cosmwasm_schema::cw_serde]
pub struct ModuleDefaultConfiguration {
    pub metadata: ModuleMetadata,
}

impl ModuleDefaultConfiguration {
    pub fn new(metadata: ModuleMetadata) -> Self {
        Self { metadata }
    }
}

impl ModuleConfiguration {
    pub fn new(
        monetization: Monetization,
        metadata: Option<ModuleMetadata>,
        instantiation_funds: Vec<Coin>,
    ) -> Self {
        Self {
            monetization,
            metadata,
            instantiation_funds,
        }
    }

    pub fn from_storage(
        storage: &dyn Storage,
        module: &ModuleInfo,
    ) -> cosmwasm_std::StdResult<Self> {
        let mut mod_cfg = MODULE_CONFIG.may_load(storage, module)?.unwrap_or_default();

        if mod_cfg.metadata.is_none() {
            // Destructure so we notice any field changes at compile time
            if let Some(ModuleDefaultConfiguration { metadata }) =
                MODULE_DEFAULT_CONFIG.may_load(storage, (&module.namespace, &module.name))?
            {
                mod_cfg.metadata = Some(metadata);
            }
        }

        Ok(mod_cfg)
    }
}

#[cosmwasm_schema::cw_serde]
pub struct ModulesListResponse {
    pub modules: Vec<ModuleResponse>,
}

#[cosmwasm_schema::cw_serde]
pub enum NamespaceResponse {
    Claimed(NamespaceInfo),
    Unclaimed {},
}

impl NamespaceResponse {
    pub fn unwrap(self) -> NamespaceInfo {
        match self {
            NamespaceResponse::Claimed(info) => info,
            NamespaceResponse::Unclaimed {} => {
                panic!("called `NamespaceResponse::unwrap()` on a `Unclaimed` value")
            }
        }
    }
}

#[cosmwasm_schema::cw_serde]
pub struct NamespaceInfo {
    pub account_id: AccountId,
    pub account: Account,
}

#[cosmwasm_schema::cw_serde]
pub struct NamespacesResponse {
    pub namespaces: Vec<(Namespace, AccountId)>,
}

#[cosmwasm_schema::cw_serde]
pub struct NamespaceListResponse {
    pub namespaces: Vec<(Namespace, AccountId)>,
}

#[cosmwasm_schema::cw_serde]
pub struct ConfigResponse {
    pub security_enabled: bool,
    pub namespace_registration_fee: Option<Coin>,
    pub local_account_sequence: u32,
}

#[cosmwasm_schema::cw_serde]
pub enum MigrateMsg {
    /// Migrating from blob contract
    Instantiate(InstantiateMsg),
    /// Migrating from previous version
    Migrate {},
}