diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/contract.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/contract.rs index 25145ae5..61fca363 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/contract.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/contract.rs @@ -253,13 +253,13 @@ pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { @@ -235,11 +248,19 @@ pub fn compute_swap( let protocol_fee_amount: Uint256 = pool_fees.protocol_fee.compute(return_amount); let burn_fee_amount: Uint256 = pool_fees.burn_fee.compute(return_amount); - let return_amount = return_amount + #[cfg(feature = "osmosis")] + let osmosis_fee_amount: Uint256 = pool_fees.osmosis_fee.compute(return_amount); + + let mut return_amount = return_amount .checked_sub(swap_fee_amount)? .checked_sub(protocol_fee_amount)? .checked_sub(burn_fee_amount)?; + #[cfg(feature = "osmosis")] + { + return_amount = return_amount.checked_sub(osmosis_fee_amount)?; + } + Ok(SwapComputation { return_amount: return_amount .try_into() @@ -256,6 +277,10 @@ pub fn compute_swap( burn_fee_amount: burn_fee_amount .try_into() .map_err(|_| ContractError::SwapOverflowError {})?, + #[cfg(feature = "osmosis")] + osmosis_fee_amount: osmosis_fee_amount + .try_into() + .map_err(|_| ContractError::SwapOverflowError {})?, }) } } @@ -269,6 +294,8 @@ pub struct SwapComputation { pub swap_fee_amount: Uint128, pub protocol_fee_amount: Uint128, pub burn_fee_amount: Uint128, + #[cfg(feature = "osmosis")] + pub osmosis_fee_amount: Uint128, } pub fn compute_offer_amount( @@ -283,9 +310,15 @@ pub fn compute_offer_amount( // ask => offer // offer_amount = cp / (ask_pool - ask_amount / (1 - fees)) - offer_pool - let fees = pool_fees.swap_fee.to_decimal_256() + let mut fees = pool_fees.swap_fee.to_decimal_256() + pool_fees.protocol_fee.to_decimal_256() + pool_fees.burn_fee.to_decimal_256(); + + #[cfg(feature = "osmosis")] + { + fees = fees + pool_fees.osmosis_fee.to_decimal_256(); + } + let one_minus_commission = Decimal256::one() - fees; let inv_one_minus_commission = Decimal256::one() / one_minus_commission; @@ -308,12 +341,17 @@ pub fn compute_offer_amount( let protocol_fee_amount: Uint256 = pool_fees.protocol_fee.compute(before_commission_deduction); let burn_fee_amount: Uint256 = pool_fees.burn_fee.compute(before_commission_deduction); + #[cfg(feature = "osmosis")] + let osmosis_fee_amount: Uint256 = pool_fees.osmosis_fee.compute(before_commission_deduction); + Ok(OfferAmountComputation { offer_amount: offer_amount.try_into()?, spread_amount: spread_amount.try_into()?, swap_fee_amount: swap_fee_amount.try_into()?, protocol_fee_amount: protocol_fee_amount.try_into()?, burn_fee_amount: burn_fee_amount.try_into()?, + #[cfg(feature = "osmosis")] + osmosis_fee_amount: osmosis_fee_amount.try_into()?, }) } @@ -325,6 +363,8 @@ pub struct OfferAmountComputation { pub swap_fee_amount: Uint128, pub protocol_fee_amount: Uint128, pub burn_fee_amount: Uint128, + #[cfg(feature = "osmosis")] + pub osmosis_fee_amount: Uint128, } pub fn assert_slippage_tolerance( diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/migrations.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/migrations.rs index ef64bbdf..a2e9f73d 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/migrations.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/migrations.rs @@ -19,7 +19,7 @@ use crate::helpers::instantiate_fees; use crate::state::PAIR_INFO; use crate::state::{ALL_TIME_BURNED_FEES, CONFIG}; -#[cfg(not(feature = "injective"))] +#[cfg(all(not(feature = "injective"), not(feature = "osmosis")))] /// Migrate state of the factory from PascalCase to snake_case for the following items: /// [`PairInfoRaw`], [`PairInfo`] /// as identified by commit c8d8462c6933b93245acdc8abbe303287fdc1951 which changed the structs to use @@ -74,7 +74,7 @@ pub fn migrate_to_v110(deps: DepsMut) -> Result<(), StdError> { Ok(()) } -#[cfg(not(feature = "injective"))] +#[cfg(all(not(feature = "injective"), not(feature = "osmosis")))] pub fn migrate_to_v120(deps: DepsMut) -> Result<(), StdError> { #[cw_serde] struct ConfigV110 { @@ -124,7 +124,7 @@ pub fn migrate_to_v120(deps: DepsMut) -> Result<(), StdError> { Ok(()) } -#[cfg(not(feature = "injective"))] +#[cfg(all(not(feature = "injective"), not(feature = "osmosis")))] /// Migrate to the StableSwap deployment /// /// Default to a ConstantProduct pool diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/queries.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/queries.rs index f4ac84ec..c3a563bb 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/queries.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/queries.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use cosmwasm_std::{Decimal256, Deps, Fraction, StdResult, Uint128}; +use cosmwasm_std::{Decimal256, Deps, Fraction, StdResult, Uint128, Uint256}; use cw_storage_plus::Item; use white_whale::pool_network::asset::{ @@ -129,6 +129,8 @@ pub fn query_simulation( swap_fee_amount: swap_computation.swap_fee_amount, protocol_fee_amount: swap_computation.protocol_fee_amount, burn_fee_amount: swap_computation.burn_fee_amount, + #[cfg(feature = "osmosis")] + osmosis_fee_amount: swap_computation.osmosis_fee_amount, }) } @@ -196,6 +198,8 @@ pub fn query_reverse_simulation( swap_fee_amount: offer_amount_computation.swap_fee_amount, protocol_fee_amount: offer_amount_computation.protocol_fee_amount, burn_fee_amount: offer_amount_computation.burn_fee_amount, + #[cfg(feature = "osmosis")] + osmosis_fee_amount: offer_amount_computation.osmosis_fee_amount, }) } PairType::StableSwap { amp } => { @@ -248,12 +252,17 @@ pub fn query_reverse_simulation( let protocol_fee_amount = pool_fees.protocol_fee.compute(before_fees_ask); let burn_fee_amount = pool_fees.burn_fee.compute(before_fees_ask); + #[cfg(feature = "osmosis")] + let osmosis_fee_amount: Uint256 = pool_fees.osmosis_fee.compute(before_fees_ask); + Ok(ReverseSimulationResponse { offer_amount, spread_amount, swap_fee_amount: swap_fee_amount.try_into()?, protocol_fee_amount: protocol_fee_amount.try_into()?, burn_fee_amount: burn_fee_amount.try_into()?, + #[cfg(feature = "osmosis")] + osmosis_fee_amount: osmosis_fee_amount.try_into()?, }) } } diff --git a/packages/white-whale/src/pool_network/mock_querier.rs b/packages/white-whale/src/pool_network/mock_querier.rs index b1564e6e..07fa6389 100644 --- a/packages/white-whale/src/pool_network/mock_querier.rs +++ b/packages/white-whale/src/pool_network/mock_querier.rs @@ -234,6 +234,8 @@ impl WasmMockQuerier { spread_amount: Uint128::zero(), protocol_fee_amount: Uint128::zero(), burn_fee_amount: Uint128::zero(), + #[cfg(feature = "osmosis")] + osmosis_fee_amount: Uint128::zero(), }))) } Ok(PairQueryMsg::ReverseSimulation { ask_asset }) => SystemResult::Ok( @@ -243,6 +245,8 @@ impl WasmMockQuerier { spread_amount: Uint128::zero(), protocol_fee_amount: Uint128::zero(), burn_fee_amount: Uint128::zero(), + #[cfg(feature = "osmosis")] + osmosis_fee_amount: Uint128::zero(), })), ), _ => match from_binary(msg).unwrap() { diff --git a/packages/white-whale/src/pool_network/pair.rs b/packages/white-whale/src/pool_network/pair.rs index 0c534434..1adbf3d6 100644 --- a/packages/white-whale/src/pool_network/pair.rs +++ b/packages/white-whale/src/pool_network/pair.rs @@ -110,6 +110,8 @@ pub struct PoolFee { pub protocol_fee: Fee, pub swap_fee: Fee, pub burn_fee: Fee, + #[cfg(feature = "osmosis")] + pub osmosis_fee: Fee, } impl PoolFee { @@ -120,15 +122,24 @@ impl PoolFee { self.swap_fee.is_valid()?; self.burn_fee.is_valid()?; - if self + let mut total_fee = self .protocol_fee .share .checked_add(self.swap_fee.share)? - .checked_add(self.burn_fee.share)? - >= Decimal::percent(100) + .checked_add(self.burn_fee.share)?; + + // add the osmosis_fee if applicable + #[cfg(feature = "osmosis")] { + self.osmosis_fee.is_valid()?; + total_fee = total_fee.checked_add(self.osmosis_fee.share)?; + } + + // Check if the total fee exceeds 100% + if total_fee >= Decimal::percent(100) { return Err(StdError::generic_err("Invalid fees")); } + Ok(()) } } @@ -158,6 +169,8 @@ pub struct SimulationResponse { pub swap_fee_amount: Uint128, pub protocol_fee_amount: Uint128, pub burn_fee_amount: Uint128, + #[cfg(feature = "osmosis")] + pub osmosis_fee_amount: Uint128, } /// ReverseSimulationResponse returns reverse swap simulation response @@ -174,6 +187,8 @@ pub struct ReverseSimulationResponse { pub swap_fee_amount: Uint128, pub protocol_fee_amount: Uint128, pub burn_fee_amount: Uint128, + #[cfg(feature = "osmosis")] + pub osmosis_fee_amount: Uint128, } /// We currently take no arguments for migrations