Skip to content

Commit

Permalink
feat: add interface of update route
Browse files Browse the repository at this point in the history
  • Loading branch information
shenao78 committed Jul 24, 2024
1 parent 8476ec9 commit d890808
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let state = State {
route: msg.route.clone(),
admin: info.sender,
tokens: BTreeMap::default(),
handled_tickets: BTreeSet::default(),
handled_directives: BTreeSet::default(),
Expand Down Expand Up @@ -67,6 +68,7 @@ pub fn execute(
ExecuteMsg::BurnToken { token_id, amount } => {
execute::burn_token(deps, env, info, token_id, amount)
}
ExecuteMsg::UpdateRoute { route } => execute::update_route(deps, info, route),
}?;
Ok(response.add_event(Event::new("execute_msg").add_attribute("contract", contract)))
}
Expand Down Expand Up @@ -298,6 +300,25 @@ pub mod execute {
))
}

pub fn update_route(
deps: DepsMut,
info: MessageInfo,
route: Addr,
) -> Result<Response, ContractError> {
if read_state(deps.storage, |s| info.sender != s.admin) {
return Err(ContractError::Unauthorized);
}

STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
state.route = route.clone();
Ok(state)
})?;

Ok(Response::new().add_event(
Event::new("RouteUpdated").add_attributes(vec![Attribute::new("new_route", route)]),
))
}

fn build_burn_msg(
contract_addr: Addr,
sender: Addr,
Expand Down
3 changes: 3 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub enum ExecuteMsg {
token_id: String,
amount: String,
},
UpdateRoute {
route: Addr,
}
}

#[cw_serde]
Expand Down
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cw_storage_plus::Item;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub route: Addr,
pub admin: Addr,
pub tokens: BTreeMap<String, Token>,
pub handled_tickets: BTreeSet<String>,
pub handled_directives: BTreeSet<u64>,
Expand Down

0 comments on commit d890808

Please sign in to comment.