From 61bd61e75d28bd9c174d6eabfa246baa758e67cb Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Fri, 20 Oct 2023 10:32:29 +0100 Subject: [PATCH] chore: cleanup --- .../fee_collector/src/tests/integration.rs | 2 +- .../stableswap_3pool/src/commands.rs | 23 +------ .../stableswap_3pool/src/error.rs | 3 - .../stableswap_3pool/src/helpers.rs | 60 +------------------ .../stableswap_3pool/src/tests/testing.rs | 3 +- .../terraswap_pair/src/commands.rs | 4 +- .../pool-network/terraswap_pair/src/error.rs | 3 - .../terraswap_pair/src/helpers.rs | 57 +----------------- .../terraswap_pair/src/tests/testing.rs | 3 +- .../terraswap_router/src/testing/tests.rs | 3 +- packages/white-whale/src/pool_network/mod.rs | 1 + packages/white-whale/src/pool_network/swap.rs | 42 +++++++++++++ 12 files changed, 57 insertions(+), 147 deletions(-) create mode 100644 packages/white-whale/src/pool_network/swap.rs diff --git a/contracts/liquidity_hub/fee_collector/src/tests/integration.rs b/contracts/liquidity_hub/fee_collector/src/tests/integration.rs index 030e1bb8..fdb11861 100644 --- a/contracts/liquidity_hub/fee_collector/src/tests/integration.rs +++ b/contracts/liquidity_hub/fee_collector/src/tests/integration.rs @@ -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(), diff --git a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs index d2feb158..bced40b2 100644 --- a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs +++ b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs @@ -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; @@ -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 {}); } @@ -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 {}); } @@ -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 {}); } @@ -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, diff --git a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/error.rs b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/error.rs index 6b6e4926..7a93ab0e 100644 --- a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/error.rs +++ b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/error.rs @@ -19,9 +19,6 @@ pub enum ContractError { #[error("Invalid zero amount")] InvalidZeroAmount {}, - #[error("Spread limit exceeded")] - MaxSpreadAssertion {}, - #[error("Slippage tolerance exceeded")] MaxSlippageAssertion {}, diff --git a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/helpers.rs b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/helpers.rs index 14643e31..9c4674fa 100644 --- a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/helpers.rs +++ b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/helpers.rs @@ -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; @@ -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, - max_spread: Option, - 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, deposits: &[Uint128; 3], diff --git a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/tests/testing.rs b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/tests/testing.rs index 334fe0ee..6b718104 100644 --- a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/tests/testing.rs +++ b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/tests/testing.rs @@ -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] diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs index b637ed7d..afb99946 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs @@ -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; @@ -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, diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/error.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/error.rs index d6a7ae01..22a97034 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/error.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/error.rs @@ -34,9 +34,6 @@ pub enum ContractError { #[error("Invalid zero amount")] InvalidZeroAmount {}, - #[error("Spread limit exceeded")] - MaxSpreadAssertion {}, - #[error("Slippage tolerance exceeded")] MaxSlippageAssertion {}, diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/helpers.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/helpers.rs index 6092e0d9..81e49aef 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/helpers.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/helpers.rs @@ -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; @@ -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, - max_spread: Option, - 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, deposits: &[Uint128; 2], diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/tests/testing.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/tests/testing.rs index 71fe8723..8efa2aaa 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/tests/testing.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/tests/testing.rs @@ -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] diff --git a/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs b/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs index 41728fec..8222d7eb 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs @@ -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; diff --git a/packages/white-whale/src/pool_network/mod.rs b/packages/white-whale/src/pool_network/mod.rs index 91f88096..52c2d7fc 100644 --- a/packages/white-whale/src/pool_network/mod.rs +++ b/packages/white-whale/src/pool_network/mod.rs @@ -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; diff --git a/packages/white-whale/src/pool_network/swap.rs b/packages/white-whale/src/pool_network/swap.rs new file mode 100644 index 00000000..ef58ce18 --- /dev/null +++ b/packages/white-whale/src/pool_network/swap.rs @@ -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, + max_spread: Option, + 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"))?; + 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(()) +}