.unwrap();
println!("{:?}", res);
//try revoke_admin() sender "creator" admin_address "creator" admin ""
let res = execute_revoke_admin(
deps.as_mut(),
env.clone(),
info.clone(),
"creator".to_string(),
)
.unwrap_err();
assert_eq!(res, ContractError::Unauthorized {});
}
}
```
### msg.rs
```
use crate::state::{Operation, OperationStatus};
use cosmwasm_std::{Addr, Binary, Uint64};
use cw_utils::{Duration, Scheduled};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InstantiateMsg {
pub admins: Option<Vec<String>>,
pub proposers: Vec<String>,
pub min_delay: Duration,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Schedule {
target_address: String,
data: Binary,
title: String,
description: String,
execution_time: Scheduled,
executors: Option<Vec<String>>,
},
Cancel {
operation_id: Uint64,
},
Execute {
operation_id: Uint64,
},
RevokeAdmin {
admin_address: String,
},
AddProposer {
proposer_address: String,
},
RemoveProposer {
proposer_address: String,
},
UpdateMinDelay {
new_delay: Duration,
},
Freeze {},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetOperationStatus {
operation_id: Uint64,
},
GetExecutionTime {
operation_id: Uint64,
},
GetAdmins {},
GetOperations {
start_after: Option<u64>,
limit: Option<u32>,
},
GetMinDelay {},
GetProposers {},
GetExecutors {
operation_id: Uint64,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OperationResponse {
pub id: Uint64,
pub status: OperationStatus,
pub proposer: Addr,
pub executors: Option<Vec<Addr>>,
pub execution_time: Scheduled,
pub target: Addr,
pub data: Binary,
pub title: String,
pub description: String,
}
//impl Into<OperationResponse> for Operation changed to from due to lint warning
impl From<Operation> for OperationResponse {
fn from(operation: Operation) -> OperationResponse {
OperationResponse {
id: operation.id,
status: operation.status,
proposer: operation.proposer,
executors: operation.executors,
execution_time: operation.execution_time,
target: operation.target,
data: operation.data,
title: operation.title,
description: operation.description,
}
}
}
// We define a custom struct for each query response
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[allow(non_snake_case)]
pub struct OperationListResponse {
pub operationList: Vec<OperationResponse>,
}
Error.rs
use cosmwasm_std::StdError;
use thiserror::Error;
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
#[error("Unauthorized")]
Unauthorized {},
#[error("Delay time not ended")]
Unexpired {},
#[error("Address {address:?} not found in Proposers")]
NotFound { address: String },
#[error("Executed operations can not be cancelled.")]
NotDeletable {},
#[error("Proposers list already contains this proposer address")]
AlreadyContainsProposerAddress {},
#[error("Minimum Delay condition not satisfied.")]
MinDelayNotSatisfied {},
#[error("This operation already executed.")]
Executed {},
#[error("Changes can not be made on a frozen Timelock contract.")]
TimelockFrozen {},
}
```
### state.rs
```
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_std::{Addr, Binary, Uint64};
use cw_storage_plus::{Item, Map};
use cw_utils::{Duration, Scheduled};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Timelock {
pub admins: Vec<Addr>,
pub proposers: Vec<Addr>,
pub min_time_delay: Duration,
pub frozen: bool,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Operation {
pub id: Uint64,
pub status: OperationStatus,
pub proposer: Addr,
pub executors: Option<Vec<Addr>>,
pub execution_time: Scheduled,
pub target: Addr,
pub data: Binary,
pub title: String,
pub description: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum OperationStatus {
Pending,
Ready,
Done,
}
pub const CONFIG: Item<Timelock> = Item::new("timelock");
pub const OPERATION_LIST: Map<u64, Operation> = Map::new("operation_list");
pub const OPERATION_SEQ: Item<Uint64> = Item::new("operation_seq");
```
{% hint style="info" %}
Attribution: '[CosmWasm by Example](https://www.cosmwasmbyexample.com/examples)' provides access to the code on [GitHub](https://github.com/athena-consulting/cosmwasm-by-example/tree/main/timelock).
{% endhint %}프로젝트로 돌아가기