@@ -2,19 +2,20 @@ use abstract_std::{
2
2
objects:: {
3
3
module:: { Module , ModuleInfo } ,
4
4
module_reference:: ModuleReference ,
5
+ module_version:: MODULE ,
5
6
namespace:: Namespace ,
6
7
version_control:: VersionControlContract ,
7
8
AccountId ,
8
9
} ,
9
10
version_control:: { ModuleConfiguration , ModuleResponse , NamespaceResponse , NamespacesResponse } ,
10
11
} ;
11
- use cosmwasm_std:: Deps ;
12
+ use cosmwasm_std:: { Addr , Deps } ;
12
13
13
14
use super :: { AbstractApi , ApiIdentification } ;
14
15
use crate :: {
15
16
cw_helpers:: ApiQuery ,
16
17
features:: { AbstractRegistryAccess , ModuleIdentification } ,
17
- AbstractSdkResult ,
18
+ AbstractSdkError , AbstractSdkResult ,
18
19
} ;
19
20
20
21
/// Access the Abstract Version Control and access module information.
@@ -142,4 +143,58 @@ impl<'a, T: ModuleRegistryInterface> ModuleRegistry<'a, T> {
142
143
. query_standalone_info_raw ( code_id, & self . deps . querier )
143
144
. map_err ( |error| self . wrap_query_error ( error) )
144
145
}
146
+
147
+ /// Queries the Module information for an address.
148
+ /// This will error if the Address is not an Abstract Module (Native, Account, App, Adapter or Standalone)
149
+ pub fn module_info ( & self , address : Addr ) -> AbstractSdkResult < Module > {
150
+ // We start by testing if the address is a module
151
+ let module_response = MODULE
152
+ . query ( & self . deps . querier , address. clone ( ) )
153
+ . map_err ( |e| AbstractSdkError :: NotAModule {
154
+ addr : address. clone ( ) ,
155
+ err : e. to_string ( ) ,
156
+ } ) ?;
157
+
158
+ // We verify the module is indeed registered inside the version registry
159
+ let module = self . query_module ( ModuleInfo :: from_id (
160
+ & module_response. module ,
161
+ module_response. version . into ( ) ,
162
+ ) ?) ?;
163
+
164
+ match module. reference . clone ( ) {
165
+ ModuleReference :: Adapter ( queried_address)
166
+ | ModuleReference :: Native ( queried_address) => {
167
+ if queried_address == address {
168
+ Ok ( module)
169
+ } else {
170
+ Err ( AbstractSdkError :: WrongModuleInfo {
171
+ addr : address. clone ( ) ,
172
+ module : module. to_string ( ) ,
173
+ err : format ! ( "Expected address {queried_address}, got address {address}" , ) ,
174
+ } )
175
+ }
176
+ }
177
+ ModuleReference :: App ( queried_code_id)
178
+ | ModuleReference :: Standalone ( queried_code_id)
179
+ | ModuleReference :: AccountBase ( queried_code_id) => {
180
+ let request_contract = self . deps . querier . query_wasm_contract_info ( & address) ?;
181
+ if queried_code_id == request_contract. code_id {
182
+ Ok ( module)
183
+ } else {
184
+ Err ( AbstractSdkError :: WrongModuleInfo {
185
+ addr : address,
186
+ module : module. to_string ( ) ,
187
+ err : format ! (
188
+ "Expected code_id {queried_code_id}, got code_id {}" ,
189
+ request_contract. code_id
190
+ ) ,
191
+ } )
192
+ }
193
+ }
194
+ _ => Err ( AbstractSdkError :: NotAModule {
195
+ addr : address,
196
+ err : "got an un-implemented module reference" . to_string ( ) ,
197
+ } ) ,
198
+ }
199
+ }
145
200
}
0 commit comments