forked from icon-project/xcall-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.rs
63 lines (57 loc) · 2.45 KB
/
admin.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
use super::*;
impl<'a> CwCallService<'a> {
/// This function queries the admin of a smart contract from the storage.
///
/// Arguments:
///
/// * `store`: `store` is a reference to a trait object of type `dyn Storage`. This is used to
/// interact with the contract's storage, which is where data is stored permanently on the
/// blockchain. The `query_admin` function uses the `store` parameter to load the current admin
/// address from storage and return
///
/// Returns:
///
/// The function `query_admin` returns a `Result` containing either a `String` representing the admin
/// address if it exists in the storage or a `ContractError` if it does not exist.
pub fn query_admin(&self, store: &dyn Storage) -> Result<String, ContractError> {
let admin = self
.admin()
.load(store)
.map_err(|_| ContractError::AdminNotExist)?;
Ok(admin.to_string())
}
/// This function adds an admin to the contract if the sender is the owner and the admin does not
/// already exist.
///
/// Arguments:
///
/// * `store`: `store` is a mutable reference to a trait object of type `dyn Storage`. It is used to
/// interact with the contract's storage and persist data on the blockchain.
/// * `info`: `info` is a parameter of type `MessageInfo` which contains information about the message
/// being executed, such as the sender's address, the amount of coins being sent, and the gas limit.
/// This parameter is used to check if the sender is authorized to add an admin.
/// * `admin`: A string representing the address of the new admin to be added to the contract.
///
/// Returns:
///
/// a `Result<Response, ContractError>`
pub fn set_admin(
&self,
store: &mut dyn Storage,
admin: Addr,
) -> Result<Response, ContractError> {
self.admin().save(store, &admin)?;
Ok(Response::new()
.add_attribute("method", "set_admin")
.add_attribute("admin", admin.to_string()))
}
pub fn validate_address(api: &dyn Api, address: &str) -> Result<Addr, ContractError> {
if !address.chars().all(|x| x.is_alphanumeric()) {
return Err(ContractError::InvalidAddress {
address: address.to_string(),
});
}
let validated_address = api.addr_validate(address).map_err(ContractError::Std)?;
Ok(validated_address)
}
}