From f5d12b98f75911f4d79f8a4370f9fc4d36602599 Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Mon, 15 Apr 2024 15:30:43 +0100 Subject: [PATCH] refactor: remove owner from Config since it's in ownable --- .../pool-manager/schema/pool-manager.json | 15 ------------- .../pool-manager/schema/raw/execute.json | 7 ------ .../pool-manager/schema/raw/instantiate.json | 4 ---- .../schema/raw/response_to_config.json | 4 ---- .../pool-manager/src/contract.rs | 7 ++---- .../pool-manager/src/manager/update_config.rs | 22 ++++++------------- .../src/tests/integration_tests.rs | 17 +++++++------- .../pool-manager/src/tests/suite.rs | 3 --- packages/white-whale-std/Cargo.toml | 2 +- packages/white-whale-std/src/pool_manager.rs | 5 ----- 10 files changed, 19 insertions(+), 67 deletions(-) diff --git a/contracts/liquidity_hub/pool-manager/schema/pool-manager.json b/contracts/liquidity_hub/pool-manager/schema/pool-manager.json index c683efee..00e7e240 100644 --- a/contracts/liquidity_hub/pool-manager/schema/pool-manager.json +++ b/contracts/liquidity_hub/pool-manager/schema/pool-manager.json @@ -8,16 +8,12 @@ "type": "object", "required": [ "fee_collector_addr", - "owner", "pool_creation_fee" ], "properties": { "fee_collector_addr": { "type": "string" }, - "owner": { - "type": "string" - }, "pool_creation_fee": { "$ref": "#/definitions/Coin" } @@ -331,13 +327,6 @@ } ] }, - "owner_addr": { - "description": "The new owner address of the contract, which is allowed to update configuration.", - "type": [ - "string", - "null" - ] - }, "pool_creation_fee": { "description": "The new fee that must be paid when a pool is created.", "anyOf": [ @@ -891,7 +880,6 @@ "type": "object", "required": [ "feature_toggle", - "owner", "pool_creation_fee", "whale_lair_addr" ], @@ -899,9 +887,6 @@ "feature_toggle": { "$ref": "#/definitions/FeatureToggle" }, - "owner": { - "$ref": "#/definitions/Addr" - }, "pool_creation_fee": { "$ref": "#/definitions/Coin" }, diff --git a/contracts/liquidity_hub/pool-manager/schema/raw/execute.json b/contracts/liquidity_hub/pool-manager/schema/raw/execute.json index 64657b06..b5b77b8f 100644 --- a/contracts/liquidity_hub/pool-manager/schema/raw/execute.json +++ b/contracts/liquidity_hub/pool-manager/schema/raw/execute.json @@ -284,13 +284,6 @@ } ] }, - "owner_addr": { - "description": "The new owner address of the contract, which is allowed to update configuration.", - "type": [ - "string", - "null" - ] - }, "pool_creation_fee": { "description": "The new fee that must be paid when a pool is created.", "anyOf": [ diff --git a/contracts/liquidity_hub/pool-manager/schema/raw/instantiate.json b/contracts/liquidity_hub/pool-manager/schema/raw/instantiate.json index ba6a54e3..e87e515e 100644 --- a/contracts/liquidity_hub/pool-manager/schema/raw/instantiate.json +++ b/contracts/liquidity_hub/pool-manager/schema/raw/instantiate.json @@ -4,16 +4,12 @@ "type": "object", "required": [ "fee_collector_addr", - "owner", "pool_creation_fee" ], "properties": { "fee_collector_addr": { "type": "string" }, - "owner": { - "type": "string" - }, "pool_creation_fee": { "$ref": "#/definitions/Coin" } diff --git a/contracts/liquidity_hub/pool-manager/schema/raw/response_to_config.json b/contracts/liquidity_hub/pool-manager/schema/raw/response_to_config.json index db80f0aa..b10cd154 100644 --- a/contracts/liquidity_hub/pool-manager/schema/raw/response_to_config.json +++ b/contracts/liquidity_hub/pool-manager/schema/raw/response_to_config.json @@ -4,7 +4,6 @@ "type": "object", "required": [ "feature_toggle", - "owner", "pool_creation_fee", "whale_lair_addr" ], @@ -12,9 +11,6 @@ "feature_toggle": { "$ref": "#/definitions/FeatureToggle" }, - "owner": { - "$ref": "#/definitions/Addr" - }, "pool_creation_fee": { "$ref": "#/definitions/Coin" }, diff --git a/contracts/liquidity_hub/pool-manager/src/contract.rs b/contracts/liquidity_hub/pool-manager/src/contract.rs index 18c60135..3bb7b165 100644 --- a/contracts/liquidity_hub/pool-manager/src/contract.rs +++ b/contracts/liquidity_hub/pool-manager/src/contract.rs @@ -19,13 +19,12 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); pub fn instantiate( deps: DepsMut, _env: Env, - _info: MessageInfo, + info: MessageInfo, msg: InstantiateMsg, ) -> Result { set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; let config: Config = Config { whale_lair_addr: deps.api.addr_validate(&msg.fee_collector_addr)?, - owner: deps.api.addr_validate(&msg.owner)?, // We must set a creation fee on instantiation to prevent spamming of pools pool_creation_fee: msg.pool_creation_fee, feature_toggle: FeatureToggle { @@ -37,7 +36,7 @@ pub fn instantiate( MANAGER_CONFIG.save(deps.storage, &config)?; // initialize vault counter PAIR_COUNTER.save(deps.storage, &0u64)?; - cw_ownable::initialize_owner(deps.storage, deps.api, Some(msg.owner.as_str()))?; + cw_ownable::initialize_owner(deps.storage, deps.api, Some(info.sender.as_str()))?; Ok(Response::default()) } @@ -150,13 +149,11 @@ pub fn execute( ExecuteMsg::AddSwapRoutes { swap_routes: _ } => Ok(Response::new()), ExecuteMsg::UpdateConfig { whale_lair_addr, - owner_addr, pool_creation_fee, feature_toggle, } => manager::update_config( deps, info, - owner_addr, whale_lair_addr, pool_creation_fee, feature_toggle, diff --git a/contracts/liquidity_hub/pool-manager/src/manager/update_config.rs b/contracts/liquidity_hub/pool-manager/src/manager/update_config.rs index 54d3cc1c..d33b0f1f 100644 --- a/contracts/liquidity_hub/pool-manager/src/manager/update_config.rs +++ b/contracts/liquidity_hub/pool-manager/src/manager/update_config.rs @@ -1,4 +1,5 @@ -use cosmwasm_std::{Coin, DepsMut, MessageInfo, Response}; +use cosmwasm_std::{ensure, Coin, DepsMut, MessageInfo, Response}; +use white_whale_std::pool_manager::Config; use white_whale_std::pool_network::pair::FeatureToggle; use crate::{state::MANAGER_CONFIG, ContractError}; @@ -6,22 +7,14 @@ use crate::{state::MANAGER_CONFIG, ContractError}; pub fn update_config( deps: DepsMut, info: MessageInfo, - owner_addr: Option, whale_lair_addr: Option, pool_creation_fee: Option, feature_toggle: Option, ) -> Result { - MANAGER_CONFIG.update(deps.storage, |mut config| { - // permission check - if info.sender != config.owner { - return Err(ContractError::Unauthorized {}); - } - - if let Some(owner) = owner_addr { - let owner_addr = deps.api.addr_validate(&owner)?; - config.owner = owner_addr; - } + // permission check + cw_ownable::assert_owner(deps.storage, &info.sender)?; + MANAGER_CONFIG.update(deps.storage, |mut config| { if let Some(whale_lair_addr) = whale_lair_addr { let whale_lair_addr = deps.api.addr_validate(&whale_lair_addr)?; config.whale_lair_addr = whale_lair_addr; @@ -34,9 +27,8 @@ pub fn update_config( if let Some(feature_toggle) = feature_toggle { config.feature_toggle = feature_toggle; } - - Ok(config) + Ok::(config) })?; - Ok(Response::new().add_attribute("action", "update_config")) + Ok(Response::default().add_attribute("action", "update_config")) } diff --git a/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs b/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs index 8f4fe553..55833bff 100644 --- a/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs +++ b/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs @@ -1850,12 +1850,15 @@ mod ownership { None, None, None, - None, - |res| { - assert_eq!( - res.unwrap_err().downcast_ref::(), - Some(&ContractError::Unauthorized {}) - ) + |result| { + let err = result.unwrap_err().downcast::().unwrap(); + + match err { + ContractError::OwnershipError { .. } => {} + _ => { + panic!("Wrong error type, should return ContractError::OwnershipError") + } + } }, ); } @@ -1872,7 +1875,6 @@ mod ownership { suite.update_config( creator, - Some(other.clone()), Some(other), Some(coin( current_pool_creation_fee @@ -1893,7 +1895,6 @@ mod ownership { ); let config = suite.query_config(); - assert_ne!(config.owner, initial_config.owner); assert_ne!(config.whale_lair_addr, initial_config.whale_lair_addr); assert_ne!(config.pool_creation_fee, initial_config.pool_creation_fee); assert_ne!(config.feature_toggle, initial_config.feature_toggle); diff --git a/contracts/liquidity_hub/pool-manager/src/tests/suite.rs b/contracts/liquidity_hub/pool-manager/src/tests/suite.rs index f1827b35..cee724d1 100644 --- a/contracts/liquidity_hub/pool-manager/src/tests/suite.rs +++ b/contracts/liquidity_hub/pool-manager/src/tests/suite.rs @@ -122,7 +122,6 @@ impl TestingSuite { pub(crate) fn instantiate(&mut self, whale_lair_addr: String) -> &mut Self { let msg = InstantiateMsg { fee_collector_addr: whale_lair_addr, - owner: self.creator().to_string(), pool_creation_fee: coin(1_000, "uusd"), }; @@ -370,7 +369,6 @@ impl TestingSuite { &mut self, sender: Addr, new_whale_lair_addr: Option, - new_owner_addr: Option, new_pool_creation_fee: Option, new_feature_toggle: Option, result: impl Fn(Result), @@ -380,7 +378,6 @@ impl TestingSuite { self.pool_manager_addr.clone(), &white_whale_std::pool_manager::ExecuteMsg::UpdateConfig { whale_lair_addr: new_whale_lair_addr.map(|addr| addr.to_string()), - owner_addr: new_owner_addr.map(|addr| addr.to_string()), pool_creation_fee: new_pool_creation_fee, feature_toggle: new_feature_toggle, }, diff --git a/packages/white-whale-std/Cargo.toml b/packages/white-whale-std/Cargo.toml index 7ad29f12..bbbcf61a 100644 --- a/packages/white-whale-std/Cargo.toml +++ b/packages/white-whale-std/Cargo.toml @@ -46,4 +46,4 @@ cw20-base.workspace = true [dev-dependencies] cw-multi-test.workspace = true -test-case.workspace = true \ No newline at end of file +test-case.workspace = true diff --git a/packages/white-whale-std/src/pool_manager.rs b/packages/white-whale-std/src/pool_manager.rs index db88d839..a8cec409 100644 --- a/packages/white-whale-std/src/pool_manager.rs +++ b/packages/white-whale-std/src/pool_manager.rs @@ -115,7 +115,6 @@ impl PairInfo {} #[cw_serde] pub struct Config { pub whale_lair_addr: Addr, - pub owner: Addr, // We must set a creation fee on instantiation to prevent spamming of pools pub pool_creation_fee: Coin, // Whether or not swaps, deposits, and withdrawals are enabled @@ -125,7 +124,6 @@ pub struct Config { #[cw_serde] pub struct InstantiateMsg { pub fee_collector_addr: String, - pub owner: String, pub pool_creation_fee: Coin, } @@ -209,9 +207,6 @@ pub enum ExecuteMsg { UpdateConfig { /// The new whale-lair contract address. whale_lair_addr: Option, - /// The new owner address of the contract, which is allowed to update - /// configuration. - owner_addr: Option, /// The new fee that must be paid when a pool is created. pool_creation_fee: Option, /// The new feature toggles of the contract, allowing fine-tuned