Skip to content

Commit

Permalink
Make config update optional per @maxrobot feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
shapeshed committed Aug 31, 2023
1 parent d5ee3da commit a876400
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
16 changes: 13 additions & 3 deletions contracts/liquidator/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,11 +24,14 @@ pub fn instantiate(
) -> Result<Response, ContractError> {
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,
},
)?;
Expand All @@ -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,
Expand Down
43 changes: 27 additions & 16 deletions contracts/liquidator/src/handle.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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(
Expand All @@ -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<String>,
ibc_channel_id: Option<String>,
liquidation_target: Option<String>,
) -> Result<Response, ContractError> {
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<Response, ContractError> {
Expand All @@ -61,7 +72,7 @@ pub fn ibc_transfer(deps: Deps, env: Env, _info: MessageInfo) -> Result<Response

let msg = IbcMsg::Transfer {
channel_id: config.ibc_channel_id,
to_address: config.ibc_to_address,
to_address: config.ibc_to_address.to_string(),
amount: Coin {
amount: balance,
denom: config.liquidation_target,
Expand Down
3 changes: 2 additions & 1 deletion contracts/liquidator/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmwasm_std::Addr;
use cw_controllers::Admin;
use cw_storage_plus::{Item, Map};
use osmosis_std::types::osmosis::poolmanager::v1beta1::SwapAmountInRoute;
Expand All @@ -11,6 +12,6 @@ pub const ROUTING_TABLE: Map<(&str, &str), Vec<SwapAmountInRoute>> = 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,
}

0 comments on commit a876400

Please sign in to comment.