Skip to content

Commit 63d68a4

Browse files
KayanskiBuckram123
andauthored
Added api to query wether an address is a module (#329)
* Added api to query wether an address is a module * update changelog * Update framework/packages/abstract-sdk/src/error.rs * Fixed clippy, added support for Accounts and Native * error not large anymore --------- Co-authored-by: Buckram <buckram123@gmail.com> Co-authored-by: Mykhailo Donchenko <91957742+Buckram123@users.noreply.github.com>
1 parent de399e1 commit 63d68a4

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

framework/docs/src/releases/v0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `state.json` now included in binary in release mode, allowing using binaries on a different environment than it's been built.
88
- `module_instantiate2_address_raw` for `AbstractClient`, allowing to install a different version than the dependency version.
99
- Added helper functions `assert_registered` and `is_registered` to the ANS client API.
10+
- Added method `module_info` for querying and verifying wether an address is a module to the ModuleRegistry API.
1011
- Added default IBC-Client installation on remote modules inside Client and Account interfaces
1112

1213
### Changed

framework/packages/abstract-sdk/src/apis/version_registry.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ use abstract_std::{
22
objects::{
33
module::{Module, ModuleInfo},
44
module_reference::ModuleReference,
5+
module_version::MODULE,
56
namespace::Namespace,
67
version_control::VersionControlContract,
78
AccountId,
89
},
910
version_control::{ModuleConfiguration, ModuleResponse, NamespaceResponse, NamespacesResponse},
1011
};
11-
use cosmwasm_std::Deps;
12+
use cosmwasm_std::{Addr, Deps};
1213

1314
use super::{AbstractApi, ApiIdentification};
1415
use crate::{
1516
cw_helpers::ApiQuery,
1617
features::{AbstractRegistryAccess, ModuleIdentification},
17-
AbstractSdkResult,
18+
AbstractSdkError, AbstractSdkResult,
1819
};
1920

2021
/// Access the Abstract Version Control and access module information.
@@ -142,4 +143,58 @@ impl<'a, T: ModuleRegistryInterface> ModuleRegistry<'a, T> {
142143
.query_standalone_info_raw(code_id, &self.deps.querier)
143144
.map_err(|error| self.wrap_query_error(error))
144145
}
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+
}
145200
}

framework/packages/abstract-sdk/src/error.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ pub enum AbstractSdkError {
6565
module_id: String,
6666
error: Box<AbstractError>,
6767
},
68+
69+
// Queried address is not a module
70+
#[error("Queried address {addr} is not a module: {err}")]
71+
NotAModule { addr: Addr, err: String },
72+
73+
// Queried address is not a module
74+
#[error(
75+
"Queried address {addr} is a module ({module}) but has the wrong stored address : {err}"
76+
)]
77+
WrongModuleInfo {
78+
addr: Addr,
79+
module: String,
80+
err: String,
81+
},
6882
}
6983

7084
impl AbstractSdkError {

0 commit comments

Comments
 (0)