Skip to content

Commit

Permalink
chore: consolidated error types.
Browse files Browse the repository at this point in the history
  • Loading branch information
desamtralized committed Jul 29, 2022
1 parent 56c8ed7 commit 1446b56
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 221 deletions.
26 changes: 18 additions & 8 deletions contracts/contracts/hub/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use cosmwasm_std::{entry_point, Addr, Binary, Deps, StdResult};
use cosmwasm_std::{to_binary, CosmosMsg, DepsMut, Env, MessageInfo, Response, SubMsg, WasmMsg};
use cw20::Denom;
use localterra_protocol::errors::ContractError;
use localterra_protocol::errors::ContractError::Unauthorized;

use crate::errors::HubError;
use crate::errors::HubError::Unauthorized;
use crate::state::{ADMIN, CONFIG};
use localterra_protocol::hub::{Admin, ExecuteMsg, HubConfig, InstantiateMsg, QueryMsg};
use localterra_protocol::offer::ExecuteMsg::RegisterHub as OfferRegisterHub;
Expand All @@ -16,7 +16,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, HubError> {
) -> Result<Response, ContractError> {
let admin = Admin {
addr: msg.admin_addr.clone(),
};
Expand All @@ -32,7 +32,7 @@ pub fn execute(
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, HubError> {
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::UpdateConfig(config) => update_config(deps, info, config),
ExecuteMsg::UpdateAdmin { admin_addr } => update_admin(deps, info, admin_addr),
Expand All @@ -43,10 +43,13 @@ fn update_config(
deps: DepsMut,
info: MessageInfo,
config: HubConfig,
) -> Result<Response, HubError> {
) -> Result<Response, ContractError> {
let admin = ADMIN.load(deps.storage).unwrap();
if !info.sender.eq(&admin.addr) {
return Err(Unauthorized {});
return Err(Unauthorized {
owner: admin.addr.clone(),
caller: info.sender.clone(),
});
}
CONFIG.save(deps.storage, &config).unwrap();
let local_denom = match config.local_denom {
Expand Down Expand Up @@ -85,10 +88,17 @@ fn update_config(
Ok(res)
}

fn update_admin(deps: DepsMut, info: MessageInfo, new_admin: Addr) -> Result<Response, HubError> {
fn update_admin(
deps: DepsMut,
info: MessageInfo,
new_admin: Addr,
) -> Result<Response, ContractError> {
let mut admin = ADMIN.load(deps.storage).unwrap();
if !info.sender.eq(&admin.addr) {
return Err(Unauthorized {});
return Err(Unauthorized {
owner: admin.addr.clone(),
caller: info.sender.clone(),
});
}

let old_admin = admin.addr.clone();
Expand Down
11 changes: 0 additions & 11 deletions contracts/contracts/hub/src/errors.rs

This file was deleted.

1 change: 0 additions & 1 deletion contracts/contracts/hub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod contract;
pub mod errors;
mod state;
24 changes: 12 additions & 12 deletions contracts/contracts/offer/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cosmwasm_std::{
use cw_storage_plus::Bound;

use localterra_protocol::currencies::FiatCurrency;
use localterra_protocol::errors::GuardError;
use localterra_protocol::errors::GuardError::{HubAlreadyRegistered, Unauthorized};
use localterra_protocol::errors::ContractError;
use localterra_protocol::errors::ContractError::{HubAlreadyRegistered, Unauthorized};
use localterra_protocol::guards::{assert_min_g_max, assert_ownership, assert_range_0_to_99};
use localterra_protocol::hub::HubConfig;
use localterra_protocol::hub_utils::{
Expand All @@ -25,7 +25,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
offers_count_storage(deps.storage).save(&OffersCount { count: 0 })?;
Ok(Response::default())
}
Expand All @@ -36,7 +36,7 @@ pub fn execute(
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Create { offer } => create_offer(deps, env, info, offer),
ExecuteMsg::UpdateOffer { offer_update } => update_offer(deps, env, info, offer_update),
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn create_offer(
env: Env,
info: MessageInfo,
msg: OfferMsg,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
assert_min_g_max(msg.min_amount, msg.max_amount)?;

let mut offers_count = offers_count_storage(deps.storage).load().unwrap();
Expand Down Expand Up @@ -159,7 +159,7 @@ pub fn execute_update_trade_arbitrator(
_env: Env,
info: MessageInfo,
arbitrator: Addr,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
// TODO assert the calling contract can only update its own arbitrator and only if the arbitrator is not yet set. LOCAL-660
let mut trade = trades().load(deps.storage, &info.sender.as_str())?;

Expand All @@ -183,7 +183,7 @@ pub fn execute_update_last_traded(
env: Env,
info: MessageInfo,
offer_id: String,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
let hub_addr = query_hub_addr(deps.as_ref()).unwrap();
let hub_config = get_hub_config(&deps.querier, hub_addr.addr.to_string());

Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn create_arbitrator(
info: MessageInfo,
arbitrator: Addr,
asset: FiatCurrency,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
let hub_addr = HUB_ADDR.load(deps.storage).unwrap();
let admin = get_hub_admin(&deps.querier, hub_addr.addr.to_string());
assert_ownership(info.sender, admin)?;
Expand Down Expand Up @@ -248,7 +248,7 @@ pub fn delete_arbitrator(
info: MessageInfo,
arbitrator: Addr,
asset: FiatCurrency,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
let hub_addr = HUB_ADDR.load(deps.storage).unwrap();
let admin = get_hub_admin(&deps.querier, hub_addr.addr.to_string());
assert_ownership(info.sender, admin)?;
Expand All @@ -270,7 +270,7 @@ pub fn update_offer(
_env: Env,
info: MessageInfo,
msg: OfferUpdateMsg,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
assert_min_g_max(msg.min_amount, msg.max_amount)?;

let mut offer_model = OfferModel::may_load(deps.storage, &msg.id);
Expand All @@ -287,15 +287,15 @@ pub fn update_offer(
Ok(res)
}

fn register_hub(deps: DepsMut, info: MessageInfo) -> Result<Response, GuardError> {
fn register_hub(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
register_hub_internal(info.sender, deps.storage, HubAlreadyRegistered {})
}

fn increment_trades_count(
deps: DepsMut,
info: MessageInfo,
offer_id: String,
) -> Result<Response, GuardError> {
) -> Result<Response, ContractError> {
let hub_addr = query_hub_addr(deps.as_ref()).unwrap();
let hub_cfg: HubConfig = get_hub_config(&deps.querier, hub_addr.addr.to_string());

Expand Down
17 changes: 0 additions & 17 deletions contracts/contracts/offer/src/errors.rs

This file was deleted.

1 change: 0 additions & 1 deletion contracts/contracts/offer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod contract;
pub mod errors;
pub mod state;
45 changes: 22 additions & 23 deletions contracts/contracts/trade/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use std::str::FromStr;

use localterra_protocol::constants::{FUNDING_TIMEOUT, LOCAL_FEE, REQUEST_TIMEOUT};
use localterra_protocol::denom_utils::denom_to_string;
use localterra_protocol::errors::ContractError;
use localterra_protocol::errors::ContractError::{
FundEscrowError, HubAlreadyRegistered, InvalidTradeStateChange, OfferNotFound,
RefundErrorNotExpired, TradeExpired,
};
use localterra_protocol::guards::{
assert_caller_is_buyer_or_seller, assert_ownership, assert_trade_state_and_type,
assert_trade_state_change_is_valid, assert_value_in_range, trade_request_is_expired,
Expand All @@ -24,9 +29,6 @@ use localterra_protocol::trade::{
};
use localterra_protocol::trading_incentives::ExecuteMsg as TradingIncentivesMsg;

use crate::errors::TradeError;
use crate::errors::TradeError::HubAlreadyRegistered;

pub const SWAP_REPLY_ID: u64 = 1u64;

#[entry_point]
Expand All @@ -35,7 +37,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
Ok(Response::default())
}

Expand All @@ -45,7 +47,7 @@ pub fn execute(
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Create(new_trade) => create_trade(deps, env, new_trade),
ExecuteMsg::AcceptRequest { trade_id } => accept_request(deps, env, info, trade_id),
Expand All @@ -59,7 +61,7 @@ pub fn execute(
}
}

fn create_trade(deps: DepsMut, env: Env, new_trade: NewTrade) -> Result<Response, TradeError> {
fn create_trade(deps: DepsMut, env: Env, new_trade: NewTrade) -> Result<Response, ContractError> {
//Load Offer
let hub_addr = HUB_ADDR.load(deps.storage).unwrap();
let hub_cfg = get_hub_config(&deps.querier, hub_addr.addr.to_string());
Expand All @@ -71,7 +73,7 @@ fn create_trade(deps: DepsMut, env: Env, new_trade: NewTrade) -> Result<Response
hub_cfg.offer_addr.to_string(),
);
if offer.is_none() {
return Err(TradeError::OfferNotFound {
return Err(OfferNotFound {
offer_id: new_trade.offer_id.to_string(),
});
}
Expand Down Expand Up @@ -156,7 +158,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
}
}

fn register_hub(deps: DepsMut, info: MessageInfo) -> Result<Response, TradeError> {
fn register_hub(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
register_hub_internal(info.sender, deps.storage, HubAlreadyRegistered {})
}

Expand Down Expand Up @@ -211,7 +213,7 @@ fn fund_escrow(
env: Env,
info: MessageInfo,
trade_id: String,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
let mut trade = TradeModel::from_store(deps.storage, &trade_id);

let offer = load_offer(
Expand All @@ -226,7 +228,7 @@ fn fund_escrow(
trade.state = TradeState::RequestExpired;
TradeModel::store(deps.storage, &trade).unwrap();

return Err(TradeError::Expired {
return Err(TradeExpired {
timeout: REQUEST_TIMEOUT,
expired_at: env.block.time.seconds() + REQUEST_TIMEOUT,
created_at: trade.created_at,
Expand All @@ -250,7 +252,7 @@ fn fund_escrow(
if balance.amount >= trade.amount {
trade.state = TradeState::EscrowFunded;
} else {
return Err(TradeError::FundEscrowError {
return Err(FundEscrowError {
required_amount: trade.amount.clone(),
sent_amount: balance.amount.clone(),
});
Expand All @@ -272,7 +274,7 @@ fn dispute_escrow(
env: Env,
info: MessageInfo,
trade_id: String,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
let mut trade = TradeModel::from_store(deps.storage, &trade_id);
// TODO check escrow funding timer*
// Only the buyer or seller can start a dispute
Expand Down Expand Up @@ -328,7 +330,7 @@ fn accept_request(
_env: Env,
info: MessageInfo,
trade_id: String,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
let mut trade = TradeModel::from_store(deps.storage, &trade_id);
// Only the buyer can accept the request
assert_ownership(info.sender, trade.buyer.clone()).unwrap();
Expand Down Expand Up @@ -357,7 +359,7 @@ fn fiat_deposited(
deps: DepsMut,
info: MessageInfo,
trade_id: String,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
let mut trade = TradeModel::from_store(deps.storage, &trade_id);
// The buyer is always the one depositing fiat
// Only the buyer can mark the fiat as deposited
Expand Down Expand Up @@ -386,7 +388,7 @@ fn cancel_request(
deps: DepsMut,
info: MessageInfo,
trade_id: String,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
let mut trade = TradeModel::from_store(deps.storage, &trade_id);
// Only the buyer or seller can cancel the trade.
assert_caller_is_buyer_or_seller(info.sender, trade.buyer.clone(), trade.seller.clone())
Expand All @@ -396,7 +398,7 @@ fn cancel_request(
if !((trade.state == TradeState::RequestCreated)
|| (trade.state == TradeState::RequestAccepted))
{
return Err(TradeError::InvalidStateChange {
return Err(InvalidTradeStateChange {
from: trade.state,
to: TradeState::RequestCanceled,
});
Expand All @@ -413,11 +415,9 @@ fn release_escrow(
deps: DepsMut,
info: MessageInfo,
trade_id: String,
) -> Result<Response, TradeError> {
) -> Result<Response, ContractError> {
let mut trade = TradeModel::from_store(deps.storage, &trade_id);
let trade_denom = denom_to_string(&trade.denom);

let arbitrator = trade.arbitrator.clone().unwrap_or(Addr::unchecked(""));
if trade.seller.eq(&info.sender) {
assert_trade_state_change_is_valid(
trade.state.clone(),
Expand All @@ -426,9 +426,8 @@ fn release_escrow(
)
.unwrap();
} else {
return Err(TradeError::Unauthorized {
return Err(ContractError::Unauthorized {
owner: trade.seller.clone(),
arbitrator: arbitrator.clone(),
caller: info.sender.clone(),
});
}
Expand Down Expand Up @@ -581,7 +580,7 @@ fn handle_swap_reply(_deps: DepsMut, msg: Reply) -> StdResult<Response> {
Ok(res)
}

fn refund_escrow(deps: DepsMut, env: Env, trade_id: String) -> Result<Response, TradeError> {
fn refund_escrow(deps: DepsMut, env: Env, trade_id: String) -> Result<Response, ContractError> {
// Refund can only happen if trade state is TradeState::EscrowFunded and FundingTimeout is expired
let trade = TradeModel::from_store(deps.storage, &trade_id);
assert_trade_state_change_is_valid(
Expand All @@ -595,7 +594,7 @@ fn refund_escrow(deps: DepsMut, env: Env, trade_id: String) -> Result<Response,
// no one except arbitrator can refund if the trade is in arbitration
let expired = env.block.time.seconds() > trade.created_at + FUNDING_TIMEOUT;
if !expired {
return Err(TradeError::RefundErrorNotExpired {
return Err(RefundErrorNotExpired {
message:
"Only expired trades that are not disputed can be refunded by non-arbitrators."
.to_string(),
Expand Down
Loading

0 comments on commit 1446b56

Please sign in to comment.