From a8764009aec1d26c9e5c0b885491b20f209085fd Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Thu, 31 Aug 2023 18:05:13 +0100 Subject: [PATCH] Make config update optional per @maxrobot feedback --- contracts/liquidator/src/contract.rs | 16 +++++++++-- contracts/liquidator/src/handle.rs | 43 +++++++++++++++++----------- contracts/liquidator/src/state.rs | 3 +- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/contracts/liquidator/src/contract.rs b/contracts/liquidator/src/contract.rs index f83454e..afc1d8b 100644 --- a/contracts/liquidator/src/contract.rs +++ b/contracts/liquidator/src/contract.rs @@ -1,5 +1,6 @@ use cosmwasm_std::{ - entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, + entry_point, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, + StdResult, }; use cw2::set_contract_version; @@ -23,11 +24,14 @@ pub fn instantiate( ) -> Result { set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), CONTRACT_VERSION)?; + let ibc_to_address_string: String = msg.ibc_to_address; + let ibc_address: Addr = Addr::unchecked(ibc_to_address_string); + CONFIG.save( deps.storage, &Config { ibc_channel_id: msg.ibc_channel_id, - ibc_to_address: msg.ibc_to_address, + ibc_to_address: ibc_address, liquidation_target: msg.liquidation_target, }, )?; @@ -54,7 +58,13 @@ pub fn execute( ibc_to_address, ibc_channel_id, liquidation_target, - } => update_config(deps, info, ibc_to_address, ibc_channel_id, liquidation_target), + } => update_config( + deps, + info, + Some(ibc_to_address), + Some(ibc_channel_id), + Some(liquidation_target), + ), ExecuteMsg::SetRoute { input_denom, output_denom, diff --git a/contracts/liquidator/src/handle.rs b/contracts/liquidator/src/handle.rs index 06a3fe0..20cb302 100644 --- a/contracts/liquidator/src/handle.rs +++ b/contracts/liquidator/src/handle.rs @@ -1,6 +1,6 @@ use cosmwasm_std::{ - ensure, Addr, BalanceResponse, BankQuery, Coin, Deps, DepsMut, Env, IbcMsg, MessageInfo, Order, - QueryRequest, Response, Uint128, + ensure, Addr, BalanceResponse, BankQuery, Coin, Deps, DepsMut, Env, Event, IbcMsg, MessageInfo, + Order, QueryRequest, Response, Uint128, }; use osmosis_std::types::osmosis::poolmanager::v1beta1::{MsgSwapExactAmountIn, SwapAmountInRoute}; @@ -9,7 +9,7 @@ pub const PACKET_LIFETIME: u64 = 60 * 60; // One hour use crate::{ error::ContractError, helpers::{generate_swap_msg, validate_pool_route}, - state::{Config, CONFIG, OWNER, ROUTING_TABLE}, + state::{CONFIG, OWNER, ROUTING_TABLE}, }; pub fn update_owner( @@ -27,21 +27,32 @@ pub fn update_owner( pub fn update_config( deps: DepsMut, info: MessageInfo, - ibc_to_address: String, - ibc_channel_id: String, - liquidation_target: String, + ibc_to_address: Option, + ibc_channel_id: Option, + liquidation_target: Option, ) -> Result { + let mut config = CONFIG.load(deps.storage)?; ensure!(OWNER.is_admin(deps.as_ref(), &info.sender)?, ContractError::Unauthorized {}); - CONFIG.save( - deps.storage, - &Config { - ibc_to_address, - ibc_channel_id, - liquidation_target, - }, - )?; + let mut event = Event::new("update_config"); + + if let Some(ibc_to_address) = ibc_to_address { + config.ibc_to_address = deps.api.addr_validate(&ibc_to_address)?; + event = event.add_attribute("ibc_to_address", ibc_to_address); + } + + if let Some(ibc_channel_id) = ibc_channel_id { + config.ibc_channel_id = ibc_channel_id.clone(); + event = event.add_attribute("ibc_channel_id", ibc_channel_id); + } + + if let Some(liquidation_target) = liquidation_target { + config.liquidation_target = liquidation_target.clone(); + event = event.add_attribute("liquidation_target", liquidation_target); + } + + CONFIG.save(deps.storage, &config)?; - Ok(Response::new().add_attribute("action", "update_config")) + Ok(Response::default().add_event(event)) } pub fn ibc_transfer(deps: Deps, env: Env, _info: MessageInfo) -> Result { @@ -61,7 +72,7 @@ pub fn ibc_transfer(deps: Deps, env: Env, _info: MessageInfo) -> Result> = Map::new("r #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct Config { pub ibc_channel_id: String, - pub ibc_to_address: String, + pub ibc_to_address: Addr, pub liquidation_target: String, }