Skip to content

Commit

Permalink
chore: add osmosis fee to pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed Jan 8, 2024
1 parent b393989 commit 0affa2c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Respons
migrations::migrate_to_v13x(deps.branch())?;
}

#[cfg(not(feature = "injective"))]
#[cfg(all(not(feature = "injective"), not(feature = "osmosis")))]
if storage_version <= Version::parse("1.0.4")? {
migrations::migrate_to_v110(deps.branch())?;
} else if storage_version == Version::parse("1.1.0")? {
migrations::migrate_to_v120(deps.branch())?;
}
#[cfg(not(feature = "injective"))]
#[cfg(all(not(feature = "injective"), not(feature = "osmosis")))]
if storage_version == Version::parse("1.2.0")? {
migrations::migrate_to_v130(deps.branch())?;
}
Expand Down
48 changes: 44 additions & 4 deletions contracts/liquidity_hub/pool-network/terraswap_pair/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,19 @@ pub fn compute_swap(
let swap_fee_amount: Uint256 = pool_fees.swap_fee.compute(return_amount);
let protocol_fee_amount: Uint256 = pool_fees.protocol_fee.compute(return_amount);
let burn_fee_amount: Uint256 = pool_fees.burn_fee.compute(return_amount);
#[cfg(feature = "osmosis")]
let osmosis_fee_amount: Uint256 = pool_fees.osmosis_fee.compute(return_amount);

// swap and protocol fee will be absorbed by the pool. Burn fee amount will be burned on a subsequent msg.
let return_amount: Uint256 =
return_amount - swap_fee_amount - protocol_fee_amount - burn_fee_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
Expand All @@ -204,6 +213,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 {})?,
})
}
PairType::StableSwap { amp } => {
Expand Down Expand Up @@ -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()
Expand All @@ -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 {})?,
})
}
}
Expand All @@ -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(
Expand All @@ -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;

Expand All @@ -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()?,
})
}

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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 } => {
Expand Down Expand Up @@ -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()?,
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/white-whale/src/pool_network/mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Check warning on line 238 in packages/white-whale/src/pool_network/mock_querier.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/pool_network/mock_querier.rs#L237-L238

Added lines #L237 - L238 were not covered by tests
})))
}
Ok(PairQueryMsg::ReverseSimulation { ask_asset }) => SystemResult::Ok(
Expand All @@ -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(),

Check warning on line 249 in packages/white-whale/src/pool_network/mock_querier.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/pool_network/mock_querier.rs#L248-L249

Added lines #L248 - L249 were not covered by tests
})),
),
_ => match from_binary(msg).unwrap() {
Expand Down
21 changes: 18 additions & 3 deletions packages/white-whale/src/pool_network/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 warning on line 135 in packages/white-whale/src/pool_network/pair.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/pool_network/pair.rs#L134-L135

Added lines #L134 - L135 were not covered by tests
}

// Check if the total fee exceeds 100%
if total_fee >= Decimal::percent(100) {
return Err(StdError::generic_err("Invalid fees"));
}

Ok(())
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0affa2c

Please sign in to comment.