Skip to content

Commit

Permalink
Use cw-controllers per @maxrobot's suggest
Browse files Browse the repository at this point in the history
  • Loading branch information
shapeshed committed Aug 31, 2023
1 parent c6e692a commit d5ee3da
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
8 changes: 5 additions & 3 deletions contracts/liquidator/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
};
use cw2::set_contract_version;

Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn instantiate(

let owner_address = deps.api.addr_validate(&msg.owner)?;

OWNER.save(deps.storage, &owner_address)?;
OWNER.set(deps, Some(owner_address))?;

Ok(Response::new().add_attribute("method", "instantiate").add_attribute("owner", info.sender))
}
Expand Down Expand Up @@ -68,7 +68,9 @@ pub fn execute(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetOwner {} => to_binary(&query_owner(deps)?),
QueryMsg::GetOwner {} => {
to_binary(&query_owner(deps).map_err(|err| StdError::generic_err(err.to_string()))?)
}
QueryMsg::GetConfig {} => to_binary(&query_config(deps)?),
QueryMsg::GetRoute {
input_denom,
Expand Down
6 changes: 6 additions & 0 deletions contracts/liquidator/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Owner not set")]
NoOwner {},

#[error("Owner update error")]
OwnerUpdateError {},

#[error("InvalidTokenShare")]
InvalidTokenShare {},

Expand Down
19 changes: 9 additions & 10 deletions contracts/liquidator/src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
Addr, BalanceResponse, BankQuery, Coin, Deps, DepsMut, Env, IbcMsg, MessageInfo, Order,
ensure, Addr, BalanceResponse, BankQuery, Coin, Deps, DepsMut, Env, IbcMsg, MessageInfo, Order,
QueryRequest, Response, Uint128,
};
use osmosis_std::types::osmosis::poolmanager::v1beta1::{MsgSwapExactAmountIn, SwapAmountInRoute};
Expand All @@ -8,7 +8,7 @@ pub const PACKET_LIFETIME: u64 = 60 * 60; // One hour

use crate::{
error::ContractError,
helpers::{generate_swap_msg, validate_is_owner, validate_pool_route},
helpers::{generate_swap_msg, validate_pool_route},
state::{Config, CONFIG, OWNER, ROUTING_TABLE},
};

Expand All @@ -17,12 +17,11 @@ pub fn update_owner(
info: MessageInfo,
owner: String,
) -> Result<Response, ContractError> {
validate_is_owner(deps.as_ref(), info.sender)?;
let new_owner = deps.api.addr_validate(&owner)?;
OWNER.save(deps.storage, &new_owner)?;
Ok(Response::new()
.add_attribute("action", "change_contract_owner")
.add_attribute("new_owner", new_owner))
let valid_owner = deps.api.addr_validate(&owner)?;
match OWNER.execute_update_admin(deps, info, Some(valid_owner)) {
Ok(response) => Ok(response),
Err(_e) => Err(ContractError::OwnerUpdateError {}),
}
}

pub fn update_config(
Expand All @@ -32,7 +31,7 @@ pub fn update_config(
ibc_channel_id: String,
liquidation_target: String,
) -> Result<Response, ContractError> {
validate_is_owner(deps.as_ref(), info.sender)?;
ensure!(OWNER.is_admin(deps.as_ref(), &info.sender)?, ContractError::Unauthorized {});
CONFIG.save(
deps.storage,
&Config {
Expand Down Expand Up @@ -119,7 +118,7 @@ pub fn set_route(
output_denom: String,
pool_route: Vec<SwapAmountInRoute>,
) -> Result<Response, ContractError> {
validate_is_owner(deps.as_ref(), info.sender)?;
ensure!(OWNER.is_admin(deps.as_ref(), &info.sender)?, ContractError::Unauthorized {});

validate_pool_route(
deps.as_ref(),
Expand Down
16 changes: 1 addition & 15 deletions contracts/liquidator/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,7 @@ use osmosis_std::{
},
};

use crate::{
error::ContractError,
state::{OWNER, ROUTING_TABLE},
};

// Validate if sender is the contract owner.
// Returns success if sender is the owner, error otherwise.
pub fn validate_is_owner(deps: Deps, sender: Addr) -> Result<(), ContractError> {
let owner = OWNER.load(deps.storage).unwrap();
if owner != sender {
Err(ContractError::Unauthorized {})
} else {
Ok(())
}
}
use crate::{error::ContractError, state::ROUTING_TABLE};

// validate_pool_route validates if the pool route is valid.
// Returns success if it is, error otherwise.
Expand Down
14 changes: 9 additions & 5 deletions contracts/liquidator/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use cosmwasm_std::{Deps, StdError, StdResult};

use crate::{
error::ContractError,
msg::{GetOwnerResponse, GetRouteResponse},
state::{Config, CONFIG, OWNER, ROUTING_TABLE},
};

/// Queries contract owner from the admin
pub fn query_owner(deps: Deps) -> StdResult<GetOwnerResponse> {
let owner = OWNER.load(deps.storage)?;
Ok(GetOwnerResponse {
owner: owner.into_string(),
})
pub fn query_owner(deps: Deps) -> Result<GetOwnerResponse, ContractError> {
if let Some(owner) = OWNER.get(deps)? {
Ok(GetOwnerResponse {
owner: owner.to_string(),
})
} else {
Err(ContractError::NoOwner {})
}
}

/// Queries config
Expand Down
4 changes: 2 additions & 2 deletions contracts/liquidator/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use cosmwasm_std::Addr;
use cw_controllers::Admin;
use cw_storage_plus::{Item, Map};
use osmosis_std::types::osmosis::poolmanager::v1beta1::SwapAmountInRoute;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub const CONFIG: Item<Config> = Item::new("config");
pub const OWNER: Item<Addr> = Item::new("owner");
pub const OWNER: Admin = Admin::new("owner");
pub const ROUTING_TABLE: Map<(&str, &str), Vec<SwapAmountInRoute>> = Map::new("routing_table");

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down

0 comments on commit d5ee3da

Please sign in to comment.