Skip to content

Commit

Permalink
add tokenfactory update params
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagni10 committed Jun 20, 2024
1 parent af56c72 commit b0466a4
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/dao/neutron-chain-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ schemars = "0.8.8"
serde = {version = "1.0.175", default-features = false, features = ["derive"]}
serde_with = {version = "3.7.0", features = ["json"]}
thiserror = {version = "1.0"}
neutron-sdk = "0.8.0"
neutron-sdk = { git = "https://github.com/neutron-org/neutron-sdk.git", branch = "feat/whitelist-tf-hooks" }
serde-json-wasm = "1.0.1"
prost = "0.9.0"

Expand Down
57 changes: 55 additions & 2 deletions contracts/dao/neutron-chain-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use crate::cron_module_param_types::{
MsgUpdateParamsCron, ParamsRequestCron, ParamsResponseCron, MSG_TYPE_UPDATE_PARAMS_CRON,
PARAMS_QUERY_PATH_CRON,
};
use crate::tokenfactory_module_param_types::{MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY, MsgUpdateParamsTokenfactory, PARAMS_QUERY_PATH_TOKENFACTORY, ParamsRequestTokenfactory, ParamsResponseTokenfactory};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cw2::set_contract_version;
use neutron_sdk::bindings::msg::{AdminProposal, NeutronMsg, ProposalExecuteMessage};
use neutron_sdk::proto_types::neutron::cron::QueryParamsRequest;
use neutron_sdk::proto_types::neutron::cron::QueryParamsRequest as QueryParamsRequestCron;
use neutron_sdk::proto_types::osmosis::tokenfactory::v1beta1::QueryParamsRequest as QueryParamsRequestTokenfactory;
use neutron_sdk::stargate::aux::make_stargate_query;

use crate::error::ContractError;
Expand Down Expand Up @@ -229,6 +231,9 @@ fn check_proposal_execute_message(
if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_CRON {
check_cron_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY {
check_tokenfactory_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else {
Err(ContractError::Unauthorized {})
}
Expand Down Expand Up @@ -266,7 +271,55 @@ fn check_cron_update_msg_params(

/// 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))
make_stargate_query(deps, PARAMS_QUERY_PATH_CRON, QueryParamsRequestCron::from(req))
}

/// Checks that the strategy owner is authorised to change the parameters of the
/// tokenfactory 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 check_tokenfactory_update_msg_params(
deps: Deps,
strategy: Strategy,
proposal: ProposalExecuteMessage,
) -> Result<(), ContractError> {
let msg_update_params: MsgUpdateParamsTokenfactory =
serde_json_wasm::from_str(proposal.message.as_str())?;

let tokenfactory_update_param_permission = strategy
.get_tokenfactory_update_param_permission()
.ok_or(ContractError::Unauthorized {})?;

let tokenfactory_params = get_tokenfactory_params(deps, ParamsRequestTokenfactory {})?;
if tokenfactory_params.params.denom_creation_fee != msg_update_params.params.denom_creation_fee
&& !tokenfactory_update_param_permission.denom_creation_fee
{
return Err(ContractError::Unauthorized {});
}

if tokenfactory_params.params.denom_creation_gas_consume != msg_update_params.params.denom_creation_gas_consume
&& !tokenfactory_update_param_permission.denom_creation_gas_consume
{
return Err(ContractError::Unauthorized {});
}

if tokenfactory_params.params.fee_collector_address != msg_update_params.params.fee_collector_address
&& !tokenfactory_update_param_permission.fee_collector_address
{
return Err(ContractError::Unauthorized {});
}

if tokenfactory_params.params.whitelisted_hooks != msg_update_params.params.whitelisted_hooks
&& !tokenfactory_update_param_permission.whitelisted_hooks
{
return Err(ContractError::Unauthorized {});
}

Ok(())
}

/// Queries the parameters of the cron module.
pub fn get_tokenfactory_params(deps: Deps, req: ParamsRequestTokenfactory) -> StdResult<ParamsResponseTokenfactory> {
make_stargate_query(deps, PARAMS_QUERY_PATH_TOKENFACTORY, QueryParamsRequestTokenfactory::from(req))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
2 changes: 2 additions & 0 deletions contracts/dao/neutron-chain-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ pub mod contract;
mod error;
pub mod msg;
pub mod state;
pub mod utils;

mod cron_module_param_types;
mod tokenfactory_module_param_types;
#[cfg(test)]
mod testing;
56 changes: 49 additions & 7 deletions contracts/dao/neutron-chain-manager/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,38 @@ impl Strategy {
}),
Strategy::AllowOnly(permissions) => {
match permissions.get(&PermissionType::UpdateParamsPermission) {
Some(Permission::UpdateParamsPermission(update_params_permission)) => {
match update_params_permission {
UpdateParamsPermission::CronUpdateParamsPermission(
cron_update_params,
) => Some(cron_update_params.clone()),
}
}
Some(Permission::UpdateParamsPermission(
UpdateParamsPermission::CronUpdateParamsPermission(cron_update_params),
)) => Some(cron_update_params.clone()),
_ => None,
}
}
}
}

