Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed Oct 20, 2023
1 parent eaa125a commit 61bd61e
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8070,7 +8070,7 @@ fn user_weight_accounts_for_unbondings() {
)
.unwrap();

let mut epoch_res: EpochResponse = app
let epoch_res: EpochResponse = app
.wrap()
.query_wasm_smart(
fee_distributor_address.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use white_whale::pool_network::asset::{
use white_whale::pool_network::denom::{Coin, MsgBurn, MsgMint};
#[cfg(feature = "osmosis_token_factory")]
use white_whale::pool_network::denom_osmosis::{Coin, MsgBurn, MsgMint};
use white_whale::pool_network::swap;
use white_whale::pool_network::trio::{Config, Cw20HookMsg, FeatureToggle, PoolFee, RampAmp};

use crate::error::ContractError;
Expand Down Expand Up @@ -387,24 +388,16 @@ pub fn swap(
let ask_pool: Asset;
let offer_pool: Asset;
let unswapped_pool: Asset;
let ask_decimal: u8;
let offer_decimal: u8;

if ask_asset.equal(&pools[0].info) {
if offer_asset.info.equal(&pools[1].info) {
ask_pool = pools[0].clone();
offer_pool = pools[1].clone();
unswapped_pool = pools[2].clone();

ask_decimal = trio_info.asset_decimals[0];
offer_decimal = trio_info.asset_decimals[1];
} else if offer_asset.info.equal(&pools[2].info) {
ask_pool = pools[0].clone();
offer_pool = pools[2].clone();
unswapped_pool = pools[1].clone();

ask_decimal = trio_info.asset_decimals[0];
offer_decimal = trio_info.asset_decimals[2];
} else {
return Err(ContractError::AssetMismatch {});
}
Expand All @@ -413,16 +406,10 @@ pub fn swap(
ask_pool = pools[1].clone();
offer_pool = pools[0].clone();
unswapped_pool = pools[2].clone();

ask_decimal = trio_info.asset_decimals[1];
offer_decimal = trio_info.asset_decimals[0];
} else if offer_asset.info.equal(&pools[2].info) {
ask_pool = pools[1].clone();
offer_pool = pools[2].clone();
unswapped_pool = pools[0].clone();

ask_decimal = trio_info.asset_decimals[1];
offer_decimal = trio_info.asset_decimals[2];
} else {
return Err(ContractError::AssetMismatch {});
}
Expand All @@ -431,16 +418,10 @@ pub fn swap(
ask_pool = pools[2].clone();
offer_pool = pools[0].clone();
unswapped_pool = pools[1].clone();

ask_decimal = trio_info.asset_decimals[2];
offer_decimal = trio_info.asset_decimals[0];
} else if offer_asset.info.equal(&pools[1].info) {
ask_pool = pools[2].clone();
offer_pool = pools[1].clone();
unswapped_pool = pools[0].clone();

ask_decimal = trio_info.asset_decimals[2];
offer_decimal = trio_info.asset_decimals[1];
} else {
return Err(ContractError::AssetMismatch {});
}
Expand Down Expand Up @@ -478,7 +459,7 @@ pub fn swap(
.checked_add(swap_computation.burn_fee_amount)?;

// check max spread limit if exist
helpers::assert_max_spread(
swap::assert_max_spread(
belief_price,
max_spread,
offer_asset.amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ pub enum ContractError {
#[error("Invalid zero amount")]
InvalidZeroAmount {},

#[error("Spread limit exceeded")]
MaxSpreadAssertion {},

#[error("Slippage tolerance exceeded")]
MaxSlippageAssertion {},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::cmp::Ordering;
use std::str::FromStr;

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_binary, Decimal, Decimal256, Deps, DepsMut, Env, Fraction, ReplyOn, Response, StdError,
StdResult, Storage, SubMsg, Uint128, Uint256, WasmMsg,
to_binary, Decimal, Decimal256, Deps, DepsMut, Env, ReplyOn, Response, StdError, StdResult,
Storage, SubMsg, Uint128, Uint256, WasmMsg,
};
use cw20::MinterResponse;
use cw_storage_plus::Item;
Expand Down Expand Up @@ -127,59 +124,6 @@ pub struct OfferAmountComputation {
pub protocol_fee_amount: Uint128,
pub burn_fee_amount: Uint128,
}

/// Default swap slippage in case max_spread is not specified
pub const DEFAULT_SLIPPAGE: &str = "0.01";
/// Cap on the maximum swap slippage that is allowed. If max_spread goes over this limit, it will
/// be capped to this value.
pub const MAX_ALLOWED_SLIPPAGE: &str = "0.5";

/// If `belief_price` and `max_spread` both are given,
/// we compute new spread else we just use pool network
/// spread to check `max_spread`
pub fn assert_max_spread(
belief_price: Option<Decimal>,
max_spread: Option<Decimal>,
offer_amount: Uint128,
return_amount: Uint128,
spread_amount: Uint128,
) -> Result<(), ContractError> {
println!("assert_max_spread: belief_price: {:?}, max_spread: {:?}, offer_amount: {:?}, return_amount: {:?}, spread_amount: {:?}", belief_price, max_spread, offer_amount, return_amount, spread_amount);

let max_spread: Decimal256 = max_spread
.unwrap_or(Decimal::from_str(DEFAULT_SLIPPAGE)?)
.min(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?)
.into();

println!("max_spread: {:?}", max_spread);
println!(
"Decimal256::from_ratio(spread_amount, return_amount + spread_amount) {:?}",
Decimal256::from_ratio(spread_amount, return_amount + spread_amount)
);
println!(
"Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread: {:?}",
Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread
);

if let Some(belief_price) = belief_price {
let expected_return = offer_amount
* belief_price
.inv()
.ok_or_else(|| StdError::generic_err("Belief price can't be zero"))?;
let spread_amount = expected_return.saturating_sub(return_amount);

if return_amount < expected_return
&& Decimal256::from_ratio(spread_amount, expected_return) > max_spread
{
return Err(ContractError::MaxSpreadAssertion {});
}
} else if Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread {
return Err(ContractError::MaxSpreadAssertion {});
}

Ok(())
}

pub fn assert_slippage_tolerance(
slippage_tolerance: &Option<Decimal>,
deposits: &[Uint128; 3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use white_whale::pool_network::asset::{Asset, AssetInfo, TrioInfo};
#[cfg(feature = "token_factory")]
use white_whale::pool_network::denom::MsgCreateDenom;
use white_whale::pool_network::mock_querier::mock_dependencies;
use white_whale::pool_network::swap::assert_max_spread;
use white_whale::pool_network::token::InstantiateMsg as TokenInstantiateMsg;
use white_whale::pool_network::trio::ExecuteMsg::UpdateConfig;
use white_whale::pool_network::trio::{Config, InstantiateMsg, MigrateMsg, PoolFee, QueryMsg};

use crate::contract::{execute, instantiate, migrate, query, reply};
use crate::error::ContractError;
use crate::helpers::{assert_max_spread, assert_slippage_tolerance};
use crate::helpers::assert_slippage_tolerance;
use crate::queries::query_trio_info;

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use white_whale::pool_network::denom::{Coin, MsgBurn, MsgMint};
#[cfg(feature = "osmosis_token_factory")]
use white_whale::pool_network::denom_osmosis::{Coin, MsgBurn, MsgMint};
use white_whale::pool_network::pair::{Config, Cw20HookMsg, FeatureToggle, PoolFee};
use white_whale::pool_network::U256;
use white_whale::pool_network::{swap, U256};

use crate::error::ContractError;
use crate::helpers;
Expand Down Expand Up @@ -409,7 +409,7 @@ pub fn swap(
.checked_add(swap_computation.burn_fee_amount)?;

// check max spread limit if exist
helpers::assert_max_spread(
swap::assert_max_spread(
belief_price,
max_spread,
offer_asset.amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ pub enum ContractError {
#[error("Invalid zero amount")]
InvalidZeroAmount {},

#[error("Spread limit exceeded")]
MaxSpreadAssertion {},

#[error("Slippage tolerance exceeded")]
MaxSlippageAssertion {},

Expand Down
57 changes: 2 additions & 55 deletions contracts/liquidity_hub/pool-network/terraswap_pair/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::ops::Mul;
use std::str::FromStr;

use cosmwasm_schema::cw_serde;
#[cfg(any(feature = "token_factory", feature = "osmosis_token_factory"))]
use cosmwasm_std::CosmosMsg;
use cosmwasm_std::{
to_binary, Decimal, Decimal256, DepsMut, Env, Fraction, ReplyOn, Response, StdError, StdResult,
Storage, SubMsg, Uint128, Uint256, WasmMsg,
to_binary, Decimal, Decimal256, DepsMut, Env, ReplyOn, Response, StdError, StdResult, Storage,
SubMsg, Uint128, Uint256, WasmMsg,
};
use cw20::MinterResponse;
use cw_storage_plus::Item;
Expand Down Expand Up @@ -322,58 +321,6 @@ pub struct OfferAmountComputation {
pub burn_fee_amount: Uint128,
}

/// Default swap slippage in case max_spread is not specified
pub const DEFAULT_SLIPPAGE: &str = "0.01";
/// Cap on the maximum swap slippage that is allowed. If max_spread goes over this limit, it will
/// be capped to this value.
pub const MAX_ALLOWED_SLIPPAGE: &str = "0.5";

/// If `belief_price` and `max_spread` both are given,
/// we compute new spread else we just use pool network
/// spread to check `max_spread`
pub fn assert_max_spread(
belief_price: Option<Decimal>,
max_spread: Option<Decimal>,
offer_amount: Uint128,
return_amount: Uint128,
spread_amount: Uint128,
) -> Result<(), ContractError> {
println!("assert_max_spread: belief_price: {:?}, max_spread: {:?}, offer_amount: {:?}, return_amount: {:?}, spread_amount: {:?}", belief_price, max_spread, offer_amount, return_amount, spread_amount);

let max_spread: Decimal256 = max_spread
.unwrap_or(Decimal::from_str(DEFAULT_SLIPPAGE)?)
.min(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?)
.into();

println!("max_spread: {:?}", max_spread);
println!(
"Decimal256::from_ratio(spread_amount, return_amount + spread_amount) {:?}",
Decimal256::from_ratio(spread_amount, return_amount + spread_amount)
);
println!(
"Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread: {:?}",
Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread
);

if let Some(belief_price) = belief_price {
let expected_return = offer_amount
* belief_price
.inv()
.ok_or_else(|| StdError::generic_err("Belief price can't be zero"))?;
let spread_amount = expected_return.saturating_sub(return_amount);

if return_amount < expected_return
&& Decimal256::from_ratio(spread_amount, expected_return) > max_spread
{
return Err(ContractError::MaxSpreadAssertion {});
}
} else if Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread {
return Err(ContractError::MaxSpreadAssertion {});
}

Ok(())
}

pub fn assert_slippage_tolerance(
slippage_tolerance: &Option<Decimal>,
deposits: &[Uint128; 2],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ use white_whale::pool_network::denom::MsgCreateDenom;
use white_whale::pool_network::mock_querier::mock_dependencies;
use white_whale::pool_network::pair::ExecuteMsg::UpdateConfig;
use white_whale::pool_network::pair::{Config, InstantiateMsg, PoolFee, QueryMsg};
use white_whale::pool_network::swap::assert_max_spread;
use white_whale::pool_network::token::InstantiateMsg as TokenInstantiateMsg;

use crate::contract::{execute, instantiate, query, reply};
use crate::error::ContractError;
use crate::helpers::{assert_max_spread, assert_slippage_tolerance};
use crate::helpers::assert_slippage_tolerance;
use crate::queries::query_pair_info;

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cosmwasm_std::testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{
attr, coin, from_binary, to_binary, Addr, Coin, CosmosMsg, Decimal, StdError, SubMsg, Uint128,
WasmMsg,
attr, coin, from_binary, to_binary, Addr, Coin, CosmosMsg, StdError, SubMsg, Uint128, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use white_whale::pool_network;
Expand Down
1 change: 1 addition & 0 deletions packages/white-whale/src/pool_network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod incentive_factory;
pub mod pair;
pub mod querier;
pub mod router;
pub mod swap;
pub mod token;
pub mod trio;

Expand Down
42 changes: 42 additions & 0 deletions packages/white-whale/src/pool_network/swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use cosmwasm_std::{Decimal, Decimal256, Fraction, StdError, StdResult, Uint128};
use std::str::FromStr;

/// Default swap slippage in case max_spread is not specified
pub const DEFAULT_SLIPPAGE: &str = "0.01";
/// Cap on the maximum swap slippage that is allowed. If max_spread goes over this limit, it will
/// be capped to this value.
pub const MAX_ALLOWED_SLIPPAGE: &str = "0.5";

/// If `belief_price` and `max_spread` both are given,
/// we compute new spread else we just use pool network
/// spread to check `max_spread`
pub fn assert_max_spread(
belief_price: Option<Decimal>,
max_spread: Option<Decimal>,
offer_amount: Uint128,
return_amount: Uint128,
spread_amount: Uint128,
) -> StdResult<()> {
let max_spread: Decimal256 = max_spread
.unwrap_or(Decimal::from_str(DEFAULT_SLIPPAGE)?)
.min(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?)
.into();

if let Some(belief_price) = belief_price {
let expected_return = offer_amount
* belief_price
.inv()
.ok_or_else(|| StdError::generic_err("Belief price can't be zero"))?;

Check warning on line 29 in packages/white-whale/src/pool_network/swap.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/pool_network/swap.rs#L29

Added line #L29 was not covered by tests
let spread_amount = expected_return.saturating_sub(return_amount);

if return_amount < expected_return
&& Decimal256::from_ratio(spread_amount, expected_return) > max_spread
{
return Err(StdError::generic_err("Spread limit exceeded"));
}
} else if Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread {
return Err(StdError::generic_err("Spread limit exceeded"));
}

Ok(())
}

0 comments on commit 61bd61e

Please sign in to comment.