Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Zavgorodnii committed Feb 5, 2024
1 parent a4f68fe commit fd6eeea
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions contracts/dao/neutron-chain-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{
StdResult,
};
use cw2::set_contract_version;
use neutron_sdk::bindings::msg::{AdminProposal, NeutronMsg};
use neutron_sdk::bindings::msg::{AdminProposal, NeutronMsg, ProposalExecuteMessage};
use neutron_sdk::proto_types::neutron::cron::QueryParamsRequest;
use neutron_sdk::stargate::aux::make_stargate_query;

Expand Down Expand Up @@ -159,31 +159,11 @@ pub fn execute_execute_messages(
}
}
AdminProposal::ProposalExecuteMessage(proposal) => {
let typed_proposal: ProposalExecuteMessageJSON =
serde_json_wasm::from_str(proposal.message.as_str())?;

if typed_proposal.type_field.as_str() == "/neutron.cron.MsgUpdateParams" {
let msg_update_params: MsgUpdateParamsCron =
serde_json_wasm::from_str(proposal.message.as_str())?;

let cron_update_param_permission = strategy
.get_cron_update_param_permission()
.ok_or(ContractError::Unauthorized {})?;

let cron_params = get_cron_params(deps.as_ref(), ParamsRequestCron {})?;
if cron_params.params.limit != msg_update_params.params.limit
&& !cron_update_param_permission.limit
{
return Err(ContractError::Unauthorized {});
}

if cron_params.params.security_address
!= msg_update_params.params.security_address
&& !cron_update_param_permission.security_address
{
return Err(ContractError::Unauthorized {});
}
}
process_proposal_execute_message(
deps.as_ref(),
strategy.clone(),
proposal,
)?;
}
_ => {}
},
Expand All @@ -199,6 +179,54 @@ pub fn execute_execute_messages(
.add_messages(messages))
}

/// Processes ProposalExecuteMessage messages. Message type has to be checked
/// as a string; after that, you can parse the JSON payload into a specific
/// message.
fn process_proposal_execute_message(
deps: Deps,
strategy: Strategy,
proposal: ProposalExecuteMessage,
) -> Result<(), ContractError> {
let typed_proposal: ProposalExecuteMessageJSON =
serde_json_wasm::from_str(proposal.message.as_str())?;

if typed_proposal.type_field.as_str() == "/neutron.cron.MsgUpdateParams" {
process_cron_update_msg_params(deps, strategy, proposal)?;
}

Ok(())
}
/// Checks that the strategy owner is authorised to change the parameters of the
/// cron module. We query the current values for each parameter & compare them to
/// the values in the proposal; all modifications must be allowed by the strategy.
fn process_cron_update_msg_params(
deps: Deps,
strategy: Strategy,
proposal: ProposalExecuteMessage,
) -> Result<(), ContractError> {
let msg_update_params: MsgUpdateParamsCron =
serde_json_wasm::from_str(proposal.message.as_str())?;

let cron_update_param_permission = strategy
.get_cron_update_param_permission()
.ok_or(ContractError::Unauthorized {})?;

let cron_params = get_cron_params(deps, ParamsRequestCron {})?;
if cron_params.params.limit != msg_update_params.params.limit
&& !cron_update_param_permission.limit
{
return Err(ContractError::Unauthorized {});
}

if cron_params.params.security_address != msg_update_params.params.security_address
&& !cron_update_param_permission.security_address
{
return Err(ContractError::Unauthorized {});
}

Ok(())
}

/// Queries the parameters of the cron module.
pub fn get_cron_params(deps: Deps, req: ParamsRequestCron) -> StdResult<ParamsResponseCron> {
make_stargate_query(deps, PARAMS_QUERY_PATH_CRON, QueryParamsRequest::from(req))
Expand Down

0 comments on commit fd6eeea

Please sign in to comment.