pub fn get_tokenfactory_update_param_permission(&self) -> Option<TokenfactoryUpdateParamsPermission> {
match self {
Strategy::AllowAll => Some(TokenfactoryUpdateParamsPermission {
denom_creation_fee: true,
denom_creation_gas_consume: true,
fee_collector_address: true,
whitelisted_hooks: true,
}),
Strategy::AllowOnly(permissions) => {
match permissions.get(&PermissionType::UpdateParamsPermission) {
Some(Permission::UpdateParamsPermission(
UpdateParamsPermission::TokenfactoryUpdateParamsPermission(tokenfactory_update_params),
)) => Some(tokenfactory_update_params.clone()),
_ => None,
}
}
}
}

}



#[cw_serde]
#[derive(Eq)]
pub enum Permission {
Expand All @@ -145,6 +163,7 @@ pub enum Permission {
// For new-style parameter updates.
UpdateParamsPermission(UpdateParamsPermission),
CronPermission(CronPermission),
TokenfactoryPermission(TokenfactoryPermission),
}

impl From<Permission> for PermissionType {
Expand All @@ -153,6 +172,7 @@ impl From<Permission> for PermissionType {
Permission::ParamChangePermission(_) => PermissionType::ParamChangePermission,
Permission::UpdateParamsPermission(_) => PermissionType::UpdateParamsPermission,
Permission::CronPermission(_) => PermissionType::CronPermission,
Permission::TokenfactoryPermission(_) => PermissionType::TokenfactoryPermission,
}
}
}
Expand All @@ -163,6 +183,7 @@ pub enum PermissionType {
ParamChangePermission,
UpdateParamsPermission,
CronPermission,
TokenfactoryPermission,
}

#[cw_serde]
Expand All @@ -184,6 +205,7 @@ pub struct ParamPermission {
#[derive(Eq)]
pub enum UpdateParamsPermission {
CronUpdateParamsPermission(CronUpdateParamsPermission),
TokenfactoryUpdateParamsPermission(TokenfactoryUpdateParamsPermission),
}

#[cw_serde]
Expand All @@ -202,6 +224,26 @@ pub struct CronPermission {
pub remove_schedule: bool,
}

#[cw_serde]
#[derive(Eq)]
#[serde(rename_all = "snake_case")]
pub struct TokenfactoryUpdateParamsPermission {
pub denom_creation_fee: bool,
pub denom_creation_gas_consume: bool,
pub fee_collector_address: bool,
pub whitelisted_hooks: bool,
}

#[cw_serde]
#[derive(Eq)]
#[serde(rename_all = "snake_case")]
pub struct TokenfactoryPermission {
pub denom_creation_fee: bool,
pub denom_creation_gas_consume: bool,
pub fee_collector_address: bool,
pub whitelisted_hooks: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ProposalExecuteMessageJSON {
Expand Down
14 changes: 13 additions & 1 deletion contracts/dao/neutron-chain-manager/src/testing/mock_querier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::cron_module_param_types::{ParamsCron, ParamsResponseCron};
use crate::tokenfactory_module_param_types::{ParamsTokenfactory, ParamsResponseTokenfactory, WhitelistedHook};
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{
from_json, to_json_binary, ContractResult, Empty, OwnedDeps, Querier, QuerierResult,
QueryRequest, SystemError, SystemResult,
QueryRequest, SystemError, SystemResult, coin
};
use std::marker::PhantomData;

Expand Down Expand Up @@ -50,6 +51,17 @@ impl WasmMockQuerier {
});
SystemResult::Ok(ContractResult::from(resp))
}
"/neutron.tokenfactory.Query/Params" => {
let resp = to_json_binary(&ParamsResponseTokenfactory {
params: ParamsTokenfactory {
denom_creation_fee: vec![coin(1, "untrn")],
denom_creation_gas_consume: 0,
fee_collector_address: "test_addr".to_string(),
whitelisted_hooks: vec![],
},
});
SystemResult::Ok(ContractResult::from(resp))
}
_ => todo!(),
},
_ => self.base.handle_query(request),
Expand Down
Loading

0 comments on commit b0466a4

Please sign in to comment.